Skip to content

Avoid overflow when accumulating chunked half-precision losses - #2412

Open
Afloat16 wants to merge 1 commit into
Lightning-AI:mainfrom
Afloat16:fix/chunked-loss-half-accumulation-20261112
Open

Afloat16 wants to merge 1 commit into
Lightning-AI:mainfrom
Afloat16:fix/chunked-loss-half-accumulation-20261112

Conversation

@Afloat16

Copy link
Copy Markdown

Fixes #2411.

Problem

Both chunked cross-entropy branches sum token losses in the original dtype before dividing by the valid-target count. With direct float16 inputs, the sum can exceed 65504 even when every token loss and the correct mean are small and finite. A 4 x 4096-token, 128-class uniform example returns inf instead of approximately 4.852. This also affects an actual half-precision GPT training loss.

Change

Use at least float32 for the scalar sum and normalization, then convert the mean back to the original loss dtype. Preserve float64 accumulation, the existing chunk construction, masking, valid-target count and result dtype. This adds no full-size float32 copy of the per-token losses.

Validation

Base: 5341568a65e918a85cb284fac038de2157eebf3f.

  • Identical 34 new native cases on unchanged main: 8 failed, 26 passed. With the fix: 34 passed.
  • New tests plus all eight existing chunked-cross-entropy tests: 42 passed.
  • Tensor/list inputs, float16/bfloat16/float32/float64, custom ignore indices, long finite losses, 65536-token counts, all-ignored zero gradients and valid NaN propagation are covered.
  • Native half/bfloat16 GPT models train for two momentum-SGD steps with 1024 x 16-token batches. Losses, all parameter gradients and resulting weights are compared with a float32 cross-entropy reference; the float16 baseline fails with infinite loss in both LM-head layouts.
  • All applicable pre-commit hooks and git diff --check passed.
python -m pytest tests/test_chunked_loss_accumulation.py \
  tests/test_utils.py::test_chunked_cross_entropy -q

Linux, Python 3.12, PyTorch 2.15.0+cpu. These are direct half-precision inputs; this does not claim the same overflow for autocast paths that already return float32 CE outputs. No downloaded model, numerical mocks, GPU/distributed run or full repository suite. Prettier and pyproject-only hooks were skipped because neither changed file uses those formats; applicable Python hooks passed.

The native issue was opened before implementation. Checked current/legacy source history, all-state chunked-loss/overflow/dtype/precision searches and all nine existing fork heads. #751 adds valid-target normalization, #1374 removes synchronization, #2312 changes chunk-size budgeting and #2347 adds accumulation-window normalization; none widens this sum. #2398 fixes a different unchunked all-ignored-target path.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Half-precision chunked cross entropy overflows while averaging finite token losses

1 participant