research26 min studyLesson 757 in Engineering

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

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.

Elocal=N(2r+1)−r(r+1),0≤r<NE_{\rm local}=N(2r+1)-r(r+1),\qquad 0\le r<N
This exact directed-pair count includes self pairs and uses a symmetric radius-r window clipped at the ends of a single noncausal N-token sequence. N is positive and r is a nonnegative integer. It does not describe a causal or circular window.↔ Scroll wide equations horizontally.

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.

E=L∪(G×V)∪(V×G),∣E∣≤Elocal+2Ng−g2\mathcal E=\mathcal L\cup(G\times V)\cup(V\times G),\qquad |\mathcal E|\le E_{\rm local}+2Ng-g^2
V is the set of N existing token indices, G⊆V has size g, and L is the local ordered-pair set. The global-row/column union has exactly 2Ng−g² pairs; overlap with L makes the displayed sum an upper bound, not generally an equality.↔ Scroll wide equations horizontally.

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.

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.

  1. 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.

  2. 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.

  3. 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.

The result

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.

Explore the model

Interactive lab

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.

A transition pattern controls multi-step reachabilityAddition 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 sequence of allowed transitions carries information only along its reachable routes.

Check your understanding

Why does a fixed number of global tokens preserve a linear pair-count bound for a fixed local radius?

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.

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.