Pratham Patel
· 20 min read

Mathematical Prerequisites for Sparse and Sliding Window Attention

Building the discrete math foundations for sparse attention patterns — the triangular number formula for counting pairs, modular arithmetic for strided patterns, the pigeonhole principle for existence proofs, and set union with inclusion-exclusion for combining connectivity sets — all derived step by step with one consistent 8-house example.

The next two posts in this series derive sparse factorized attention (the Sparse Transformer) and sliding window attention (Longformer). Both papers replace the dense n×nn \times n attention pattern with structured sparse patterns and then prove that these patterns preserve the model’s ability to route information between any two positions.

To follow those derivations, we need four tools from discrete mathematics. The triangular number formula counts the total entries in a causal attention matrix — it appears every time we need the sum 1+2+3++n1 + 2 + 3 + \cdots + n. Modular arithmetic defines which positions are “aligned” at a given stride — it is the language of strided attention patterns. The pigeonhole principle proves that two-hop paths always exist in factorized attention — it turns a counting argument into an existence guarantee. And set union with its cardinality rule combines local windows with global tokens — it is how Longformer merges two attention patterns without double-counting.

We will build all four tools from a single running example and verify every formula numerically.


The Running Example

We use 8 houses on a street, numbered 0 through 7, arranged in a row. The houses are grouped into 2 blocks of 4:

0123Block 04567Block 1\underbrace{0 \quad 1 \quad 2 \quad 3}_{\text{Block 0}} \qquad \underbrace{4 \quad 5 \quad 6 \quad 7}_{\text{Block 1}}

We will use this setup to build every tool: counting connections between houses, determining which block a house belongs to, proving that a mail carrier can always find a relay, and combining neighborhoods.


1. The Triangular Number Formula

1.1 Motivation

Suppose every house wants to send a greeting card to every house that comes before it on the street (lower-numbered houses only). House 0 sends nothing. House 1 sends to house 0. House 2 sends to houses 0 and 1. And so on. How many greeting cards are sent in total?

This is exactly the question that arises in causal attention: position ii attends to all positions jij \leq i, and we want the total number of attention entries.

1.2 Counting directly

Let C(i)C(i) be the number of cards house ii sends. Since house ii sends to all houses 0,1,,i10, 1, \ldots, i - 1 plus itself (the self-connection in attention), we have C(i)=i+1C(i) = i + 1:

House iiRecipientsC(i)=i+1C(i) = i + 1
0{0}\{0\}1
1{0,1}\{0, 1\}2
2{0,1,2}\{0, 1, 2\}3
3{0,1,2,3}\{0, 1, 2, 3\}4
4{0,1,2,3,4}\{0, 1, 2, 3, 4\}5
5{0,1,2,3,4,5}\{0, 1, 2, 3, 4, 5\}6
6{0,1,2,3,4,5,6}\{0, 1, 2, 3, 4, 5, 6\}7
7{0,1,2,3,4,5,6,7}\{0, 1, 2, 3, 4, 5, 6, 7\}8

The total is:

T=1+2+3+4+5+6+7+8=36T = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36

But we want a formula that works for any nn, not just n=8n = 8. We want a closed form for:

Tn=i=0n1(i+1)=k=1nk=1+2+3++nT_n = \sum_{i=0}^{n-1} (i + 1) = \sum_{k=1}^{n} k = 1 + 2 + 3 + \cdots + n

1.3 Derivation: Gauss’s pairing trick

Write the sum forwards and backwards and add them:

Tn=1+2+3++(n1)+nTn=n+(n1)+(n2)++2+1\begin{aligned} T_n &= 1 + 2 + 3 + \cdots + (n-1) + n \\ T_n &= n + (n-1) + (n-2) + \cdots + 2 + 1 \end{aligned}

Add the two rows term by term. Each column sums to n+1n + 1:

2Tn=(1+n)=n+1+(2+(n1))=n+1+(3+(n2))=n+1++(n+1)=n+12 T_n = \underbrace{(1 + n)}_{= n+1} + \underbrace{(2 + (n-1))}_{= n+1} + \underbrace{(3 + (n-2))}_{= n+1} + \cdots + \underbrace{(n + 1)}_{= n+1}

