TFMLab
0:00 / 0:00

The causal mask in 20 seconds #Causalmask#Attention

4 Oct 2026BeginnerSnap

Why attention only looks backwards.

A snap is a short loop that explains one idea. This one was pasted into the editor as code.

Transcript

Perfect in training, useless the moment it goes live. Here, every payment after Tuesday's fuel is greyed. What stops the model reading ahead? Every Transformer that predicts the next item, ChatGPT included, needs this rule. Without it, a model peeks at the answer and copies it. By the end, you'll know how one triangle of minus infinities stops the peeking, and why it turns eight payments into seven lessons.

Here are one fictional card's last eight payments, Saturday's grocery to Thursday evening's. Each row is a payment reading; each column, one it reads. With no mask, every payment reads every payment: all sixty-four cells open, the future included.

In training, the model learns by guessing each next payment from the ones before. Row five is the Wednesday grocery. Its answer is payment six, Thursday's coffee. With every cell open, row five can simply read that answer. So the training score would climb to perfect: the answer is right there to copy. Then the model goes live. When a payment is scored, the next one has not happened. There is nothing to copy, and it is useless.

The fix: before softmax, which turns scores into shares, every cell above the diagonal, every later payment, is set to minus infinity. Twenty-eight cells. In the Transformer paper's words: masking out, setting to minus infinity, all values in the input of the softmax which correspond to illegal connections.

Row five again: raw scores from the kind head of the last episode, now over the last eight payments, itself included. Now the three later payments drop to minus infinity. Softmax raises e to each score, and e to minus infinity is exactly zero. Why minus infinity, not zero? A score of zero still gets weight after softmax, since e to zero is one. Only minus infinity gets none. The five open cells still share one hundred percent. Saturday's grocery and the row's own grocery take almost three quarters between them.

What stays open is the lower triangle: each payment reads itself and everything before it. Eight times nine, divided by two: thirty-six open cells of sixty-four.

Here is the payoff. With the mask, every row is an honest lesson: guess the next payment from the past alone. Eight payments give seven lessons, and the model learns all seven in a single pass. The last row's answer has not come yet. Without the mask, training honestly would take seven separate passes, one for each stretch of the past. It’s the rule from the episode on how a TFM teaches itself: a window of two hundred fifty-six payments gives two hundred fifty-five lessons.

One place needs no mask: inside a single payment. Its fields arrive together, so field attention reads them all. A language model masks across every word. A TFM never masks within one payment, only across payments, in its sequence layers.

In our code it is one line. D is the query's position minus the key's. D at least zero: never a later payment. A practitioner's trap: PyTorch's attention function reads True as may attend; its MultiheadAttention module reads True as masked out. The mask stops peeking inside the model. Leaks through the calendar are another matter, for the episode on unseen months. To recap: minus infinity on every later payment, zero weight after softmax, and every row becomes one honest lesson. The same line has a second half: d less than window. If each layer looks back only a few payments, how does it reach further?

Sources

  1. Attention Is All You Need — Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, Polosukhin, NeurIPS 2017