Module Tolk_frontend.Op
Composed operations.
Higher-level operations built from movement, element-wise, and reduction primitives.
Broadcasting
broadcasted a b is (a, b) broadcast to a common shape and promoted to a common dtype; with ~reverse:true the pair is returned swapped. Linking this module installs it as Tensor.broadcasted, which the element-wise operations use.
Assignment
assign t x writes the values of x into the storage of t and returns t. The write is recorded in the graph as an effect on t's buffer: nothing executes until a realization, and every read of the buffer built after the assignment observes the written values. When t is a view (a slice of a larger tensor), the write lands in the viewed region and every live tensor aliasing the underlying buffer is repointed to depend on it. x is broadcast to the shape of t; assigning a tensor to itself is a no-op.
Statistics
mean t is the arithmetic mean along the reduced axes (default: all). Integer inputs produce a float result.
var t is the variance along the reduced axes. correction (default 1) is subtracted from the element count in the denominator, giving the unbiased estimator by default.
std t is the standard deviation, the square root of var.
layernorm t normalises t along axis (default [-1]) to zero mean and unit variance, computed with the biased variance estimator and stabilised by eps (default 1e-5) inside the square root.
Joining
cat t others concatenates t and others along axis dim. All tensors must share every axis except dim.
stack t others joins equally shaped tensors along a new axis inserted at dim.
Matrix multiplication
val dot : ?dtype:Tolk_uop.Dtype.Val.t -> Tensor.t -> Tensor.t -> Tensor.tdot a w contracts the last axis of a with the matching axis of w: the last axis of w when w is 1-D, otherwise its second-to-last. Leading axes broadcast, giving batched matrix multiplication. dtype sets the accumulation dtype.
val matmul : ?dtype:Tolk_uop.Dtype.Val.t -> Tensor.t -> Tensor.t -> Tensor.tmatmul a b is dot a b.
Padding
val pad_constant :
Tensor.t ->
(int * int) option list ->
Tensor.scalar ->
Tensor.tpad_constant t padding value pads t with the constant value. padding gives, per axis in order, the count to add before and after; None leaves an axis unchanged, and negative counts shrink. When value is non-zero the result dtype is promoted to hold it.
val pad :
?mode:pad_mode ->
?value:Tensor.scalar ->
Tensor.t ->
(int * int) option list ->
Tensor.tpad t padding pads t as in pad_constant, with mode choosing how new positions are filled (value applies only to Constant, the default). Negative counts shrink the axis after padding.
Scans
cumsum t is the cumulative sum along axis (default 0); the result has the same shape as t.
cumprod t is the cumulative product along axis (default 0).
cummax t is (values, indices) of the cumulative maximum along axis (default 0): values.(i) is the maximum of the prefix up to i, and indices.(i) is the position of that maximum.
cummin t is (values, indices) of the cumulative minimum along axis.
Indexing
val getitem : Tensor.t -> Movement.index list -> Tensor.tgetitem t indices selects a sub-tensor of t, with one Movement.index per axis applied from the outermost inward. Integer indices (Movement.index.I) drop their axis, slices (Movement.index.R, Movement.index.All) keep a strided range, Movement.index.New inserts a size-1 axis, and Movement.index.Ellipsis fills the unaddressed axes.
An integer-tensor index (Movement.index.T) performs advanced indexing: its elements gather positions along the axis, and several such indices broadcast against each other into a shared leading block of axes. Out-of-bounds gathered positions read as 0.
one_hot index num_classes adds a trailing axis of length num_classes and sets, for each index value, a 1 at that position and 0 elsewhere. index must be an integer tensor.
argmax t is the integer index of the maximum along axis. With no axis the whole tensor is flattened first. On ties the last occurrence wins.
argmin t is the index of the minimum, as argmax on the reflected tensor.
sort t is (values, indices) sorting t along dim (default -1), ascending unless descending. values is t reordered and indices gives each sorted element's original position along dim. Equal elements keep their input order.
argsort t is the indices of sort: the permutation that orders t along dim.
topk t k is (values, indices) of the k largest elements of t along dim (default -1), or the smallest when largest is false, ordered.
gather t ~dim index selects along dim using index: the output has the shape of index, and output position p holds t at p with its dim coordinate replaced by index.(p). index must be an integer tensor with the same rank as t, and no larger than t on every axis other than dim.
scatter t ~dim index src is a copy of t with each element of src written at the position given by the matching element of index along dim. index and src must have the same rank as t; index must be no larger than src, and no larger than t off dim. When an index repeats, the last write wins. Use scatter_reduce to combine colliding writes.
val scatter_reduce :
Tensor.t ->
dim:int ->
Tensor.t ->
Tensor.t ->
reduce:[ `Sum | `Prod | `Mean | `Amax | `Amin ] ->
?include_self:bool ->
unit ->
Tensor.tscatter_reduce t ~dim index src ~reduce () scatters src into t along dim like scatter, but reduces all values landing on the same position with reduce (sum, product, mean, maximum, or minimum). The original t value participates in the reduction unless include_self is false.
val masked_select :
?fill_value:Tensor.scalar ->
Tensor.t ->
Tensor.t ->
size:int ->
Tensor.tmasked_select t mask ~size is the 1-D tensor of the elements of t where mask is true, in row-major order, packed into a fixed length size. mask must be boolean and broadcast to the shape of t. If fewer than size elements are kept the remainder is filled with fill_value (default 0); if more, the excess is dropped. The fixed size keeps the result shape static (and thus jittable).
val nonzero : ?fill_value:Tensor.scalar -> Tensor.t -> size:int -> Tensor.tnonzero t ~size is a size-by-rank integer tensor whose rows are the coordinates of the non-zero elements of t, in row-major order. Rows past the number of non-zero elements are filled with fill_value (default 0); excess coordinates are dropped. As with masked_select, the fixed size keeps the shape static.
Triangular masks
triu t keeps the upper triangle of the last two axes of t and zeros the rest. diagonal shifts the boundary: 0 is the main diagonal, positive moves it up, negative down.
tril t keeps the lower triangle of the last two axes of t and zeros the rest, with diagonal as in triu.
Log-space reductions
Numerically stable reductions that work in log space, subtracting the running maximum before exponentiating. Scalar inputs are returned unchanged.
logsumexp t is log (sum (exp t)) over axis (default: all axes).
val softmax : ?axis:int -> ?dtype:Tolk_uop.Dtype.Val.t -> Tensor.t -> Tensor.tsoftmax t rescales t along axis (default -1) into non-negative values summing to 1. dtype casts the shifted input before exponentiating.
val log_softmax :
?axis:int ->
?dtype:Tolk_uop.Dtype.Val.t ->
Tensor.t ->
Tensor.tlog_softmax t is the logarithm of softmax, computed stably.
logcumsumexp t is the cumulative logsumexp along axis (default 0): output position k is log (sum (exp t.(0..k))).
Attention
val scaled_dot_product_attention :
?attn_mask:Tensor.t ->
?is_causal:bool ->
Tensor.t ->
Tensor.t ->
Tensor.t ->
Tensor.tscaled_dot_product_attention q k v is softmax (q @ k^T / sqrt d) @ v over the last two axes, where d is the size of the last axis of q. The score product accumulates in at least float32 and the softmax is applied at q's dtype. attn_mask is added to the scores before the softmax; a boolean mask contributes 0 where true and negative infinity where false. is_causal (default false) instead masks each query position from attending past its own, as if a lower-triangular boolean mask were given.
Convolution and pooling
These operate on tensors shaped (batch, channels, spatial...). The number of spatial axes follows the kernel, so the same functions cover 1-D, 2-D, and higher convolutions. stride and dilation accept a single-element list, broadcast to every spatial axis. padding is either a single value (all sides), one value per spatial axis, or two per spatial axis.
val conv2d :
?bias:Tensor.t ->
?groups:int ->
?stride:int list ->
?dilation:int list ->
?padding:int list ->
?dtype:Tolk_uop.Dtype.Val.t ->
Tensor.t ->
Tensor.t ->
Tensor.tconv2d x weight convolves x with weight (shape (out_channels, in_channels/groups, kernel...)), optionally adding bias and splitting channels into groups. dtype sets the accumulation dtype.
val avg_pool2d :
?kernel_size:int list ->
?stride:int list ->
?dilation:int list ->
?padding:int list ->
Tensor.t ->
Tensor.tavg_pool2d x averages each sliding window over the spatial axes. stride defaults to kernel_size (non-overlapping windows).
val max_pool2d :
?kernel_size:int list ->
?stride:int list ->
?dilation:int list ->
?padding:int list ->
Tensor.t ->
Tensor.tmax_pool2d x takes the maximum of each sliding window over the spatial axes. stride defaults to kernel_size.
Ranges
val arange :
?stop:int ->
?step:int ->
?dtype:Tolk_uop.Dtype.Val.t ->
int ->
Tensor.tarange start is the 1-D tensor [0; 1; ...; start-1]. arange start ~stop ranges over [start, stop), and ~step sets the spacing (which may be negative). The length is ceil((stop - start) / step), clamped to zero. dtype defaults to the default integer type.
val linspace : ?dtype:Tolk_uop.Dtype.Val.t -> float -> float -> int -> Tensor.tlinspace start stop steps is the 1-D tensor of steps values evenly spaced over [start, stop], inclusive of both ends. dtype defaults to the default float type.
val eye : ?m:int -> ?dtype:Tolk_uop.Dtype.Val.t -> int -> Tensor.teye n is the nxn identity matrix. ~m sets a different column count, giving an nxm matrix with ones on the main diagonal. dtype defaults to the default float type.