AIToday
Large Language ModelsAI Coding AssistantsQiita 機械学習Published: Nov 2, 2026, 13:00 JST

PyTorch FP32 LayerNorm dies silently past 3.4×10^38

Send on LINE
PyTorch FP32 LayerNorm dies silently past 3.4×10^38

3 Key Points

  1. What happened

    Training a 7.0B weight-shared recursive Transformer on one RTX 3090, the author found PyTorch's FP32 nn.LayerNorm returns only its bias once a row's sum of squared deviations exceeds FP32's max of about 3.4×10^38, with no NaN.

  2. Why it matters

    The output becomes a constant independent of the input, so NaN checks and anomaly detection miss it, and logits stay identical across inputs, meaning a model can look like it is training while a layer has stopped passing information.

Not sure about something? Ask the AI

Questions and answers are published on this page.

Summaries like this, every morning.

Context & Analysis

A practitioner trained a weight-shared recursive Transformer, a design that applies the same block repeatedly, so hidden-state magnitudes accumulate with depth. At the 200M-token checkpoint, the hidden state just before the final LayerNorm reached a sum of squared deviations of 2.70×10^38, close to FP32's 3.4×10^38 ceiling, and rsqrt of the variance fell to 7.7×10^-19, effectively zero.

The minimal CPU script isolates the mechanism. Because LayerNorm sums the squared deviations across the hidden dimension, a single element's square can be only about 1/65 of the FP32 maximum while the row total already overflows. In the sweep, rows collapsed exactly when that row sum exceeded FP32's max, with zero mismatches across 12,248 rows. The CE during collapse sat near ln(32000)=10.37, a finite value that looks plausible in a training log.

The reported fix was to compute only the final LayerNorm in FP64 while keeping input and output dtypes unchanged. In the real model, collapse occurrences went from 6 to 0, usable recursion depth rose from 8 to 15, and validation CE at 200M tokens fell from 10.673 to 9.806, though the author notes other changes were also included. Applying FP64 to every layer was not benchmarked, and the author notes values could still grow beyond FP64's range.

FAQ
Does mixed precision prevent this LayerNorm overflow?
No. PyTorch's autocast classifies LayerNorm as an FP32 operation, so training in bf16 still leaves the LayerNorm's internal math bound by FP32's limit.
How was the collapse detected in the real model?
By counting rows where the normalized part of the LayerNorm output is zero, and by checking whether logits differ across inputs in a batch. In the reported model the logits' batch spread was 0.000 and the final-norm variance calculation overflowed.
What fixed it in the reported model?
Replacing only the final LayerNorm's variance and rsqrt computation with FP64 removed the collapse in both the reproduction and the real model. FP64's max of about 1.8×10^308 handles values that overflow FP32.
Qiita 機械学習Read Original Article
Send on LINE

AI news that matters for your work, delivered every morning.

Pick your industry and the AI tools you use, and get news related to your work every day.

Free · 30 seconds with Google · unsubscribe anytimeWhat is AIToday? →

Ask AI

Ask AI anything about this article. The AI reads this article, earlier AIToday articles, and Wikipedia, and cites its sources. Q&As are published on this page for other readers too.

Questions and answers are published on this page.

Related Articles

Next articleAltman: pool scarce DRAM in data centers, not personal PCs