TorchLean API

NN.Kernel.Target

Structured kernel target #

Primitive operands are values, not computations. Bindings therefore fix the order of reads and arithmetic before CUDA rendering. Branches and loops retain their own blocks, so lowering cannot hoist a failing read out of a branch or reassociate a sequential accumulation.

The target evaluator specifies these blocks independently of the source evaluator. Its lowering theorem is about this representation, not NVIDIA's compiler or execution of rendered text.

theorem NN.Kernel.Target.Internal.increment_toNat {i count : UInt64} (h : i < count) :
(i + 1).toNat = i.toNat + 1

Incrementing below an unsigned loop bound cannot wrap, even at the largest bound.

@[irreducible]
def NN.Kernel.Target.loop {α : Type} (step : UInt64 → α → Except Error α) (count i : UInt64) (acc : α) :

An unsigned counter loop, stopping before count and at the first failed step.

The strict guard ensures that incrementing the counter cannot wrap, even when count is the largest representable index. Starting at or beyond the bound leaves the accumulator unchanged.

Instances For
    theorem NN.Kernel.Target.loop_eq_iterate {α : Type} (step : UInt64 → α → Except Error α) (count i : UInt64) (acc : α) :
    loop step count i acc = iterate step (count.toNat - i.toNat) i acc

    The unsigned guard executes exactly the remaining sequential steps, including their errors.

    inductive NN.Kernel.Target.Atom (α : Type) :
    List Ty → Ty → Type

    Constants and typed local variables, with no effects or arithmetic.

    Instances For
      inductive NN.Kernel.Target.Primitive (α : Type) :
      List Ty → Ty → Type

      One primitive operation whose operands have already been evaluated.

      Instances For
        inductive NN.Kernel.Target.Block (α : Type) :
        List Ty → Ty → Type

        Scoped statement blocks. A binding finishes its value block before entering its body.

        Instances For
          def NN.Kernel.Target.Atom.eval {α : Type} {Γ : List Ty} {t : Ty} (env : Env α Γ) :
          Atom α Γ t → Ty.Value α t

          Read an atomic value from the current environment.

          Instances For
            def NN.Kernel.Target.Primitive.eval {α : Type} (arithmetic : Arithmetic α) (read : Reader α) {Γ : List Ty} {t : Ty} (env : Env α Γ) :
            Primitive α Γ t → Except Error (Ty.Value α t)

            Perform one operation, retaining the reader's error rather than supplying a default value.

            Instances For
              def NN.Kernel.Target.Block.eval {α : Type} (arithmetic : Arithmetic α) (read : Reader α) {Γ : List Ty} {t : Ty} (env : Env α Γ) :
              Block α Γ t → Except Error (Ty.Value α t)

              Sequential block semantics, with lazy branches and increasing-index loop accumulation.

              Instances For
                def NN.Kernel.Target.Atom.rename {α : Type} {Γ Δ : List Ty} {t : Ty} (ρ : Renaming Γ Δ) :
                Atom α Γ t → Atom α Δ t

                Move an atom's variable addresses under a new scope.

                Instances For
                  def NN.Kernel.Target.Primitive.rename {α : Type} {Γ Δ : List Ty} {t : Ty} (ρ : Renaming Γ Δ) :
                  Primitive α Γ t → Primitive α Δ t

                  Rename only the already-evaluated operands of a primitive.

                  Instances For
                    def NN.Kernel.Target.Block.rename {α : Type} {Γ Δ : List Ty} {t : Ty} (ρ : Renaming Γ Δ) :
                    Block α Γ t → Block α Δ t

                    Rename a block without changing its sequencing or control-flow regions.

                    Instances For
                      theorem NN.Kernel.Target.Atom.eval_rename {α : Type} {Γ Δ : List Ty} {t : Ty} (value : Atom α Γ t) (ρ : Renaming Γ Δ) (env : Env α Δ) :
                      eval env (rename (fun {t : Ty} => ρ) value) = eval (Env.pull (fun {t : Ty} => ρ) env) value

                      Renaming an atom is equivalent to looking up its original address in a pulled environment.

                      theorem NN.Kernel.Target.Primitive.eval_rename {α : Type} {Γ Δ : List Ty} {t : Ty} (operation : Primitive α Γ t) (arithmetic : Arithmetic α) (read : Reader α) (ρ : Renaming Γ Δ) (env : Env α Δ) :
                      eval arithmetic read env (rename (fun {t : Ty} => ρ) operation) = eval arithmetic read (Env.pull (fun {t : Ty} => ρ) env) operation

                      Renaming cannot change primitive results or failed reads.

                      theorem NN.Kernel.Target.Block.eval_rename {α : Type} {Γ : List Ty} {t : Ty} (block : Block α Γ t) (arithmetic : Arithmetic α) (read : Reader α) {Δ : List Ty} (ρ : Renaming Γ Δ) (env : Env α Δ) :
                      eval arithmetic read env (rename (fun {t : Ty} => ρ) block) = eval arithmetic read (Env.pull (fun {t : Ty} => ρ) env) block

                      Renaming scoped statements preserves both values and errors.

                      def NN.Kernel.Target.lower {α : Type} {Γ : List Ty} {t : Ty} :
                      Expr α Γ t → Block α Γ t

                      Lower source expressions to blocks with atomic primitive operands.

                      Instances For
                        theorem NN.Kernel.Target.eval_lower {α : Type} {Γ : List Ty} {t : Ty} (expr : Expr α Γ t) (arithmetic : Arithmetic α) (read : Reader α) (env : Env α Γ) :
                        Block.eval arithmetic read env (lower expr) = Expr.eval arithmetic read env expr

                        Lowering preserves results and failures without scalar associativity or field assumptions.