Skip to content

Studio diffusion: keep the checkpointing budget with DiT block swap - #18684

Merged
danielhanchen merged 3 commits into
mainfrom
studio-diffusion-block-swap-keeps-budget
Nov 20, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-diffusion-block-swap-keeps-budget

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

#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_swap marks each swapped block. BudgetedCheckpoint always 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.
  • Swapped blocks go through a plain non-reentrant checkpoint without the saved-attention policy. Their swap hooks copy the next block's weights inside the checkpointed region, and in the other direction during the recompute. The selective checkpoint rejected that in backward (aten.copy_.default encountered during backward but not found in storage). Resident blocks keep saved attention.
  • On an out-of-memory rerun, reset_block_swaps returns every slot to the pool before the step replays.
  • The swap pool's auto prefetch growth gets no reservation. The pool only grows into the room left by the previous step's peak, and that peak already includes the kept activations.
  • With UNSLOTH_DIFFUSION_GC_MODE=plain or partial, 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.

arm steady s/step median [range] loop peak alloc GiB mean loss over 50 steps [range]
main (every block checkpointed) 1.186 [1.168-1.196] 10.49 0.40719-0.40723
this PR (24 of 24 resident blocks kept) 0.921 [0.917-0.930] 14.02 0.40722-0.40739
no swap, budget (reference) 0.788 [0.783-0.793] 17.29 0.40723-0.40735
  • Speed: 1.29x faster per step with the same 10 blocks streamed. The two ranges do not overlap.
  • Loss: the largest per-step loss gap between main and this PR has a median of 0.0014, inside the spread between this PR's own repeats (median 0.0016). Mean losses agree to 2e-4.
  • Compile: 0 graph breaks, 5 unique graphs and 0 recompiles on both sides.
  • Out-of-memory retry: a test allocation after the plan left room for only a third of the kept activations. Step 2 ran out of memory and stepped down five times, from 24 kept blocks to 12, 6, 3, 2 and 1, replaying the step each time. All 50 steps finished (2 of 2 runs), with mean loss 0.40725 and 0.40723, the same as main. Steady speed was then 1.19 s/step, the same as main.
  • FLUX.2-klein 9B, 1024 px, 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.
  • No-swap path on A100 40 GB: FLUX.2-klein 9B auto swaps 0 blocks there. Main and this PR are identical, at 0.959-0.961 s/step, 21 of 32 blocks kept and a 31.9 GiB peak.

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_swap marks exactly the planned blocks (BlockSwap stubbed).
  • test_rerun_resets_the_swap_pools: reset_block_swaps resets every installed swapper.

tests/test_diffusion_block_swap*.py, tests/test_diffusion_gc_budget*.py, tests/test_diffusion_gc_saved_attention.py and tests/test_diffusion_train_speed.py all pass. Differential test against main: 1 fixed, 4 new, 117 unchanged, 0 failing on this branch.

Not proven here

  • The final revision has no L4 run with a model that needs 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.
  • The CPU tests cannot reproduce the selective-checkpoint 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.
  • Only L4 and A100 were tested. No consumer 24 GB card, no Blackwell, no AMD. Block swap is CUDA-only.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T22:30:10.539207Z 327898b Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 327898bcf6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen merged commit 647cf77 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-block-swap-keeps-budget branch November 20, 2026 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant