Repository navigation
Studio: pre-seed the float specialisations of an image load's first compiled render - #16917
Merged
Merged
Conversation
…ompiled render A load that compiles inside its first render (no background compile, e.g. FLUX.2-klein on an A100) threw away a whole Dynamo attempt per block type on every start: dynamic=True turns the blocks' eps / dropout floats into SymFloats, tensorify fails and Dynamo retraces. The background compile and video's first clip already keep the learned names in the compile-cache key dir; the image inline path now seeds from and writes to the same file.
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. Already looking forward to the next diff. 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". |
danielhanchen
added a commit
to danielhanchen/unsloth-staging-2
that referenced
this pull request
Nov 7, 2026
…t compiled render traces once (unslothai#16939) ## Summary unslothai#16917 pre-seeds the Dynamo float specialisation names (norm `eps`, dropout `p`, ...) that an image load's first compiled render learned, so a returning start traces each block type once. A first-ever start has no saved file yet, so it still paid the full restart: the default tier compiles the repeated blocks with `dynamic=True`, the first attempt fails on the float values (`TensorifyScalarRestartAnalysis`) and Dynamo traces every block type again. This PR ships those names per known denoiser, so a fresh install starts from the same seeds a returning user already has: - `_FLOAT_SEED_DEFAULTS` in `diffusion_bg_compile.py`: the names a real first compile learns, for FLUX.2-klein 4B / 9B (and base), FLUX.2-dev, FLUX.1-dev / schnell / Krea / Kontext, Qwen-Image / 2512, Z-Image / Turbo, Wan2.1-T2V-1.3B, Wan2.2-T2V-A14B and Wan2.2-TI2V-5B. - Used only when the seed file of that compile-cache key has no names for this code; saved names always win. After the first compile the built-in names are written to the seed file like learned ones, so later loads take the existing returning path. - Keyed by `_default_float_seed_key`: torch minor, diffusers version, the transformer config less its layer counts, and per repeated block class every submodule class, the source of every forward and attention processor it runs, and the module-level helpers they call by name. A Studio load patch (arch fusions, fused RoPE, int8 swaps), a LoRA, another diffusers or torch version or any other config moves the key, so nothing is seeded and the compile restarts exactly as on main. - Kill switches: the existing `UNSLOTH_DIFFUSION_BG_COMPILE_FLOAT_SEEDS=0` (all seeding), and `UNSLOTH_DIFFUSION_FLOAT_SEED_DEFAULTS=0` (only the built-in names). - The same seeding path serves the background compile and video's first clip, so those get the built-in names too. How the table was recorded: one layer of each model (real config, random weights) on an A100 through this backend's own load patches and `_compile_repeated_blocks`, under the Studio install (torch 2.13.0+cu130, diffusers 0.42.1). Dynamo numbers a SymFloat from a hash of its source name (`ShapeEnv._generate_unique_id`), not from tensor sizes, so the names do not depend on the layer count or resolution (checked: 64 vs 256 tokens, real vs shrunk widths give the same names). For the two families timed below, the recorded names equal what a real fresh start wrote to its seed file. ## Results Colab A100-SXM4-40GB, one VM per run, Studio installed with `install.sh --local` (python 3.13.16, torch 2.13.0+cu130, diffusers 0.42.1). Base = `06d5015f28` (main), head = base + this change. 1024x1024, 3 prompts after the first render, 3 reps with base and head interleaved (B H, H B, B H). Fresh = new process, empty Studio home and inductor / Triton caches; returning = later processes on that home. FLUX.2-klein-4B, this PR's code (the later commits only touch tests and comments): | first compiled render | base | head | |---|---|---| | fresh | 21.63 / 21.81 / 22.56 s (median 21.81) | 17.02 / 17.11 / 17.27 s (median 17.11) | | returning, 2nd process | 6.31 / 6.35 / 6.42 s | 6.39 / 6.39 / 6.49 s | | returning, 3rd process | 6.34 / 6.38 / 6.39 s | 6.39 / 6.43 / 6.44 s | Z-Image-Turbo, an earlier revision with the same table and key (it differed only in not yet writing the built-in names to the seed file, so its returning runs seeded from the table again rather than the file; the +0.37 s median there is inside both arms' 0.50 / 0.56 s spread): | first compiled render | base | head | |---|---|---| | fresh | 22.76 / 22.88 / 23.34 s (median 22.88) | 19.72 / 20.16 / 20.64 s (median 20.16) | | returning | 8.19 / 8.62 / 8.70 s | 8.61 / 8.99 / 9.18 s | - Fresh first render: klein -4.70 s (-22%), Z-Image -2.72 s (-12%), each well outside both arms' spread (klein 0.93 / 0.25 s, Z-Image 0.58 / 0.92 s). The same klein comparison on the earlier revision gave 22.21 to 17.18 s. - Dynamo `_compile.compile_inner` on a fresh klein start: base 10.24 + 5.07 s with a `compile_attempt_1` per block type, head 7.01 + 3.48 s with none. The head logs `diffusion.float_seeds: 11 built-in float specialisation(s) pre-seeded for the Flux2Transformer2DModel compile` (7 for `ZImageTransformer2DModel`); its returning runs then read the same 11 names from the seed file, like base. - Returning first render: within 0.05 s of base at medians, inside the spread of each arm. Steady renders unchanged (klein 0.71-0.77 s, Z-Image 2.20-2.36 s on both arms). - The names base's fresh starts wrote to their seed files are exactly the shipped ones for both models. ## Same images, same graphs - Every PNG md5 is identical across all cells of a model, base and head, fresh and returning, per prompt: klein 57 PNGs over 19 cells and 3 distinct (one per prompt); Z-Image 39 PNGs over 13 cells and 3 distinct. - A head start in a base fresh home (`head_inbase`) hits both AOTAutograd entries the unseeded base compile wrote, 0 misses, so the seeded first trace produces the same graphs as main's restarted one. - 0 graph breaks (blocks compile with `fullgraph=True`). Recompiles: 0 for klein; 1 for Z-Image on both arms alike (a pre-existing guard on `adaln_input` being `None`). - Per family on CUDA (one layer, random weights, the same load patches): with the built-in names every listed model traces with 0 restarts. - Cost on a miss: computing the key takes about 5 ms once per load, only when the seed file has no names for this code. ## Tests - `tests/test_diffusion_float_seed_defaults.py` (new, CPU, `aot_eager`): a fresh compile with a matching key traces once into the same output as main's restarted compile and writes the names to the seed file; the next load reads them from the file; saved names win over the table; both kill switches keep the restart; a name no float carries is never consulted and a missing one is still learned by the restart, output unchanged; the key ignores layer counts but moves with the config, a swapped submodule, an instance forward, a class forward patch, a swapped helper function, and is absent for a LoRA-adapted or config-less module; the table is well formed. - With the one functional line reverted (no built-in lookup), the fresh-compile and stale-name tests fail; the other new tests are negative controls and pass on both. - `test_diffusion_inline_float_seeds.py`, `test_video_float_seeds.py`, `test_diffusion_bg_compile_float_seeds.py`, `test_diffusion_backend.py`, `test_video_backend.py`: 1249 passed, 1 skipped (torch 2.12.1). The four seed test files also pass on torch 2.10.0, 2.13.0 and 2.14.1. ## Not proven - Timed on FLUX.2-klein-4B and Z-Image-Turbo with real weights only. FLUX.1, FLUX.2-dev, klein-9B, Qwen-Image and Wan were checked with one random-weight layer through the same load patches and compile (0 restarts, the learned names equal the table), not with a full Studio render. - The Wan entries assume the pipelines' timestep layout (per-token timesteps for TI2V-5B, as its `expand_timesteps` flag sets); the video backend's own first-clip path was not timed. - Another speed tier on the same model (for example an int8 or fp8 transformer) swaps block modules, so its key differs and it keeps main's restart. - ROCm, Windows and macOS were not run. The change is host-side Python around the compile; on a host where the key does not match, nothing is seeded.
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
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:
dynamic=True, so every Python float a block reads becomes a SymFloat: the normeps, dropoutp, the attention dispatch default.TensorifyScalarRestartAnalysis, and Dynamo retraces each block type with the floats specialised (compile_attempt_0andcompile_attempt_1for both FLUX.2 block types).The background compile (#15393) and video's first clip (#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
InlineFloatSeedsinto the imagegenerate:bg_compile_float_specializations.jsonin the compile-cache context dir), stamped with the torch version and the block forward bytecode, so a mismatch falls back to the current restart;compiledin the speed optims, a compile-cache context, and a DiT compiled withdynamic=True; a deferred load is asked again once it compiles;float_seed_ditsandseed_before_first_forwardmoved fromvideo.pyintodiffusion_bg_compile.pyso both backends share them (video keeps the old names as aliases);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._compile.compile_inner, returningReturning 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
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:generateseeds 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.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
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.dynamic=NoneorFalse(U-Net, torchao stream-merging DiTs) do not restart and are untouched; not timed here.