Skip to content

Studio diffusion: faster returning first render and first clip (reuse tuned reductions, warm video compile keys) - #16981

Merged
danielhanchen merged 7 commits into
mainfrom
studio-diffusion-autotune-key
Nov 7, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
studio-diffusion-autotune-key

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Summary

Two one-time costs that every Studio restart paid again in its first compiled render or clip:

  1. Image: tuned Triton reductions were re-benchmarked on every returning start. On SDXL on an L4 that was about 40 kernels and 9.5 s of a 22 s first render.
  2. Video: the compile workers and keys were never warmed ahead of the first clip. The image path has done this since its first render started compiling inline; on a returning LTX-2.5 first clip the Triton install hash alone (1.4 to 1.5 s on an L4) ran on the render thread.

1. Autotune results keyed as the compile workers key them

  • Inductor saves a kernel's tuned config as a .best_config file whose name hashes the kernel file name and torch.compiler.config.cache_key_tag.
  • The compile workers are fresh interpreters, so their tag is torch's default (the TORCH_COMPILE_CACHE_KEY_TAG env var, else empty). The Studio process carries the tags Unsloth adds to that key (on these runs, Unsloth Zoo's unsloth/inductor-rand4x-off, logged by the new info line).
  • A kernel compiled in a worker is checked and saved there under the worker's key. A later process loads the same kernel from the FX graph cache in the Studio process, looks under the Studio process's key, misses, and benchmarks again. It never finds what it saved, so this repeated on every start. torch main still passes only the cache dirs and libdevice path to the workers, not the tag.

diffusion_aot_cache.pin_autotune_key replaces AutotuneCache._prepare_key with the same formula under the workers' tag:

  • The kernel file name is already a hash of the kernel source, so the tag adds nothing to this key. FX graph and AOTAutograd cache keys keep the process's tag; nothing about the compile changes.
  • Probed, not version-gated: the formula has been the same from torch 2.6 to main; if _prepare_key produces anything else, or a future torch hands its tag to the workers, nothing is patched.
  • Installed next to the other cross-process cache fixes in _compile_repeated_blocks (SDPA AOT cache, libdevice pin) and when a compile-cache context begins, so the VAE and GGUF dequant compiles are covered too.
  • Existing caches benefit at once: the files the workers already saved are the ones now found.
  • Kill switch: UNSLOTH_DIFFUSION_AUTOTUNE_KEY_PIN=0.

2. Compiled video loads warm the workers and keys

