Skip to content

Studio: pick DiT block swap from the training API and the Train tab - #18640

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

danielhanchen merged 3 commits into
mainfrom
studio-diffusion-block-swap-ui

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

#18604 added a block swap for DiT LoRA training (cfg.block_swap / UNSLOTH_DIT_BLOCK_SWAP = off, auto or a block count). It could only be turned on through the trainer config dict or the env var. The training API dropped the field, because DiffusionTrainingStartRequest did not declare it, and the Train tab had no control for it. This PR lets you pick it from both.

Change

Backend:

  • DiffusionTrainingStartRequest.block_swap: optional, "off", "auto" or an integer 0..1024. The default is unset, which sends nothing to the trainer, so behaviour is the same as today, and UNSLOTH_DIT_BLOCK_SWAP still applies. A count is passed on in the trainer's string form.
  • /api/train/diffusion/start now calls block_swap_start_error, which runs before any GPU residents are freed. It refuses a block swap the trainer would only refuse after loading the base, plus families whose trainer has no swap path:
    • families outside the DiT trainer (SDXL, MiniMax-H3)
    • base_precision other than bf16
    • gradient_checkpointing off
    • the pre-Ampere fp16 fallback
    • no CUDA device
    • an integrated GPU
      off and 0 are accepted for every family. Only the request field is checked here, not the env var.
  • family_train_infos reports supports_block_swap, which is true for DiT families that list bf16 and are not on the fp16 fallback.
  • The DiT trainer emits a block_swap event right after the swap is installed. The fields are swapped_blocks, total_blocks, host_gib and pinned_gib. The training service folds it into a new block_swap field on /api/train/diffusion/status. Before this, the only trace was a warning message that the first progress event overwrote.

Frontend (Images > Train):

  • A Block swap select (Off / Auto / Fixed number of blocks, plus Blocks to swap). It shows only when all of these hold:
    • the family reports supports_block_swap
    • Base precision is bf16
    • gradient checkpointing is on
    • the host passes the same hardware rule as core LLM layer offload (offloadHardwareSupported, reused from features/training/api/mappers.ts)
  • When hidden or set to Off, nothing is sent.
  • While a run is going, the run card shows the resolved outcome under the stats, e.g. "Block swap: 21 of 57 blocks from host RAM (8.1 GiB pinned)".

Tests

  • studio/backend/tests/test_diffusion_block_swap_api.py (18, CPU) covers:
    • request parsing
    • forwarding of auto and a count through the real route to the service, and that the forwarded value parses back in the trainer
    • unset stays unset
    • every refusal returns 400 without freeing the GPU
    • off is accepted for SDXL
    • the env var is not judged at the route
    • the family flag
    • the status fold
    • the trainer emitting the event
      test_differential.py against b57c06f: DISCRIMINATING, 3 FIXED + 15 NEW_API, head_fail 0.
  • Existing suites (test_diffusion_training.py, test_diffusion_block_swap.py, test_diffusion_base_precision.py, test_diffusion_dit_trainer.py, checkpoint resume, samples, LTX-2): all pass.
  • studio/frontend/tests/diffusion-train-block-swap.test.ts covers when the control shows, the payload, the status line and the panel wiring. The full npm test passes (16915 tests, 0 failures), and so does npm run build.

GPU check (Colab L4, 22.5 GiB, standing in for a 24 GB card)

Wan2.2-TI2V-5B, bf16 base, 512 px, rank 16, 50 steps, seed 0. Each arm ran in a fresh process, interleaved off / N / auto / N / off, plus one more repeat of each. The request went through DiffusionTrainingStartRequest and the route preflight, using the host's real GPU facts. The status came from the service's event fold.

arm (request block_swap) runs steady s/step, median [range of run medians] peak alloc GiB loop host RSS GiB status block_swap mean abs dloss vs off_1
unset / off 3 0.170 [0.169-0.172] 11.39 5.5-5.6 none A/A 0.00031-0.00032
auto 2 0.170 [0.168-0.172] 11.39 5.6 0 of 30 blocks 0.00015-0.00024
10 3 0.519 [0.519-0.519] 7.89 4.9 10 of 30, 3.05 GiB pinned 0.00023-0.00034

