Skip to content

Studio: ship built-in float specialisations so a fresh install's first compiled render traces once - #16939

Merged
danielhanchen merged 6 commits into
mainfrom
studio-image-float-seed-defaults
Nov 7, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-image-float-seed-defaults

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Summary

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

@danielhanchen danielhanchen self-assigned this Nov 6, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 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-07T07:53:02.111857Z ef7cd77 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: ef7cd77915

ℹ️ 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 e29419a into main Nov 7, 2026
@danielhanchen
danielhanchen deleted the studio-image-float-seed-defaults branch November 7, 2026 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant