In this article
A custom scan combine must match the intended ordered recurrence; an associative operation can still give another result when its operands are swapped.
Go beyond a sum-only mental model
The associative_scan interface accepts a tensor or tuple of tensors, an axis, and a JIT-compiled combining function. This supports richer prefix computations than a cumulative sum. However, sum-based tests can hide an operand-order mistake because addition is commutative in its mathematical idealization. A custom recurrence needs an explicit definition of which group of earlier positions is combined with which later group.
Derive an ordered affine example
Represent an affine map by coefficients a and b, meaning multiplication by a followed by addition of b. Applying a left segment and then a right segment composes their maps in that order. The combined coefficients are a_right * a_left and a_right * b_left + b_right. Function composition is associative but generally not commutative. This mathematical example explains why exchanging the combine arguments can preserve a superficially reasonable operator while implementing a different recurrence.
Keep tuple components synchronized
The language implementation constructs the scan combining region from its supplied tensors and function. Coefficient components must represent corresponding positions and return the intended combined state together. Scanning coefficients independently and combining them afterward does not generally reproduce affine composition. This is a dependency issue, separate from whether the tensor shapes match or whether each individual scan compiles successfully.
Test a recurrence that exposes direction
The cumsum documentation also exposes reverse direction for the sum specialization. For a custom ordered scan, compare against a simple sequential reference in the intended direction using nonidentical transforms. Include a case where swapping segment order changes the result. This reveals an error that symmetric or uniform examples can miss. Floating-point reassociation can still introduce numerical differences, so distinguish a changed recurrence from an expected tolerance issue in a valid parallel evaluation.
Specify the scan contract
Record axis, direction, state representation, combine argument convention, padding policy, and output interpretation. Guides to tensor axes and mixed-precision accumulation provide context. Validate intermediate prefixes rather than only the final aggregate. A correct parallel scan preserves the intended ordered operation at every prefix; associativity permits regrouping, but it does not authorize arbitrary reordering or replacement with independent scans of coupled state components.
Earn rental income from GPUs
Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.