Notes on the results:

  • At this size Wan 5B fits on the L4. So auto swaps nothing and runs at the same speed as off, and the status says so.
  • A forced N=10 frees 3.5 GiB of peak allocation and costs about 3x per step. At 512 px the step is copy-bound: about 12 GB/s over PCIe against roughly 0.17 s of compute.
  • The losses match off within the off-vs-off spread.
  • Graph breaks were 0 in every arm, with 2 unique graphs.

Resume with the same block_swap=10: one process stopped and saved at step 25 (checkpoint-25). A new process resumed from that output dir to 50. The resumed run reported resumed at step 25, picked up the same swap (10 of 30 blocks), and trained steps 26 to 50. Its loss over steps 26 to 50 differs from the three uninterrupted 10 runs by a mean absolute 0.00028-0.00046 per step. The uninterrupted runs differ from each other by 0.00041-0.00055 on the same steps, so the resumed curve is inside run-to-run noise. Mean loss over steps 26 to 50: 0.2375 for the resumed run, 0.2375 for the uninterrupted one.

Wan renders no previews (video families refuse sample_every), so the preview leg used FLUX.2-klein-base-4B with bf16 and block_swap=8. Status showed 8 of 25 blocks with 2.29 GiB pinned, at 0.83 s/step with a 6.6 GiB peak. The samples rendered at steps 0, 10 and 20 while the swap was on.

Not proven here

  • Only an L4 was tested: no 3090/4090/5090 and no PCIe Gen3 host. No model that needs the swap on a 24 GB card was run end to end through the API in this PR. FLUX.1-dev and FLUX.2-klein-9B were measured in Studio diffusion training: DiT block swap, used by auto where int8 was the only fit #18604 through the trainer config.
  • ROCm discrete GPUs: the route and the panel allow an explicit swap there, as the trainer does, but it is unmeasured. The integrated-GPU refusal is CPU-tested only.
  • The UI shots were taken on a CPU host, with /api/system and the family listing rewritten in the browser to a 24 GB CUDA card. The rewrite was the same on both sides.

UI

Train tab, FLUX.1 with a bf16 base. Before (main) and after (this branch): the Block swap select and, for a fixed count, Blocks to swap.

Train settings before and after

The running job's status line after the base loads (the job is faked with the FLUX.1-dev numbers from #18604: 21 of 57 blocks, 8.1 GiB pinned).

Training status before and after

Adds an optional block_swap field (off, auto or a block count) to the
diffusion training start request. The start route refuses it before
freeing GPU residents for families without the DiT trainer, non-bf16
bases, runs without gradient checkpointing, the fp16 fallback and hosts
without a discrete CUDA or ROCm GPU. Unset keeps today's behaviour.

Families report supports_block_swap, the Train tab shows a Block swap
control for them on bf16, and the trainer reports what the swap resolved
to (blocks swapped, pinned GiB) in the training status.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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-20T14:34:50.795723Z 1acdee0 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ddc28aeba7

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

Comment on lines +1127 to +1128
"'auto' swaps as few as fit, a number swaps that many, 'off' or 0 none. Needs gradient "
"checkpointing and a discrete CUDA / ROCm GPU. null keeps the server default (UNSLOTH_DIT_BLOCK_SWAP)."

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor explicit off values instead of inheriting the environment

When the server has UNSLOTH_DIT_BLOCK_SWAP=auto or a positive count, requesting block_swap="off" or 0 does not disable swapping as this API contract promises. The route forwards these as strings, but block_swap_request() treats both as reasons to fall back to the environment, while block_swap_start_error() skips validation for them. Consequently, an explicitly disabled request can still incur block-swapping overhead or fail in the trainer when using nf4 or disabled gradient checkpointing. Preserve the distinction between an omitted value (inherit the server default) and an explicit off value through the trainer's request resolution.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8998c0b and 529a3cf7b. The config default is now "" (unset), which still takes UNSLOTH_DIT_BLOCK_SWAP, while an explicit "off" or 0 is off even when the env asks for a swap. An explicit off also skips auto's int8-band bf16 + swap, so a request that says off never swaps. The API keeps omitting the field when it is null, so an omitted block_swap still inherits the server default. Tests cover "off", "0" and 0 against UNSLOTH_DIT_BLOCK_SWAP=auto through _config_from_dict (the start route's path), and the unset cases.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 20, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 20, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 1acdee07e4

ℹ️ 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 f304637 into main Nov 20, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-block-swap-ui branch November 20, 2026 15:29
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