Skip to content

Studio diffusion: skip the offload cost probe's pinned buffer on an integrated GPU - #15178

Merged
danielhanchen merged 1 commit into
mainfrom
studio-diffusion-cost-probe-integrated
Oct 25, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-diffusion-cost-probe-integrated

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

#15163 added a 0.1 s probe that times pinned host-to-device bandwidth and bf16 matmul throughput, which the whole-module vs streamed offload choice uses. The probe allocates a 256 MiB pinned host buffer without asking host_pinning_allowed(). On an integrated GPU (an APU, where the "VRAM" is system RAM), a pinned copy only takes RAM away from the card. Every other pinning site already skips pinning there.

tests/test_integrated_gpu_no_host_pin.py::test_every_pinning_site_asks_the_integrated_gpu_predicate_or_is_reached_through_one has failed on main since #15163:

pinning without asking host_pinning_allowed: core/inference/diffusion_memory.py:4072 _offload_cost_measurements

Changes

Tests

  • tests/test_integrated_gpu_no_host_pin.py passes again: it failed on main with the message above.
  • tests/test_diffusion_offload_tier_per_request.py: 52 passed, 2 skipped together with the guard test. The planner and offload suites pass: 612 passed, 129 skipped.

Limits

No run on a real integrated GPU here. The change only skips a probe on the path the predicate already marks as integrated.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T00:43:39.724148Z 75e2ca6 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. Nice work!

Reviewed commit: 75e2ca6892

ℹ️ 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 9f499ff into main Oct 25, 2026
34 of 42 checks passed
@danielhanchen
danielhanchen deleted the studio-diffusion-cost-probe-integrated branch October 25, 2026 01:41
danielhanchen added a commit that referenced this pull request Oct 25, 2026
…15182)

## Summary

`tests/utils/test_single_row_cached_attention.py` errors on all 64 cases
on main since #15171 (K/V straight into the cache slot, opt-in
CUDA-graph decode). That fails the `Core (HF=...)` legs on unrelated
PRs, for example #15176 and #15178:

```
AssertionError: Qwen3Attention_fast_forward_inference now reads ['_decode_attention_core', '_decode_graphs']; give the harness a CPU stand-in for each
```

The harness lifts `LlamaAttention_fast_forward_inference` and
`Qwen3Attention_fast_forward_inference` out of their files with `ast`,
and refuses to run if they read a global it does not provide. #15171
moved the cache and scratch buffer setup into
`unsloth/models/_decode_graphs.py` (`alloc_decode_buffers`,
`resize_decode_buffers`) and added the captured-step branch that calls
`_decode_attention_core`. The product is fine. The guard did what it is
meant to do and asked for stand-ins.

## Change

- `_decode_graphs`: the real module, loaded by file path. Its top level
is torch and the standard library only. `ENABLED` is forced to `False`,
because graph replay is CUDA-only. The test keeps running the real
`alloc_decode_buffers` / `resize_decode_buffers` instead of a copy.
- `_decode_attention_core`: a stand-in that raises if called. It is only
reached from a captured decode step, which this CPU harness never
enters, so reaching it would mean graph mode leaked in.

## Verification

- On main: `64 errors`. With this change: `64 passed`. It also passes
with `UNSLOTH_DECODE_CUDA_GRAPHS=1` set.
- Sabotage 1: writing the wrong V rows in
`_decode_graphs.alloc_decode_buffers` gives 56 failures. The real helper
is what the test exercises.
- Sabotage 2: skipping qwen3's 2D padding mask conversion in the decode
function gives 8 failures. The masking checks the file was written for
still bite.
dayanruben pushed a commit to dayanruben/unsloth that referenced this pull request Oct 25, 2026
…unslothai#15179)

## Summary

