Torch Utils #
Small helpers for writing PyTorch-style training loops on top of Runtime.Autograd.Torch.
This file focuses on demo/training ergonomics:
- extract scalar values,
- build small
TLists, - run simple SGD loops for
Torch.ScalarTrainer.
Stateful optimizer loops live in Runtime.Autograd.TorchLean, because those depend on
TorchLean.Optim. Keeping that dependency out of this low-level utility module prevents the
session/ref layer from depending upward on the model/optimizer facade.
Initialization helpers (Float constants) #
Deterministic initialization schemes for small demos (stored as Float constants).
PyTorch comparison:
.zeros/.onescorrespond totorch.nn.init.zeros_/torch.nn.init.ones_.uniform lo hicorresponds totorch.nn.init.uniform_(with explicita=lo,b=hi).xavierUniform fanIn fanOutcorresponds totorch.nn.init.xavier_uniform_.kaimingUniform fanIncorresponds totorch.nn.init.kaiming_uniform_withnonlinearity="relu"
References:
- https://pytorch.org/docs/stable/nn.init.html
- https://pytorch.org/docs/stable/generated/torch.nn.init.xavier_uniform_.html
- https://pytorch.org/docs/stable/generated/torch.nn.init.kaiming_uniform_.html
- zeros : Scheme
- ones : Scheme
- uniform (lo hi : Float) : Scheme
- xavierUniform (fanIn fanOut : ℕ) : Scheme
- kaimingUniform (fanIn : ℕ) : Scheme
Instances For
Next state of a simple 32-bit linear congruential generator (LCG).
This exists to make demos deterministic and reproducible; it is not statistically strong and it is not cryptographically secure.
Instances For
Interpret the 32-bit LCG state as a Float in [0, 1).
Instances For
Deterministic U[0,1) sampler derived from a seed and scalar index.
Implementation note: this is the LCG iterated idx+1 times, then normalized to [0,1).
Instances For
Sample the idx-th scalar of a tensor initialized using Scheme.
This is the scalar-level primitive used by Init.tensor.
Instances For
Create a Tensor Float s by sampling a Scheme deterministically.
This is intended for small demo models. It is not optimized.
PyTorch comparison: this mimics using torch.nn.init.* routines on freshly allocated parameters,
but here we work with pure Tensor Float s values (no mutation) and use a deterministic
seeded sampler for reproducibility.
Instances For
Instances For
Xavier/Glorot-uniform initializer for 2D weight matrices.
PyTorch comparison: torch.nn.init.xavier_uniform_ with gain=1.
Instances For
Kaiming/He-uniform initializer for 2D weight matrices.
PyTorch comparison: torch.nn.init.kaiming_uniform_ with nonlinearity="relu" and default
parameters (so the bound is sqrt(6/fan_in)).
Instances For
Small Sample Generators (Float Constants) #
Turn a point (x1,x2) into a Tensor Float (.dim 2 .scalar).
Instances For
Turn a scalar y into a Tensor Float (.dim 1 .scalar).
Instances For
Affine map y = w1*x1 + w2*x2 + b for building small regression datasets.
Instances For
Small conveniences for scalar training demos #
Extract the scalar value from a scalar-shaped tensor.
PyTorch comparison: like t.item() for a 0-dim tensor.
Instances For
Build a one-element TList (useful for curried trainer APIs).
Instances For
TList syntax sugar #
Build a two-element TList (useful for curried trainer APIs).
Instances For
Build a three-element TList (useful for curried trainer APIs).
Instances For
Build a four-element TList (useful for curried trainer APIs).
Instances For
Uncurried forward pass for ScalarTrainer.
ScalarTrainer.forward is stored as a curried function over the input shapes; this helper lets you
pass a TList (like a tuple of tensors).
Instances For
Uncurried backward pass for ScalarTrainer.
Returns per-parameter gradients (aligned with paramShapes).
Instances For
Uncurried SGD step for ScalarTrainer.
PyTorch comparison: analogous to loss.backward(); optimizer.step() for a fixed SGD optimizer,
except here the trainer bundles the update rule.
Instances For
Train steps SGD updates, cycling through samples.
PyTorch comparison: this matches the common eager training skeleton:
for step in range(steps):
batch = dataset[step % len(dataset)]
loss = forward(batch)
step(lr, batch) # typically: loss.backward(); optimizer.step()
Note: ScalarTrainer.step is the "bundled SGD optimizer" for the trainer. Stateful optimizers
(Adam, RMSProp, ...) are exposed from Runtime.Autograd.TorchLean.
Instances For
Evaluate mean loss over a dataset.
PyTorch comparison: like running a model in torch.no_grad() over a dataloader and averaging
the scalar loss values, except here we call ScalarTrainer.forwardT directly.