Skip to content

[graph_trainer] Fix full-inductor OOM: serialize ChunkedCELoss chunks - #3646

Closed
SherlockNoMad wants to merge 1 commit into
gh/SherlockNoMad/59/basefrom
gh/SherlockNoMad/59/head
Closed

SherlockNoMad wants to merge 1 commit into
gh/SherlockNoMad/59/basefrom
gh/SherlockNoMad/59/head

Conversation

@SherlockNoMad

@SherlockNoMad SherlockNoMad commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

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.

[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 12, 2026
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
@tianyu-l tianyu-l closed this Oct 7, 2026
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