Count local and global connections without double-counting
Derive window edge counts, global-token unions, and multi-layer routes under explicit boundary assumptions.
On this page
The question for today
What you’ll understand
- Count directed local-window pairs including sequence boundaries.
- Add global rows and columns as a set union.
- Distinguish multi-layer reachability from dense single-layer attention.
A sliding window gives each token a bounded neighborhood, reducing pair work while encoding a locality assumption. Global tokens add selected long-range routes: a global query can read the whole sequence, and ordinary queries can read the global keys. Their cost and benefit depend on how many such tokens exist and where they occur. Counting the union of allowed pairs reveals both the computational scaling and the overlap that a naïve sum counts twice. It also prevents a claim that a few global routes reproduce every dependency of dense attention in one layer.
Boundaries change exact window counts
For a noncausal sequence indexed zero through N−1, a radius-r local window includes keys whose distance in position is at most r, including the token itself. Interior rows have 2r+1 keys, but boundary rows have fewer. Summing clipped row widths gives an exact count smaller than N(2r+1). Causal windows use a different one-sided pattern, so a radius or width must be defined before comparing formulas or library settings.
Longformer combines local windows with selected symmetric global connections; BigBird combines local, random, and global patterns in its original construction. These choices define different operators and inductive biases. The simple count in this lesson describes a boolean dependency pattern with shared ordinary score semantics, not every projection or positional-bias detail of either architecture. In particular, actual Longformer includes separate projections for global attention, which must be preserved in an implementation-equivalence reference.
Global rows and columns form a union
Let a selected set of g existing tokens be global. Add all edges from every global query to all keys and from every ordinary query to every global key, then union them with the local pattern. The global-global submatrix lies in both added rectangles, and many global-local edges were already local. Counting each distinct ordered pair once avoids exaggerating work or accidentally duplicating a key’s exponential weight in the denominator.
For fixed radius and fixed global-token count, the upper bound is linear in N. If the task marks a number of global tokens that grows proportionally with the sequence length, the added rectangles can become quadratic. Thus a report of linear attention must state which parameters are held constant. Random blocks introduce another term and seed-dependent realized pattern, so their count and overlap should be included explicitly rather than hidden inside the word sparse.
A route takes layers to carry new information
With only radius-r local attention, stacking layers can move information across a larger neighborhood through intermediate tokens. Global tokens create shorter routes, but an ordinary query reading a global token in the current layer reads that token’s input representation, not its newly computed output from the same parallel layer. A distant ordinary token’s new information generally needs another layer to travel through a global intermediary. Structural connectivity is a capacity for communication, not a guarantee that training uses it effectively.
Evaluate the kernel against the exact intended window/global mask and projections, then evaluate task quality against the desired dense or alternative architecture. Include globals at boundaries and in the middle, overlapping local/global entries, and cases where every token is global. A useful implementation should neither count duplicates twice nor silently expand a bounded window into dense work. Measure actual executed tiles as well as logical pairs, because a fine window can occupy partially masked hardware blocks.
Work it through
A boundary global token adds eight distinct pairs
Use N=6, a symmetric local radius r=1, and one existing global token at position 0. Every query can read its local neighbors and global key 0; query 0 can read every key. Count directed allowed pairs with self pairs included.
The local count is 6×3−1×2=16 pairs. The row widths before global additions are 2,3,3,3,3,2, reflecting the two sequence boundaries rather than a circular window.
Global row 0 and column 0 have 2×6−1=11 distinct pairs. Their overlap with the local pattern is {(0,0),(0,1),(1,0)}, totaling 3 pairs. The union therefore has 16+11−3=24 pairs.
Check row by row after the union: widths are 6,3,4,4,4,3, summing to 24. The global addition contributes 8 new pairs, and dense attention would have 36 pairs, so the pattern uses two thirds of the dense pair count.
Adding a global token is a union operation with overlap. The simple upper bound 16+11=27 is valid but is not the exact 24-pair count for this placement.
The living notebook
Explore the model
Interactive lab · modular
A transition pattern controls multi-step reachability
Reset, set Modulus m to 6 and Additive step a to 2. Predict period 3 and residues 2 then 4 after two Steps. Reset, set m to 6 and a to 1, predict period 6 and residues 1 then 2 after two Steps. This cyclic one-transition model illustrates path composition, not clipped windows, global-token aggregation, or an attention layer.
- Residue r0
- 0
- Orbit period · m/gcd(m,a)
- 12
- Distinct residues visited
- 1 of 12
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 sequence of allowed transitions carries information only along its reachable routes.
Pause & reason
Check your understanding
Your turn
Try it yourself
Use N=5, radius r=1, and existing global token 2. Count the final row widths and total union pairs. Compare with the local-only count.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
The middle global row reads all five keys; each other row includes key 2 whether or not it was already local.
Compare with the solution
Local-only count is 5×3−2=13. Final row widths are 3,3,5,3,3: rows 0 and 4 gain key 2, rows 1 and 3 already had it, and global row 2 expands from three keys to five. The union totals 17 pairs, adding four distinct pairs. Dense attention would contain 25 pairs; the placement changes overlap compared with a boundary global token.
Keep the idea
Take it with you
- Count clipped local windows with an explicit width convention.
- Add global rows and columns by set union.
- Fixed-radius linear scaling also requires bounded global and random connection counts.
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.