Repository navigation
Studio video: pre-seed the denoiser compile's float specialisations on a returning load - #15406
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.
… returning load A dynamic=True regional compile of Wan2.1 / Wan2.2-TI2V-5B / HunyuanVideo-1.5 / dense MiniMax-H3 restarts dynamo once per block type when the tensorify pass specialises the norm eps and dropout floats. Reuse the background compile's float-seed file, signature and kill switch (UNSLOTH_DIFFUSION_BG_COMPILE_FLOAT_SEEDS=0) for video's inline first-clip compile: seed at the denoise entry, keep at the decode entry, in a per-key dir under the compile-cache root. Static (LTX-2 / 2.5) and torchao (automatic dynamic) compiles never restart and are left alone. The opt-in video background compile gets the same seed dir.
# Conflicts: # studio/backend/core/inference/diffusion_bg_compile.py
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 |
|
Codex Review: Didn't find any major issues. Hooray! 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". |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 30ce2f1ee1
ℹ️ 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".
| def _enter_denoise() -> None: | ||
| if float_seeds is not None: | ||
| # Before the first denoiser forward, so the compile it triggers takes the seeded path. | ||
| float_seeds.seed() |
There was a problem hiding this comment.
Seed modular H3 before its first transformer forward
For dense MiniMax-H3, this callback runs too late to seed or learn the first compile's specialisations. The H3 loader constructs a diffusers.ModularPipeline, but its progress bar belongs to MiniMaxH3DenoiseLoopWrapper, not the pipeline, so _denoise_phase(pipe, _enter_denoise) does not intercept it. The first invocation instead comes through _scheduler_step_progress → _tick, after the transformer has already compiled. Consequently, returning loads still pay the restart, and fresh loads snapshot the learned names into before, preventing _keep_floats from saving them. Hook the modular denoise-loop entry or the first transformer invocation so seeding happens before compilation.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 5a6cde9. The seeder now also installs a one-shot forward pre-hook on the dynamic DiTs for the render, so the first transformer forward seeds before its compile even when the pipe's own progress bar is never opened (H3 modular's bar belongs to MiniMaxH3DenoiseLoopWrapper). seed() is idempotent, so pipelines whose progress bar fires first are unchanged, and the hooks are removed when the render ends. Added test_first_forward_hook_seeds_before_the_compile (real dynamo, 0 restarts on the second load through the hook alone) and test_first_clip_seeds_floats_at_the_first_dit_forward_without_a_pipe_progress_bar; both fail without the change.
…rogress bar is its loop wrapper's)
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 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". |
…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.
Summary
Follow-up to #15393, against main.
Studio compiles Wan2.1, Wan2.2-TI2V-5B, HunyuanVideo-1.5 and dense MiniMax-H3 with a
dynamic=Trueregionalcompile. Like the image compiles in #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 areturning one whose FX cache hits. There it is most of the compile.
This PR applies #15393's float seeds to video's first-clip compile:
UNSLOTH_DIFFUSION_BG_COMPILE_FLOAT_SEEDS=0);video_float_seeds/<key>, keyed like the compile cache), sincevideo 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-quantisedloads (automatic dynamic).
Results
Colab L4, torch 2.14.1. Each number is the first compiled forward through Studio's
_compile_repeated_blocksonthe real transformer config with 2 blocks, at a first-clip latent shape. Returning load, 3 runs per arm, base and
head interleaved:
graphs). The FX key is a hash of the graph, so the seeded graph is main's graph.
VideoBackend.generateon tiny Wan2.1 / Wan2.2 pipes, frames match base on every render. Seeded loadshave 0 restarts, and the kill switch brings the restart back.
Changes
diffusion_bg_compile.py:BackgroundCompile, with no behaviour change._modules_signaturesigns exactly like_code_signaturefor one module and in order for a dual-DiT pipe.InlineFloatSeeds: seeds once, keeps once, never raises.video.py:_float_seed_dits,_video_float_seed_dirandVideoBackend._inline_float_seeds.generateseeds in_enter_denoiseand keeps in_on_decode. Keeping at the decoder entry means a later VAEdecode compile never adds names to the denoiser's file.
armgetsseed_dir.Tests
tests/test_video_float_seeds.py(9 tests) and ageneratewiring test intest_video_backend.py. All 10fail on the base and pass here.
test_video_backend,test_video_static_skip,test_video_cosmos3and 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_cosmos3listing tests.VideoBackend.generateon tiny Wan2.1 / Wan2.2 pipes with main as the base, frames are identical onevery render across main fresh, main returning, head learning and head seeded.
Not covered
generate.