Skip to content

Studio: stop the first Wan2.2-A14B render from waiting for the second expert's pin - #15906

Merged
danielhanchen merged 7 commits into
mainfrom
studio-a14-returning-pin
Oct 29, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
studio-a14-returning-pin

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Studio: stop the first Wan2.2-A14B render from waiting for the second expert's pin

Problem

When a user comes back to Studio and loads Wan2.2-T2V-A14B again, the load itself is quick (about 22 s on an A100). The first render then sits for 47.7 s before encoding starts (pre_s) and logs video.generate: waited 47.6 s for the host weights to finish pinning.

On this path the streamed weights are pinned in the background after the load returns. Three pinners start together, all copying from the prequant cache files on disk:

  • the UMT5 encoder (9.2 GiB)
  • the high-noise expert, transformer (13.2 GiB)
  • the low-noise expert, transformer_2 (13.2 GiB)

VideoBackend.generate then calls finish_background_pins and waits for all three. A cold Colab A100 disk delivers about 0.74 GiB/s in total, so both experts finish at 48 s. The low-noise expert is not used until the high-noise steps are done, so nothing needs it that early.

ComfyUI does not pin up front at all. Each expert is loaded and pinned when its own sampler first runs. On the same A100 its first clip takes 107 s, against 124.4 s for Studio.

Change

  • At the end of the load, defer_late_expert_pins marks the transformer_2 pinner of a two-expert (is_moe) family as late. The late pinner starts only after the encoder and first-expert pinners finish, so those two get the disk to themselves.
  • The render-time wait (finish_background_pins(..., include_late=False)) waits only for those two pinners. The second expert pins during the encode and the high-noise steps. Each of its offload groups still waits for its own pin before it onloads, through the existing install_group_pin_wait hook, so no group ever streams from a host copy that is being swapped.
  • Hosts where pinning is not disk-bound are handled separately. On a G4, with fast local disk, all three pinners finish in 4.4 s. Chaining there only moved the register cost into the encode: +1.1 s on the first clip in a first version of this change. The late pinner therefore measures the early pinners' rate, counted per copied tensor, for 0.2 s. At 3 GiB/s or more it pins beside them and the render waits for it, as before.
  • Kill switch: UNSLOTH_VIDEO_LATE_EXPERT_PIN=0 restores the old behaviour, where all pinners run in parallel and the render waits for every one.
  • The change does not alter weights, kernels, placement or the denoise itself. It only changes which pins the first render waits for, and in what order the pinners read from disk.

Results

Wan2.2-T2V-A14B, 832x480, 33 frames, 10 steps, CFG 3.5, int8 denoiser, fp8 text encoder.

Setup:

  • Budgets of 16 and 12 GiB, enforced by a VRAM ballast.
  • Returning home: a fresh process each arm, with the page cache of the model files evicted first.
  • Both trees share one home, and arms are interleaved on one VM.
  • Base is cf80abd639 (main). Head is this branch.
  • First clip = load + first render.

A100-SXM4-80GB, Colab high-RAM, final code:

budget arm first clip s (median, n) A/A spread pre_s cold denoise s warm wall s (median)
16 GiB main 124.40 (2) 0.31% 47.66 51.97 45.758
16 GiB this PR 107.28 (2) 0.90% 28.74 53.31 45.743
16 GiB this PR, kill switch 124.23 (1) 47.66 51.99 45.741
12 GiB main 124.05 (2) 0.15% 47.65 51.96 45.704
12 GiB this PR 106.39 (2) 0.03% 28.92 53.02 45.724
  • Two more A100 VMs ran earlier versions of this branch. Their slow-disk behaviour is the same as the final code, because the rate probe never trips at 0.74 GiB/s.
    • VM 1, n=3 per arm: 124.42 -> 107.03 s at 16 GiB and 124.51 -> 106.86 s at 12 GiB.
    • VM 2, n=2 per arm: 124.55 -> 106.84 s and 124.15 -> 106.61 s.
  • That is 17.1 to 17.7 s off the first clip, about 14%, on every VM.
  • ComfyUI's first clip on the same A100 tier is 107 s.
  • The cold denoise is about 1 s longer, because the second expert is still registering during the first steps.
  • The warm wall (renders 2 and 3) changes by at most 0.06 s, inside the base A/A spread.

RTX PRO 6000 Blackwell (G4), Colab:

budget arm first clip s (median, n) cold render s (median, A/A spread) pre_s cold denoise s warm wall s (median)
16 GiB main 44.57 (3) 33.741 (0.34%) 4.62 27.84 26.144
16 GiB this PR 44.59 (3) 33.765 (2.4%, one 34.59 outlier; the other two are 33.76 / 33.77) 4.65 27.86 26.141
16 GiB this PR, kill switch 44.47 (1) 33.683 4.57 27.81 26.159
12 GiB main 44.49 (3) 33.716 (0.11%) 4.60 27.87 26.151
12 GiB this PR 44.53 (3) 33.753 (0.27%) 4.65 27.83 26.121
  • On the G4 the probe measured 4.0 to 4.2 GiB/s, so the second expert pins beside the others and the first clip is unchanged, within the A/A spread.
  • That is the intended behaviour: there is no disk wait to hide on this host.
  • Earlier versions of this change measured on the same tier:
    • always chaining: +1.1 s on the first clip;
    • a group-granular rate probe: +0.25 s pre-render wait.

Both are fixed by the per-tensor probe here.

