TorchLean API

NN.Runtime.Autograd.Compiled.GraphM.Core

GraphM Core #

Typed variables, builder state, input binding, constants, random nodes, and detach for the proof-compiled graph authoring API.

@[reducible, inline]

Shorthand for the underlying executable SSA graph type from Proofs.Autograd.Algebra.

Instances For
    @[reducible, inline]

    Executable node payload for the proof-compiled SSA graph (GraphData).

    Instances For

      A typed handle to a value in the growing compiled context.

      Var s carries its expected Shape at the type level, while id is the runtime index into the concatenated context Γ ++ ss.

      • id :

        Runtime id of the value inside the concatenated context Γ ++ ss.

        The shape index on Var s is the static guarantee; this numeric id is the executable handle used when constructing Idx proofs for GraphData nodes.

      Instances For
        @[implicit_reducible]

        GraphM.arg is correct but a little noisy for examples (you must repeat the index and shape).

        VarList + args give a typed variable layer: args returns one Var per entry in Γ, in order, without spelling indices.

        Dependent list of typed variables, aligned with a list of shapes.

        VarList Γ contains exactly one Var s for each s ∈ Γ, in order.

        Instances For

          First variable in a nonempty VarList.

          Instances For

            Tail variables in a nonempty VarList.

            Instances For
              @[reducible, inline]

              State for the GraphM builder.

              It is a sigma pair of:

              • the list of intermediate shapes ss produced so far, and
              • the corresponding executable SSA graph payload GraphData α Δ Γ ss.
              Instances For
                @[reducible, inline]

                Default GraphM state with no extra environment (Δ := Unit).

                Instances For
                  @[reducible, inline]

                  StateT builder monad for authoring a GraphData program, with explicit environment Δ.

                  Instances For
                    @[reducible, inline]

                    Default GraphM builder monad with Δ := Unit.

                    Instances For

                      Empty builder state (no intermediate nodes yet).

                      Instances For

                        Empty builder state for an explicit environment type Δ.

                        Instances For
                          def Runtime.Autograd.Compiled.GraphM.run {α : Type} {Γ : List Spec.Shape} {β : Type} (m : M α Γ β) :
                          Result (β × State α Γ)

                          Run a GraphM program from an empty state.

                          Instances For

                            Build a GraphData by running a GraphM program.

                            This is the usual entry point: write a do-block that constructs the graph using arg, ops, and returns Unit; get back the finalized builder state containing ss and the graph.

                            Instances For

                              Length of the current context Γ ++ ss (inputs + intermediates).

                              Instances For

                                Convert a Var s into a dependent Idx (Γ ++ ss) s.

                                This performs bounds checking and a runtime shape check, returning a structured error if the variable points outside the current context or has the wrong shape.

                                Instances For
                                  def Runtime.Autograd.Compiled.GraphM.push {α Δ : Type} {Γ ss : List Spec.Shape} {s : Spec.Shape} (g : PGraphData α Δ Γ ss) (node : PNodeData α Δ (Γ ++ ss) s) :
                                  MWith α Δ Γ (Var s)

                                  Append a node to the graph state and return a fresh Var pointing to its output.

                                  The returned variable id is Γ.length + ss.length, i.e. it points at the newly appended entry.

                                  Instances For

                                    Forward-mode JVP availability for a compiled graph builder op.

                                    • implemented : JvpAvailability

                                      The op supplies a real forward-mode JVP rule.

                                    • reverseOnly (op : String) : JvpAvailability

                                      The op supplies reverse-mode VJP only. Forward-mode requests fail loudly.

                                    Instances For

                                      Compiled ops that provide VJP for training but no forward-mode JVP rule.

                                      Keeping the list executable gives callers a stable preflight hook instead of discovering the gap only after a directional-derivative run reaches the node. The list is intentionally empty when all compiled builder ops have concrete JVP rules.

                                      Instances For

                                        Return the JVP status for a named compiled op.

                                        Instances For

                                          Diagnostic message for reverse-only compiled ops.

                                          Instances For

                                            Fail-fast marker for compiled nodes whose forward-mode JVP rule is intentionally absent.

                                            Returning a zero tangent here would silently corrupt forward-mode autodiff. Reverse-mode users are unaffected because these nodes still provide real vjp implementations. Forward-mode callers get a loud error, and reverseOnlyJvpOps provides a preflight list for tools that want to reject such graphs before running a JVP.

                                            Instances For

                                              Reference an input variable from the initial context Γ.

                                              This checks that the provided index is within bounds and that the requested shape matches the shape at that position in Γ.

                                              PyTorch comparison: this is like naming a graph input tensor in a traced graph.

                                              Instances For

                                                Pure helper to build VarList Γ starting at a given id offset.

                                                Instances For

                                                  Return one Var per entry of Γ, in order.

                                                  This is the canonical argument environment for a graph with input context Γ.

                                                  Instances For
                                                    def Runtime.Autograd.Compiled.GraphM.const {α Δ : Type} [Zero α] {Γ : List Spec.Shape} {s : Spec.Shape} (t : Spec.Tensor α s) :
                                                    MWith α Δ Γ (Var s)

                                                    Embed a constant tensor as a node in the compiled graph.

                                                    This node has no input dependencies (vjp = 0, jvp = 0), i.e. it is treated as a constant with respect to the graph inputs.

                                                    PyTorch comparison: a constant literal captured into a traced/compiled graph.

                                                    Instances For
                                                      def Runtime.Autograd.Compiled.GraphM.randUniform {α : Type} [Context α] {Δ : Type} {Γ : List Spec.Shape} {s : Spec.Shape} (seed : ) :
                                                      MWith α Δ Γ (Var s)

                                                      Deterministic U[0,1) tensor generator (seeded, pure).

                                                      Instances For

                                                        Deterministic {0,1} mask generator (seeded, pure).

                                                        Note: for differentiation purposes, this node is treated as a stop-gradient op: jvp = 0 and vjp = 0 for all inputs (including keepProb). This matches the intended use in dropout where the probability is a hyperparameter (not differentiated), while keeping execution deterministic in the .compiled backend.

                                                        Instances For

                                                          Stop-gradient boundary.

                                                          Forward semantics: identity (detach(x) = x). Backward semantics: no gradient flows to x (treated as constant w.r.t. the graph inputs).

                                                          Instances For