Repository navigation
[graph_trainer] Fix full-inductor OOM: serialize ChunkedCELoss chunks - #3646
Closed
SherlockNoMad wants to merge 1 commit into
Closed
SherlockNoMad wants to merge 1 commit into
SherlockNoMad wants to merge 1 commit into
Conversation
This was referenced Jun 12, 2026
Closed
IvanKobzarev
pushed a commit
to IvanKobzarev/torchtitan
that referenced
this pull request
Jun 17, 2026
Full inductor (--compile.inductor_compilation full) OOM'd DSv3-16B at B=16 and ran ~12-15 GiB heavier than regional at B=8. Root cause: inductor de-streams the ChunkedCELoss -- it batches all num_chunks lm_head matmuls so every chunk's [tokens, vocab] logits (fwd) + grad-logits (bwd) are live at once (inductor-planned peak 40 GB at the loss backward; 16 x 1.68 GiB chunk buffers), reconstituting the full logits tensor that chunking exists to avoid. reorder_for_peak_memory does not serialize these independent regions; the interpreted/regional path streams them. Not fusion, CSE, or the cudagraph static pool -- all ruled out by direct measurement (a realize fusion-barrier pass had zero effect). Fix: serialize_loss_chunks_pass (before full_inductor_compilation_pass, full branch only) makes each chunk's lm_head matmul input data-depend on the previous chunk's nll_loss_forward via `hidden + nan_to_num(prev_loss).to(hidden.dtype) * 0` -- numerically identity (additive same-dtype zero), but the read orders the chunks so each chunk's logits free before the next allocates. nan_to_num guards inf*0=nan; the dtype cast stops the fp32 loss anchor from promoting the bf16 matmul (which produced nan at B=16). Disable with --compile.disable_passes serialize_loss_chunks_pass. Bitwise-verified identity (deterministic B=8, serialize ON vs OFF: loss and grad_norm match at steps 1 & 10). Result: B=16 full+cg OOM -> 75.48 GiB, MFU 23.51% (~4% faster than regional 22.54%); B=8 55.71 -> 50.24 GiB. The remaining ~18 GiB vs regional is the still-batched backward grad-logits (follow-up). Also adds INDUCTOR/CUDAGRAPH/BATCH toggles to run_graph_trainer_dsv3.sh and documents the investigation as Runs 11-12 in dsv3_scaling_experiment_results.md. ghstack-source-id: 7513d06 Pull-Request: pytorch#3646
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
Full inductor (--compile.inductor_compilation full) OOM'd DSv3-16B at B=16 and ran
~12-15 GiB heavier than regional at B=8. Root cause: inductor de-streams the
ChunkedCELoss -- it batches all num_chunks lm_head matmuls so every chunk's
[tokens, vocab] logits (fwd) + grad-logits (bwd) are live at once (inductor-planned
peak 40 GB at the loss backward; 16 x 1.68 GiB chunk buffers), reconstituting the
full logits tensor that chunking exists to avoid. reorder_for_peak_memory does not
serialize these independent regions; the interpreted/regional path streams them.
Not fusion, CSE, or the cudagraph static pool -- all ruled out by direct
measurement (a realize fusion-barrier pass had zero effect).
Fix: serialize_loss_chunks_pass (before full_inductor_compilation_pass, full branch
only) makes each chunk's lm_head matmul input data-depend on the previous chunk's
nll_loss_forward via
hidden + nan_to_num(prev_loss).to(hidden.dtype) * 0--numerically identity (additive same-dtype zero), but the read orders the chunks so
each chunk's logits free before the next allocates. nan_to_num guards inf*0=nan; the
dtype cast stops the fp32 loss anchor from promoting the bf16 matmul (which produced
nan at B=16). Disable with --compile.disable_passes serialize_loss_chunks_pass.
Bitwise-verified identity (deterministic B=8, serialize ON vs OFF: loss and grad_norm
match at steps 1 & 10). Result: B=16 full+cg OOM -> 75.48 GiB, MFU 23.51% (~4% faster
than regional 22.54%); B=8 55.71 -> 50.24 GiB. The remaining ~18 GiB vs regional is
the still-batched backward grad-logits (follow-up).
Also adds INDUCTOR/CUDAGRAPH/BATCH toggles to run_graph_trainer_dsv3.sh and documents
the investigation as Runs 11-12 in dsv3_scaling_experiment_results.md.