Eager Tensor Operations #
PyTorch-style tensor operations backed by the eager CPU/CUDA tapes. These wrappers record runtime
nodes, dispatch CUDA kernels when requested, and preserve the typed TensorRef surface.
Tensor ops (eager tape wrappers) #
The following definitions are the eager front-end for Runtime.Autograd.Tape.* primitives. Each one:
- reads the current tape from
s.tape, - appends a new node/leaf via a
Tape.*constructor, - writes the updated tape back, and
- returns a fresh
TensorRefpointing to the new node id.
PyTorch comparison: this is the standard eager autograd mechanism (a dynamic tape of ops).
Dispatch an eager op with optional CUDA support.
When Options.device = .cuda, any op whose CUDA implementation returns none will throw.
TorchLean's CUDA eager mode has no per-op CPU fallback: either the op is supported by CUDA, or it errors immediately.
Instances For
Record elementwise addition a + b. PyTorch: torch.add.
Instances For
Record elementwise subtraction a - b. PyTorch: torch.sub.
Instances For
Record elementwise multiplication a * b. PyTorch: torch.mul.
Instances For
Record scaling by a scalar constant. PyTorch: x * c.
Instances For
Record elementwise absolute value. PyTorch: torch.abs.
Instances For
Record elementwise square root. PyTorch: torch.sqrt.
Instances For
Record elementwise clamp to [minVal,maxVal]. PyTorch: torch.clamp.
Instances For
Record elementwise maximum. PyTorch: torch.maximum.
Instances For
Record elementwise minimum. PyTorch: torch.minimum.
Instances For
Record elementwise ReLU. PyTorch: torch.relu / torch.nn.functional.relu.
Instances For
Record elementwise sigmoid. PyTorch: torch.sigmoid.
Instances For
Record elementwise tanh. PyTorch: torch.tanh.
Instances For
Record softmax (shape-preserving).
PyTorch comparison: torch.softmax(x, dim=...) (dimension convention is chosen by the underlying
tape op).
Instances For
Record stable log-softmax (shape-preserving, last-axis convention).
PyTorch comparison: torch.nn.functional.log_softmax(x, dim=-1).
Instances For
Record elementwise softplus. PyTorch: torch.nn.functional.softplus.
Instances For
Record elementwise exponential. PyTorch: torch.exp.
Instances For
Record elementwise log. PyTorch: torch.log.
Instances For
Record elementwise inverse 1/x. PyTorch: torch.reciprocal.
Instances For
Record elementwise log with epsilon guard.
PyTorch comparison: torch.log(torch.clamp(x, min=ε)).
Instances For
Sum-reduce all elements to a scalar. PyTorch: x.sum().
Instances For
Flatten a tensor to a 1D vector. PyTorch: torch.flatten.
Instances For
Reshape a tensor while preserving total number of elements.
PyTorch comparison: torch.reshape / view (when valid).
Instances For
Transpose a 2D matrix. PyTorch: x.t() / x.transpose(0,1).
Instances For
Swap two adjacent axes at a given depth. PyTorch analogue: x.transpose(dim, dim+1).
Instances For
Permute a 3D tensor (a,b,c) → (b,c,a). PyTorch: x.permute(1,2,0).
Instances For
Permute a 3D tensor (a,b,c) → (c,a,b). PyTorch: x.permute(2,0,1).
Instances For
Swap the last two axes of a 3D tensor (a,b,c) → (a,c,b). PyTorch: x.transpose(1,2).
Instances For
Broadcast a tensor to a larger shape. PyTorch: implicit broadcasting / expand.
Instances For
Sum-reduce along axis. PyTorch: torch.sum(x, dim=axis).
Instances For
Mean-reduce along axis. PyTorch: torch.mean(x, dim=axis).
Instances For
Gather a scalar from a 1D vector with a Fin n index. PyTorch: x[i].
Instances For
Gather a row from a 2D tensor with a Fin rows index. PyTorch: x[i] for 2D tensors.
Instances For
Gather a scalar from a 1D vector with a raw Nat index (totalized by the tape op).
Instances For
Dynamic gather scalar using an index stored in NatRef.
Instances For
Dynamic gather row using an index stored in NatRef (out-of-range gives a zero row).
Instances For
Gather k scalars using an explicit index tensor. PyTorch analogue: gather / advanced
indexing.
Instances For
Gather k rows using an explicit index tensor. PyTorch: index_select(dim=0, index=...).
Instances For
Gather k scalars using indices stored in the nat-environment (NatVecRef).
Instances For
Gather k rows using indices stored in the nat-environment (NatVecRef).
Instances For
Scatter-add into a vector: return a copy of x with x[i] += v.
Instances For
Scatter-add into a matrix row: return a copy of x with x[i,:] += v.
Instances For
Fully-connected linear layer y = w x + b (matvec).
If opts.fastKernels is enabled, uses a runtime-only fast kernel implementation.
PyTorch comparison: torch.nn.functional.linear(x, weight=w, bias=b).
Instances For
Mean-squared-error loss returning a scalar.
If opts.fastKernels is enabled, uses a runtime-only fast kernel implementation.
PyTorch comparison: torch.nn.functional.mse_loss(..., reduction=\"mean\").
Instances For
Layer normalization over embedding dimension. PyTorch: nn.LayerNorm / functional.layer_norm.
Instances For
BatchNorm for channel-first images (C,H,W) (no batch axis). PyTorch: nn.BatchNorm2d
(conceptually).
Instances For
Multi-head self-attention (typed, proof-friendly). PyTorch: nn.MultiheadAttention
(conceptually).
Instances For
N-D convolution for channels-first tensors (inC, spatial...) (no batch axis).
This is the generic counterpart to conv2d.
PyTorch comparison: torch.nn.functional.conv{d}d specialized to a single sample.
Instances For
2D convolution for channel-first images (C,H,W) (no batch axis). PyTorch:
torch.nn.functional.conv2d.
Instances For
N-D transpose convolution for channels-first tensors (inC, spatial...) (no batch axis).
This is the generic counterpart to conv_transpose2d.
PyTorch comparison: torch.nn.functional.conv_transpose{d}d specialized to a single sample.
Instances For
2D transpose convolution for channel-first images (C,H,W) (no batch axis). PyTorch:
torch.nn.functional.conv_transpose2d.
Instances For
2D matrix multiplication. PyTorch: torch.matmul for 2D tensors.
Instances For
Batched matrix multiplication. PyTorch: torch.bmm.
Instances For
Concatenate two vectors along dim 0. PyTorch: torch.cat([a,b], dim=0).
Instances For
Concatenate along dim 0 for tensors with leading dimension. PyTorch: torch.cat(..., dim=0).
Instances For
Slice along dim 0: x[start:start+len]. PyTorch: standard slicing.
Instances For
N-D max pooling for channels-first tensors (C, spatial...) (no batch axis).
PyTorch comparison: torch.nn.functional.max_pool1d / max_pool2d / max_pool3d depending on the
spatial rank d.
Instances For
N-D average pooling for channels-first tensors (C, spatial...) (no batch axis).
PyTorch comparison: torch.nn.functional.avg_pool1d / avg_pool2d / avg_pool3d depending on the
spatial rank d.
Instances For
N-D smooth max pooling (log-sum-exp surrogate) for channels-first tensors (C, spatial...).
This is a differentiable approximation to max pooling; PyTorch does not expose it as a single
primitive, but it can be emulated with logsumexp over local windows.
Instances For
2D max-pooling (no batch axis). PyTorch: torch.nn.functional.max_pool2d.
Instances For
2D max-pooling with padding (no batch axis). PyTorch: max_pool2d(..., padding=...).
Instances For
Alias for max_pool2d_pad (PyTorch-style shorthand).
Instances For
Smooth max-pooling (softmax pooling). Not a standard PyTorch primitive; see
Torch.LinkedSession.smooth_max_pool2d.
Instances For
2D average-pooling (no batch axis). PyTorch: torch.nn.functional.avg_pool2d.
Instances For
2D average-pooling with padding (no batch axis). PyTorch: avg_pool2d(..., padding=...).
Instances For
Alias for avg_pool2d_pad (PyTorch-style shorthand).