Associative means the work can be split. Even a snail can parallelize.
. Snails agree.
Associative Scan
Recently a plethora of methods within machine learning have made use of state-space models (SSMs) to model sequences [1], [2], [3], [4]. While the early S4 model is computed as a convolution [1], a big reason for the success of later models such as S5 [2] and Mamba [3] is their usage of the associative scan operation to parallelize the state computation on the GPU. In my opinion this is a neat trick that was poorly explained in the original papers (although Appendix B in [4] does explain it for the scalar case), and I will therefore explain it in more detail in this blog post. The aim is to show that composing the transitions of a (linear) state-space model
(1)
is associative11 Associativity simply means that repeated applications of the operator (denoted by ) can be grouped arbitrarily, i.e., that . The order of the operands must still be kept, as the operator need not be commutative. Examples of associative operators are addition and multiplication, including matrix multiplication.. In short this means that the states of a (linear) state-space model up until time step can be computed in parallel using an associative scan operation. For details on the associative operation open the example box below.
ExampleΒ 1: Cumulative sum using associative scan
Example 1 (Cumulative sum using associative scan)Β Β The (parallel) associative scan, as the name suggests, is a way to apply an associative operator in parallel. The simplest such operator is addition, which is associative because we can group the additions however we like (i.e., ). In JAX the associative scan is implemented in the accurately named lax.associative_scan function, while Juliaβs accumulate function computes the same scan sequentially. If we supply the add operator the resulting computation will be equivalent to the cumulative sum, e.g. in Python
>>> lax.associative_scan(jnp.add, jnp.arange(0, 4))
Array([0, 1, 3, 6], dtype=int32)or in Julia
julia> accumulate(+, 0:3)
4-element Vector{Int64}:
0
1
3
6While the above computation is trivial and easily computed by looping from the first to last element in the vector, the main idea of the associative scan is that the operator can be applied pairwise in parallel. That means if enough processors are available the computation can be done in time rather than the trivial implementation of .
The first step in showing the associativity of the state-space model is to define the transition of the state-space models using matrix multiplication (which is associative) by embedding the transition into a larger matrix as follows
(2)
Using the definition of the state transition from state to state can be computed using matrix multiplication as
(3)
Using this we can compute the th state of the state-space model as
(4)
Given that the cumulative product can be computed using the associative scan operator the full dynamics can be computed as
(5)
While the above works, it can be simplified slightly. As we are really only interested in the top row of blocks (the accumulated product has in its top-right block), we can instead define elements by just the top row, i.e., , and then define the operator (denoted by ) by how the top row propagates. For general pairs this reads
(6)
which is the top block row of the product . The operator is therefore associative, because matrix multiplication is. The SSM states can then be computed as with , and is the right block of .
As a final remark note that while the associative scan is parallelizable it performs matrix-matrix products of the form which will be computationally prohibitive unless has structure (e.g., diagonal or low-rank). This is one of the reasons why e.g. Mamba-2 utilizes a scaled identity as its [4].
Cite this post
@misc{paltorp2025associativescan, author = {Paltorp, Mikkel}, title = {{Associative Scan}}, year = {2025}, month = jul, url = {https://mipals.github.io/pdf/2025/07-30-associative-scan/associative-scan.pdf}, note = {Blog post}}Bibliography
- [1] A. Gu, K. Goel, and C. RΓ©, βEfficiently Modeling Long Sequences with Structured State Spaces,β in International Conference on Learning Representations (ICLR), 2022. [Online]. Available: https://arxiv.org/abs/2111.00396
- [2] J. T. H. Smith, A. Warrington, and S. W. Linderman, βSimplified State Space Layers for Sequence Modeling,β in International Conference on Learning Representations (ICLR), 2023. [Online]. Available: https://arxiv.org/abs/2208.04933
- [3] A. Gu and T. Dao, βMamba: Linear-Time Sequence Modeling with Selective State Spaces.β [Online]. Available: https://arxiv.org/abs/2312.00752
- [4] T. Dao and A. Gu, βTransformers are SSMs: Generalized Models and Efficient Algorithms Through Structured State Space Duality,β in International Conference on Machine Learning (ICML), 2024. [Online]. Available: https://arxiv.org/abs/2405.21060