Repository navigation
Studio: start a first video load's cache writes after its first render - #17001
Conversation
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.
Codex Review Summary: ✅ CompletedThis 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. |
|
@codex review |
There was a problem hiding this comment.
💡 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".
| 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. 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". |
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_Sseconds (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=0brings 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.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;generatereleases them in itsfinally, and the unload discards them before the finish calls.tests/test_video_native_cache_background.pyandtests/test_video_resident_quant_cache.pyare updated for the new starter. Both files pass, as doestests/test_diffusion_cast_cache.py.