Structured Masked Attention (SMA)

Masked Attention (MA) is given by the relation [1]

๐Œ=๐โŠ™๐‹,

(1)

where ๐‹ is a mask applied to the matrix ๐ using the elementwise Hadamard product (denoted by โŠ™). When the mask is structured one can most often apply multiplication with ๐Œ efficiently, and we refer to the masked attention as Structured Masked Attention (SMA). In the simplest case of ๐‹ being a lower triangular matrix filled with ones the SMA reduces to

๐Œ=๐โŠ™๐‹=tril(๐),

(2)

which can be viewed as a weighted cumulative sum. While the above is a structured computation, it is not efficient unless ๐ itself has some structure. An example of when ๐ is structured is when it is of low-rank (i.e., ๐=๐”๐•โŠค). The resulting SMA is a semiseparable matrix for which multiplication can be applied in linear time [2].

In general if ๐‹ is a low-rank matrix (of rank ๐‘) then multiplication with ๐Œ scales as ๐‘-times the scaling of multiplication with ๐ as

๐Œ๐ฑ=(๐โŠ™๐”๐•โŠค)๐ฑ=โˆ‘๐‘–=1๐‘diag(๐ฎ๐‘–)๐diag(๐ฏ๐‘–)๐ฑ,

(3)

for which we see that we need to perform ๐‘ multiplications with ๐ as well as 2๐‘ diagonal multiplications.

