Sliding window: how far back can it see? #Attention#Efficiency#Interactive
How our TFM handles attention over the history → How it’s built
Each layer looks at only the last w events. Stack layers and the reach grows, while each layer stays cheap.
Sequence attentionTransformer foundationsEncoder architecture
Full causal attention compares every event with every earlier one: the cost grows with S² for a sequence of length S. A sliding window limits each event to its last w events, so the cost per layer is about S × w.
The trick is stacking: layer 2 reads layer 1, which already summarised a window. After L layers an event is influenced by roughly L × w earlier events.
Transcript
You'd think long memory needs every layer to see everything. It doesn't. One line of code limits each: d less than window. A late-night nine-hundred-dollar payment lands on one card. How far back can the model reach to judge it? By the end, you'll know how six layers that each see sixteen payments reach ninety-one, and why sixteen payments can mean two days or ten. In this lab, each layer, one round of reading, has a window of sixteen: a payment reads itself and the fifteen before it. The trade-off: see too little, and old habits drop from view; see everything, and cost climbs.
But a model has more than one layer. Layer two reads the outputs of layer one, not the raw payments. Each of those outputs has already read its own sixteen. So layer two reaches fifteen payments further back. Fifteen, not sixteen, because neighbouring windows share their newest payment. Two layers reach thirty-one. So the newest payment's layer-two output carries traces of thirty-one payments, though it read only sixteen. Think of a message passed down a line of people: each one only talks to the next, yet the message travels far.
Add a layer, add fifteen. Three layers reach forty-six, then sixty-one, then seventy-six. Six layers reach ninety-one payments back, counting the payment itself. The rule: layers times the window minus one, plus one. Six times fifteen is ninety; plus one makes ninety-one. Widen the window and each layer adds more; add a layer and the reach grows by the window minus one.
That reach is a ceiling, not a guarantee. The farthest payment touches the top only through six hops, one per layer. At every hop it is mixed with up to fifteen others, so by the top its share can be faint. Reach says what can matter, never what does. What does matter is learnt. Engineers call this reach the receptive field: the payments that can influence one output at the top.
Why not let every layer see everything? Cost. Take a history of two hundred and fifty-six payments. Full causal attention scores every payment against every earlier one: thirty-two thousand, eight hundred and ninety-six scores per layer. With a window of sixteen, it's three thousand, nine hundred and seventy-six: about an eighth. And the gap widens as histories grow. Each score is one dot product, a multiply-and-add of two lists, as in the episode on softmax. Fewer scores means less compute and less memory. At serving time, scoring live payments, the window caps the work for each new payment: sixteen earlier payments per layer, however long the history grows.
The trick isn't ours. Longformer, a 2020 model for long documents, stacks sliding windows the same way. In its own words: in a transformer with l layers, the receptive field size at the top layer is l times w. Longformer's window looks both ways, before and after. A TFM's looks only back, as the episode on peeking showed. So its rule of thumb is layers times window. Counted exactly for a look-back window, it's ninety-one.
Now real data, not Maya. On Sparkov, the public card dataset, how much time do a card's sixteen payments span? These spans come from all nine hundred and ninety-nine cards, every stretch of sixteen payments in a row. At the median, four point two days. One window in ten spans under one point nine days; another one in ten, over ten. So the same sixteen payments can be two busy days or a quiet ten. So measure these spans on your own data before you set a window. That's a TFM's twist on the window. A language model's counts words of one text; a TFM's counts payments, which keep no steady pace.
Our base design uses a different setting: a window of sixty-four, and four sequence layers, over a history of a hundred and twenty-eight payments. Layer one sees sixty-four. Layer two, a hundred and twenty-seven. By layer three, it covers the whole history. With sixty-four and four layers, the ceiling would be two hundred and fifty-three, more than the history holds. And sixty-four Sparkov payments span about eighteen days at the median, so one layer alone sees more than two weeks. So in our design, the history, not the window, sets the reach.
To recap: each layer looks back one window; stacked, the windows add up, to ninety-one in the lab, as a ceiling. Here are two real windows of sixteen payments on one time axis. One spans about two days; the other, about ten. To the window, both are just sixteen. So does the model know which is which? That's the next episode.
Chapters
Watch first
In code (PyTorch)
import torch
S, w = 256, 16
i = torch.arange(S)
# allowed[q, k]: query q may look at key k
allowed = (i[None, :] <= i[:, None]) & (i[:, None] - i[None, :] < w)
scores = torch.randn(S, S).masked_fill(~allowed, float("-inf"))
weights = scores.softmax(dim=-1) # each row sums to 1 over its window