There are nn such pairs, so:

2Tn=n(n+1)2 T_n = n(n + 1)

Divide both sides by 2:

Tn=k=1nk=n(n+1)2\boxed{T_n = \sum_{k=1}^{n} k = \frac{n(n+1)}{2}}

This is the triangular number formula, also called Gauss’s summation formula. The name “triangular” comes from the fact that TnT_n counts the number of dots in a triangle with nn rows.

1.4 Numerical check

For n=8n = 8:

T8=8×92=722=36T_8 = \frac{8 \times 9}{2} = \frac{72}{2} = 36

We computed 1+2+3+4+5+6+7+8=361 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36 directly. Both sides match. \checkmark

1.5 A second useful form

In the attention blogs, we often encounter the sum starting from 0:

i=0n1(i+1)=n(n+1)2\sum_{i=0}^{n-1}(i + 1) = \frac{n(n+1)}{2}

This is identical to TnT_n — we are just re-indexing. Substituting k=i+1k = i + 1, the sum runs from k=1k = 1 to k=nk = n, which is exactly TnT_n. The re-indexing changes nothing.

1.6 Interpretation

The triangular number formula converts a sum that takes nn additions into a single multiplication and division. In the sparse attention blogs, this formula appears every time we count total entries in a causal attention matrix. For a sequence of length nn, full causal attention has Tn=n(n+1)2n22T_n = \frac{n(n+1)}{2} \approx \frac{n^2}{2} entries — the formula makes the quadratic scaling explicit.


2. Modular Arithmetic

2.1 Motivation

In the sparse attention blog, strided attention connects position ii to every position jj such that ii and jj are separated by a multiple of the stride ll. The formal definition says: ”jj is in position ii‘s strided set if (ij)modl=0(i - j) \bmod l = 0.” To understand this, we need the mod\bmod operator.

2.2 Definition

The modulo operator amodma \bmod m returns the remainder when aa is divided by mm. Formally, for integers aa and m>0m > 0:

amodm=amama \bmod m = a - m \left\lfloor \frac{a}{m} \right\rfloor

where \lfloor \cdot \rfloor is the floor function (the largest integer \leq the argument), which we derived in Mathematical Prerequisites for Mixture of Experts — Part 2.

The result is always in the set {0,1,2,,m1}\{0, 1, 2, \ldots, m - 1\}.

2.3 Concrete examples with our houses

With m=4m = 4 (block size):

House iii/4\lfloor i / 4 \rfloor (block number)imod4i \bmod 4 (position within block)
000
101
202
303
410
511
612
713

The floor gives the block number. The mod gives the position within the block. Together they partition the house number into two pieces:

i=imblock×m+imodmoffseti = \underbrace{\left\lfloor \frac{i}{m} \right\rfloor}_{\text{block}} \times m + \underbrace{i \bmod m}_{\text{offset}}

This is the Euclidean division identity: every integer ii can be uniquely written as qm+rqm + r where q=i/mq = \lfloor i/m \rfloor is the quotient and r=imodmr = i \bmod m is the remainder, with 0r<m0 \leq r < m.

2.4 Numerical check

For house 7 with m=4m = 4:

7=17/4×4+37mod47 = \underbrace{1}_{\lfloor 7/4 \rfloor} \times 4 + \underbrace{3}_{7 \bmod 4}

Check: 1×4+3=71 \times 4 + 3 = 7. \checkmark

2.5 Residue classes

A residue class modulo mm is the set of all integers with the same remainder when divided by mm. With m=4m = 4, there are exactly 4 residue classes:

Class 0:{0,4}(imod4=0)Class 1:{1,5}(imod4=1)Class 2:{2,6}(imod4=2)Class 3:{3,7}(imod4=3)\begin{aligned} \text{Class 0}: &\quad \{0, 4\} \quad (i \bmod 4 = 0) \\ \text{Class 1}: &\quad \{1, 5\} \quad (i \bmod 4 = 1) \\ \text{Class 2}: &\quad \{2, 6\} \quad (i \bmod 4 = 2) \\ \text{Class 3}: &\quad \{3, 7\} \quad (i \bmod 4 = 3) \end{aligned}

