CourseTransformers: From Attention to Trained, Served and Interpreted Models · Module 3: The Transformer Block · part 26 of 111
Part 26 · Module 3: The Transformer Block

Incident: Fine in bfloat16, NaN in float16

16 min read·13 Oct 2026
Module 3 · Incident file · About 40 minutes

What happened

T5 IN FLOAT16: A FIX, A RELAPSE, AN EXPLANATION 8 Jan 2021 PR #9487 clamp opened 12 Jan 2021 merged fp16 'fixed' 21 Mar 2021 #10830 NaNs again 29 Mar 2021 stas00 FF autocast 30 May 2023 Graphcore bf16-trained
The clamp (blue) was declared a fix in January 2021 (green), NaNs returned in March (red), and the root cause, a bfloat16-trained model, was written up in 2023 (cited).

On 12 January 2021 Suraj Patil (Hugging Face user valhalla) announced on the Hugging Face forum that "t5-small was the only T5 model that was working in fp16. The rest of the models produce nan loss/logits" (discuss.huggingface.co/t/3139). T5 is Google's encoder-decoder transformer, then "one most widely used model" in the transformers library, in his words. For "all the models and versions (v1, v1.1, mT5)", he wrote, "we get inf values in hidden_states after applying the final linear layer (wo)", the down projection of the feed-forward network, "which results in nan values in T5LayerNorm". For larger models the infinities also appeared "where we add the attn output with the hidden_states": the residual addition.

The fix, pull request #9487 ("[T5] enable T5 fp16", opened 8 January, merged 12 January 2021, github.com/huggingface/transformers/pull/9487), clamped the hidden states "to the max value of the current data type" after the feed-forward and both attention outputs. It did not hold. On 21 March 2021 Yuval Kirstain reported NaNs again with t5-large, from the norm's rsqrt line, adding "it happens in the last layers" (issue #10830); On 29 March stas00 wrote that "the problem is due to autocast in T5LayerFF", the feed-forward layer, and proposed turning mixed precision off there. In May 2023 Harry Mellor of Graphcore wrote that T5 "has only worked with hardware that supports bfloat16, the format that the model was originally trained with", measured the down projection's activations at a standard deviation of about 4,400, and noted that the residual additions overflowed too (graphcore.ai, 30 May 2023). The cost was not a crash. Users training or running T5 in float16 got NaN losses and logits, and could not have the "decent speed-up" the announcement promised, for more than two years on some hardware.

The rest of this course is yours to keep

This course is bought on its own, once, and stays readable afterwards, including the parts added to it later.