TorchLean API

NN.Backend.Capsule

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.

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

    Contiguous CUDA tensor storage managed by LibTorch.

Instances For

    The four contract fields carried by every kernel capsule.

    Instances For

      A structured backend obligation, independent of how evidence for it is obtained.

      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.

          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.

              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

                      Record that a forward-only capsule intentionally has no reverse-mode implementation.

                      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.

                        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 permitted reduction order; this does not specify every source of numerical variation.

                            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.

                                • 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.

                                      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 routine; backendVJP requests the latter specifically. The maintained LibTorch routines execute under disabled gradient recording: neither mode delegates a local or global graph to LibTorch autograd.

                                                        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.

                                                              Instances For