﻿# Triton associative scans: preserving composition order matters beyond cumulative sums

Use an affine-composition example to distinguish associativity from commutativity when implementing a custom Triton scan over tuples of state coefficients.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-triton-associative-scan-noncommutative-combine/
Language: en

## Go beyond a sum-only mental model

The [associative_scan interface](https://triton-lang.org/main/python-api/generated/triton.language.associative_scan.html) 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](https://raw.githubusercontent.com/triton-lang/triton/main/python/triton/language/core.py) 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](https://triton-lang.org/main/python-api/generated/triton.language.cumsum.html) 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](/journal/tensor-axes-batch-sequence-hidden/) and [mixed-precision accumulation](/journal/learn-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.
