Skip to content

Studio: quantise an offloaded native int8 denoiser on the card, one linear at a time - #15936

Merged
danielhanchen merged 2 commits into
mainfrom
studio-a14-fresh-first-clip
Oct 29, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-a14-fresh-first-clip

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Studio: quantise an offloaded native int8 denoiser on the card, one linear at a time

Problem

The first Wan2.2-T2V-A14B load in a new Studio home, with the DiT offloaded (16 / 12 GiB budget), builds the int8 experts from the dense Wan-AI shards. On a Colab A100-80 high-RAM VM that load takes 268-273 s, and the first clip takes 362-367 s.

About 86 s of that load is the int8 build:

  • _stage_denoiser_for_quant moves whole blocks onto the card until the staging budget is used up. At 16 GiB that is 13.2 of the 28.6 GB per expert, and at 12 GiB it is 8.3 GB.
  • apply_native_weight_quant then runs on every Linear wherever it sits, so the remaining blocks go through the ConvRot rotation and int8 packing on the host CPU. In a py-spy profile that is 66 s of host work: the fp32 rotation matmul, amax, divide / round, and the float() cast.
  • The staged blocks are then moved back to the host, because the plan offloads them anyway. The staging and unstaging moves cost another 17.5 s.

On a smaller budget less fits, so the host share and the load time both grow: 282.8 s at 12 GiB.

Change

  • When the plan offloads the DiT and the build is native int8, video.py skips the block staging. It runs the quant inside quantise_host_linears_on(device).
  • Inside that scope, apply_native_weight_quant builds each host-resident Linear on the card, one at a time:
    • the bf16 weight is uploaded through a reused pinned buffer;
    • NativeWeightOnlyLinear is built on the card with the same kernels a staged layer uses;
    • its buffers are copied back to the host.
  • The bias and every non-Linear parameter stay on the host.
  • If the card raises (out of memory or a device fault), that layer and the rest are built on the host, as before.
  • Engagement log line: diffusion.transformer_quant: built 403 host linear(s) on cuda one at a time (28.5 GB in 9.4 s).
  • Kill switch: UNSLOTH_NATIVE_QUANT_ON_DEVICE=0 restores the staged plus host build.
  • Resident loads, torchao schemes, hosted / cached builds, MPS and CPU targets are unchanged.

Results

Setup:

  • Colab A100-SXM4-80GB high-RAM (167 GB RAM). Every arm ran on one VM.
  • Wan2.2-T2V-A14B, 832x480, 33 frames, 10 steps, CFG 3.5, int8 denoiser, fp8 text encoder.
  • Each arm is a fresh process in a brand new Studio home, with its own inductor and compile caches.
  • The HF cache was filled before the timed arms, so no download is timed. The model files were evicted from the page cache before each arm.
  • Base is 9258bfc732 (main). Arms were interleaved S F F S S F.
  • First clip = load + first render.
budget arm first clip s (each arm) median A/A spread load s (median) cold render s (median) warm wall s warm s/step
16 GiB main 367.19, 361.57, 361.84 361.84 1.55% 268.63 93.31 45.76-45.93 4.395-4.424
16 GiB this PR 299.84, 299.00, 300.44 299.84 0.48% 204.94 94.06 45.76-45.86 4.391-4.422
16 GiB this PR, kill switch 363.78 268.95 94.83 45.75-45.84 4.389-4.406
12 GiB main 376.67 282.75 93.92 45.75-45.87 4.391-4.421
12 GiB this PR 298.38 204.03 94.36 45.76-45.81 4.390-4.415
  • At 16 GiB the first clip is 62.0 s faster (-17.1%). The A/A spread is 1.55% (5.6 s).
  • At 12 GiB it is 78.3 s faster, from one arm per tree.
  • The int8 build itself went from about 86 s (16 GiB) and 97 s (12 GiB) to 26 s at both budgets: 16.6 s for the first expert and 9.4 s for the second.
  • The cold render, the warm wall and s/step are unchanged, within the A/A spread. One head arm had a 6.1 s encode outlier.
  • Peak VRAM (nvidia-smi, under the ballast):
    • 16 GiB: 78.54 GiB on main, 74.24 GiB with this PR, because the staged blocks no longer go onto the card;
    • 12 GiB: 78.27 and 78.24 GiB.
  • Peak process-tree RSS is 160.9 GiB on main and 161.5-161.6 GiB with this PR (+0.7 GiB).
  • Engagement:
    • every head arm logs built 403 host linear(s) on cuda one at a time twice;
    • the kill-switch arm and main log quantising on cuda with 13.2 of 28.6 GB of the DiT staged there instead.

