Repository navigation
Block compile: keep each compiled block whole under DDP (DeepSeek-OCR torchrun) - #15359
Conversation
…tenceTransformer save Plain transformers Trainer (and Seq2SeqTrainer, other subclasses) now gets the same DDP find_unused_parameters default the generated TRL trainers already apply, before Trainer.__init__ builds the DDP kwargs. A LoRA model with reentrant gradient checkpointing no longer fails the first backward with "marked as ready twice". An explicit ddp_find_unused_parameters is kept. SentenceTransformer.save is collective under native FSDP2: every rank calls it, the shards are gathered to rank 0, which writes; the other ranks return None. The native Trainer.save_model hook gathers for a SentenceTransformer too. The saved folder equals a one-process save byte for byte.
…figs (cross-attention layers skipped without images) [skip ci]
Inside DDP.forward dynamo hands each compiled decoder block to DDPOptimizer, which splits it at gradient bucket boundaries. Under DDP Unsloth checkpoints VLMs non-reentrantly, and the recompute runs from backward outside DDP.forward, so a block compiled there is whole and saves a different number of tensors than its split forward. DeepSeek-OCR and DeepSeek-OCR-2 under torchrun on 2 GPUs failed the first backward with 'A different number of tensors was saved during the original forward and recomputation' (50 vs 49). The block call now runs with optimize_ddp off while a DDP module is active; single process, FSDP2 and the global setting are unchanged.
…dp-more # Conflicts: # unsloth/models/_utils.py
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…ecompute that recompiles [skip ci]
|
Follow-up from a deeper check (CPU gloo, 2 ranks, torch 2.6 / 2.10 / 2.13 / 2.14): the crash needs the checkpoint recompute to recompile rather than reuse the forward's cached block graph; forcing that miss reproduces @codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Targets main (follows #15324, merged as e0a8f07).
Problem
torchrun --nproc-per-node 2on the DeepSeek-OCR notebook fails on the first backward, on main and on #15324 alike:One GPU and
UNSLOTH_FSDP2=1train fine.Cause
Under DDP, Unsloth checkpoints VLMs non-reentrantly (
vision.py,vlm_gradient_checkpointing_reentrant()), and every decoder block is compiled as one region (_block_compile.py). When a block is called insideDDP.forward, dynamo hands its graph to DDPOptimizer (torch._dynamo.config.optimize_ddp), which splits it at gradient bucket boundaries. The checkpoint recompute runs from backward, outsideDDP.forward, where dynamo compiles without DDPOptimizer. A block compiled whole in the recompute does not save the same tensors as the split forward, so the checkpoint's count check fails.Evidence on one Kaggle T4x2 kernel, same script, same install: default fails as above;
torch._dynamo.config.optimize_ddp = Falsebeforetrainer.train()trains (losses 3.3576, 3.7844, 3.9564);UNSLOTH_BLOCK_COMPILE=0also trains. Forcing non-reentrant checkpointing in a single process trains, so the split is the DDP-only ingredient.Fix
_block_compile.py: while a DDP module is active andoptimize_ddpis on, the block call runs undertorch._dynamo.config.patch(optimize_ddp = False). Each block is already its own compiled region, so its gradients still reach DDP's buckets block by block. Nothing changes for a single process, FSDP2, a user's owntorch.compileof the DDP module, or the global setting.Tests
tests/python/test_block_compile_ddp_no_split.py: two CPU gloo ranks, a Llama with layers above DDP's 1 MB first bucket, block compile installed, non-reentrant checkpointing, DDP withfind_unused_parameters=Trueandbucket_cap_mb=1. It counts DDPOptimizer calls on decoder block graphs, checks every layer ran its compiled region and that both ranks hold the same gradients.[70])tests/test_block_compile.py: same results as on #15324 (43 passed, 6 skipped;test_sm_count_is_read_outside_the_tracefails on both trees).Notebook runs (Kaggle T4x2, torch 2.9.0, transformers 4.57.6, 3 steps)
Not fixed here
Recomputed values for the following tensors have different metadata(a[2, 287, 10, 128]tensor saved,[2, 10, 287, 128]recomputed). It reproduces in one process when non-reentrant checkpointing is forced, andUNSLOTH_BLOCK_COMPILE=0trains, so it is a block compile plus non-reentrant checkpoint issue of its own. Workaround until then:UNSLOTH_BLOCK_COMPILE=0under torchrun.