Ocannl_tensor.OperationComputational primitives for neural networks, integrating Tensor with Assignments.
module Asgns = Ir.Assignmentsmodule Idx = Ir.Indexingmodule Tn = Ir.Tnodemodule At : sig ... endmodule Initial_NTDSL : sig ... endval compose_op_of_spec :
?spec:??? ->
?capture_dims:??? ->
unit ->
Shape.compose_typeHelper to compute compose_op: if spec is provided, use Einsum; otherwise use Pointwise_bin.
val transpose_op_of_spec :
?spec:??? ->
?capture_dims:??? ->
unit ->
Shape.transpose_typeHelper to compute transpose_op: if spec is provided, use Permute; otherwise use Pointwise_un.
val add :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funval sub :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funval mul :
Shape.compose_type ->
op_asn:
(t:Tensor.t ->
t1:Tensor.t ->
t2:Tensor.t ->
projections:Tensor.projections ->
Tensor.comp) ->
?op_label:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funval pointmul :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funval matmul : ?grad_spec:??? -> Tensor.t -> Tensor.t -> Tensor.op_funmodule NDO_before_pow : sig ... endval is_prohibit_grad : Tensor.grad_spec option -> boolval pointpow :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Base.float ->
Tensor.t ->
Tensor.op_funmodule NDO_before_div : sig ... endmodule NTDSL_before_div : sig ... endval pointdiv :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funval relu :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval sat01 :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval exp :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval log :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval exp2 :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval log2 :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval sin :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval cos :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval sqrt :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval recip :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval recip_sqrt :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval tanh :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval neg :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval cast :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funAn identity operation whose sole purpose is to give the result a tensor node distinct from the argument's: the assignment-level precision conversion then happens between the two nodes when their precisions differ. This is the "cast twin" primitive of the mixed-precision recipes (gh-ocannl-492): pin the result at a reduced precision to make it a low-precision copy of an f32 master weight; the gradient accumulates back through the cast, widening (e.g. f16 -> f32) at the accumulating assignment.
val stretch :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
?label:??? ->
?top_down_prec:??? ->
?batch_dims:??? ->
?batch_axes:??? ->
?input_dims:??? ->
?output_dims:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
unit ->
Tensor.tAn identity operation whose result's rows resolve at the use site (gh-ocannl-544): the result widens to what its consumers demand, and the argument broadcasts into it. Operation results otherwise close down to their arguments' shapes, so this is the explicit request for use-site widening — e.g. stretch !.1.0 is a shape-inferred constant 1 usable as an einsum operand of any shape (formerly spelled 0.5 + 0.5).
val not :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funval lt :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funval le :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funval eq :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funval ne :
?spec:??? ->
?capture_dims:??? ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funval interleave : ?grad_spec:??? -> Tensor.t -> Tensor.t -> Tensor.op_funval deinterleave_even : ?grad_spec:??? -> Tensor.t -> Tensor.op_funval deinterleave_odd : ?grad_spec:??? -> Tensor.t -> Tensor.op_funval fma :
grad_spec:Tensor.grad_spec ->
Tensor.t ->
Tensor.t ->
Tensor.t ->
Tensor.op_funval where :
?spec:??? ->
?capture_dims:??? ->
grad_spec:Tensor.grad_spec ->
Tensor.t ->
Tensor.t ->
Tensor.t ->
Tensor.op_funTernary select: computes where(pred, a, b) element-wise. When ~spec is given it routes through ternary einsum: over the product space of all spec labels, at each point the predicate element selects the branch-2 or branch-3 element (select happens *before* accumulation). Labels absent from the LHS become reduction axes.
Gradient: flows to a masked by the predicate and to b masked by its complement; no gradient to the predicate.
Documented caveat: when a reduced axis is absent from a branch (branch broadcasts along it), the broadcast value is accumulated once per selected reduction iteration ("count-weighted") — this is the honest semantics of reduce-after-select. For example, with spec "p;a;b=>out" and a broadcasting along a reduced axis, a's scalar value is summed once for each reduction step where the predicate is 1.
val einsum :
?capture_dims:??? ->
Base.string ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funSimilar to the explicit mode of numpy.einsum, the binary variant. Can compute various forms of matrix multiplication, inner and outer products, etc.
Note that "a,b->c" from numpy is "a;b=>c" in OCANNL, since "->" is used to separate the input and the output axes.
val outer_sum :
?capture_dims:??? ->
Base.string ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funLike einsum, but adds instead than multiplying the resulting values.
val einsum3 :
?capture_dims:??? ->
Base.string ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.t ->
Tensor.op_funTernary einsum: contracts three tensors under a three-RHS spec rhs1 ; rhs2 ; rhs3 => lhs. The forward pass computes v[lhs] =+ mul3(v1[rhs1], v2[rhs2], v3[rhs3]) over the product space of all axis labels. Axis labels present in any RHS but absent from the LHS become reduction axes.
WARNING: for chain-structured contractions such as "ij;jk;km=>im" the fused kernel iterates the full product space (O(N⁴) for N-dimensional operands), which is less efficient than a binary chain (O(N³)). Use ternary einsum for patterns where all three operands share reduction axes, e.g. bilinear forms.
val einsum1 :
?capture_dims:??? ->
Base.string ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funSimilar to the explicit mode of numpy.einsum, the unary variant. Can permute axes, extract diagonals, compute traces etc.
Note that "a->c" from numpy is "a=>c" in OCANNL, since "->" is used to separate the input and the output axes.
val concat_sum :
?capture_dims:??? ->
?negated:??? ->
Base.string ->
?grad_spec:??? ->
Tensor.t Base.Array.t ->
Tensor.op_funval concat :
?capture_dims:??? ->
?negated:??? ->
Base.string ->
?grad_spec:??? ->
Tensor.t Base.Array.t ->
Tensor.op_funThe axis kind along which stack introduces the fresh leading axis.
val stack :
stack_axis ->
?grad_spec:??? ->
Tensor.t Base.Array.t ->
Tensor.op_funStack rhses along a fresh leading axis of kind axis (result rank = operand rank + 1, true stacking rather than concatenation along an existing axis). Each operand is unsqueezed with a size-1 axis via einsum1, then the unsqueezed intermediates are concatenated along that new axis via concat. The expansion and concatenation einsum specs are generated on the fly from the operand count.
module NDO_before_einmax1 : sig ... endval einmax1 :
?capture_dims:??? ->
?nonoverlapping:??? ->
Base.string ->
?grad_spec:??? ->
Tensor.t ->
Tensor.op_funMax-reduction einsum. The gradient gate lives in the product space of the operation — one bit per (result position, reduced position) pair — so it is exact even when reduction windows overlap (e.g. convolution-style specs with stride < window). Ties gate the full upstream gradient to every achieving position (duplication, not a normalized split).
nonoverlapping (gh-ocannl-527): the caller asserts that each RHS1 position feeds at most one result position — per convolution axis, stride at least the window extent (a full reduction trivially qualifies). On that domain the gate contracts to one bit per input position (cond_rhs1, the pre-gh-512 formulation): every input position belongs to at most one window, so no gate-bit collisions exist and the two formulations agree exactly, ties included — this is a cost specialization, not a semantics choice. The product-space intermediates it avoids cost 1.8-2.6x on the non-overlapping conv-pooling benchmarks. Outside the asserted domain the cheap gate is last-write-wins across colliding windows, i.e. wrong — the flag is a contract, not a heuristic; when in doubt keep the default.
val tropical :
?capture_dims:??? ->
?nonoverlapping:??? ->
Base.string ->
?grad_spec:??? ->
Tensor.t ->
Tensor.t ->
Tensor.op_funThis generalizes the tropical matrix multiplication to arbitrary indices combinations.
The gradient gate lives in the product space of the operation — one bit per (result position, contracted position) pair — so both gradients are exact for arbitrary specs, including overlapping reduction windows (stride < window) and RHS2 indices independent of RHS1. Ties gate the full upstream gradient to every achieving pair (duplication, not a normalized split).
nonoverlapping (gh-ocannl-527): the caller asserts the einmax1 domain condition — each RHS1 position feeds at most one result position — and additionally that the (result, contracted) pair is recoverable from the RHS1 position (RHS1's index space covers RHS2's, the convolution-like shape: kernel indices contracted against strided input indices; the pre-gh-512 g2 limitation delimits exactly this). On that domain the gate contracts to one bit per input position and both gradients agree with the product-space gate exactly, ties included — a cost specialization, not a semantics choice (1.8-2.6x on the non-overlapping conv-pooling benchmarks). Outside it the cheap gate is wrong (last-write-wins collisions, misattributed g2); when in doubt keep the default.
val offsets : ?grad_spec:??? -> Tensor.op_funA fully-shape-inferred tensor that is initialized with the offset of each cell.
val range :
?label:??? ->
?grad_spec:??? ->
?axis_basis:??? ->
Base__Int.t ->
Tensor.trange is a 1D tensor of shape upto, spans 0 inclusive, upto exclusive.
val range_of_shape :
?label:??? ->
?grad_spec:??? ->
?batch_dims:??? ->
?input_dims:??? ->
?output_dims:??? ->
?batch_axes:??? ->
?input_axes:??? ->
?output_axes:??? ->
unit ->
Tensor.tval stop_gradient : ?spec:??? -> ?capture_dims:??? -> Tensor.t -> Tensor.op_funA stop_gradient is an identity in the forward pass and a no-op in the backprop pass.
val slice : Idx.static_symbol -> ?grad_spec:??? -> Tensor.t -> Tensor.op_funval embed_symbol :
?grad_spec:??? ->
?label:??? ->
Ir.Indexing.static_symbol ->
Tensor.tval embed_self_id : ?grad_spec:??? -> ?label:??? -> unit -> Tensor.tval embed_dim :
?grad_spec:??? ->
?label:??? ->
Shape.delayed_var_ref ->
Tensor.tval uniform :
?grad_spec:??? ->
unit ->
?label:??? ->
?top_down_prec:??? ->
?batch_dims:??? ->
?batch_axes:??? ->
?input_dims:??? ->
?output_dims:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
unit ->
Tensor.tval uniform_at :
?grad_spec:??? ->
Tensor.t ->
?label:??? ->
?top_down_prec:??? ->
?batch_dims:??? ->
?batch_axes:??? ->
?input_dims:??? ->
?output_dims:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
unit ->
Tensor.tGenerates a single uniform random number using a counter symbol for PRNG state. This is useful for sequential sampling in recurrent contexts.
Note: unlike uniform, this stays pointwise (no pin_counter_spec): the user counter's shape must propagate to the result through the key operand, so the key slot cannot be reduced away. Entropy across draws comes from the user counter; within one draw, range_over_offsets may close smaller than the result and repeat values along broadcast axes.
val uniform1 :
?grad_spec:??? ->
unit ->
?label:??? ->
?top_down_prec:??? ->
?batch_dims:??? ->
?batch_axes:??? ->
?input_dims:??? ->
?output_dims:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
unit ->
Tensor.tDEPRECATED (gh-ocannl-509): a wasteful variant of uniform that produces a single value from each 4x32 random bits. The packed uniform is now total over shapes (round-up counter inference, tail-peeled lowering) and virtualizes via lane extraction, so this shape-safety fallback is no longer needed; it remains as the IR-internal substrate and for reproducing pre-0.9 random streams. The bit-spreading in int32_to_uint4x32/uint32_to_uint4x32 ensures good entropy even with the 2-round "light" threefry variant.
val centered_uniform_param_init :
?scale:??? ->
unit ->
?label:??? ->
?top_down_prec:??? ->
?batch_dims:??? ->
?batch_axes:??? ->
?input_dims:??? ->
?output_dims:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
unit ->
Tensor.tA centered uniform distribution over [-scale/2, scale/2)] built from the packed {!uniform}. With the default [scale = 0.5] this is the default parameter initialization (see {!Make_DSL.default_param_init}).
val default_uniform_param_init :
unit ->
?label:??? ->
?top_down_prec:??? ->
?batch_dims:??? ->
?batch_axes:??? ->
?input_dims:??? ->
?output_dims:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
unit ->
Tensor.tval centered_uniform1_param_init :
?scale:??? ->
unit ->
?label:??? ->
?top_down_prec:??? ->
?batch_dims:??? ->
?batch_axes:??? ->
?input_dims:??? ->
?output_dims:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
unit ->
Tensor.tDEPRECATED (gh-ocannl-509): use centered_uniform_param_init. Kept for reproducing pre-0.9 random streams.
val default_uniform1_param_init :
unit ->
?label:??? ->
?top_down_prec:??? ->
?batch_dims:??? ->
?batch_axes:??? ->
?input_dims:??? ->
?output_dims:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
unit ->
Tensor.tDEPRECATED (gh-ocannl-509): use default_uniform_param_init.
val uniform_at1 :
?grad_spec:??? ->
Tensor.t ->
?label:??? ->
?top_down_prec:??? ->
?batch_dims:??? ->
?batch_axes:??? ->
?input_dims:??? ->
?output_dims:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
unit ->
Tensor.tA wasteful variant of uniform_at that produces a single value from each 4x32 random bits. The bit-spreading in int32_to_uint4x32/uint32_to_uint4x32 ensures good entropy even with the 2-round "light" threefry variant.
val reshape :
l:Base.string ->
?b:??? ->
?i:??? ->
?o:??? ->
Ir.Ndarray.t ->
?fetch_op:??? ->
?grad_spec:??? ->
?top_down_prec:??? ->
?batch_axes:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
Base.unit ->
Tensor.tThe input i dimensions default to empty. The batch and output dimensions will be inferred if omitted. Note: the data should have no padding and if padding is inferred, the data will be copied; otherwise, the resulting tensor value shares host memory with the ndarray.
val wrap :
l:Base.string ->
?prec:??? ->
?b:??? ->
?i:??? ->
?o:??? ->
Ir.Ndarray.t ->
?fetch_op:??? ->
?grad_spec:??? ->
?top_down_prec:??? ->
?batch_axes:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
Base.unit ->
Tensor.tThe dimensions are taken from the provided ndarray, but the split into axis kinds still needs to be inferred (or provided). Assumes no padding. Input axes are not inferred (empty if omitted). See also: reshape and TDSL.wrap_param.
val wrap_padded :
l:Base.string ->
?b:??? ->
?i:??? ->
?o:??? ->
padding:Ir.Ops.axis_padding Base.array ->
padded_value:Base.float ->
Ir.Ndarray.t ->
?fetch_op:??? ->
?grad_spec:??? ->
?top_down_prec:??? ->
?batch_axes:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
Base.unit ->
Tensor.tAssumes the ndarray is padded as given. This means the dimensions of the ndarray will differ from the dimensions of the tensor by the padding. See also: TDSL.wrap.
val rebatch :
l:Base.string ->
Ir.Ndarray.t ->
?fetch_op:??? ->
?grad_spec:??? ->
?top_down_prec:??? ->
?batch_dims:??? ->
?batch_axes:??? ->
?deduced:??? ->
Base.unit ->
Tensor.tThe output dimensions are taken from the provided ndarray, assuming precisely the first axis is a batch axis, assumes no input axes and the batch dimensions are inferred. Empty output dimensions are allowed and represent scalars. Assumes the data has no padding, and data is copied if padding is inferred. See also: reshape and wrap.
val init :
l:Base.string ->
prec:Ir.Ops.prec ->
?b:??? ->
?i:??? ->
?o:??? ->
f:(Base.int Base.array -> Base.float) ->
?fetch_op:??? ->
?grad_spec:??? ->
?top_down_prec:??? ->
?batch_axes:??? ->
?input_axes:??? ->
?output_axes:??? ->
?deduced:??? ->
Base.unit ->
Tensor.tCreates a tensor by initializing values using a function from indices to values. The dimensions are split into axis kinds as specified, there is no shape inference. Recall that input axes are rightmost.
module DSL_modules : sig ... end