Repository navigation
Studio diffusion: pre-seed the background compile's float specialisations on a returning load - #15393
Merged
Merged
Conversation
…ions on a returning load A dynamic=True block compile wraps every float a block reads (norm eps, dropout p) as a SymFloat; dynamo specialises the ones it cannot tensorify only after a failed first attempt, so every block type is traced twice. Keep the names it specialised beside the load's compile cache and pre-seed them on the next background compile of the same key, which then traces once into the same graph (FX graph cache hits on the graph main compiled). Kill switch UNSLOTH_DIFFUSION_BG_COMPILE_FLOAT_SEEDS=0.
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. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! 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". |
oobabooga
pushed a commit
to oobabooga/unsloth
that referenced
this pull request
Oct 27, 2026
…n a returning load (unslothai#15406) ## Summary Follow-up to unslothai#15393, against main. Studio compiles Wan2.1, Wan2.2-TI2V-5B, HunyuanVideo-1.5 and dense MiniMax-H3 with a `dynamic=True` regional compile. Like the image compiles in unslothai#15393, each block type is traced twice. dynamo wraps the floats a block reads (norm eps, dropout p) as SymFloats, and the tensorify pass specialises them only after a failed first attempt (`Restarting analysis due to fx/passes/_tensorify_python_scalars.py`). This happens on every load, including a returning one whose FX cache hits. There it is most of the compile. This PR applies unslothai#15393's float seeds to video's first-clip compile: - seed at the denoise entry and keep at the decoder entry; - the same file format, signature and kill switch (`UNSLOTH_DIFFUSION_BG_COMPILE_FLOAT_SEEDS=0`); - a per-key dir under the compile-cache root (`video_float_seeds/<key>`, keyed like the compile cache), since video loads take no compile-cache ctx. The opt-in video background compile (`UNSLOTH_DIFFUSION_BG_COMPILE=1`) gets the same seed dir. Static compiles never restart, so they are left alone: LTX-2 / LTX-2.5 (`compile_static`) and torchao-quantised loads (automatic dynamic). ## Results Colab L4, torch 2.14.1. Each number is the first compiled forward through Studio's `_compile_repeated_blocks` on the real transformer config with 2 blocks, at a first-clip latent shape. Returning load, 3 runs per arm, base and head interleaved: | family | base s | head s | delta | base A/A spread | |---|---|---|---|---| | Wan2.1-T2V-1.3B | 7.22 | 5.17 | -2.06 s | 0.06 s | | Wan2.2-TI2V-5B | 7.92 | 5.70 | -2.22 s | 0.07 s | | HunyuanVideo-1.5 480p | 14.69 | 9.70 | -4.99 s | 0.21 s | | MiniMax-H3 (dense) | 7.89 | 6.11 | -1.78 s | 0.04 s | - Fresh loads are unchanged. Seeds exist only after one compile of that key. - Later forwards are unchanged. There are 0 graph breaks, 0 recompiles, and no new graph after the first forward. - Outputs are bit-identical across all arms on L4. - A seeded head compile in a home whose caches base wrote hits every FX and AOT entry with 0 misses (HV15: 3 of 3 graphs). The FX key is a hash of the graph, so the seeded graph is main's graph. - Through `VideoBackend.generate` on tiny Wan2.1 / Wan2.2 pipes, frames match base on every render. Seeded loads have 0 restarts, and the kill switch brings the restart back. ## Changes - `diffusion_bg_compile.py`: - The seed / keep logic is factored out of `BackgroundCompile`, with no behaviour change. - `_modules_signature` signs exactly like `_code_signature` for one module and in order for a dual-DiT pipe. - New `InlineFloatSeeds`: seeds once, keeps once, never raises. - `video.py`: - `_float_seed_dits`, `_video_float_seed_dir` and `VideoBackend._inline_float_seeds`. - `generate` seeds in `_enter_denoise` and keeps in `_on_decode`. Keeping at the decoder entry means a later VAE decode compile never adds names to the denoiser's file. - The background compile `arm` gets `seed_dir`. ## Tests - New `tests/test_video_float_seeds.py` (9 tests) and a `generate` wiring test in `test_video_backend.py`. All 10 fail on the base and pass here. - `test_video_backend`, `test_video_static_skip`, `test_video_cosmos3` and the bg-compile suites, with `-n 4`: head 756 passed, main 746 passed. Both trees have the same 3 failures, which are pre-existing on main: `test_video_cosmos3` listing tests. - Through `VideoBackend.generate` on tiny Wan2.1 / Wan2.2 pipes with main as the base, frames are identical on every render across main fresh, main returning, head learning and head seeded. ## Not covered - No real-weight, end-to-end clip timing. The timing covers the denoiser compile only. - No CUDA-graph capture count on a real model. The tiny pipes capture none in either tree. - HunyuanVideo-1.5 and MiniMax-H3 were checked through the compile probes, not through `generate`. - Wan2.2-T2V-A14B (dual DiT) is armed but was not run.
Stanley00
pushed a commit
to stanley-fork/unsloth
that referenced
this pull request
Nov 6, 2026
…ompiled render (unslothai#16917) ## Summary When an image model compiles inside its first render (the default speed tier on a card where the denoiser fits, for example FLUX.2-klein-4B on an A100), every Studio start threw away a whole Dynamo attempt per block type before compiling: - The default tier compiles the repeated blocks with `dynamic=True`, so every Python float a block reads becomes a SymFloat: the norm `eps`, dropout `p`, the attention dispatch default. - Those ops guard the float to a constant, the tensorify pass raises `TensorifyScalarRestartAnalysis`, and Dynamo retraces each block type with the floats specialised (`compile_attempt_0` and `compile_attempt_1` for both FLUX.2 block types). - On a returning start (warm compile cache) the first attempt also misses the AOTAutograd cache, since its graph still has the float inputs. It cost 5.7 s of the 8.3 s compile on FLUX.2-klein-4B. The background compile (unslothai#15393) and video's first clip (unslothai#15406) already keep the SymFloat names a compile specialised in the compile-cache key dir and pre-seed them on the next load. The image path that compiles inline in the render never did. This PR wires the same `InlineFloatSeeds` into the image `generate`: - seeded at the denoise entry (and at the first DiT forward, for a loop without a progress bar), kept at the decode entry, so only the denoiser's names are saved; - same file and key dir as the background compile (`bg_compile_float_specializations.json` in the compile-cache context dir), stamped with the torch version and the block forward bytecode, so a mismatch falls back to the current restart; - only for loads with no background compile, `compiled` in the speed optims, a compile-cache context, and a DiT compiled with `dynamic=True`; a deferred load is asked again once it compiles; - `float_seed_dits` and `seed_before_first_forward` moved from `video.py` into `diffusion_bg_compile.py` so both backends share them (video keeps the old names as aliases); - kill switch: the existing `UNSLOTH_DIFFUSION_BG_COMPILE_FLOAT_SEEDS=0`. A first-ever start (empty cache) still learns the names through the restart, as on main. ## Results Colab A100-SXM4-40GB, one VM, torch 2.13.0+cu130, diffusers 0.42.1, Studio installed with `install.sh --local`. FLUX.2-klein-4B, 1024x1024, 4 steps, 3 prompts after the first render. Base = `ccc8a827d2`, head = base + this change. 3 reps, base and head interleaved (B H, H B, B H). Fresh = new process, empty Studio home and inductor / Triton caches; returning = a second and third process on that home. | | base | head | |---|---|---| | first render, returning (6 processes each) | 12.01 / 12.07 / 12.08 / 12.09 / 12.11 / 12.32 s | 7.77 / 7.80 / 7.83 / 7.84 / 7.86 / 7.88 s | | first render, fresh home | 23.27 / 23.50 / 23.96 s | 22.93 / 23.14 / 23.23 s (unchanged by design) | | Dynamo `_compile.compile_inner`, returning | 5.66 + 2.59 s (2 attempts per frame) | 2.55 + 1.09 s (1 attempt) | | AOTAutograd cache, returning | 2 miss + 2 hit | 2 hit, 0 miss | | steady renders | 0.716-0.778 s | 0.712-0.770 s | | process wall, returning | 34.2-34.6 s | 29.8-30.1 s | Returning first render: -4.2 s (-35%), median 12.09 s to 7.84 s, outside the A/A spread of either arm (0.31 s base, 0.11 s head). ## Same images, same graphs - Every PNG md5 is identical across all 19 cells (base and head, fresh and returning), per prompt. - A seeded head start hits the AOTAutograd entries written by the unseeded fresh compile with 0 misses. The AOT cache key hashes the graph, so the seeded trace produces the same graph as the restarted one. - Graph count stays 2 (`unique_graphs`), 0 recompiles, 0 graph breaks on every cell. ## Tests - `tests/test_diffusion_inline_float_seeds.py` (new, CPU, `aot_eager`): a returning load traces its first render once into the same output (0 restarts, `torch.equal`), the file is the background compile's, one seeder per load until kept, no seeder for a background compile / not compiled / static compile / no cache context / kill switch, and a deferred load is asked again once it compiles. - `tests/test_diffusion_backend.py`: `generate` seeds before the first denoiser forward and keeps before the decode, seeds at the first DiT forward without a progress bar, and an auxiliary pass takes no seeds. - 13 new tests fail on main and pass here. `test_video_float_seeds.py`, `test_video_backend.py`, `test_diffusion_bg_compile_float_seeds.py`, `test_diffusion_backend.py`: 1240 passed, 1 skipped. ## Not proven - Only FLUX.2-klein-4B on an A100 was timed. Other families compiled inline with `dynamic=True` (FLUX.1, Qwen-Image where it fits) take the same path; the background compile PR measured FLUX.1 and Qwen-Image-2.1 with the same seeding, not this inline path. - Loads that compile with `dynamic=None` or `False` (U-Net, torchao stream-merging DiTs) do not restart and are untouched; not timed here. - ROCm, Windows and macOS were not run. The change is host-side Python around the compile and does not touch device code.
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.
Summary
On the deferred tier, Studio compiles an offloaded image denoiser in the background after image 3, and image 4 waits for that compile. A returning user (warm compile cache) still waited 11 s on FLUX.1-schnell bf16 and 17 s on Qwen-Image-2.1 bf16 on an L4.
Most of that returning wait is a thrown-away first dynamo attempt:
dynamic=True, so every float a block reads becomes a SymFloat: normeps, dropoutp, the attention dispatch default.TensorifyScalarRestartAnalysis, and dynamo retraces every block type with those floats specialised.This PR keeps the SymFloat names a background compile specialised in the load's compile-cache key dir (
bg_compile_float_specializations.json). The next background compile of the same key pre-seeds them, so dynamo takes main's second-attempt path directly and traces once into the same graph.UNSLOTH_DIFFUSION_BG_COMPILE_FLOAT_SEEDS=0.Results (Colab L4, one VM, base and head interleaved,
UNSLOTH_IS_PRESENT=1)Steady renders are unchanged: FLUX 7.0-7.7 s, Qwen-Image-2.1 34.1-35.7 s.
Same images, same graphs
Not covered
Tests
tests/test_diffusion_bg_compile_float_seeds.py(CPU,aot_eager) covers:It fails on main and passes here. The existing suites touching this path pass unchanged.