Repository navigation
Studio diffusion: keep the checkpointing budget with DiT block swap - #18684
Merged
Merged
Conversation
Swapped blocks stay checkpointed and are never measured or planned; the resident blocks are planned into the room the swap leaves. The plan holds back the swap pool's auto prefetch growth, and an out-of-memory rerun resets the slot pools.
…on policy A swapped block's hooks copy the next block's weights inside the checkpointed region, in the other direction during the recompute, which the selective checkpoint rejected in backward. Swapped blocks use a plain non-reentrant checkpoint; resident blocks keep saved attention.
Reserving the pool's full auto growth left no room for kept activations on FLUX.2-klein at 1024 px; the pool already grows only into room the last step's peak leaves, so no reservation is needed.
Codex Review Summary: ✅ CompletedThis 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. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
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.
#18604 added DiT block swap and switched the gradient checkpointing budget off whenever a run swaps blocks (
gc_budget = None if swapping), so every block was checkpointed. On an L4 with Z-Image at 512 px and 10 of 34 blocks swapped, that run trained at 1.19 s/step with a 10.5 GiB loop peak, leaving about 12 GiB of the card unused. The same model without swap trains at 0.79 s/step with the budget.Change
The budget now runs alongside block swap:
install_dit_block_swapmarks each swapped block.BudgetedCheckpointalways checkpoints a marked block and never measures or plans it, because kept activations would hold its streamed weights on the card until backward. Only resident blocks are planned, into the room the swap leaves after step 1.aten.copy_.default encountered during backward but not found in storage). Resident blocks keep saved attention.reset_block_swapsreturns every slot to the pool before the step replays.UNSLOTH_DIFFUSION_GC_MODE=plainorpartial, or on ROCm, nothing changes:_checkpoint_mode(swapping)still forces plain checkpointing for a swapped run.Results
Colab L4 (22.5 GiB), torch 2.14.1, Z-Image bf16 at 512 px, LoRA r16, 50 steps,
block_swap=10, compile auto. Base is origin/main. Each arm ran in a fresh process, arms interleaved, 6 runs per side over two passes on the same VM.block_swap=auto, L4 (14 of 32 blocks swapped): tested on an earlier revision of this branch that held back room for pool growth. That revision planned 0 of 18 resident blocks and ran at 5.44 s/step against 5.53 on main, 4 runs each, losses inside the A/A spread. This is the reason the reservation was dropped.Tests
New file
tests/test_diffusion_block_swap_budget.py, CPU only:test_trainer_keeps_the_budget_with_block_swap: the trainer enables the budget for swapped runs, and the out-of-memory rerun resets the swap pools before replaying. Fails on main, passes here.test_swapped_blocks_are_never_measured_or_kept: swapped blocks always go through the plain checkpoint, are never measured, and are never kept after the plan. Resident blocks are planned. Fails on main, passes here.test_swapped_blocks_use_a_checkpoint_without_the_selective_policy: the swapped-block checkpoint has no saved-attention policy, and its backward runs.test_install_marks_only_the_swapped_blocks:install_dit_block_swapmarks exactly the planned blocks (BlockSwap stubbed).test_rerun_resets_the_swap_pools:reset_block_swapsresets every installed swapper.tests/test_diffusion_block_swap*.py,tests/test_diffusion_gc_budget*.py,tests/test_diffusion_gc_saved_attention.pyandtests/test_diffusion_train_speed.pyall pass. Differential test against main: 1 fixed, 4 new, 117 unchanged, 0 failing on this branch.Not proven here
block_swap=auto. On FLUX.2-klein 9B at 1024 px it should now plan some resident blocks into the room the pool reservation used to hold. The out-of-memory step-down still covers a plan that turns out too large.copy_failure, because it needs the real CUDA swap hooks. It showed up on the L4 runs before the fix and is gone after it.