Accuracy:

  • Frames are bit-identical: every arm on a card gives the same per-seed frame MD5s, base, head and kill switch alike.
  • Pair scoring shows PSNR 99 dB, max abs 0 and LPIPS 0 for base against head at both budgets.
  • Peak VRAM, measured with nvidia-smi under the ballast, is unchanged: 74.26 / 78.26 GiB on the A100 and 90.08 / 94.08 GiB on the G4.
  • Peak process-tree RSS is unchanged: 66-69 GiB on both arms.

Engagement:

  • Head logs diffusion.memory: 1 late expert(s) (13.2 GiB) pin after the other 2 pinner(s) (22.3 GiB); the first render waits for those only.
  • On the A100 the second expert then pins in 20.7 s after the others, with onloads waited 0.0 s.
  • On the G4 it logs host weights pin at N GiB/s; WanTransformer3DModel pins beside the others.
  • With the kill switch on, pre_s goes back to 47.6 s.

Tests

  • New: studio/backend/tests/test_video_late_expert_pin.py. It covers:
    • ordering of the late pinner;
    • the render wait skipping it;
    • an early onload waiting for its pin;
    • the kill switch;
    • no deferral without both an early and a late pinner;
    • stop while waiting;
    • fast hosts pinning beside the others;
    • slow hosts staying behind;
    • per-tensor copy counting.
  • The existing background-pin GPU tests pass on A100 and G4 for both trees: test_group_offload_background_pin, test_video_fast_pin, test_video_encoder_exact_pin, test_h3_background_block_pin and test_video_native_cache_background.
  • The CPU video suites give the same pass and fail IDs on base and head (1090 passed).

Not covered

  • A fresh home, where the first load quantises and the sources are in RAM, was measured on base only. On a fast host the probe should put the late pinner beside the others, as on the G4. That was not run with this branch.
  • Hosts without registered pins (capped pinned memory, ROCm, WSL / Windows) still defer the second expert. There the probe falls back to group-granular pinned bytes. None of them was run.
  • Wan2.2-I2V-A14B takes the same is_moe path but was not run.
  • Kaggle T4x2 and other cards were not run.

A returning load pins both experts and the text encoder from disk in parallel,
and the first render waited for all of them (47.8 s on an A100 at 16 GiB).
The low-noise expert first runs after the high-noise steps, so it now pins
after the other pinners finish and the render waits only for those; its
group onloads still wait for their own pins. UNSLOTH_VIDEO_LATE_EXPERT_PIN=0
restores the old wait.
…bound

On a G4 the three pinners finish in 4.3 s; chaining the second expert behind
the others and overlapping it with the render moved its register cost into
the encode (+1.1 s on the first clip). The late pinner now joins the others
once their combined rate passes 3 GiB/s, and the render then waits for it.
The group-granular count kept a fast host's late expert idle for about a
second (G4: +0.25 s pre-render wait). Count bytes as each registered copy
completes and decide after 0.2 s.
@danielhanchen danielhanchen self-assigned this Oct 29, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 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-29T04:57:57.968355Z c36ba70 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

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: c36ba70d76

ℹ️ 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 added a commit that referenced this pull request Oct 29, 2026
… a fresh process (#15912)

The `(Python 3.13, a-f)` shard on #15906's staging run failed one test
the PR does not touch:

```
tests/test_diffusion_comfy_block.py:979: in test_int8_fp8_dequant_is_checked_for_its_float32_peak
    assert seen and max(seen) >= 0.9 * growth, (seen, growth)
E   AssertionError: ([201326592], 237703168)
```

The test passes on main and on #15906's head when run on its own.

## Cause

This is the flake #14579 already fixed for
`test_block_dequant_is_checked_for_its_peak_intermediates`. RssAnon is
process-wide, and in a long-lived xdist worker it carries whatever
earlier tests left behind. #14579 moved that test into a fresh process,
with a `malloc_trim` baseline and the best of three. Two siblings in the
same file kept measuring in the worker:
`test_int8_fp8_dequant_is_checked_for_its_float32_peak` and the peak
half of `test_swap_nibbles_is_bit_identical_and_checked_for_its_peak`.

The CI reading shows how far off a worker reading can be. Measured on
its own, the int8 ConvRot dequant grows by about two thirds of what it
charges. The worker saw 237.7 MB against the 201.3 MB charge.

## Fix

Both now run through the same kind of fresh-process probe as #14579
(`_HOST_PEAK_PROBE`, driven by `_host_peak_in_a_fresh_process`). The
assertion is unchanged: the charge must cover at least 90% of the
measured peak. The bit-identity half of the nibble-swap test still runs
in process. `_rss_anon_now`, now unused, is removed.

## Verification

Charge over measured growth in the fresh process, three runs each:

| op | charge / growth |
|---|---|
| int8 ConvRot dequant | 1.50, 1.50, 1.50 |
| fp8 dequant | 1.031, 1.035, 1.048 |
| nibble swap | 1.179, 1.027, 1.097 |

Each case stays above the 0.9 the test requires.

- `test_diffusion_comfy_block.py`: 55 passed, 5 skipped (CUDA-only),
serially and under `-n 8`.
- Sabotage: undercharging `_dequant` (`codes.numel() * 4` instead of the
float32 intermediates) fails both `float32_peak` cases in the fresh
process.
@danielhanchen
danielhanchen merged commit 9258bfc into main Oct 29, 2026
@danielhanchen
danielhanchen deleted the studio-a14-returning-pin branch October 29, 2026 06:18
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