← All writing

Language modeling · Experiments

Two NanoGPT experiments: attention gradients and table votes

Skipping some attention backward work, and letting an n-gram table vote on the answer.

On this page

On the modded-nanogpt short track, the goal is to get below 3.28 validation loss as fast as possible. Here are two things I tried, and both set records when I submitted them. The table-vote run is the current record, pending the official leaderboard update. In one run I let an attention head look further back than I was willing to backpropagate. In the other I gave a lookup table a vote on the next token, even though the model had already seen its hints.

A shorter backward pass#

Say a head puts almost all its attention on the last few tokens. In the forward pass it still has to score the distant tokens to find out how much they matter. By the time we go backward, though, we already know where the attention went. How much work would we save by skipping the distant tokens for that head?

Attention · one example headWhere the backward pass can skip work
ForwardRead the full window
BackwardSkip distant tiles

Read across a row from older tokens to newer ones. Brighter tiles show stronger attention in this sketch. Hatched tiles still counted in the forward softmax; the backward kernel skips them.

In my run, I picked the window by trying it. On steps 592 and 593 I ran backward with windows of 64, 128, and 256 tokens, then compared each head's query, key, and value gradients with the full-window gradients. If all three came within 20% relative L2 error on both steps and every GPU, that window was good enough. Each eligible head got the cheapest window that passed. A head that needed the whole window kept it.

The nice part is that nothing changes about what the model reads. Even in backward, the nearby tokens keep the attention weights they got when they competed with all the tokens. The kernel just skips the distant tiles.

I also cut down the work on the output layer. For its weight gradient, I sampled one token row in four from the dense positive terms and multiplied its contribution by four. I kept the target-token corrections and the gradient flowing back into the model in full. With both changes on the same eight H100s, average training time went from 40.34 to 39.58 seconds. The last 87 updates used the regular backward pass.

Give the table a vote#

Herman Brunborg's run brought training time down from about 40 seconds to 6.7. While the GPUs trained the transformer, the CPUs built a lookup table from 37 billion training tokens. If the last few tokens of a passage looked familiar, the table could tell the transformer what usually came next.

The model gets those hints during training. But 540 updates isn't much time to learn how seriously to take a match. So I tried something that sounds a little silly: give the transformer the hint, let it make its prediction, then ask the same table for its opinion again.

In the table-vote run, each matching continuation gets one vote. The model gets k votes to split among tokens according to its probabilities. Add up the votes and divide by the total. A miss leaves the prediction alone.

Table vote · worked exampleHow much should three matches count?

Going into the 20-token lookup, a particular next token has a 20% chance. In this example every matching passage continues with that token.

Before the lookup20%
After the vote62.1%

With three matches and k = 2.694, the probability is (3 + 2.694 × 0.20) / (3 + 2.694) ≈ 62.1%.

I do this for matches of length 6, then 10, then 20, feeding each result into the next vote. I picked k on a training shard outside the table. For a six-token match the model gets about 707 votes; for ten tokens it gets 43; for twenty it gets fewer than three. So a handful of repeats can move the prediction quite a bit if they agree for twenty tokens first.

I used 526 updates and built the table from 36.1 billion tokens. With those same trained weights, switching the vote on took mean validation loss from 3.2933 to 3.2737 across seven seeds. Without the vote, every seed missed 3.28. With it, all seven cleared it.