On Linux / CUDA, Studio's video background pinner already pins one
page-rounded block per tensor (`cudaHostRegister`). One video path still
went through power-of-two rounding: the whole-module ("model") offload
tier, `_pin_host_weights`.
- **Chunk sizing:** it packs weights into chunks, but sizes each chunk
to the next power of two of its largest tensor, and rounds the last
chunk too.
- **Effect on Wan2.2-TI2V-5B at a 12 GiB budget:** 14.5 GB of weights
took 16.1 GB of pinned host memory:
  - text encoder: 7.74 -> 8.59 GB;
  - DiT: 5.08 -> 5.37 GB;
  - VAE: 1.71 -> 2.15 GB.
- **GGUF weights:** the background pinner used `pin_memory()` for these,
so a 5.3 MB Q4_K weight took 8 MiB.

## Changes

- **Model tier, video loads only:** each chunk is pinned in registered
host memory sized to its bytes (rounded to 4 KB pages), with the stock
slot layout, so device uploads and VRAM are unchanged.
- Each chunk's weights are re-pointed as soon as that chunk is
registered.
- If the first chunk is refused, the tier falls back to the stock pin.
If a later chunk is refused, the chunks already pinned are kept and the
rest stay pageable.
- **GGUF weights in the background pinner:** they get the same
registered copy, re-wrapped with their quant type.
- **Integrated GPUs:** the exact pin asks `host_pinning_allowed()`
first, so nothing is pinned there.
- **Kill switch:** `UNSLOTH_VIDEO_EXACT_PIN=0`.
- **Image loads:** unchanged.

## Results

Exclusive B200 lease for the whole run. Wan2.2-TI2V-5B, 12 GiB cap,
model tier, 1280x704x17, 8 steps. 4 reps per arm in rotating order, 3
renders per process; medians (min-max).

| arm | pinned GB | steady anon+pinned GB | steady s / forward | render
1 s (includes pinning) | frames |
|---|---|---|---|---|---|
| base | 16.11 | 22.76 (22.70-22.81) | 0.0505 (0.049-0.054) | 26.13
(20.74-37.47) | identical in 12 / 12 renders |
| head | **14.53** | **21.23** (21.22-21.23) | 0.0508 (0.049-0.0635) |
27.96 (24.07-54.71) | identical in 12 / 12 renders |
| kill switch | 16.11 | 22.80 | 0.0505 (0.049-0.0525) | 27.09
(19.94-33.63) | identical in 12 / 12 renders |

- **Memory:** pinned memory falls by 1.58 GB and now equals the weight
bytes. Steady RAM falls by 1.53 GB.
- **Peak RAM:** 26.16 GB, inside base's 25.87-27.53 GB.
- **Speed:** steady speed and render 1 are inside base's range. The slow
head maximum came from a rep where the host was loaded, and it slowed
base too (base's steady renders in that rep took 2.7-3.3 s against
1.5-1.9 s elsewhere).
- **Frames:** identical across all 36 renders of the 12 processes.
- **Wan2.1-1.3B (group tier, 10 GiB):** unchanged at 11.3 GB pinned.
That path was already page-exact.

## Tests

- **New `tests/test_video_exact_pin.py`:**
  - exact chunk sizes;
  - the stock slot layout;
- the stock pin when registration is unsupported or the first chunk is
refused;
  - pinned chunks kept when a later one is refused;
  - only video pipes asking for exact chunks;
  - GGUF re-wrap with the quant type;
  - the kill switch for both paths.
- **`tests/test_video_*.py` and the pin suites:** 2812 passed, 109
skipped. One failure,
`test_every_pinning_site_asks_the_integrated_gpu_predicate_or_is_reached_through_one`,
also fails on main because of the offload cost probe; unslothai#15178 fixes it.

## Limits

- ROCm, Windows and WSL keep the per-tensor fallback unchanged; they
were not measured here.
- Not run: A14B (126 GB) and an end-to-end GGUF video load. GGUF has a
GPU unit check (byte equality, quant type kept, device upload works).
- Pinning does not explain all of the ~2x host RAM against ComfyUI on
T4. The remaining gap is elsewhere.
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