Skip to content

Studio video: pre-seed the denoiser compile's float specialisations on a returning load - #15406

Merged
danielhanchen merged 7 commits into
mainfrom
studio-video-compile-float-seeds
Oct 27, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
studio-video-compile-float-seeds

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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=True regional
compile. 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 a
returning 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:

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

…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
@danielhanchen danielhanchen self-assigned this Oct 26, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T00:08:18.915154Z 5a6cde9 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

2 similar comments
@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. Hooray!

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

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 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".

Comment on lines 12285 to +12288
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()

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 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 👍 / 👎.

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

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 5a6cde9f51

ℹ️ 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 e417bfd into main Oct 27, 2026
33 of 43 checks passed
@danielhanchen
danielhanchen deleted the studio-video-compile-float-seeds branch October 27, 2026 01:11
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.
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