Accuracy

The quant algorithm is not changed. Only the device that runs it changes for the layers main did not stage. These checks compare the native prequant cache entries (per-tensor sha256 and an element diff) and the rendered frames:

  • Layers main staged on the card are bit-identical. Blocks 0-17 of the first expert and 0-16 of the second are byte for byte the same in main's and this PR's cache entries.
  • Layers main built on the host differ by rounding only. In blocks 18-39 (17-39), the CPU and GPU fp32 rotation sums round differently in the last ulp:
    • 671 of 7.26e9 codes (9.2e-8) in the first expert, and 716 of 7.55e9 in the second, differ, each by exactly 1;
    • per-row scales differ by at most 1 fp32 ulp (1.19e-7 relative).
  • This PR's build no longer depends on the budget. The 16 GiB and 12 GiB builds, and three separate 16 GiB loads, have identical digests, and their frames are identical (PSNR 99 dB, LPIPS 0).
    • On main the build changes with free VRAM: the 16 GiB and 12 GiB cache entries differ.
  • The kill switch reproduces main exactly: same digests, identical frames.
  • A returning load reads this PR's build back: load 26.9 s, frames identical to the fresh render.
  • Frames against main are not bit-identical. Main at 16 GiB against this PR: r0 PSNR 19.8 dB / LPIPS 0.160, r1 36.3 dB / LPIPS 0.021.
    • This 10-step trajectory amplifies 1-ulp weight changes.
    • Main against main, at 16 against 12 GiB, differs by more: r0 18.5 dB / LPIPS 0.211, r1 36.7 dB / 0.018.
    • So the change sits inside main's own budget-to-budget variation, and this PR's output equals the build main makes for every layer it can stage.

Tests

  • New studio/backend/tests/test_video_native_quant_on_device.py (14 tests). It covers:
    • the kill switch;
    • no device scope, which keeps the host build;
    • scope reset;
    • the mover keeping the bias on the host and restoring the source weight;
    • a device failure part-way, after which the remaining layers build on the host with identical bytes;
    • routing in the video load;
    • device gating (cuda, mps, cpu, switch off, CUDA unavailable).
  • Three CUDA tests:
    • a card build of host linears is bit-identical to a fully staged build;
    • the same forward;
    • partially staged layers are unchanged.
  • The CUDA tests passed on the A100 VM. On CPU they are skipped.
  • Touched-file suites (video quantised offload, native quant, native prequant cache, VRAM budget, native cache background, native stream build, late expert pin) give the same failing ID on base and head:
    • CPU: test_diffusion_native_quant.py::test_unpinned_torchao_host_copy_is_a_distinct_wrapper_over_the_same_data;
    • A100: test_diffusion_native_quant.py::test_eager_w8a8_chunks_long_inputs_without_changing_the_result.
  • Both failures predate this change.

Not covered

  • No G4 or other card was run. The code path is the same, but a host with a fast CPU or a small card was not timed.
  • ROCm, Windows / WSL and hosts where pinning is refused were not run. On those the pinned buffer falls back to pageable copies.
  • Wan2.2-I2V-A14B, the 5B and LTX / Hunyuan video families take the same branch when offloaded with native int8, and were not run.
  • The image pipelines keep their own quant path and are unchanged.

…ar at a time

A fresh-home Wan2.2-A14B load at a 16 GiB budget staged only part of each
expert on the card and quantised the rest on the host (66 s of host ConvRot
and int8 packing, plus 17 s of block staging). Under offload every
host-resident Linear is now uploaded through a pinned buffer, built with the
same kernels a staged layer uses, and downloaded again. Kill switch:
UNSLOTH_NATIVE_QUANT_ON_DEVICE=0.
@danielhanchen danielhanchen self-assigned this Oct 29, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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 ⚠️ Failed 2026-10-29T10:24:12.395369Z
We couldn’t complete this review. Comment @codex review to try again.
608771b Draft marked ready
ℹ️ 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. Hooray!

Reviewed commit: 608771bd4f

