Skip to content

Studio: start a first video load's cache writes after its first render - #17001

Merged
danielhanchen merged 4 commits into
mainfrom
studio-video-cache-writes-after-render
Nov 7, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-video-cache-writes-after-render

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

A first video load in a Studio home misses the cast cache, the native int8 cache and the resident int8 cache, so it queues their writes in the background. Those writes were started the moment the load was published, which put them right on top of the first render: its compile, its denoise and the writes all competed for the same CPU, disk and host RAM.

This PR holds those writes until the first render ends (success, failure or cancel). If no render starts, they begin once the backend has sat idle for UNSLOTH_VIDEO_CACHE_WRITE_IDLE_S seconds (120 by default) after the load. An unload before then discards them, which matches what an unload already did to unfinished writes; the next load in that home writes them instead. UNSLOTH_VIDEO_CACHE_WRITES_AFTER_RENDER=0 brings back the old start-at-load behaviour.

Pixels are unchanged: the writes only copy weights that already exist.

Measurements

Colab L4 (53 GB host), Wan2.2-TI2V-5B, 8 GiB VRAM budget (ballast), 1280x704, 41 frames, 10 steps. Every arm runs in a fresh process with a fresh Studio home, after the page cache is evicted. Arm order was base, head, head, base, base, head. Base is main 3d41bf4af3, and head is this branch on top of it.

fresh home (3 each) base head
load (s) 98.2, 98.6, 97.8 (median 98.2) 102.5, 97.4, 99.1 (median 99.1)
first clip (s) 130.1, 126.9, 129.3 (median 129.3) 122.5, 124.6, 124.0 (median 124.0)
first clip denoise (s) 83.0, 80.4, 81.9 77.3, 77.6, 77.8
native int8 cache write (s) 24.1, 24.7, 25.4 (beside the render) 18.3, 18.6, 18.4 (after it)
steady clip (s) 65.1, 65.5, 64.7 64.7, 64.9, 65.3
peak VRAM (GiB, nvidia-smi) 22.15 22.15
process tree peak RSS (GiB) 29.2, 31.1, 31.1 29.3, 31.1, 31.1

The returning load, in the home of the first fresh arm, shows that the held writes still land and get read back: base loaded in 31.0 s and head in 23.3 s (first clip 96.0 s vs 92.1 s).

Frames: 4 of the 5 base/head pairs are bit-identical on every frame (fresh pairs 1 and 3, the returning pair, and Wan2.1-1.3B resident). Pair 2 differs, but base pair 1 vs base pair 2 differs in exactly the same way (PSNR 28.98, LPIPS 0.043). So this is run-to-run variation that already exists on main, and head pair 2 matches base pair 1 bit for bit.

What this does not show: in the profiled survey run on an earlier L4 VM, the first denoise took 270 s while the native write ran beside it. That pathology did not reproduce on this VM without the profiler, so the measured gain here is about 5 s (4%) on the first clip. The fresh-load RSS spike seen on a 31 GB Kaggle host was not re-measured, since this host has 53 GB.

Tests

  • tests/test_video_cache_writes_after_render.py (new): writes are held until a render ends and start once; a busy backend waits for the render; an idle one starts them after the window; an unload before that discards them and none ever start; the env switches; generate releases them in its finally, and the unload discards them before the finish calls.
  • tests/test_video_native_cache_background.py and tests/test_video_resident_quant_cache.py are updated for the new starter. Both files pass, as does tests/test_diffusion_cast_cache.py.

A first load in a Studio home writes the cast cache, the native int8 build and the resident int8
build in the background as soon as it is published, so they ran beside the first render: on an
L4 the Wan2.2-5B native write (4.7 GiB, 227 s) slowed the first clip's compile and denoise to
270 s. The writes are now held until the first render ends (success, failure or cancel), or
until the backend sits idle for UNSLOTH_VIDEO_CACHE_WRITE_IDLE_S (120 s) after the load. An
unload before then discards them, as an unload already cancelled unfinished writes.
UNSLOTH_VIDEO_CACHE_WRITES_AFTER_RENDER=0 restores the old start.
@danielhanchen danielhanchen self-assigned this Nov 7, 2026
@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-07T09:50:25.475019Z 8ce7a10 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

@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: 7ce1e421b5

ℹ️ 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 thread studio/backend/core/inference/video.py Outdated
Comment on lines +3489 to +3493
writes = self._take()
pipe = self._pipe()
if writes is None or pipe is None:
return
logger.info("diffusion.prequant_cache: first-load cache writes start now (%s)", why)

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 Synchronize idle release with pipeline teardown

If the idle timer fires concurrently with unload, release() can take the writes and pause before _start_cache_writes() attaches them to the pipeline. Teardown then finds nothing in held.discard() or any of the three _finish_* helpers and returns, after which the timer starts the writers against the unloaded pipeline. This allows weight reads and substantial host-memory retention to outlive unload and overlap the next model load. The holder lock currently protects only ownership transfer; synchronize startup/registration with discard so teardown either cancels the pending writes or observes and finishes every started writer.

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 8ce7a10: release now takes, starts and registers the writers under the holder lock, and discard takes the same lock, so an unload either cancels them first or waits until the finish calls can see them. Added a race test that fails without the fix.

An idle-timer release could take the writes and start them after a concurrent unload had
already looked for them, so the writers outlived the pipeline. The release now starts and
registers them under the same lock the unload's discard takes: the discard either cancels
them first or waits until the finish calls can see every started writer.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 8ce7a101ae

ℹ️ 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 482514b into main Nov 7, 2026
@danielhanchen
danielhanchen deleted the studio-video-cache-writes-after-render branch November 7, 2026 12:10
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