TorchLean API

NN.Kernel.Program

Certified custom-operation programs #

A program retains the Lean function supplied to the frontend and a proof that its typed expression has the same result, including failed reads. The certificate concerns Lean semantics. Native CUDA emission, compilation and execution have the separate trust boundary described in Runtime.

def NN.Kernel.Env.output {α : Type} (index : UInt64) :

Bind the single free variable to the row-major output index.

Instances For
    def NN.Kernel.evaluate {α : Type} (arithmetic : Arithmetic α) (expr : Expr α [Ty.index] Ty.scalar) (read : Reader α) (index : UInt64) :

    Evaluate one output element using a supplied arithmetic implementation and input reader.

    Instances For
      structure NN.Kernel.Program (α : Type) :

      A frontend result with its original Lean function and checked semantic correspondence.

      Instances For
        structure NN.Kernel.Elementwise {α : Type} (f : α → α) :

        An elementwise program whose reference calculation is the supplied scalar function.

        Instances For
          structure NN.Kernel.Zip {α : Type} (f : α → α → α) :

          A binary scalar function compiled with left-to-right reads of its two tensor inputs.

          Instances For
            theorem NN.Kernel.Program.lower_correct {α : Type} (program : Program α) (read : Reader α) (index : UInt64) :
            Target.Block.eval program.arithmetic read (Env.output index) (Target.lower program.expression) = program.reference read index

            The scoped target before CUDA name allocation preserves the original source function.

            theorem NN.Kernel.Program.named_correct {α : Type} (program : Program α) (read : Reader α) (index : UInt64) (locals : Cuda.Locals α) (hIndex : locals.get Cuda.Name.index = index) :
            match StateT.run (Cuda.Internal.lower Cuda.Internal.Names.output (Target.lower program.expression)) 0 with | ((statement, result), snd) => Option.map (fun (x : Except Error (Cuda.Locals α)) => Except.map (fun (x : Cuda.Locals α) => x.get result) x) (Cuda.Statement.eval program.arithmetic read statement locals).run = some (program.reference read index)

            The named statements consumed by CUDA rendering preserve the original Lean function.

            Only the output index must be initialized. Other local values may be arbitrary; the allocation proof ensures that none can affect the result. Read failures are retained, and scalar arithmetic follows the supplied operation order. Rendering, NVRTC and GPU execution are separate boundaries.