In the 8-house street, each residue class contains exactly 8/4=28 / 4 = 2 houses. These are exactly the houses that are connected by strided attention with stride l=4l = 4: house 0 and house 4 are in the same residue class, so strided attention connects them. House 3 and house 7 are in the same residue class, so strided attention connects them.

2.6 Congruence

Two integers aa and bb are congruent modulo mm, written ab(modm)a \equiv b \pmod{m}, if they have the same remainder:

ab(modm)    amodm=bmodm    m(ab)a \equiv b \pmod{m} \quad \iff \quad a \bmod m = b \bmod m \quad \iff \quad m \mid (a - b)

The notation m(ab)m \mid (a - b) means ”mm divides (ab)(a - b)” — that is, (ab)(a - b) is a multiple of mm, which is equivalent to saying (ab)modm=0(a - b) \bmod m = 0.

2.7 Numerical check

Are houses 3 and 7 congruent modulo 4?

73=4,4mod4=07 - 3 = 4, \qquad 4 \bmod 4 = 0

Yes: 73(mod4)7 \equiv 3 \pmod{4}. And indeed, 7mod4=37 \bmod 4 = 3 and 3mod4=33 \bmod 4 = 3 — same remainder. \checkmark

Are houses 2 and 5 congruent modulo 4?

52=3,3mod4=305 - 2 = 3, \qquad 3 \bmod 4 = 3 \neq 0

No: 5≢2(mod4)5 \not\equiv 2 \pmod{4}. And indeed, 5mod4=15 \bmod 4 = 1 while 2mod4=22 \bmod 4 = 2 — different remainders. \checkmark

2.8 The key property for strided attention

The strided connectivity set in the sparse attention blog is defined as:

Ai(2)={j:ji and (ij)modl=0}A_i^{(2)} = \{j : j \leq i \text{ and } (i - j) \bmod l = 0\}

Using what we just derived, (ij)modl=0(i - j) \bmod l = 0 means l(ij)l \mid (i - j), which means ij(modl)i \equiv j \pmod{l}. So the strided set connects position ii to all earlier positions in the same residue class modulo ll. The residue classes are exactly the columns of the image grid when ll equals the row width — which is why strided attention naturally attends along columns.

2.9 Spacing of residue classes

Within any residue class modulo mm, consecutive elements are exactly mm apart. The class with remainder rr contains the elements r,r+m,r+2m,r+3m,r, r + m, r + 2m, r + 3m, \ldots This is an arithmetic sequence with common difference mm.

In our example with m=4m = 4: class 2 contains {2,6,10,14,}\{2, 6, 10, 14, \ldots\}, with consecutive elements spaced exactly 4 apart.

This spacing property will be critical in the next section, where we prove that a relay position always exists within any interval of length mm.


3. The Pigeonhole Principle

3.1 Motivation

The sparse attention blog proves that any two positions can communicate through a two-hop path. The proof relies on showing that an intermediate relay position must exist. The tool that guarantees existence is the pigeonhole principle.

3.2 Definition

The pigeonhole principle (also called the Dirichlet box principle) states:

If n items are placed into m containers, and n>m, then at least one container holds 2 items.\boxed{\text{If } n \text{ items are placed into } m \text{ containers, and } n > m, \text{ then at least one container holds } \geq 2 \text{ items.}}

The name comes from the physical setup: if 9 pigeons fly into 8 pigeonholes, at least one hole contains at least 2 pigeons. There is no way around it — the arithmetic forces a collision.

3.3 Proof

Suppose, for contradiction, that every container holds at most 1 item. Then the total number of items is at most 1×m=m1 \times m = m. But we have n>mn > m items. Contradiction. Therefore at least one container must hold 2\geq 2 items.

