Typed GraphSpec DAG syntax #
This module defines the shape-indexed variables, terms, argument lists, substitutions, and multi-result blocks used by the canonical GraphSpec DAG representation.
Primitives (arbitrary arity) #
An n-ary primitive operation.
Compared to the sequential GraphSpec.Primitive, a PrimOp here is parameter-free: parameters are
just ordinary inputs in the environment. This is what makes the DAG language flexible: a “layer”
is expressed by let1-binding its parameters and then using them as inputs to ops.
Type indices:
ins : List Shapeis the ordered list of input tensor shapes the op expects.τ : Shapeis the output tensor shape.
- name : String
Debug name for error messages / inspection.
- specFwd {α : Type} [TorchLean.Storage α] [Context α] : TorchLean.TensorPack α ins → TorchLean.Tensor α τ
Pure reference semantics (
insarguments packed as a typed list). Executable TorchLean program with arguments of shapes
ins.
Instances For
Typed variables #
A de Bruijn variable whose result shape is part of its type.
Unlike a bare Fin Γ.length, Var Γ s records that the selected entry of Γ has shape s.
Environment lookup therefore reduces structurally, without dependent casts through List.get.
ofFin remains available to programmatic lowerings that discover positions dynamically.
- head
{s : Spec.Shape}
{Γ : List Spec.Shape}
: Var (s :: Γ) s
The first value in a nonempty environment.
- tail
{Γ : List Spec.Shape}
{s t : Spec.Shape}
: Var Γ t → Var (s :: Γ) t
A variable inherited from the tail of an environment.
Instances For
Convert a numeric environment position to a shape-indexed variable.
Instances For
Preserve a variable when one value is appended to its environment.
Instances For
Embed a variable from the left side of an appended environment.
Instances For
Embed a variable from the right side of an appended environment.
Instances For
The final variable in an environment extended by one value.
Instances For
Extend a shape-preserving variable renaming across one value appended to both environments.
Instances For
DAG terms + arguments (mutual) #
A well-typed DAG term.
Read this as: “under environment Γ, this term produces a tensor of shape τ”.
- var
{Γ : List Spec.Shape}
{s : Spec.Shape}
(i : Var Γ s)
: Term Γ s
Variable read (shape-indexed de Bruijn position in the environment).
- cast
{Γ : List Spec.Shape}
{σ τ : Spec.Shape}
: Term Γ σ → σ = τ → Term Γ τ
Cast a term’s output shape along a propositional equality.
This is an internal hygiene tool: when we build terms programmatically (e.g. by lowering a higher-level syntax into DAG form), we often end up with goals like “
Γ.get i = τ” that are true but not definitional.Using a
castnode keeps the term in constructor form (so evaluators/compilers can still pattern match), and pushes the non-definitional equality into the semantics where it can be handled bycases h. - castEnv
{Γ Γ' : List Spec.Shape}
{τ : Spec.Shape}
: Term Γ τ → Γ = Γ' → Term Γ' τ
Cast a term’s environment along a propositional equality.
This is useful when normalizing list-association/parenthesization choices in
Γwithout changing meaning. - op
{Γ ins : List Spec.Shape}
{τ : Spec.Shape}
: PrimOp ins τ → Args Γ ins → Term Γ τ
Apply an n-ary primitive op to n arguments.
- let1
{Γ : List Spec.Shape}
{σ τ : Spec.Shape}
: Term Γ σ → Term (Γ ++ [σ]) τ → Term Γ τ
Let-bind a single intermediate value, extending the environment.
Instances For
A typed list of argument terms.
Args Γ [s₁, …, sₙ] is a tuple of n terms, each well-typed under the same environment Γ,
with corresponding shapes s₁, …, sₙ.
- nil {Γ : List Spec.Shape} : Args Γ []
- cons {Γ : List Spec.Shape} {s : Spec.Shape} {ss : List Spec.Shape} : Term Γ s → Args Γ ss → Args Γ (s :: ss)
Instances For
Rename every free variable in a typed operation-argument list.
Instances For
Rename every free variable of a term while preserving its tensor shape.
Instances For
Preserve a term when an unrelated value is prepended to its environment.
Instances For
Preserve typed operation arguments when a value is prepended to their environment.
Instances For
Preserve a term when an unrelated value is appended to its environment.
Instances For
Preserve a term when an arbitrary typed environment is appended.
Instances For
Select the term assigned to a shape-indexed variable.
Instances For
Concatenate two typed argument lists living in the same graph environment.
Instances For
Split arguments at a type-level list boundary.
This is the argument-list counterpart of TensorPack.splitAppend. It is useful when a model owns a
concatenated parameter ABI but its implementation is assembled recursively from smaller models:
each component receives exactly the terms belonging to its part of the ABI, with every tensor
shape retained by the type checker.
Instances For
Splitting arguments immediately after concatenating them recovers both original lists.
Concatenating both parts of a split recovers the original typed argument list.
View every entry of a typed environment as a term in that same environment.
The result preserves the order and shape indices of Γ. Large graph definitions can therefore
pattern-match once on vars Γ instead of selecting every input by a numeric index and separately
proving that the selected position has the expected shape.
Instances For
Selecting from renamed arguments renames the selected term.
Lookup commutes with embedding an argument list below one new graph variable.
Selecting a variable from the complete environment returns that variable as a term.
Typed substitution #
Substitution is the operation used to inline one graph into another. A substitution assigns a
well-typed term in Δ to every variable in Γ; applying it replaces the free variables of a term
without changing the term's result shape. The let-binding case lifts the assignment across the
new value appended by the binder.
A shape-preserving assignment of terms to every variable in an environment.
Instances For
Extend a substitution across one value appended to both environments.
Instances For
Replace every free variable in a typed operation-argument list.
Instances For
Replace every free variable in a term by its assigned term.
Instances For
Inline a term by supplying one typed argument term for each free variable.
Instances For
A multi-result DAG block with shared A-normal-form bindings.
Block.let1 computes an intermediate once and makes it available to every eventual result. This
is the representation needed by recurrent steps that return both a new state and an output derived
from that state.
- ret
{Γ outs : List Spec.Shape}
: Args Γ outs → Block Γ outs
Return one term for each output shape.
- let1
{Γ outs : List Spec.Shape}
{σ : Spec.Shape}
: Term Γ σ → Block (Γ ++ [σ]) outs → Block Γ outs
Compute one shared intermediate and append it to the environment.
Instances For
Replace every free variable in a multi-result block.
Instances For
Inline a multi-result block by supplying all of its free variables.
Instances For
Compose two multi-output blocks.
The second block sees the original environment followed by every result of the first block. Any
let1 bindings inside the first block remain shared. This is the typed DAG analogue of binding a
tuple-valued computation and is the basic operation needed to compose recurrent cells, residual
branches, and encoder-decoder stages.
Instances For
Feed every output of first to second, preserving the original environment.