A compiled CUDA video load now starts the same side warm-up the image path uses (inductor's compile workers, Triton's triton_key(), inductor's torch_key()), once per process, so it runs while the rest of the load finishes. The 3rd-image engagement imports are not part of it. Same kill switch as the image path: UNSLOTH_DIFFUSION_DEFERRED_PREWARM=0.

Results

Colab L4, torch 2.13.0+cu130, triton 3.7.1, diffusers 0.42.1, Studio installed with install.sh --local, base and head interleaved on one VM per table. Fresh = new process with an empty Studio home and empty inductor / Triton caches; returning = a later process on that home.

SDXL base 1.0, 1024x1024, 20 steps, CFG 5, compiled inside the first render (change 1)

Base = 42489f20e2, head = base + change 1, 3 reps (B H, H B, B H).

base head
first render, returning (6 processes each) 21.74 / 21.86 / 21.89 / 21.91 / 22.15 / 22.25 s 12.51 / 12.54 / 12.58 / 12.58 / 12.65 / 12.70 s
reduction benchmarks in a returning first render 39 to 41 0
first render, fresh home 70.29 / 71.40 / 71.78 s 69.10 / 69.42 / 69.70 s (unchanged by design)
steady renders 6.86 to 7.11 s 6.93 to 7.10 s
process wall, returning 69.7 to 71.9 s 59.9 to 61.2 s

Returning first render: median 21.90 s to 12.58 s (-9.3 s, -43%), outside the A/A spread of both arms (0.51 s base, 0.19 s head). A head process started on a base home (the caches an existing install already has) ran 1 benchmark and rendered its first image in 13.0 s.

That run measured the first revision of change 1 (same keys, computed through cache_key_tag). The final code was re-checked on a second L4 VM against 93e09c8e5a, 2 reps: returning first render 22.15 / 22.24 s base vs 12.91 / 12.64 s head, 39 vs 0 benchmarks, head on a base home 13.06 s with 1 benchmark, same image hashes as the first run.

LTX-2.5, tiny random weights (real block structure), 768x512, 49 frames, 8 steps (both changes)

Base = 93e09c8e5a, head = this branch, 3 reps interleaved. The video path's compiling thread carries no tag, so change 1 does nothing here (its returning clip already ran 0 benchmarks); the gain is change 2.

base head
first clip, warm caches (5 base, 6 head processes) 10.42 / 10.45 / 10.47 / 10.57 / 10.65 s 8.70 / 8.84 / 8.92 / 8.95 / 9.08 / 9.25 s
first clip, fresh caches (rep 3; reps 1-2 found an earlier run's caches on this VM, so they count as warm) 32.33 s 30.32 s
first clip denoise phase, returning 6.94 s 5.45 s
load 19.17 to 19.21 s 19.16 to 19.31 s
steady clips 1.41 to 1.52 s 1.42 to 1.60 s

Warm first clip: median 10.47 s to 8.94 s (-1.53 s, -15%), outside both arms' spread (0.23 s base, 0.55 s head). The head log shows triton key ready in 1.38 s during the load.

Same images

  • SDXL head: every returning render is byte-identical to the fresh render that tuned the kernels (all 3 prompts, every rep, both VMs, and the head-on-base-home cell).
  • SDXL base: the re-benchmark can pick a different reduction config, so returning renders differ from the first session's (2 distinct outputs across 6 base returning processes, neither equal to the fresh one). With this change a returning start reproduces the first session's pixels.
  • 19 graphs, 0 graph breaks, 0 new recompiles on every SDXL cell; 19 AOTAutograd cache hits on every returning cell in both arms. LTX: 2 graphs, 0 breaks, both arms.
  • Change 2 sets no compile knob on the render thread; it only runs the warm-ups earlier.

Tests

  • tests/test_diffusion_autotune_key_pin.py (new, CPU): with a tagged Studio process the key differs from a fresh worker interpreter's (the bug) and matches once pinned; the process's own cache_key_tag is unchanged; an untagged process keeps its key; TORCH_COMPILE_CACHE_KEY_TAG is honoured as the workers honour it; idempotent; kill switch; a torch without _prepare_key, with another key formula, or one that hands its tag to the workers is left alone; both entry points call it.
  • tests/test_video_compile_prewarm.py (new, CPU): the warm-up runs the pool, Triton key and inductor key on its own thread, once per process, past a failing step; kill switch; the video load starts it only for a compiled CUDA load.
  • Related suites on this branch (compile cache, speed, U-Net block compile, deferred prewarm, video backend, video float seeds, SDPA AOT cache, libdevice pin): 946 passed, 1 skipped.

Not proven

  • Timed on an L4 only: SDXL for change 1, a tiny LTX-2.5 for change 2. Other compiled image families take the same path as SDXL when their compiling thread carries a tag; their saving scales with their reduction count and was not timed. The real 22B LTX-2.5 was not run.
  • ROCm, Windows and macOS were not run. Both changes are host-side Python around the compile cache key and the warm-up thread, and touch no device code.

…a returning start

Inductor's compile workers start with torch's default cache_key_tag, while the
Studio process carries Unsloth's kernel tags, so a kernel tuned in a worker saved
its best config under one key and every later process looked it up under another.
A returning start re-benchmarked every multi-config reduction (SDXL on L4: about
40 kernels, 9.5 s of a 22 s first render). The parent now keys autotune lookups
as the workers do; UNSLOTH_DIFFUSION_AUTOTUNE_KEY_PIN=0 turns it off.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 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:18.171645Z a1825ed 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 danielhanchen self-assigned this Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

…o load finishes

The image path has done this since the first render started compiling inline;
the video path never did, so a returning LTX-2.5 first clip hashed the Triton
install (1.5 s on an L4) on the render thread. Same kill switch:
UNSLOTH_DIFFUSION_DEFERRED_PREWARM=0.
@danielhanchen danielhanchen changed the title Studio diffusion: read back the compile workers' autotune results on a returning start Studio diffusion: faster returning first render and first clip (reuse tuned reductions, warm video compile keys) Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: a1825edf33

ℹ️ 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 06766f7 into main Nov 7, 2026
@danielhanchen
danielhanchen deleted the studio-diffusion-autotune-key branch November 7, 2026 08:00
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