This is a proof by contradiction — we assumed the opposite of what we wanted to show and derived a false statement (nmn \leq m when we know n>mn > m), which means our assumption was wrong. \square

3.4 Concrete example

Suppose 5 packages must be delivered to the 4 houses in Block 0 (houses 0, 1, 2, 3). By the pigeonhole principle, at least one house receives 2\geq 2 packages.

Verify: the only way to distribute 5 packages among 4 houses is to give each house at least 1 and have 1 left over. That leftover must go to some house, giving it 2. \checkmark

3.5 The generalized pigeonhole principle

A stronger version gives a tighter bound:

If n items are placed into m containers, then at least one container holds nm items.\boxed{\text{If } n \text{ items are placed into } m \text{ containers, then at least one container holds } \geq \left\lceil \frac{n}{m} \right\rceil \text{ items.}}

Here \lceil \cdot \rceil is the ceiling function: x\lceil x \rceil is the smallest integer x\geq x. For example, 5/4=1.25=2\lceil 5/4 \rceil = \lceil 1.25 \rceil = 2.

3.6 Proof of the generalized version

Suppose every container holds at most n/m1\lceil n/m \rceil - 1 items. Then the total is at most m(n/m1)m \cdot (\lceil n/m \rceil - 1). We need to show this is less than nn.

Since n/mn/m+1\lceil n/m \rceil \leq n/m + 1 (the ceiling exceeds the value by less than 1), we have:

m(n/m1)m(n/m+11)=m(n/m)=nm \cdot (\lceil n/m \rceil - 1) \leq m \cdot (n/m + 1 - 1) = m \cdot (n/m) = n

But this bound is not strict enough. We need the sharper fact: n/m1<n/m\lceil n/m \rceil - 1 < n/m, which gives:

m(n/m1)<mnm=nm \cdot (\lceil n/m \rceil - 1) < m \cdot \frac{n}{m} = n

So the total is strictly less than nn. But we have nn items. Contradiction. \square

3.7 Numerical check

With n=5n = 5 packages and m=4m = 4 houses:

54=1.25=2\left\lceil \frac{5}{4} \right\rceil = \lceil 1.25 \rceil = 2

So at least one house gets 2\geq 2 packages. This matches our direct reasoning above. \checkmark

3.8 Application to strided attention

The sparse attention blog uses the pigeonhole principle in the following form: “In any interval of length ll, there is at least one representative from each residue class modulo ll.”

Let us derive this. Consider an interval of ll consecutive integers: [a,a+l1]={a,a+1,a+2,,a+l1}[a, a + l - 1] = \{a, a+1, a+2, \ldots, a+l-1\}. This interval contains exactly ll integers. When we compute each of these modulo ll, we get ll remainders, each in {0,1,,l1}\{0, 1, \ldots, l-1\}.

We claim these ll remainders are all distinct. The proof: take any two integers a+sa + s and a+ta + t from the interval with 0s<tl10 \leq s < t \leq l-1. Their difference is tst - s, which satisfies 1tsl11 \leq t - s \leq l - 1. Since tst - s is strictly between 0 and ll, it is not divisible by ll. By the definition of congruence, a+s≢a+t(modl)a + s \not\equiv a + t \pmod{l}, so they have different remainders.

Since ll consecutive integers produce ll distinct remainders out of ll possible values {0,1,,l1}\{0, 1, \ldots, l-1\}, every residue class is represented exactly once.

3.9 Numerical check

Take the interval [2,5]={2,3,4,5}[2, 5] = \{2, 3, 4, 5\} (length l=4l = 4). Compute each modulo 4:

2mod4=2,3mod4=3,4mod4=0,5mod4=12 \bmod 4 = 2, \quad 3 \bmod 4 = 3, \quad 4 \bmod 4 = 0, \quad 5 \bmod 4 = 1

Remainders: {0,1,2,3}\{0, 1, 2, 3\} — all four residue classes are represented. \checkmark

Take the interval [5,8]={5,6,7,8}[5, 8] = \{5, 6, 7, 8\}:

