18/29
Part 4 of 5 · Precision and top-kLearn and do

The top token is the best in its own bin, so it can never be lost

Approximate top-k has three steps: split the scores into bins, keep the largest in each bin, then take an exact top-k of those winners.

The single most likely token is the largest value in the whole vocabulary. So it's the largest value in its own bin. Step 2 always keeps it, and step 3 always ranks it first. In the last step's 16 scores, that's id 2, at .5.

A correct approximate top-k can lose tokens near the bottom of the top k. It can never lose the top one.

The post's footnote says the same: "The approximation works by accepting potential inaccuracies in the lowest probability tokens, which shouldn't affect quality."

Removing the December workaround uncovered the bug

The rule that the top token can never be lost is what made the August bug a bug, and not an approximation error.

The August 26 rewrite fixed the precision issues. It also removed the December workaround, because Anthropic believed it had solved the root cause.

With the workaround gone, the approximate top-k "sometimes returned completely wrong results, but only for certain batch sizes and model configurations" (1 source). The December workaround had been masking this problem, by accident.

It was miserable to chase. It changed with unrelated factors, such as which operations ran before or after it, and whether debugging tools were on. "The same prompt might work perfectly on one request and fail on the next."

The reproducer Anthropic shared with the XLA:TPU engineers came with a telling caption: "The code returns correct results when run on CPUs."

Test the rule that must never break

The rule also hands you a cheap test for this class of bug. For every batch size and configuration you ship, assert that the argmax of the full distribution is in the returned set.

It needs no stored reference outputs, and it runs in CI.

In the editor, write approx_topk(x, k, bins): those three steps, on a numpy array. The last step's 16 scores in 4 bins make a good test: with k = 2 it returns ids 2 and 9.