Module Tolk_frontend.Movement
Shape movement.
These operations rearrange or resize a tensor's axes without touching the stored values: they build views. Every one returns its input unchanged when the requested shape equals the current shape.
Axis arguments accept negative indices, counted from the last axis. Shape arguments on the plain entry points are concrete integers; the symbolic_* entry points take dimension nodes instead and accept symbolic values. reshape, expand, broadcast_to, pad, squeeze, unsqueeze, flatten, transpose, permute, and flip also operate on tensors whose current shape is symbolic. The remaining operations (pool, unfold, split, repeat, unflatten, and strided indexing) require concrete shapes and raise Invalid_argument on a symbolic dimension.
reshape t dims returns a view of t with shape dims, preserving the element order and total count. At most one dimension may be -1, whose size is inferred so that the total count is preserved.
val symbolic_reshape : Tensor.t -> Tolk_uop.Uop.t list -> Tensor.tsymbolic_reshape t dims is reshape over dimension nodes, with no -1 inference. The element count must be preserved provably.
expand t dims broadcasts t to shape dims. A -1 entry keeps the corresponding input size. t is first left-aligned with 1s, so expand may add leading axes. Each broadcast axis must have input size 1.
broadcast_to t dims is expand to an explicit target shape, with no -1 handling: t is left-aligned with 1s and every axis must already match dims or have size 1.
val symbolic_broadcast_to : Tensor.t -> Tolk_uop.Uop.t list -> Tensor.tsymbolic_broadcast_to t dims is broadcast_to over dimension nodes. A symbolic axis matches when it carries the same expression in t and dims, or when t has size 1 there.
permute t order reorders the axes of t so that output axis k is input axis List.nth order k. order must be a permutation of the axes.
pad t padding pads t with zeros. padding gives, per axis in order, the number of elements to add before and after. Every axis must be listed.
shrink t bounds slices t. bounds gives, per axis in order, a half-open (start, stop) range. Every axis must be listed.
val symbolic_shrink :
Tensor.t ->
(Tolk_uop.Uop.t * Tolk_uop.Uop.t) option list ->
Tensor.tsymbolic_shrink t bounds is shrink over dimension nodes: each bound is a half-open (start, stop) pair of possibly symbolic values, whose axis size becomes stop - start, or None to leave the axis unchanged. The upper bound of every slice must provably stay inside the input axis.
squeeze t removes all size-1 axes. squeeze ~dim t removes only axis dim, and returns t unchanged if that axis is not size 1.
unsqueeze t dim inserts a new size-1 axis at position dim.
transpose t swaps axes dim0 and dim1 (default the last two).
flatten t collapses the axes from start_dim to end_dim inclusive (default: all axes) into a single axis.
unflatten t dim sizes splits axis dim into the axes sizes. The product of sizes must equal the size of dim.
repeat t repeats tiles t, repeating it List.nth repeats k times along axis k. repeats may be longer than ndim t, adding leading axes.
shrink_to t dims shrinks each axis to size dims, keeping 0 as the start. None leaves an axis unchanged. Every axis must be listed.
pad_to t dims pads each axis on the right up to size dims. None leaves an axis unchanged. Every axis must be listed.
pool t ~k () forms sliding windows over the last List.length k axes of t for a convolution or pooling. Each of those axes is replaced by an output-position axis and a kernel axis: an input of shape (..., i0, i1, ...) with kernel k, strides stride (default all 1), and dilations dilation (default all 1) becomes (..., o0, o1, ..., k0, k1, ...) where oj = ceil((ij - dj*(kj-1)) / sj). A reduction over the trailing kernel axes then realises the window operation. stride and dilation may be a single-element list, which is broadcast to every kernel axis.
unfold t dim ~size ~step replaces axis dim with overlapping windows: a new axis of window count (shape.(dim) - size) / step + 1 followed by a trailing axis of length size. The window at index w holds t's slice [w*step, w*step + size) along dim.
split t size cuts t along dim (default 0) into consecutive chunks of size elements; the final chunk is smaller when size does not divide the axis.
Indexing
An index describes how one axis (or a freshly inserted one) is selected. A list of them addresses a tensor from the outermost axis inward; the whole selection is realised by Op.getitem.
type index = | I of int(*Pick a single position, dropping the axis. Negative counts from the end.
*)| R of int option * int option * int option(*A
*)start:stop:stepslice; each bound omitted withNone. A negative bound counts from the end and a negativestepreverses direction.| All(*Keep the whole axis, equivalent to
*)R (None, None, None).| New(*Insert a new size-
*)1axis at this position.| Ellipsis| T of Tensor.t(*Advanced indexing: an integer tensor whose elements select positions along the axis. Negative elements count from the end.
*)
type parsed = {size : int;(*Length of the resulting axis (before any collapse).
*)boundary : int * int;(*Half-open
*)[lo, hi)window kept from the axis.stride : int;(*Step through the window; negative reverses it.
*)collapse_dim : bool;(*Whether the axis is dropped, as for an integer index.
*)resolved : resolved;
}A single index resolved against a concrete axis size.
parse_view_index index size resolves a non-advanced index against an axis of length size.