5mod4=1,6mod4=2,7mod4=3,8mod4=05 \bmod 4 = 1, \quad 6 \bmod 4 = 2, \quad 7 \bmod 4 = 3, \quad 8 \bmod 4 = 0

Again: {0,1,2,3}\{0, 1, 2, 3\} — all four residue classes represented. \checkmark

3.10 Interpretation

This is the key result that makes the path-length argument work in the sparse attention blog. The strided attention head connects positions in the same residue class modulo ll. The local attention head covers ll consecutive positions. Since any ll consecutive positions contain one representative from every residue class, there is always a relay point where the two heads overlap. The pigeonhole principle turns a counting fact into an existence guarantee: we do not need to search for the relay — the principle tells us it must be there.


4. Set Operations and Cardinality

4.1 Motivation

Both the sparse attention and sliding window blogs define attention patterns as sets and then combine them. The sliding window blog combines a local window set with a global token set. The sparse attention blog takes the union of two factorized patterns. To count the total entries correctly — especially when sets overlap — we need set operations and the inclusion-exclusion principle.

4.2 Basic definitions

A set is an unordered collection of distinct elements. We write sets with curly braces. For our running example:

  • The neighborhood of house 3 with radius 2: N3={1,2,3,4,5}N_3 = \{1, 2, 3, 4, 5\}
  • The set of houses in Block 0: B0={0,1,2,3}B_0 = \{0, 1, 2, 3\}
  • The set of all houses: U={0,1,2,3,4,5,6,7}U = \{0, 1, 2, 3, 4, 5, 6, 7\}

The cardinality of a set AA, written A|A|, is the number of elements it contains:

N3=5,B0=4,U=8|N_3| = 5, \qquad |B_0| = 4, \qquad |U| = 8

4.3 Union

The union of two sets AA and BB, written ABA \cup B, is the set of elements that belong to AA or BB (or both):

AB={x:xA or xB}A \cup B = \{x : x \in A \text{ or } x \in B\}

4.4 Intersection

The intersection of two sets AA and BB, written ABA \cap B, is the set of elements that belong to both AA and BB:

AB={x:xA and xB}A \cap B = \{x : x \in A \text{ and } x \in B\}

4.5 Concrete example

Let N3={1,2,3,4,5}N_3 = \{1, 2, 3, 4, 5\} (neighborhood of house 3) and G={0,7}G = \{0, 7\} (two “global” houses). Then:

N3G={0,1,2,3,4,5,7}N_3 \cup G = \{0, 1, 2, 3, 4, 5, 7\} N3G=N_3 \cap G = \emptyset

The union has 7 elements. The intersection is empty because houses 0 and 7 are not in the neighborhood of house 3. Therefore the union’s cardinality is simply the sum of the individual cardinalities: N3G=N3+G=5+2=7|N_3 \cup G| = |N_3| + |G| = 5 + 2 = 7.

Now consider N5={3,4,5,6,7}N_5 = \{3, 4, 5, 6, 7\} (neighborhood of house 5) and G={0,7}G = \{0, 7\}:

N5G={0,3,4,5,6,7}N_5 \cup G = \{0, 3, 4, 5, 6, 7\} N5G={7}N_5 \cap G = \{7\}

The union has 6 elements, not 5+2=75 + 2 = 7. The discrepancy is because house 7 appears in both sets. We counted it twice when we added the cardinalities and must subtract it once.

4.6 The inclusion-exclusion principle

The inclusion-exclusion principle gives the correct cardinality of a union:

AB=A+BAB\boxed{|A \cup B| = |A| + |B| - |A \cap B|}

In words: add the sizes of both sets, then subtract the overlap to avoid double-counting.

4.7 Derivation

Every element of ABA \cup B falls into exactly one of three categories:

  1. In AA only (not in BB): there are AAB|A| - |A \cap B| such elements
  2. In BB only (not in AA): there are BAB|B| - |A \cap B| such elements
  3. In both AA and BB: there are AB|A \cap B| such elements

The total is:

AB=(AAB)+(BAB)+AB|A \cup B| = (|A| - |A \cap B|) + (|B| - |A \cap B|) + |A \cap B|