julia
1
using LinearAlgebra
2
using Random
3
using Test
4
Random.seed!(42)
5
n, p = 10, 2
6
U, V = randn(n,p), randn(n,p)
7
B = rand(n,n)
8
M = B .* (U*V')
9
x = randn(n)
10
@test M*x โ‰ˆ sum(i -> Diagonal(U[:,i])*(B*(Diagonal(V[:,i])) * x), 1:p)

1. State-space models as structured matrices

First we recap that a state-space model is given by the equations

๐ก๐‘–=๐€๐‘–๐ก๐‘–โˆ’1+๐๐‘–๐ฑ๐‘–,๐ฒ๐‘–=๐‚๐‘–โŠค๐ก๐‘–,

(4)

An alternative way to view the state-space model is through the lens of structured matrices. We take ๐ก0=๐0๐ฑ0, corresponding to a zero state before the first input. In particular a state-space model up until time step ๐‘‡ can also be written using block matrices as well as block bidiagonal matrices as

[๐ˆโˆ’๐€1๐ˆโ‹ฑโ‹ฑโˆ’๐€๐‘‡โˆ’1๐ˆ]โŸ๐€[๐ก0๐ก1โ‹ฎ๐ก๐‘‡โˆ’1]=[๐0๐ฑ0๐1๐ฑ1โ‹ฎ๐๐‘‡โˆ’1๐ฑ๐‘‡โˆ’1]=[๐0๐1โ‹ฑ๐๐‘‡โˆ’1]โŸ๐[๐ฑ0๐ฑ1โ‹ฎ๐ฑ๐‘‡โˆ’1]

(5)

We know that by simply iterating forwards in time we can compute the states in linear time. For the following generator representation, assume that each transition matrix ๐€๐‘–, ๐‘–=1,โ€ฆ,๐‘‡โˆ’1, is invertible. A structured representation of the inverse of the bidiagonal matrix ๐€ can be constructed and applied in linear time for fixed state dimension, using

๐€โˆ’1=tril([๐ˆ๐€1๐€2๐€1โ‹ฎ๐€๐‘‡โˆ’1โ€ฆ๐€1]โŸ๐”[๐ˆ๐€1โˆ’โŠค(๐€2๐€1)โˆ’โŠคโ‹ฎ(๐€๐‘‡โˆ’1โ€ฆ๐€1)โˆ’โŠค]โŠคโŸ๐•โŠค),

(6)

This type of matrix structure is called semiseparable [2]. Using this structured representation of ๐€โˆ’1 we can compute the hidden states efficiently as

[๐ก0๐ก1โ‹ฎ๐ก๐‘‡โˆ’1]=tril([๐ˆ๐€1๐€2๐€1โ‹ฎ๐€๐‘‡โˆ’1โ€ฆ๐€1][๐ˆ๐€1โˆ’โŠค(๐€2๐€1)โˆ’โŠคโ‹ฎ(๐€๐‘‡โˆ’1โ€ฆ๐€1)โˆ’โŠค]โŠค)โŸ๐€โˆ’1[๐0๐1โ‹ฑ๐๐‘‡โˆ’1][๐ฑ0๐ฑ1โ‹ฎ๐ฑ๐‘‡โˆ’1],

(7)

Similarly, the output ๐ฒ can be computed by applying the output matrices ๐‚๐‘– in a blocked fashion to the hidden states, i.e.

[๐ฒ0๐ฒ1โ‹ฎ๐ฒ๐‘‡โˆ’1]=[๐‚0โŠค๐‚1โŠคโ‹ฑ๐‚๐‘‡โˆ’1โŠค]โž๐‚โŠค[๐ˆโˆ’๐€1๐ˆโ‹ฑโ‹ฑโˆ’๐€๐‘‡โˆ’1๐ˆ]โˆ’1[๐0๐1โ‹ฑ๐๐‘‡โˆ’1]โŸ๐Œ[๐ฑ0๐ฑ1โ‹ฎ๐ฑ๐‘‡โˆ’1]

(8)

julia
1
using SymSemiseparableMatrices
2
using BlockBandedMatrices
3
using LinearAlgebra
4
using Random
5
using Test
6
Random.seed!(42)
7
8
T = 10 # Sequence length
9
n = 6 # State size
10
input_dim = 1 # Dimension of forcing term
11
12
A_blks = [rand()*I(n) for _ in 1:T-1] # Diagonal dynamics in Mamba 2
13
D_blks = [I(n)/1.0 for i in 1:T] # Diagonal blocks are identity
14
C_blks = [rand(n,1) for _ in 1:T] # Measurements are scalars
15
B_blks = [rand(n,input_dim) for _ in 1:T] # Inputs are scalars
16
# Defining zeros blocks for the matrices
17
A_zero_blks = [zeros(n,n) for b in A_blks]
18
C_zero_blks = [zeros(n,1) for _ in 1:T-1]
19
B_zero_blks = [zeros(n,input_dim) for _ in 1:T-1]
20
# Defining the block matrices
21
A = BlockTridiagonal(-A_blks,D_blks,A_zero_blks)
22
C = BlockTridiagonal(C_zero_blks,C_blks,C_zero_blks)
23
B = BlockTridiagonal(B_zero_blks,B_blks,B_zero_blks)
24
25
# Computing states by iterating forward
26
x_blks = [randn(input_dim) for _ in 0:T-1] # input data
27
h_blks = [B_blks[1]*x_blks[1]] # initial hidden state
28
for i in 2:T
29
push!(h_blks, A_blks[i-1]*h_blks[i-1] + B_blks[i]*x_blks[i])
30
end
31
y_blks = [C'*h for (C,h) in zip(C_blks,h_blks)]
32
33
# Computing states using semiseparable matrices
34
Ai_blks = [prod(reverse(A_blks[1:i-1]),init=1.0*I(n)) for i in 1:T]
35
U = vcat(Ai_blks...)
36
V = vcat(inv.(transpose.(Ai_blks))...)
37
# "SymSemiseparableCholesky" represents the matrix A^{-1} = tril(UV')
38
Ai = SymSemiseparableCholesky(U', V')
39
x = vcat(x_blks...) # Collecting input data
40
41
@test Ai*(B*x) โ‰ˆ vcat(h_blks...) # Checking hidden states
42
@test C'*(Ai*(B*x)) โ‰ˆ vcat(y_blks...) # Checking measurement

2. State-space models as SMAs

In this section we aim to derive that the simplified SSM in the Mamba-2 paper is a special case of Structured Masked Attention (SMA) [1], i.e., that the multiplication

๐Œ๐ฑ=[๐‚0๐‚1โ‹ฑ๐‚๐‘‡โˆ’1]โŠค(๐€โˆ’1([๐0๐1โ‹ฑ๐๐‘‡โˆ’1]๐ฑ)),

(9)

can be written differently as

๐Œ๐ฑ=(๐โŠ™๐‹)๐ฑ,

(10)

The SSM in the Mamba-2 paper restricts the dynamics of ๐€ to be scalar-times-identity dynamics in order for the mask to be structured [1]. In short this restriction means that the dynamics for all hidden states are independent but equal.

๐ก๐‘ก=(๐‘Ž๐‘ก๐ˆ)๐ก๐‘กโˆ’1+๐›๐‘ก๐‘ฅ๐‘ก,๐ฒ๐‘ก=๐œ๐‘กโŠค๐ก๐‘ก.

(11)

From a practical point-of-view this means that we can collect each index of the dynamics and treat them separately. The resulting ๐€-matrix can be described by a Kronecker product (depending on how we organize the states itโ€™s either ๐€=๐šโŠ—๐ˆ๐‘› or ๐€=๐ˆ๐‘›โŠ—๐š). In the following we choose to separate the states, resulting in ๐€ having the form

๐€=๐ˆ๐‘›โŠ—๐š,๐š=[1โˆ’๐‘Ž11โ‹ฑโ‹ฑโˆ’๐‘Ž๐‘‡โˆ’11],

(12)

For the following scalar generator representation, assume that ๐‘Ž๐‘–โ‰ 0 for ๐‘–=1,โ€ฆ,๐‘‡โˆ’1. Using that the inverse of a Kronecker product is the Kronecker product of the inverses it follows that

๐€โˆ’1=๐ˆ๐‘›โŠ—๐šโˆ’1,๐šโˆ’1=tril([1๐‘Ž1โ‹ฎ๐‘Ž๐‘‡โˆ’1โ€ฆ๐‘Ž1]โŸ๐ฎ[1๐‘Ž1โˆ’1โ‹ฎ(๐‘Ž๐‘‡โˆ’1โ€ฆ๐‘Ž1)โˆ’1]โŠคโŸ๐ฏโŠค),

(13)

where we further used that the inverse of a bidiagonal matrix is semiseparable. Furthermore, we have to re-arrange ๐ and ๐‚ which results in

๐=[diag(๐›(1))โ‹ฎdiag(๐›(๐‘›))],๐›(๐‘˜)=[๐‘0(๐‘˜)โ‹ฎ๐‘๐‘‡โˆ’1(๐‘˜)],๐‚=[diag(๐œ(1))โ‹ฎdiag(๐œ(๐‘›))],๐œ(๐‘˜)=[๐‘0(๐‘˜)โ‹ฎ๐‘๐‘‡โˆ’1(๐‘˜)],

(14)

where ๐‘๐‘ก(๐‘˜) and ๐‘๐‘ก(๐‘˜) denote the ๐‘˜th entries of ๐›๐‘ก and ๐œ๐‘ก. The final multiplication will therefore look as

๐‚โŠค๐€โˆ’1๐โŸ๐Œ๐ฑ=(โˆ‘๐‘˜=1๐‘›diag(๐œ(๐‘˜))๐šโˆ’1diag(๐›(๐‘˜)))๐ฑ=(โˆ‘๐‘˜=1๐‘›(๐œ(๐‘˜)๐›(๐‘˜)โŠค)โŠ™๐šโˆ’1)๐ฑ.

(15)

Finally, using the properties of the Hadamard product we get to the Structured Masked Attention form that we were looking for

(๐‚โŠค๐€โˆ’1๐)๐ฑ=(โˆ‘๐‘˜=1๐‘›(๐œ(๐‘˜)๐›(๐‘˜)โŠค)โŠ™๐šโˆ’1)๐ฑ=(([๐œ(1)โ€ฆ๐œ(๐‘›)][๐›(1)โŠคโ‹ฎ๐›(๐‘›)โŠค])โŠ™๐šโˆ’1)๐ฑ.

(16)

This means that the SSM dynamics can be interpreted as a structured masked attention mechanism. Note that in the case of the dynamics being independent but different (i.e., we would have ๐š(๐‘˜) rather than just ๐š) the SSM dynamics would result in a sum of masked attentions, i.e.,

(๐‚โŠค๐€โˆ’1๐)๐ฑ=(โˆ‘๐‘˜=1๐‘›(๐œ(๐‘˜)๐›(๐‘˜)โŠค)โŠ™(๐š(๐‘˜))โˆ’1)๐ฑ

(17)

julia
1
using SymSemiseparableMatrices
2
using LinearAlgebra
3
using Random
4
using Test
5
Random.seed!(42)
6
7
T = 10 # Sequence length
8
n = 6 # State size
9
10
# Here we treat the matrices in terms of their states and not sequence lengths
11
a_blks = rand(T-1)
12
a = Bidiagonal(ones(T), -a_blks, :L)
13
A = kron(I(n),a) # Kronecker product with identity
14
15
# The blocks are now size equal to the sequence and a block for each state!
16
B_blks = [rand(T) for _ in 1:n]
17
C_blks = [rand(T) for _ in 1:n]
18
# Collecting the blocks into the B and C
19
B = vcat(Diagonal.(B_blks)...)
20
C = vcat(Diagonal.(C_blks)...)
21
22
# We want to see if the full matrix M = C'*(A\B) can be written as
23
# structured masked attention, i.e.,
24
# M = (CB')\circ a^{-1}. For this we start by computing a^{-1}
25
ai = inv(a) # We ignore here that inv(a) is semiseparable
26
@test C'*(A\B) โ‰ˆ sum(i-> Diagonal(C_blks[i])*ai*Diagonal(B_blks[i]), 1:n)
27
@test C'*(A\B) โ‰ˆ sum(i->(C_blks[i]*B_blks[i]') .* ai, 1:n)
28
29
# We can collect the terms and write it as Structured Masked Attention!
30
Cn = hcat(C_blks...)
31
Bn = hcat(B_blks...)
32
@test C'*(A\B) โ‰ˆ (Cn*Bn') .* ai
33
34
# We can apply the semiseparable structure of the inverse when multiplying!
35
ai_blks = [prod(a_blks[1:i-1],init=1.0) for i in 1:T]
36
u = vcat(ai_blks...)
37
v = vcat(inv.(ai_blks)...)
38
# "SymSemiseparableCholesky" represents the matrix A^{-1} = tril(UV')
39
ais = SymSemiseparableCholesky(u',v')
40
# Efficient products using the structure of "ais" and diagonal B and C
41
x = randn(T)
42
@test C'*(A\(B*x)) โ‰ˆ sum(i-> C_blks[i].*(ais*(B_blks[i].*x)), 1:n)

Cite this post

bibtex
@misc{paltorp2025structuredmaskedattention,
author = {Paltorp, Mikkel},
title = {{Structured Masked Attention (SMA)}},
year = {2025},
month = jul,
url = {https://mipals.github.io/pdf/2025/07-28-structured-masked-attention/sma.pdf},
note = {Blog post}
}

Bibliography

  • [1] 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
  • [2] M. S. Andersen and T. Chen, โ€œSmoothing Splines and Rank Structured Matrices: Revisiting the Spline Kernel,โ€ SIAM Journal on Matrix Analysis and Applications, vol. 41, no. 2, pp. 389โ€“412, 2020, doi: 10.1137/19M1267349.