Backend Capsules #
A backend capsule is TorchLean's unit of delegation to fast code.
The capsule records the contract TorchLean expects from a foreign implementation: which operation it implements, what layout and shape conventions are assumed, how refinement of the operation's canonical value and VJP semantics is justified, and what trust level the planner must account for. The contract does not prove the implementation. At runtime, a capsule is paired with a typed handler whose operation, provider, and device must agree with the selected contract.
Reference to a native/FFI symbol used by a backend capsule.
The repository linter checks that path exists, that symbol occurs in that source file, and
that buildTarget?, when present, names a Lake target in lakefile.lean.
Instances For
Instances For
Instances For
Source-level provenance for a contract descriptor. Provenance is not correctness evidence.
- nativeSymbol (ref : NativeSymbolRef) : ContractProvenance
Instances For
Instances For
Instances For
Instances For
Concrete tensor-layout convention named by a backend contract.
- canonicalTensor : TensorLayout
TorchLean's ordinary typed tensor representation.
- flatRowMajor : TensorLayout
Contiguous flat storage with the last axis varying fastest.
- libTorchCudaView : TensorLayout
A contiguous CUDA tensor view owned by LibTorch.
Instances For
The four contract fields carried by every kernel capsule.
- shape : ContractObligation
- layout : ContractObligation
- value : ContractObligation
- vjp : ContractObligation
Instances For
Instances For
A structured backend obligation, independent of how evidence for it is obtained.
- shapeSafety
(op : BackendOp)
: ContractClaim
Inputs and outputs satisfy the shape rule associated with an operation.
- layoutCompatibility
(op : BackendOp)
(layout : TensorLayout)
: ContractClaim
Runtime buffers use the declared layout for an operation.
- valueRefinement
(op : BackendOp)
: ContractClaim
Forward execution refines the canonical semantics of the operation.
- vjpRefinement
(op : BackendOp)
(mode : VJPMode)
: ContractClaim
Local backward execution refines the canonical VJP semantics of the operation.
Instances For
Instances For
How a capsule justifies one part of its contract.
These constructors record engineering evidence and explicit trust boundaries. They do not turn a foreign implementation into a proved refinement.
- runtimeGuard (name : String) : ContractEvidence
- testSuite (name : String) : ContractEvidence
- trustedBoundary (reason : String) : ContractEvidence
- notApplicable : ContractEvidence
Instances For
Instances For
Whether the policy admits contract evidence of this kind.
Instances For
A structured contract claim together with its evidence and human-readable explanation.
- claim : ContractClaim
- summary : String
- evidence : ContractEvidence
- provenance : Array ContractProvenance
Instances For
Instances For
A contract claim enforced by a named runtime guard.
Instances For
A contract claim covered by a named regression suite.
Instances For
A contract claim delegated to an explicitly named trusted boundary.
Instances For
Ordering contract for reductions. Different valid orders need not be bitwise equal.
The fixed-left graph certificate propagates a reduction only when the selected capsule promises the same left fold as the canonical tensor semantics.
- fixedLeft : ReductionPolicy
- implementationDefined : ReductionPolicy
- notApplicable : ReductionPolicy
Instances For
Stable report label for reduction order.
Instances For
Floating-point choices attached to one kernel capsule that numerical certificates consume.
- reduction : ReductionPolicy
The order a reduction may use, which fixes whether summation is reproducible.
Instances For
Instances For
Whether a claim has the expected kind and operation for a capsule contract field.
Instances For
A contract-carrying fast kernel or reference implementation.
- name : String
Name used in selection reports and runtime errors.
- op : BackendOp
Backend operation implemented by this capsule.
- provider : Provider
Provider responsible for the implementation.
- device : Device
Device on which the implementation runs.
- trustLevel : TrustLevel
Assurance level the planner must accept before selection.
- supportsForward : Bool
Whether the capsule supplies forward execution.
- vjpMode : VJPMode
Form of reverse-mode support supplied by the capsule.
- shapeContract : ContractDescriptor
Shape-safety claim and its evidence.
- layoutContract : ContractDescriptor
Tensor-layout claim and its evidence.
- valueContract : ContractDescriptor
Forward-value refinement claim and its evidence.
- vjpContract : ContractDescriptor
Reverse-mode refinement claim and its evidence.
- numericalPolicy : NumericalPolicy
Floating-point behavior consumed by numerical certificates.
Instances For
Instances For
An executable implementation for one backend operation.
The result type is local to the call site, so this structure also accommodates operations whose Lean signatures differ. The capsule argument gives specialized handlers access to the provider and VJP mode after the common identity checks have succeeded.
Instances For
A selected capsule paired with a handler for the same operation, provider, and device.
These equalities certify dispatch identity only. Numerical correctness remains exactly as strong as
the capsule's ContractEvidence; binding a handler does not turn tests or a trusted boundary into a
proof.
- capsule : KernelCapsule
- handler : KernelHandler β
Instances For
Whether a runtime handler has the identity advertised by a selected capsule.
Instances For
Whether each descriptor states the obligation advertised by its field.
Evidence is useful only when it supports the right claim. This guard prevents, for example, a value-refinement test from being placed in the shape field and then accepted as shape evidence.
Instances For
Compare registration identity only. This ignores contracts and numerical policy; use full capsule equality when grouping kernels or comparing assurance evidence.
Instances For
Validate the common part of an eager executor request.
This does not decide how an operation invokes a provider. It prevents every runtime operation from reimplementing the op, device, and wiring checks before interpreting the capsule locally.
Instances For
Pair a selected contract with the runtime handler that will execute it.
The returned equalities prevent an executor for one operation or provider from being presented as another merely because both happen to share a Lean result type.
Instances For
Whether the assurance policy admits this capsule's trust level.
Instances For
Whether the provider preference admits this capsule.
Instances For
Whether this capsule is available on the selected device.
Instances For
Whether the capsule's gradient boundary is compatible with the requested kernel policy.
none is inference mode, so any forward-capable capsule is suitable even when it also advertises a
VJP. In torchLeanTape mode TorchLean owns the global tape and backward traversal. A capsule may
still implement its local VJP either as TorchLean operations or as a named backend kernel;
backendVJP requests the latter specifically.
Instances For
Planner-side admissibility predicate for a single capsule.
Instances For
Invoke the handler bound to a selected capsule.
Instances For
Pick an admissible capsule for a typed operation.
An .only preference filters the catalog through admissible. An .auto preference preserves
catalog order. A .prefer provider request first searches that provider and then falls back to the
ordinary catalog, so preference does not depend on module registration order.