Skip to content

Studio video: fit per-request residency on the streaming tier and for HunyuanVideo-1.5 - #14865

Merged
danielhanchen merged 7 commits into
mainfrom
studio-video-resident-streaming
Oct 23, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
studio-video-resident-streaming

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Stacked on studio-diffusion-peak-pad-empty-slot (the empty transformer_2 slot fix); without it the Wan half of this change only ever sees the flat estimate.

video_stream_residency re-fits a block-streamed video denoiser's resident prefix per request, from free memory and the request's measured peak. It was gated to Wan on the group tier only, so two common low-VRAM placements streamed every block every step with most of the card idle:

  • Streaming tier. When the text encoder alone exceeds the card's budget (Wan's 10.8 to 11 GiB UMT5 on 8 to 12 GB cards), the plan is streaming. That tier streams the same block-level denoiser groups and leaf-streams the encoder, so the fit applies unchanged. The tier gate is now RESIDENT_POLICIES = ("group", "streaming").
  • HunyuanVideo-1.5. The fit is generic over offload groups, and HunyuanVideo-1.5 is a single-DiT family. RESIDENT_FAMILY_PREFIXES = ("wan", "hunyuanvideo-1.5").

MoE pairs, modular workflows, MiniMax-H3 (which has its own controller) and LTX-2 are unchanged. The kill switches are the existing ones: UNSLOTH_VIDEO_DIT_RESIDENT=0, UNSLOTH_VIDEO_DIT_RESIDENT_BLOCKS=0, UNSLOTH_VIDEO_DIT_RESIDENT_MEASURED=0.

Evidence

Single B200, card capped with set_per_process_memory_fraction, with mem_get_info reporting the capped total. Studio in-process, automatic memory mode, default speed tier, int8 auto quant. 832x480 (HunyuanVideo-1.5 848x480), 33 frames, 8 steps, new prompts. Each arm is a fresh process with a fresh Studio home. The host was heavily loaded (100 to 400 on 192 cores), so the base A/A spread is quoted beside each number.

model, cap resident DiT after the measured fit peak reserved s per DiT call, steady renders
HunyuanVideo-1.5 480p, 16 GiB (group tier) merge base, 2 processes 0 of 54 groups 7.6 / 9.2 GiB 0.36, 0.39 / 0.41, 0.41
this branch 29 of 54 groups (7.1 GiB) 15.0 GiB (93%) 0.198, 0.196
Wan2.1-T2V-1.3B, 8 GiB (streaming tier) merge base, 2 processes 0 of 30 4.4 / 4.3 GiB 0.113, 0.169 / 0.132, 0.127, 0.123
this branch 30 of 30 (1.4 GiB) 5.6 GiB (71%) 0.173, 0.178, 0.080
  • HunyuanVideo-1.5: per-call time roughly halves, well outside the base spread. The step graph also stays in use (replays 10 / 23 / 36 against the base's 5 / 5 / 5, where the speed check gave up on graphs because streaming dominated).
  • Wan 1.3B: the 0.080 render matches the uncapped resident figure (0.080 at 12 GiB). The two renders before it are inside this host's noise, which on that model spans 0.08 to 0.17 between processes.
  • Graph breaks: 0 everywhere. Recompiles after render 1: 0.
  • CUDA graphs: HunyuanVideo-1.5 re-records once on render 2, when the measured fit grows the resident set. It also records one more input shape on render 3. That is a new prompt shape: the base cannot show it because its graphs were already dropped.

Parity:

  • Frames are byte-identical to the merge base at every render for HunyuanVideo-1.5 (3 renders).
  • Wan 1.3B (4 renders) matches one of the two base processes byte for byte. The other base process differs from both: the cross-process nondeterminism is on main already.

Not covered: Wan2.2-TI2V-5B int8 at 8 / 12 GiB still streams everything. Its request peak never reaches the keyed table: the first render records 2011 MiB, but nothing is keyed and later renders record nothing, with its step graph armed. The flat estimate alone leaves no room (10112 + 2 - 3712 floor - 2048 - estimate <= 0). That is a separate root cause in the peak recording under a graphed streamed load, left for a follow-up.

Tests

  • test_video_stream_residency.py: the gate test fails on the merge base (streaming and HunyuanVideo-1.5 answered False) and passes here.
  • Every test file that touches the residency, the floor or the pad (test_diffusion_component_sizes, _dense_tier_activation, _multigpu, _h3_trainer, _learned_peaks, _offload_precision, test_h3_low_vram_stream, test_video_quantised_offload, test_video_stream_residency, test_diffusion_peak_pad_empty_slot): 724 passed, 8 skipped, 6 failed. The 6 failures have the same IDs on the merge base: test_cached_image_families_headers_match_tables, test_execution_device_reports_the_primary_of_a_split_pipeline, the 3 test_h3_routes_its_transformer_through_the_shared_regional_compile cases and test_an_image_prompt_encodes_through_the_streamed_conditioner.

…-baseline pad

diffusers' Wan pipelines register transformer_2 = None beside a single DiT
(Wan2.1, Wan2.2-TI2V-5B). stream_window_pad_mib read that slot as a placement
it could not size and returned None, so video_stream_residency kept every
measured request peak out of its keyed table and the store, and the per-request
residency fit only ever had the flat estimate.
… streaming tier too

The per-request residency fit applied only to the group tier. When the text
encoder alone exceeds the card (Wan2.2-TI2V-5B's 10.8 GiB UMT5 on a 12 GB card)
the plan is streaming, which streams the same block-level denoiser groups, so
the fit never ran and the int8 DiT (4.7 GiB) streamed every block every step at
a 5.6 GiB peak.
…t like Wan's

HunyuanVideo-1.5 int8 (13 GiB) on a 16 GB card streamed all 54 groups at a
7.6 GiB peak (0.36-0.39 s per DiT call). With the same per-request fit it keeps
29 groups (7.1 GiB) resident at a 15.0 GiB peak, 0.196-0.198 s per call, and the
frames are byte-identical.
@danielhanchen danielhanchen self-assigned this Oct 23, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 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-23T02:33:42.862581Z 3fac8e9 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 3fac8e9d50

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

Base automatically changed from studio-diffusion-peak-pad-empty-slot to main October 23, 2026 03:42
# Conflicts:
#	studio/backend/tests/test_diffusion_peak_pad_empty_slot.py

[skip ci]
@danielhanchen
danielhanchen merged commit 9a07082 into main Oct 23, 2026
2 of 34 checks passed
@danielhanchen
danielhanchen deleted the studio-video-resident-streaming branch October 23, 2026 05:41
Sneakr pushed a commit to Sneakr/unsloth that referenced this pull request Oct 23, 2026
…p graph is dropped (Wan2.2-TI2V-5B at 8 / 12 GB) (unslothai#14937)

Follow-up to unslothai#14865 (resident video streaming, merged). Based on main.

## Problem

Wan2.2-TI2V-5B int8 on a 12 GB or 8 GB card still streams all 30
denoiser blocks on every render after unslothai#14865. The 12 GB peak is about 5
GiB, and s/call is about 0.10 where resident would be about 0.035.
Whether it does depends on a coin flip. On a streamed load, the CUDA
graph speed check keeps the step graph only if replay beats eager by 1%.
For this model replay and eager land within noise (102.7 vs 104.0 ms in
one run, 102.7 vs 129.2 ms in another). When the check drops the graph,
two gates in `video_stream_residency` keep every later request
unrecorded:

1. The request's calibration key is built from the load's
`state.speed_optims`, which still names `cuda_graph`.
`_keyed_graphs_live` then reports the graph as dropped and refuses to
put the peak in the keyed table. Every later request runs eager under
that same graph key, so the table stays empty.
2. The capture that was dropped advanced the card's capture generation,
but nothing was recorded, so `_open_window` marks every later request
dirty (`_unsloth_video_graphs_clean = False`). This happens even though
no graph is alive on the card any more. The image probe already treats
"no graph alive" as clean
(`test_another_pipelines_reset_keeps_this_pipes_capture_dirty`, last
step). The video path did not.

As a result, `fit_for_request` never sees a measured peak. The flat
estimate leaves no room (10112 free - 3712 floor - overhead - estimate
<= 0), so every block streams.

## Fix

- **Calibration key.** `video_stream_residency.live_speed_optims(pipe,
speed_optims)` leaves `cuda_graph` out of the request's calibration key
only when the load's whole-step graphs are off for good. That means
every whole-step handle either sits on a placement that declined
recording for the load (`placement._declined`, set by the streamed speed
check or a failed stable device range) with an empty graph cache, or is
`poisoned`. A per-key drop (the speed probe running out of memory, or a
slower replay on a resident placement) leaves the rest of the load free
to capture other shapes, so it keeps the graph key. `reset()` clears
neither state. The video request path builds its key from this, so after
such a drop a request keys on the eager step it actually runs. Its peaks
are then recorded under that key and read back.
- **What keeps the graph key.** Any per-block fallback that is armed
(`fallback`) or engaged (`fallback_handle`) keeps the graph key, because
block graphs then run the next requests. A request-specific eager call
keeps it too (a refused argument, the graph cap, a per-call placement
refusal), because the wrapper stays armed and a later request can
capture. In those cases `_keyed_graphs_live` refuses the sample, as it
did before this PR. A load whose graphs replay keeps its graph key
unchanged.
- **Clean window when no graph is alive.** `_open_window` treats a card
with no live CUDA graph as clean and re-bases the capture generation,
the same rule the image peak probe uses. It reads the generation before
and after the live-graph count, and re-bases only when the generation
did not move. A capture racing the count therefore leaves the request
dirty. If any graph is alive, the previous logic runs unchanged.
- **Block graph count ordering.** A block graph now joins the live set
(`_LIVE_BLOCKS`) before `note_capture` bumps the generation
(`diffusion_block_graph.py`). Every capture path then counts its graph
live before the generation moves, which makes that snapshot consistent.
Whole-step captures already did this.

No new switch. The existing `UNSLOTH_VIDEO_DIT_RESIDENT*` switches still
turn the residency off.

## Tests

Unit (`studio/backend/tests/test_video_stream_residency.py`, 8 new):
- `test_a_dropped_graph_keys_the_request_on_the_eager_step`: real
`GraphedForward` handles. A drop on a declined placement or a poisoned
graph removes `cuda_graph`, and the key then passes
`_keyed_graphs_live`. A load with no handles, or optims without
`cuda_graph`, comes back unchanged.
- `test_a_per_key_drop_keeps_the_graph_key[probe_oom|resident_slower]`:
a drop that only removes one key, on a placement that has not declined,
keeps the graph key.
-
`test_a_dropped_step_graph_with_per_block_graphs_keeps_the_graph_key[fallback|fallback_handle]`:
a drop with the per-block fallback armed or engaged keeps the graph key.
- `test_a_request_specific_eager_call_keeps_the_graph_key`: refused
arguments, and a placement `Refused` without a speed verdict, keep the
graph key.
- `test_video_request_key_names_the_live_speed_optims`: the video
request path builds its key from `live_speed_optims`.
- `test_a_request_after_a_dropped_graph_is_recorded_under_its_key`:
after a capture whose graph was dropped (no live graph, generation
advanced), the next request is clean and its peak reaches the keyed
table. A graph still alive from an unrecorded capture keeps the next
request dirty, as before.
- `test_a_capture_racing_the_zero_count_keeps_the_request_dirty`: a
capture landing between the count and the generation read is not
re-based over.
- `test_every_capture_is_counted_live_before_its_generation_moves`:
every `note_capture` call site counts its graph live first.

`test_video_request_key_names_the_live_speed_optims` and
`test_a_request_after_a_dropped_graph_is_recorded_under_its_key` fail on
unslothai#14865's head (the code main now carries). The fallback,
request-specific and racing tests fail on the review-round base
0bc87de. The ordering test fails there by construction (the block
graph's live-set add came after `note_capture`); it was not run there.
The reworked dropped-graph test passes on both bases. All pass here. The
per-key drop test fails on d1d86bd (both cases) and passes on
d9ea62e; the residency and CUDA graph suites give 141 passed, 42
skipped on that head. The residency, learned-peaks, block-graph and CUDA
graph suites pass after the review round: 291 passed, plus one
parallel-run flake
(`test_capture_then_replay_uses_one_graph_and_copies_statics`) that
passes alone.

The video / CUDA graph / measured-peak suites (`tests/test_video*.py`,
`tests/*cuda_graph*.py`, `tests/*measured*.py`,
`tests/test_diffusion_learned_peaks.py`) fail the same tests on unslothai#14865's
head and on this branch (pre-existing: `test_video_i2v.py` class swap /
model offload, `test_video_ltx25.py`, `test_video_minimax_h3_vae.py`,
`test_video_stream_encode.py` compiled decode,
`test_video_vae_bf16_decode_rocm.py`), apart from two load flakes.
`test_video_ltx2_vae_tiles.py` x2 failed on the base run only, and
`test_video_routes.py::test_generate_native_crash_points_at_the_log`
failed on this branch's run only and passes alone on both. 3057 passed
here.

Real model, Wan2.2-TI2V-5B int8, 832x480, 33 frames, 8 steps,
`memory_mode=auto`, `speed_mode=default`, on a B200 capped to 12 / 8 GB
(`set_per_process_memory_fraction` plus a `mem_get_info` that reports
the cap). Each arm is a fresh process with a fresh Studio home,
rendering 3-4 different prompts. The graph drop was forced by setting
the speed check's required gain to 99%, so the drop path runs every
time.

| cap | arm | resident groups per render | peak allocated, resident
renders | frames vs streamed run |
|---|---|---|---|---|
| 12 GB | unslothai#14865 head, graph dropped | 0, 0, 0, 0 / 30 | 3.6 GiB (all
streamed) | reference |
| 12 GB | this PR, graph dropped | 0, 0, 30, 30 / 30 | 8.3 GiB |
identical (sha256 per render) |
| 12 GB | unslothai#14865 head, graph kept (unforced run) | 0, 30, 30 / 30 | 8.8
GiB | identical |
| 8 GB | unslothai#14865 head, graph dropped (unforced) | 0, 0, 0 / 30 | 3.6 GiB
| reference |
| 8 GB | this PR, graph dropped (unforced) | 0, 0, 16 / 30 | 6.2 GiB |
identical |
| 8 GB | unslothai#14865 head, graph dropped (forced) | 0, 0, 0, 0 / 30 | 3.6 GiB
| reference |
| 8 GB | this PR, graph dropped (forced) | 0, 0, 16, 16 / 30 | 6.2 GiB |
identical |

With the graph dropped, render 1 runs under the graph key and stays
unrecorded, as before. Render 2 records the eager key. Render 3 onward
places from it. After the review round (d1d86bd), the forced-drop 12
GB arm again ran 30 / 30 resident on renders 3 and 4 (0.036 s/call on
both, 8.3 GiB peak, frames identical, 0 graph breaks / recompiles). 0
graph breaks and 0 recompiles on every render of every arm.

s/call (median DiT forward): streamed 0.10-0.11. Resident 0.034 with the
graph kept at 12 GB, about 0.09-0.12 eager at 30 / 30 in the forced-drop
run, and 0.066-0.16 at 8 GB (16 / 30). Timings are from shared, loaded
cards (host load 450+), so they show the direction only.

## Not covered

- The streamed graph keep / drop decision itself is still a coin flip
when replay and eager land within noise (102.7 vs 104.0 ms here). This
PR makes residency work either way, but which path a load takes still
varies run to run. A hysteresis or keep-on-tie rule in the speed check
would remove that variance. Not changed here.
- Exclusive-card timing, and real consumer 8 / 12 GB cards (PCIe link
speed): the cap emulates the planner's view and the allocator limit, not
the link.
- AMD / ROCm, Windows, Mac: the path is CUDA-only (`applies` requires a
CUDA device), and the tests above are CPU unit tests.
- HunyuanVideo-1.5 under a dropped graph: the same key is used, so the
fix applies, but it was not rendered here.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
oobabooga pushed a commit to oobabooga/unsloth that referenced this pull request Nov 5, 2026
…est (21% faster per step) (unslothai#16787)

## Summary

`video_stream_residency` re-fits a block-streamed video denoiser's
resident prefix per request (group and streaming tiers), from free
memory and the request's measured peak. It was limited to Wan and
HunyuanVideo-1.5 (unslothai#14865), which left LTX-2 out with no stated failure.
On a 40 GB card a bf16 LTX-2 DiT then streamed every block every step
while about 17 GB of the card stayed idle.

This adds LTX-2 to the fit by exact family name (`ltx-2` covers 2.0 to
2.3). `ltx-2.5`, which uses a different decoder stack and was not
measured, stays out. Nothing else changes. The fit is generic over
offload groups, and LTX-2 is a single-DiT family.

Kill switches are unchanged: `UNSLOTH_VIDEO_DIT_RESIDENT=0`,
`UNSLOTH_VIDEO_DIT_RESIDENT_BLOCKS=0` (top group only),
`UNSLOTH_VIDEO_DIT_RESIDENT_MEASURED=0` (estimate only).

## Evidence

Colab A100-SXM4-40GB, one VM. Studio was installed with its own
`install.sh` (torch 2.13.0+cu130, diffusers 0.42.1, transformers 5.5.0).
Model: `Lightricks/LTX-2`, 768x512, 49 frames, 8 steps, CFG 4 (family
default), memory mode Fast, transformer quant off (bf16 DiT, plan
`group`: denoiser streamed block by block). Each arm is a fresh process.
Arms ran in this order: base, head, head, base, head with the kill
switch. Base is `main` 157305f (the merge base). Times are
scheduler-step medians, one DiT call per step (the pipeline batches both
guidance branches).

| arm | request 1 s/step | request 2 s/step | request 3 s/step | peak
allocated | DiT resident |
|---|---|---|---|---|---|
| base r1 (main) | 9.225 | 9.225 | 9.225 | 23.1 GiB | 0 |
| base r2 (main) | 9.242 | 9.241 | 9.241 | 23.1 GiB | 0 |
| head r1 | 9.233 | 7.301 | 7.301 | 31.2 GiB | 8021 MiB (11 groups) from
request 2 |
| head r2 | 9.235 | 7.308 | 7.308 | 31.2 GiB | 8021 MiB (11 groups) from
request 2 |
| head, `UNSLOTH_VIDEO_DIT_RESIDENT=0` | 9.246 | 9.246 | 9.246 | 23.1
GiB | 0 |

- A/A spread: base 9.225 to 9.242 s/step (0.2%), head 7.301 to 7.308
(0.1%). Once a measured peak exists (request 2 onward), head is 21%
faster per DiT call, far outside the noise.
- Output: for every request, the decoded frame bytes and the mp4 bytes
are identical across all five arms (frames sha `bc544a0a2dd4b627`,
`e0266cb88ba2bc8e`, `1c5258a24678ae47`). Resident groups run the same
kernels as streamed ones.
- Request 1 promotes nothing. With no measured peak yet, the flat
estimate leaves no room, so the first render is unchanged. The fit log
reads `8021 MiB of the streamed denoiser resident (11 groups) ... room
8284 MiB (measured peak ...)`.
- Default plan on the same card (int8 DiT quant, placed resident with
the text encoders streamed): the fit has no streamed groups to promote.
Base and head are both 1.405 s/step with identical frames, so nothing
changes there.
- Tiny `LTX2VideoTransformer3DModel` (6 blocks) under diffusers
block-level group offload on CUDA: re-fits at room 0, a partial room,
the whole model and 0 again. The forward outputs are bitwise equal to
the fully resident model every time, and groups are promoted and
released as expected.

Tests (`tests/test_video_stream_residency.py` +
`tests/test_diffusion_auto_multigpu_policy.py`, CPU):

| tree | result | failing IDs |
|---|---|---|
| base (main + this test file) | 1 failed |
`test_applies_only_to_block_streamed_single_dit_wan_hunyuanvideo_and_ltx2_on_cuda`
|
| head | all passed (6 CUDA-only skips) | none |

## Not covered

- LTX-2.3 single-file and LTX-2.5 were not run (2.5 is excluded on
purpose).
- The streaming tier (text encoder larger than the card's budget) was
not run for LTX-2; the fit itself is shared with the group tier.
- Measured on one card model (A100 40 GB). No consumer card was run.
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