ℹ️ 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 marked this pull request as ready for review October 29, 2026 10:23
@danielhanchen
danielhanchen merged commit fffb8f5 into main Oct 29, 2026
@danielhanchen
danielhanchen deleted the studio-a14-fresh-first-clip branch October 29, 2026 11:05
danielhanchen added a commit that referenced this pull request Oct 29, 2026
…Grouper CUDA test on a real card (#15948)

## Summary

Two more checks are red on main itself, so every PR based on it today
inherits them (seen on staging for #15859 and #15936, neither of which
touches these files):

1.
`tests/security/test_lint_risky_loaders.py::test_the_tree_matches_the_baseline`
(workflow-trigger lint):
   ```
   1 risky loader call site(s) not in the baseline:
     unsloth/import_fixes.py:1040  [dynamic-import] import_module
   ```
#15850 added a fourth `importlib.import_module(module_name)` in
`unsloth/import_fixes.py` (the torchao import-time probe). The baseline
keys a call on its source text, so the existing entry for that text
(count 3) now sees 4 and reports the overflow at the first site. The new
call iterates the fixed `_TORCHAO_IMPORT_TIME_MODULES` tuple, so it is
the same trusted shape as the other three. The product is fine; the
baseline needed the count and the reason updated.

2.
`tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe`
(Repo tests, CPU):
   ```
   test_prefix_grouper_layout.py:199: torch.cuda.is_available()
   ```
#15835 added `test_cuda_layout_reads_the_device_once_and_copies_once`
gated on `torch.cuda.is_available()`, which
`tests/_zoo_aggressive_cuda_spoof.py` patches to `True` process-wide, so
on a CPU runner sharing a session with the compat suites the test
un-skips and calls `.cuda()`. Here the guard is right and the new test
was wrong: it now gates on `has_real_cuda()` like the other CUDA-only
tests.

## What changed

- `scripts/risky_loaders_baseline.json`: the `59e72c01edb14c86` entry
goes from count 3 to 4 (regenerated with `scripts/lint_risky_loaders.py
--update`, only that line moved), and its reason names
`_TORCHAO_IMPORT_TIME_MODULES` beside the other fixed tables.
- `tests/test_prefix_grouper_layout.py`: `skipif(not has_real_cuda(),
...)` instead of `skipif(not torch.cuda.is_available(), ...)`.

## Verification

- Base (main at `a4aee21490`): both tests fail as above.
- Head: `test_lint_risky_loaders.py`, `test_accelerator_skip_guards.py`
and `test_prefix_grouper_layout.py` give 75 passed, 1 skipped on a
CPU-only run.
- On a real GPU (torch 2.13 + CUDA 13.0) all 6 PrefixGrouper tests run,
including the CUDA one, so the new gate does not skip it on real
hardware.
- Each guard still fails without its fix: putting
`torch.cuda.is_available()` back makes the skip-guard test fail, and
putting the baseline count back to 3 makes the loader lint fail.
danielhanchen added a commit to unslothai/unsloth-zoo that referenced this pull request Oct 29, 2026
…nch (#2245)

## Summary

`tests/test_triton_fp_fusion.py::test_every_launch_states_fp_fusion`
fails on main since #2236:

```
AssertionError: ['gpt_oss_decode_fused.py:364:_rope_decode_kernel expected True']
```

It is also what turns the unsloth `Core zoo` jobs red on every unsloth
PR that installs zoo main (seen on staging for unslothai/unsloth#15936).

#2236 launches the new `_rope_decode_kernel` with `**_NO_FMA`
(`enable_fp_fusion=False`) on purpose: the kernel replaces the compiled
`apply_rotary_pos_emb` region and `tests/test_gpt_oss_rope_decode.py`
checks it against that region bit for bit, which needs each multiply
rounded before the add, the same reason `_add_rms_norm_kernel` and
`_sink_scale_kernel` in the same module already disable contraction. The
product is right; the guard just did not know this launch is one of the
deliberate ones.

## What changed

`gpt_oss_decode_fused.py:_rope_decode_kernel` joins `_DELIBERATE_FALSE`,
with the reason beside it.

## Verification

- Base (main at `8c8b40eb`): 1 failed (`_rope_decode_kernel expected
True`).
- Head: 1 passed, 2 skipped (the 2 skips need a GPU).
- The guard still bites on this launch:
- launch changed to `enable_fp_fusion = True`: fails with
`_rope_decode_kernel expected False`
- `**_NO_FMA` removed from the launch: fails with `Triton launches
without enable_fp_fusion: [..._rope_decode_kernel]`
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