Skip to content

Fix ChunkedCELoss final FSDP wrapping - #3229

Closed
iamzainhuda wants to merge 1 commit into
pytorch:mainfrom
iamzainhuda:chunked-ce-fsdp-final-layer-fix
Closed

iamzainhuda wants to merge 1 commit into
pytorch:mainfrom
iamzainhuda:chunked-ce-fsdp-final-layer-fix

Conversation

@iamzainhuda

@iamzainhuda iamzainhuda commented May 5, 2026 •

Copy link
Copy Markdown

Summary

  • keep final norm and lm_head in separate FSDP units when ChunkedCELoss is active
  • avoid forcing an immediate lm_head reshard at the end of the chunked loss forward
  • replace the custom chunked-loss autograd bridge with an equivalent zero-value gradient bridge
  • add unit coverage showing the zero-value bridge returns the same scalar and computes the same hidden-state gradients as the legacy custom autograd bridge

ChunkedCELoss runs backward through lm_head chunk-by-chunk before the outer decoder backward reaches final norm. When norm and lm_head share one FSDP unit, lm_head backward/reshard can affect storage that norm still needs later in the same backward pass. On the 8-GPU DeepSeek V3 debug model this fails during the first backward pass with an unallocated tensor error.

Splitting norm and lm_head only for the chunked-loss path isolates their FSDP storage lifecycle while preserving the existing grouped wrapping for the standard loss path.

The zero-value gradient bridge keeps the same forward value as the old custom autograd Function while attaching the accumulated chunk gradient to hidden_states through regular PyTorch ops. The added unit test recreates the legacy bridge locally and verifies both approaches produce identical scalar values and hidden-state gradients.

Validation

  • PYTHONPATH=/home/dev/ao pytest -q tests/unit_tests/test_loss.py
  • latest origin/main fails with NGPU=8 MODULE=deepseek_v3 CONFIG=deepseek_v3_debugmodel ./run_train.sh --training.steps 3
  • this branch completes the same 8-GPU DeepSeek V3 debug-model smoke for 3 steps

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 5, 2026
@pytorch-bot

pytorch-bot Bot commented May 5, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@iamzainhuda
iamzainhuda force-pushed the chunked-ce-fsdp-final-layer-fix branch from a1678b0 to 5069287 Compare May 5, 2026 19:02
@wwwjn

wwwjn commented May 5, 2026

Copy link
Copy Markdown
Contributor

latest origin/main fails with NGPU=8 MODULE=deepseek_v3 CONFIG=deepseek_v3_debugmodel ./run_train.sh --training.steps 3

Hi thanks for this. Did you run this test with latest pytorch nightly? We have CI to guard this which is passing on ChunkedLoss change. ChunkedLoss depends on PyTorch changes pytorch/pytorch#180428

@iamzainhuda

Copy link
Copy Markdown
Author

The custom autograd function change is an additional thing I added that isn't actually needed to make this work but thought we could simplify. I'm happy to remove it if you want to keep it in. There is a small transient memory tradeoff because hidden_states * accumulated_grad, but this isn't persistent mem.

@iamzainhuda

Copy link
Copy Markdown
Author

latest origin/main fails with NGPU=8 MODULE=deepseek_v3 CONFIG=deepseek_v3_debugmodel ./run_train.sh --training.steps 3

Hi thanks for this. Did you run this test with latest pytorch nightly? We have CI to guard this which is passing on ChunkedLoss change. ChunkedLoss depends on PyTorch changes pytorch/pytorch#180428

Just ran it on the latest pytorch nightly, the issue is gone - so this PR is no longer needed. Thank you for the response!

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

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants