Skip to content

Studio: cross-forward prefetch for video renders - #15108

Merged
danielhanchen merged 3 commits into
mainfrom
studio-video-cross-forward-prefetch
Oct 24, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-video-cross-forward-prefetch

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Extends the cross-forward prefetch carry (merged in #15088 for image renders) to Studio's video renders. When the denoiser streams from host RAM, the first groups of the next forward are already prefetched while the last streamed block of the current forward runs, instead of waiting for the next forward to start.

Changes

  • video.py: renders run pipe(**kwargs) inside cross_forward_render (_carried_call, armed only when the pipeline takes callback_on_step_end). It is armed on the render thread, since the interleave check is per thread.
  • video.py: the step callback calls cross_forward_step(p, step_index, timestep=timestep), also on the cancel path.
  • diffusion_offload_prefetch.py: two-expert pipelines (Wan2.2-A14B high / low noise) carry only into a forward of the same expert.
    • _expert_router reads which expert runs a step the way the pipeline's loop does: t >= boundary_ratio * num_train_timesteps, on one host copy of the schedule per render, with no per-step sync.
    • The step before a switch carries nothing, and an expert that does not run next drops its carried copies.
  • When the next module cannot be known, nothing is carried:
    • a timestep that is not the scheduler's own schedule entry (checked by address, no device read);
    • a step-count mismatch;
    • a second denoiser under any loop other than diffusers' Wan pipelines, including subclasses that override __call__.
  • Paths that are not armed: pipelines without a step callback (HunyuanVideo-1.5, MiniMax-H3, which has its own prefetch) and LTX-2.5's two-stage render. Resident, step-graph and slot-ring paths are unchanged.
  • Kill switch: the same as for images, UNSLOTH_DIFFUSION_PREFETCH_CROSS_FORWARD=0.

Results

Exclusive B200, random-weight Wan2.1-T2V-14B DiT from the real config (40 blocks, bf16), Studio group offload with the group prefetcher, regional compile. 480x832, 8 steps, CFG 5.0. Three interleaved repeats per arm, each in a fresh process, timing prompts 2-3.

frames tokens base ms/step (A/A spread) this PR delta kill switch 0 step peak GB (all arms)
17 7800 1052.8 (1.8) 1039.5 -1.3% 1050.9 3.1263
33 14040 1064.8 (2.5) 1041.8 -2.2% 1063.0 3.5297
81 32760 2622.2 (4.0) 2614.1 -0.3% 2623.0 4.7397
  • Peak memory: step peak is identical in every arm and repeat, and memory allocated at decode entry matches base.
  • Outputs: latents sha256 match across base, this PR and the kill switch for all frame / prompt pairs.
  • Compile: 0 recompiles and 0 new graphs on prompts 2-3 in every arm.
  • Carry: 750 carried, 750 read, 0 dropped, 0 missed. The copy count is unchanged.
  • Wan2.2-A14B (two random 14B experts, boundary 0.875, switch after step 2):
    • sha256 is identical across base, this PR and the kill switch.
    • Each expert's carries were all read, with nothing carried across the switch and nothing left on the card at decode.

Tests

  • New tests/test_video_offload_cross_forward.py. It covers:
    • a switch at steps 1 / 2 / 4 / 5, with and without CFG;
    • an unreadable schedule;
    • other loops and overriding subclasses not being armed;
    • no-boundary pipelines and a re-read schedule;
    • the wiring and the kill switch;
    • a CUDA two-expert streaming run checked bit-identical.
  • test_video_offload_cross_forward.py, test_diffusion_offload_cross_forward.py, test_video_backend.py pass.

Not covered

  • The 81-frame gain is small (8 ms against a 4 ms spread): the step is compute-bound there.
  • The bench drives diffusers WanPipeline with Studio's offload installers rather than a full VideoBackend.generate with real weights.
  • LTX-2 and Wan I2V / VACE are wired but were not run on a GPU.
  • Wan2.2-A14B timing has one repeat.

…noise forwards

Wire the cross-forward carry from #15088 into the video render path. A
streamed video DiT (group offload with the Studio prefetcher) now copies
the next forward's first groups into the idle copy tail of the current
forward, so the next forward starts on resident weights.

- video.py: the pipeline call runs inside cross_forward_render on the
  render thread (only when the pipeline takes callback_on_step_end, not
  LTX-2.5's two-stage render), and _on_step calls cross_forward_step,
  also on cancel. HunyuanVideo-1.5 and MiniMax-H3 have no step callback
  and are never armed, as before.
- diffusion_offload_prefetch.py: two-expert pipelines (Wan2.2-A14B
  transformer / transformer_2). The step callback reads which expert
  runs the next two steps from the scheduler's schedule and the
  pipeline's boundary_ratio, with the loop's own t >= boundary
  comparison on a host copy (one read per schedule). The step before a
  switch carries nothing, and an expert that does not run next holds no
  carried copy. A timestep that is not a view of the scheduler's
  schedule, a length mismatch, or a second denoiser under any loop other
  than diffusers' Wan pipelines carries nothing.
- Same kill switch: UNSLOTH_DIFFUSION_PREFETCH_CROSS_FORWARD=0.
  Resident, step graph and slot ring paths are unchanged.

Measured on an exclusive B200, random-weight Wan2.1-T2V-14B DiT from the
real config (40 blocks, bf16), Studio group offload + prefetcher,
regional compile below the hooks, 480x832, 8 steps, CFG 5.0, three
interleaved repeats of base / head / kill switch, prompts 2-3 timed.

| frames | tokens | base ms/step (A/A spread) | head ms/step | delta | kill switch |
|---|---|---|---|---|---|
| 17 | 7800 | 1052.8 (1.8) | 1039.5 | -1.26% | 1050.9 |
| 33 | 14040 | 1064.8 (2.5) | 1041.8 | -2.16% | 1063.0 |
| 81 | 32760 | 2622.2 (4.0) | 2614.1 | -0.31% | 2623.0 |

Step peak allocation is identical in every arm and repeat (3.1263 /
3.5297 / 4.7397 GB), as is the allocation where the decode starts.
Latents sha256 match across base, head and kill switch for every prompt.
Prompts 2-3 add 0 recompiles and 0 graphs in every arm. Every carried
copy is read: 0 dropped, 0 missed.

Wan2.2-A14B (two random 14B experts from the real configs, boundary
0.875, 8 steps switching after step 2): sha identical base / head / kill
switch, no copy carried across the switch (carried == hits, 0 dropped,
on both experts), nothing left on the card at decode.

Tests: tests/test_video_offload_cross_forward.py (expert switch at
several steps with and without CFG, unreadable schedule, other loops and
overriding subclasses not armed, no-boundary pipelines, render wiring and
kill switch, real CUDA two-expert stream bit-identical).
@danielhanchen danielhanchen self-assigned this Oct 24, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 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-24T09:43:35.607312Z 14502b0 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. You're on a roll.

Reviewed commit: 14502b0f55

ℹ️ 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 2d0fbd6 into main Oct 24, 2026
32 of 41 checks passed
@danielhanchen
danielhanchen deleted the studio-video-cross-forward-prefetch branch October 24, 2026 10:25
NilayYadav pushed a commit to Lyxot/unsloth that referenced this pull request Oct 28, 2026
…n denoise steps (unslothai#15796)

## Summary

On a 12 GB card MiniMax-H3 streams its whole int8 denoiser, including
the 0.8 GB top-level group (embeddings, `norm_out`, `proj_out`). That
group runs from the first block to the last, so it was offloaded at the
end of every forward and uploaded again at the start of the next. The
compute stream then waited for the whole copy, about 65 ms per step on
an A100, plus another 26 ms for block 0, which could not be prefetched
while the top-level group filled the window.

H3's modular pipeline has no `callback_on_step_end`, so the
cross-forward carry (unslothai#15108) never armed for it. This PR:

- drives the carry from the `scheduler.step` wrapper H3 already uses for
progress (`_scheduler_step_carry`, gated by `_h3_carry_by_scheduler`),
and disarms and drops anything carried at decoder entry as a backstop;
- adds `keep_first` to the H3 prefetcher. When the group that opens the
recorded order also closes the forward, it stays on the card into the
next forward, which starts with it anyway. Everything else (window,
decode drop, interleave and capture checks) is the existing carry logic.

On its own, the carry did nothing for H3. When the top-level group is
offloaded, the forward's compute is already finished, so its re-upload
still sat on the critical path. That measured 4.868 / 4.871 s/step
against 4.868 / 4.869 for base. Keeping the group on the card is what
removes the wait.

Kill switch: `UNSLOTH_H3_STREAM_CARRY=0` (turns off both the carry and
the keep).

## Profile (main `85744ff385`, Colab A100-SXM4-40GB, PCIe gen4 x16,
960x544x124, 20 steps)

Measured with CUDA events around each prefetch wait and copy,
`torch.cuda.set_sync_debug_mode("warn")`, and a torch.profiler trace of
one warm forward:

| | 16 GiB | 12 GiB |
|---|---:|---:|
| resident / streamed groups | top + 8 blocks / 42 | 0-1 / 50 + top |
| H2D per forward | 16.3 GB, 1.33 s at 12.3 GB/s (pinned) | 20.3 GB,
1.64 s |
| copy overlapped with compute | 98% | 94% |
| compute stall on copies per warm forward | 0.1 ms | 92 ms (top group
65 ms + block 0 26 ms) |
| compute-stream idle per forward (trace) | 18 ms | 29 ms |
| host syncs inside a warm forward | 0 | 0 |
| `empty_cache` / allocator segment frees per warm forward | 0 / 0 | 0 /
0 |

There is one forward per step. At 16 GiB, streaming is free once the
blocks are pinned: 4.776-4.793 s/step against 4.789 fully resident on
the same card. With pinning off
(`UNSLOTH_DIFFUSION_GROUP_OFFLOAD_PIN=0`, which reproduces the log lines
of a host with 0 MiB pinnable), the same cell runs at 4.936 s/step
(+3.3%). That matches the +3% the Oct 27 benchmark measured at 16 GB on
a host whose pin budget was 0, which unslothai#15642 addresses.

## Results (same VM, interleaved base / head processes, 12 GiB ballast +
total cap)

| arm | warm s/step (2 renders) | warm end-to-end (render 2 / 3) |
|---|---|---|
| base (3 processes) | 4.868 4.869, 4.874 4.868, 4.877 4.872 | 108.6
103.2, 107.1 103.1, 108.7 103.2 s |
| head (3 processes) | 4.805 4.794, 4.804 4.798, 4.817 4.802 | 106.9
101.1, 104.7 101.8, 107.9 101.9 s |
| head, `UNSLOTH_H3_STREAM_CARRY=0` | 4.872 4.870 | 108.3 102.3 s |

- Warm s/step: base 4.871 vs head 4.803 (-1.4%). The A/A spread of the
base process means is 0.006 s, the head spread 0.010 s, and the delta is
0.068 s.
- On the head, the 12 GiB cell's per-step time is within 0.3% of the
fully resident one (4.78-4.79).
- Compute stall per warm forward goes from 92 ms to 21 ms (profiled head
arm). H2D per forward goes from 20.25 to 19.45 GB.
- 16 GiB (top-level group resident, nothing to keep): base 4.788, head
4.793 s/step. Frames are identical.
- Warm renders are bit-identical, head vs base, in 6 of 6 pairs, plus
the 16 GiB pair and the kill-switch arm. The first (cold) render of a
process alternates between two outputs on main itself: base vs base
differs at 38.2 dB, and head vs base pairs either match exactly or show
the same 38 dB variant.
- Peak VRAM per render is unchanged (NVML over base: 11339-11341 / 10981
MiB on both). Inside a forward, `max_memory_allocated` is 7311 MiB on
both: the kept group is what the next forward starts with.
- Peak host RAM is unchanged (62.9-63.3 GB cell peak on both).
- 0 OOM, 0 `Recompiling function` lines (`TORCH_LOGS=recompiles`) in
every arm.
- Engagement: every head render logs `video.h3_stream_carry: 34 carried
copies read across forwards`. The kill-switch arm logs none.

## Tests

- New `tests/test_h3_stream_carry.py` (18 tests). They cover H3's loop
shape (20 forwards, 19 `scheduler.step` calls), one call per step, too
few calls (dropped at decode), cancel, the gate (family, streamed, step
callback, env), the kill switch, the top-level group kept and uploaded
once per render while blocks still stream, and release on an aborted
render. With the scheduler-driven step, the decode drop or the keep
reverted, 2, 1 and 1 of them fail.
- Prefetch, cross-forward, H3 stream and residency tests (21 files):
base 1200 passed / 3 failed, head 1218 passed / 3 failed. The same 3
`test_diffusion_multigpu.py` IDs fail on both.

## Not covered

- Only an A100 (PCIe gen4) was measured. The RTX 6000 Ada and the RTX
3090 (PCIe 3.0 x4) from the benchmark were not available, so the 3090
streaming gap is not measured here.
- This PR does not show that the benchmark host now pins. unslothai#15642 targets
that, and Colab has no capped cgroup to reproduce it.
- Block 0 still waits about 21 ms per forward at 12 GiB. Its copy can
only start once the last block is done without raising the window above
the forward-start footprint.
- The render after a residency re-fit (1 block resident, then 0) still
runs its first forward without prefetch: 48 misses, about 1.2 s once.
This PR does not change that.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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