Functional Core #
Small functional helpers built from the primitive Runtime.Autograd.Torch.Ops API.
These definitions are shared by eager and typed graph execution, so they stay close to the primitive operation names: elementwise helpers, broadcasting, embedding lookup, reductions, and seeded RNG.
Elementwise helpers #
Elementwise square: $x\mapsto x^2$.
PyTorch analogue: torch.square.
Instances For
Elementwise transcendentals for scientific forward models #
Scientific forward models often use affine terms together with exp, log, sin, or cos.
These helpers expose the corresponding primitives through nn.functional, so the
forward equation can be written once as a pure Function.Fn and differentiated
by the autograd engine. Each helper wraps a primitive with a registered backward
rule, so reverse-mode jacrev and grad work through the expression.
PyTorch analogues: torch.exp, torch.log, torch.sin, torch.cos, and c·x / c·x + k via
torch.mul/torch.add against scalars.
Elementwise exponential $x\mapsto e^x$. PyTorch: torch.exp.
Instances For
Elementwise sine of angles in radians, differentiable through eager and typed graph execution.
Instances For
Elementwise cosine of angles in radians, with derivative -sin(x).
Instances For
Elementwise natural log $x\mapsto\log x$. PyTorch: torch.log.
Domain: for real-valued reasoning, assume positive inputs. This is the real
natural log only on $x>0$. TorchLean's eager CPU tape, IR evaluator, and proved
forward-fragment evaluator reject nonpositive inputs explicitly; typed graph
closures hit a runtime panic on a bad raw-log domain, and CUDA follows the native
buffer operation. Use safeLog when the model needs a total epsilon-protected
log-like operation.
Instances For
Multiply by a scalar $c$: $x\mapsto cx$.
A re-export of the primitive Ops.scale through the functional API. Ops.scale
already powers mean; this definition gives users the direct
nn.functional.* name too.
Instances For
Add a constant scalar $c$ to every element: $x\mapsto x+c$. Builds the
constant via Ops.const at scalar shape and broadcasts it to s (same pattern
as the dropout keep-probability broadcast).
Instances For
Scalar affine map $x\mapsto cx+k$.
This is a common building block in physical forward models, including the
SMAP-NISAR AVS surface and vegetation terms. It composes scale and shift.
Instances For
Checkpointing (semantics-first identity wrapper) #
Checkpoint wrapper matching PyTorch's memory saving pattern.
In this codebase, checkpointing is a semantic identity wrapper ($\operatorname{checkpoint}(f,x)=f(x)$). Backends that implement recomputation can refine this hook without changing the mathematical meaning.
Instances For
Detach #
Stop-gradient boundary (forward identity).
Instances For
Broadcasting helpers #
Broadcasting add: compute x + y after broadcasting both inputs to the target shape t.
PyTorch analogue: torch.add (broadcasting semantics).
Instances For
Broadcasting multiply: compute x * y after broadcasting both inputs to the target shape t.
PyTorch analogue: torch.mul (broadcasting semantics).
Instances For
Indexing helpers #
Embedding lookup on an already flat vector of token ids: row select along axis 0.
The public embedding reshapes down to this case and back, so all the interesting work happens
here and the wrapper only moves axes around.
Instances For
Embedding lookup for an arbitrary tensor of bounded token ids.
The indexing primitive operates on a flat vector of indices. This wrapper flattens any input shape,
selects the corresponding rows, and restores the original axes with the embedding dimension
appended. The element type Fin vocabularySize makes an out-of-range token unrepresentable.
Instances For
Reductions #
Mean reduction: $\operatorname{mean}(x)=\operatorname{sum}(x)/\operatorname{numel}(x)$.
PyTorch analogue: torch.mean.
Instances For
Seeded RNG helpers #
Deterministic U[0,1) tensor generator (seeded).
Instances For
Deterministic {0,1} mask generator (seeded) with scalar keep-probability input.
Instances For
Seeded dropout implemented as $x\odot\mathtt{mask}/\mathtt{keepProb}$, where
$\mathtt{mask}\in\{0,1\}$ is sampled from a
deterministic PRNG keyed by seed.
Instances For
Seeded dropout where the probability is supplied as a scalar tensor ref.
Model builders can store p as tensor data and pass it through the same interface as the input.
For 0 ≤ p ≤ 1, a retained entry is scaled by 1 / (1 - p) and a dropped entry is zero.
The seeded mask is held fixed during differentiation.
The denominator is 1 - p * mask: it equals 1 - p at retained entries and 1 at dropped
entries. At p = 1, every entry is dropped, so the forward value and gradients are zero without
evaluating a reciprocal at zero. Evaluation mode returns the input directly.