Simplify by collecting the AB|A \cap B| terms. The first two groups contribute AB-|A \cap B| each, the third contributes +AB+|A \cap B|:

=A+BABAB+AB=A+BAB= |A| + |B| - |A \cap B| - |A \cap B| + |A \cap B| = |A| + |B| - |A \cap B|

This completes the derivation.

4.8 Numerical check

For N5={3,4,5,6,7}N_5 = \{3, 4, 5, 6, 7\} and G={0,7}G = \{0, 7\}:

N5G=N5+GN5G=5+21=6|N_5 \cup G| = |N_5| + |G| - |N_5 \cap G| = 5 + 2 - 1 = 6

We computed N5G={0,3,4,5,6,7}N_5 \cup G = \{0, 3, 4, 5, 6, 7\} directly, which has 6 elements. \checkmark

For N3={1,2,3,4,5}N_3 = \{1, 2, 3, 4, 5\} and G={0,7}G = \{0, 7\}:

N3G=N3+GN3G=5+20=7|N_3 \cup G| = |N_3| + |G| - |N_3 \cap G| = 5 + 2 - 0 = 7

We computed N3G={0,1,2,3,4,5,7}N_3 \cup G = \{0, 1, 2, 3, 4, 5, 7\} directly, which has 7 elements. \checkmark

4.9 Application to Longformer

In the sliding window attention blog, each local token’s connectivity set is the union of its local window and the set of global tokens:

Si=WiGS_i = W_i \cup G

where WiW_i is the local window and GG is the set of global tokens. By inclusion-exclusion:

Si=Wi+GWiG|S_i| = |W_i| + |G| - |W_i \cap G|

When a global token happens to fall inside the local window, WiG|W_i \cap G| is nonzero, and the union is smaller than the naive sum Wi+G|W_i| + |G|. This is exactly the overlap that the sliding window blog accounts for when tracing the connectivity table: “Note that when a global token is already in the local window, the union does not add a new entry.”

4.10 Interpretation

The inclusion-exclusion principle is a bookkeeping tool: it ensures we count each position exactly once even when two connectivity patterns overlap. Without it, we would overcount the total number of attention entries by counting shared positions twice. In practice, the overlap between local windows and global tokens is small (global tokens are few relative to the window size), so the correction is minor — but the principle is what makes the counting rigorous.


Summary

All four tools were built from the same 8 houses on a street.

The triangular number formula Tn=n(n+1)2T_n = \frac{n(n+1)}{2} — derived by Gauss’s pairing trick — counts the sum 1+2++n1 + 2 + \cdots + n in closed form. In the attention blogs, this formula counts the total entries in a causal attention matrix: Tnn22T_n \approx \frac{n^2}{2}, making the quadratic scaling of full attention explicit. Modular arithmetic — the mod\bmod operator, residue classes, and congruence — partitions positions into groups that are separated by a fixed stride ll. In strided attention, positions in the same residue class modulo ll are connected: (ij)modl=0(i - j) \bmod l = 0 means ij(modl)i \equiv j \pmod{l}, and the residue classes correspond exactly to columns of an image grid when ll equals the row width. The pigeonhole principle guarantees that any ll consecutive positions contain one representative from every residue class modulo ll, because ll consecutive integers produce ll distinct remainders. This turns the existence of a relay position in factorized attention from a search problem into a mathematical certainty. And inclusion-exclusionAB=A+BAB|A \cup B| = |A| + |B| - |A \cap B| — correctly counts the entries when local windows and global tokens overlap, preventing double-counting.

With these tools in hand, we are ready for Why Full Attention Is Wasteful, where we derive sparse factorized attention patterns and prove that two-hop paths always exist, and Sliding Window Attention, where we combine local windows with global attention and count every entry precisely.


Previous: DeepSeek-V2 from Scratch: Multi-head Latent Attention and DeepSeekMoE Next: Why Full Attention Is Wasteful: Sparse Factorization from Scratch

Enjoyed this post?

Subscribe to get notified when I publish new posts. No spam, unsubscribe anytime.