Tensor operations
RDL operators record computations on Tensor values. Most numerical results receive inferred shapes and dtypes during early compiler passes.
The examples below belong inside a Graph context with their inputs already declared, unless a complete program is shown.
Arithmetic
| Operation | Meaning |
|---|---|
Add(a, b), Sub(a, b) | Elementwise addition and subtraction |
Mul(a, b), Div(a, b) | Elementwise multiplication and division |
FloorDiv(a, b), Mod(a, b) | Integer floor division and its remainder |
Exp(x), Log(x), Sqrt(x) | Exponential, natural logarithm and square root |
Sin(x), Cos(x), Erf(x) | Sine, cosine and error function |
Cast(x, dtype) | Convert logical values, including quantization |
Matmul(a, b) | Matrix multiplication |
Binary elementwise operations broadcast compatible dimensions. Their first operand is a tensor; many accept a scalar as the second operand. Use Constant when a scalar must be a tensor explicitly.
Matmul computes [M,K] @ [K,N] -> [M,N] and broadcasts supported leading dimensions. Linear handles the usual checkpoint weight shape [out,in].
FloorDiv rounds toward negative infinity. Mod(a,b) means a - FloorDiv(a,b) * b. For example, -3 divided by 2 has quotient -2 and remainder 1.
See dtypes for promotion, quantized storage and accumulator precision.
Conditions
positive = Compare(x, 0, predicate="gt")
y = Select(positive, x, 0.0)
Compare(a,b,predicate=...) supports eq, ne, lt, le, gt and ge, producing booleans.
Select(condition, when_true, when_false) chooses elementwise with broadcasting. Both value expressions are evaluated. To avoid executing an entire computation on one branch, use If.
Maximum, Minimum, Clip and Abs provide common compositions around these operators.
Shapes and views
| Operation | Meaning |
|---|---|
Shape(x) | Rank-one int64 tensor of dimensions |
Dim(x, axis) | A dimension value for one axis |
Reshape(x, shape) | Change dimensions while preserving element order/count |
Permute(x, axes) | Reorder axes, such as (1,0) for a matrix transpose |
Broadcast(x, shape) | Broadcast using trailing-axis alignment |
Iota(stop, start=0, step=1, dtype="int64") | Half-open sequence of indices |
Reshape allows at most one -1 inferred dimension. Its shape can be a tuple containing graph values or a dimension tensor. Broadcast may require an explicit Reshape to insert axes in the intended positions.
flat = Reshape(x, (-1,))
rows = Dim(x, 0)
positions = Iota(rows, dtype="int32")
The compiler can represent reshapes, broadcasts and permutations as views consumed by compatible kernels.
Gather and scatter
Gather(x, indices=..., mask=None, fill=0) indexes leading axes. The last index dimension holds coordinates. Trailing axes of x remain slices in the result.
For x shaped [rows,width], indices shaped [count,1] select count rows, giving [count,width]:
indices = Reshape(Iota(3), (3, 1))
first_rows = Gather(x, indices=indices)
Active coordinates must be in bounds and nonnegative. False mask entries use fill without accessing their coordinates.
Scatter(x, indices=..., values=..., mode="replace", mask=None) returns an updated tensor while preserving the original value of x. It uses the same coordinate convention as Gather.
replacerequires unique active coordinatesaddaccumulates repeated coordinates- False mask entries perform no update
To change persistent state, write the result to a cache explicitly.
For ordinary single-axis indexing, Take(x, indices, axis=0, rank=1, index_rank=1) is easier than constructing coordinates yourself. Set rank and index_rank to the logical ranks.
Common tensor compositions
| Operation | Purpose |
|---|---|
Slice(x, axis=..., start=0, stop=None, step=1, rank=...) | Positive-step slicing with clamped bounds |
Concat(tensors, axis=..., rank=...) | Join tensors along an axis |
Stack(tensors, axis=0, rank=...) | Insert an axis and join tensors |
Pad(x, padding=((before,after), ...), fill=0) | Constant padding per axis |
FullLike(x, shape=None, fill=0) | Filled tensor with the inferred dtype of x |
Sum, Mean, Max | Reduce specified axes, optionally keeping dimensions |
Softmax(x, axis=-1, sinks=None) | Normalize scores along an axis |
Argmax(x, axis=-1, rank=..., mask=None) | Index of the largest active value |
TopK(x, k=..., rank=...) | Descending values and indices on the last axis |
CumSum(x, rank=...) | Inclusive cumulative sum on the last axis |
Slice accepts negative bounds relative to the end, but only positive steps. Max needs a nonempty reduction domain. Argmax chooses the lowest active index on ties; an empty active domain returns the int64 maximum sentinel. Do not use that sentinel as an ordinary index.
TopK requires 0 <= k <= width. These operations describe mathematics; a core composition can be much slower than a specialized kernel for the same operation.
For custom reducers and execution loops, see control flow.