Indexed Models and Embeddings #
This module defines models with non-differentiable tensor inputs, their scalar objectives, and
embedding-table builders. Import NN.API.Neural.Indexed when constructing or executing an indexed
model; ordinary sequential builders remain in NN.API.Neural.Builders.
A shape-typed model with one non-differentiable tensor input.
Shapes of trainable parameters and persistent buffers.
- kindValue : String
- initialStateValue : TorchLean.nn.State Float self.stateShapes
- initializationPlanValue : Option (Runtime.Autograd.Model.Module.RuntimeInit.Plan self.stateShapes)
- inputValidatorValue : TorchLean.Tensor β σ → Except String Unit
- programValue (_mode : TorchLean.nn.Mode) {α : Type} [TorchLean.Storage α] [Context α] : Runtime.Autograd.Model.ProgramWithDataInputs α β self.stateShapes [σ] τ
Instances For
Construct an indexed model from its complete runtime definition.
Build an indexed-model placeholder whose static configuration is known to be invalid.
The placeholder owns no parameters or buffers and is rejected by IndexedModel.validate before
allocation or execution.
Instances For
The readers below are opaque on purpose. IndexedModel keeps every field but stateShapes
private, so the only way to look inside is through this namespace, and an opaque reader is one
that simp, decide and rfl cannot unfold back into the field. Builders therefore stay free to
change how a model is represented without any downstream proof noticing, which is the whole reason
the fields were made private in the first place.
Label the builder gave this model, used in summaries and error messages.
Parameters and buffers the model starts from, in stateShapes order.
How the runtime should fill the state at allocation time, or none for an unplanned model.
One flag per state entry saying whether the optimizer may update it.
Static verdict on the configuration, checked before anything is allocated.
Verdict on one concrete input, for conditions the shape type cannot express.
The runtime program for a given Mode, scalar-polymorphic in its execution type.
Model label used in summaries and diagnostics.
Instances For
Semantic initial values for the complete model state.
Instances For
Gradient flags aligned with stateShapes.
Instances For
Validate static model configuration before allocation or graph execution.
Instances For
Append an ordinary sequential model after an indexed-input model.
Instances For
Scalar objectives #
Pair an indexed-input model with a scalar loss.
The resulting training module accepts one ordinary target tensor followed by the model's non-differentiable input tensor. Keeping those packs separate ensures that indices cannot receive gradients or be reinterpreted through the model's floating-point element type.
The target shape is independent of the model output shape, so this constructor also supports losses whose labels use a different representation from the prediction. Training mode is the default.
Instances For
Pair an indexed-input model with mean-squared error.
Instances For
A reusable trainable embedding table with vocabularySize rows and vectors of length
embeddingWidth.
Unlike IndexedModel, this definition is independent of the eventual token-tensor shape. Calling
table.model indices specializes it to an input shape with bounded Fin vocabularySize indices.
Instantiating that model produces mutable weight storage; the definition itself remains immutable
so it can be lowered and used in proofs.
Instances For
Construct an embedding table at the runtime-builder boundary.
How the runtime should fill that matrix at allocation time.
Whether the optimizer may update the table, false for a frozen lookup.
Construct a rejected table value without exposing parameter state through Embedding.model.
Instances For
Construction options for a freshly initialized embedding table.
- weightInitialization : Init.Scheme
Initialization scheme for the table.
The default agrees with
torch.nn.Embedding.reset_parameters: independent samples from the standard normal distribution. Language-model constructors normally override this with their architecture-specific initialization, such as GPT-2's standard deviation0.02. - freeze : Bool
Freeze the table by excluding it from reverse-mode parameter gradients.
Instances For
Construct an embedding from an exact initial weight table.
This is the typed counterpart of passing _weight to torch.nn.Embedding. The supplied tensor
fixes both dimensions at compile time and is also recorded as an exact row-major runtime
initializer, so CPU and CUDA module construction start from the same payload.
Instances For
Specialize an embedding table to a concrete index-tensor shape.