research26 min studyLesson 756 in Engineering

Count active blocks and acknowledge the changed operator

Derive block-pair work, distinguish block sparsity from element masks, and test against the matching sparse reference.

On this page

What you’ll understand

  • Calculate allowed pair count from an active block pattern.
  • Explain why omitting blocks changes dense attention normalization.
  • Distinguish structured sparsity, element masks, and actual kernel work.

Tiling dense attention changes its schedule. Skipping selected tiles changes which keys participate and therefore usually changes the operator. Block-sparse attention makes that choice explicit through a pattern of allowed query-block/key-block pairs. It can reduce pair arithmetic and memory movement when a sparse kernel really avoids excluded blocks, but a sparse-looking mask passed to a dense kernel may not save the same work. The first research question is which dependency pattern the model should use; the second is how efficiently the target hardware executes that pattern.

A block mask defines a graph of allowed pairs

Divide queries and keys into blocks of b tokens for the simplest square, evenly divisible case. Each active block allows b² query-key pairs unless an additional element-level mask removes some of them. A sparse row’s softmax denominator sums only its allowed keys. Excluding a block does not merely zero its contribution after computing dense probabilities: the remaining probabilities must be renormalized over the sparse domain. Otherwise the implementation computes a different unnormalized output.

Patterns can combine local, global, or random block connections, as in original sparse-attention work, or reflect a task-specific structure. Their usefulness depends on which information must pass between tokens across layers. A mathematically exact evaluation of the selected sparse pattern is still different from exact dense attention unless excluded weights were truly irrelevant under a defined equivalence condition. The phrase exact kernel must therefore identify the operator for which it is exact.

E=∑(u,v)∈Bb2=∣B∣b2,Fpair≈4EdE=\sum_{(u,v)\in\mathcal B}b^2=|\mathcal B|b^2,\qquad F_{\rm pair}\approx4Ed
For N divisible by b, B is the active block-pair set of one head, with full b×b blocks and no finer mask. E counts allowed pairs. dk=dv=d and two FLOPs per multiply-add give the leading QKᵀ/PV work; softmax, projections, metadata, and padding are excluded.↔ Scroll wide equations horizontally.

Coarse blocks and fine masks are distinct

A lower-triangular arrangement of blocks does not automatically implement token-level causality. The diagonal block still contains future-token entries unless a triangular element mask is applied inside it. Boundary padding also changes how many logical pairs are valid. A kernel may execute a full tile while masking some lanes, so logical allowed-pair counts and executed tensor-core work need not match exactly. Report both when interpreting sparse throughput.

At fixed block size, a bounded number of active key blocks per query block gives a pair count linear in token length. If the number of active blocks per row grows with the number of blocks, the pattern can return toward quadratic complexity. Constants matter too: sparse metadata, gathers, irregular load balance, and small blocks can make fewer arithmetic operations slower than a regular dense tile schedule. Count work first, then measure the actual execution.

∣B∣≤(N/b)t⟹E≤Nbt|\mathcal B|\le (N/b)t\quad\Longrightarrow\quad E\le Nbt
t bounds active key blocks per query block, with N divisible by positive integer b. Linear growth in N requires b and t to stay bounded independently of N. This is a logical pair-count bound, not a latency guarantee or a statement about partially masked executed tiles.↔ Scroll wide equations horizontally.

Validate the pattern as data

Construct a tiny dense reference with exactly the same boolean pattern and compare sparse outputs and gradients to it. Separately compare the chosen sparse model with the original dense model on task quality. These tests answer different questions: one detects kernel errors, the other measures the effect of removing dependencies. A sparse kernel can pass its matching reference perfectly while the sparse architecture performs poorly on a task needing distant interactions.

Test active-block ordering, duplicate metadata entries, empty rows, and partial blocks. Duplicate block entries must not accidentally count the same key twice in the denominator. If a pattern is randomly sampled, preserve its seed or explicit realized mask when reproducing a numerical test. Build the reference from logical token pairs rather than assuming that a library’s compressed metadata format already means what the model designer intended.

Seven active blocks are not dense attention

Use N=8, b=2, d=2, and four query/key blocks. Activate each diagonal block and the immediately preceding key block when one exists. There is no within-block causal mask. All allowed logits are zero, and token position j from 0 through 7 has the two-component value v_j=(j,j), so both key and value widths equal d=2.

  1. The four query-block rows have 1,2,2,2 active blocks, for 7 total. Each contributes 2²=4 allowed pairs, so E=28 compared with 8²=64 dense pairs. The pattern is block-banded but not token-causal within its diagonal blocks.

  2. Leading two-product work is approximately 4×28×2=224 FLOPs, versus 4×64×2=512 for dense attention. The ratio is 28/64=0.4375 before accounting for masking overhead, metadata, or actual executed tile padding.

  3. Query token 0 belongs to block 0 and sees only values (0,0) and (1,1), averaging to (0.5,0.5). Dense zero-logit attention would average all eight value vectors to (3.5,3.5). The output difference comes from the declared pattern, not an implementation error.

The result

The sparse pattern saves logical pair work but changes the normalization domain and result. Its correct reference is the same block mask, not unmasked dense attention.

Explore the model

Interactive lab

Interactive lab · modular

Missing transitions restrict what can be reached

Reset, set Modulus m to 8 and Additive step a to 2. Predict period 4 and residue 2 after one Step. Reset, set m to 8 and a to 1, predict period 8 and residue 1 after Step. This additive cycle illustrates a dependency restriction, not a block-sparse attention matrix or learned message passing.

Missing transitions restrict what can be reachedAddition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. Current readings: Residue r0: 0; Orbit period · m/gcd(m,a): 12; Distinct residues visited: 1 of 12. All parameters are adjustable with the labeled controls below.Repeated addition modulo 12: the current residue is 0, and the orbit period is 12.01234567891011+ 5mod 12An additive orbitn = 0rₙ = 0Period = 12Cycle starts at 0:0 → 5 → 10 → 3 →8 → 1 → 6 → 11 →4 → 9 → 2 → 7 → 0Exact residues · two additions per second
Additive orbitCurrent residue / next edge
Residue r0
0
Orbit period · m/gcd(m,a)
12
Distinct residues visited
1 of 12
t = 0 s

Addition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA restricted pattern can omit reachable information even when its individual computations are exact.

Check your understanding

Does a lower-triangular block pattern alone guarantee token-level causal attention?

Try it yourself

Use N=6, b=2, d=4, with only the three diagonal blocks active and no finer mask. Count allowed pairs and leading product FLOPs, and compare with dense attention.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

There are three full 2×2 blocks, while dense attention has 6² pairs.

Compare with the solution

Allowed pairs are 3×4=12 rather than 36, one third of dense pairs. Leading product work is approximately 4×12×4=192 FLOPs versus 4×36×4=576. Each query sees two tokens in its own block, including possible future positions within that block; this diagonal-block pattern is not a token-level causal mask.

Take it with you

  • Skipping blocks usually changes the attention operator.
  • Token-level masks and block-level patterns are separate.
  • Sparse pair counts need fixed pattern-width assumptions to imply linear scaling.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.