Skip to main content
Tensor math is the foundation of all operations in TensorEngine. This page defines broadcasting rules, elementwise operations, reductions, matrix multiplication shape contracts, and precision ranges.

Broadcasting Rules

Two tensors are broadcastable if, for each dimension from trailing to leading, the dimensions are equal or one of them is 1. A missing dimension is treated as 1. Broadcasting is applied before elementwise operations. The TensorEngine validates broadcastability before executing any op.

Elementwise Operations

For elementwise add, sub, mul, div:

Reductions

Matmul Shape Rules for Transformer Weights

For a linear layer with weight W and input x:
For attention Q/K/V/O projections:
For MLP SwiGLU:

Precision and Numerical Range

Numerical Stability Rules

  1. All elementwise accumulation uses fp32 intermediates.
  2. Norms are computed in fp32 regardless of input dtype.
  3. SLERP dot products use fp64 for precision.
  4. int4 and int8 weights are dequantized to fp32 before merge ops.
  5. Results are cast to the target dtype only at the final step.