Skip to content

Studio MOSS-Image: INT8 attention route, shared Q/K/V quant, bf16 compiled from the first render (11-35% faster) - #18090

Merged
danielhanchen merged 6 commits into
mainfrom
studio-moss-image-speed
Nov 17, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-moss-image-speed

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

MOSS-Image renders faster on every card measured, with images within the INT8 floor. There are three changes, plus a per-card gate on the third.

  1. The INT8 attention route for MOSS INT8 checkpoints. bf16 keeps cuDNN attention. Q/K/V now share one ConvRot activation quant instead of quantising the same input three times.
  2. bf16 compiles from the first render. Before, compile waited until the 3rd render, so bf16 ran eager on new prompts. On L4 that showed as GPU busy at 64%.
  3. The SiTU-GLU product is built inside the down projection's activation quant (Triton), instead of written out and read back.
    • It is on by default only where it measured faster: sm120 and sm89. It measured slower on sm80, so it is off there and everywhere else.
    • UNSLOTH_DIFFUSION_MOSS_SITU_FUSED=1 forces it on.

Results

Settings for every row:

  • 1024x1024, 25 steps, cfg 2.5, the reference negative prompt. SLG is on for Base and off for SFT.
  • Each cell runs in its own process: load, one warm-up, then 3 timed renders on new prompts. The table shows the median.
  • Before and after ran on the same VM.
  • Repeated runs gave bit-identical images, with times within 0.7% on G4 and A100 and 1.5% on L4.
GPU setting main s/img this branch s/img change LPIPS vs own bf16 (mean / max), main / branch
RTX PRO 6000 (sm120) SFT default (INT8) 5.644 4.859 -13.9% 0.052/0.100 / 0.055/0.103
RTX PRO 6000 Base default 5.631 4.854 -13.8% 0.036/0.047 / 0.031/0.039
RTX PRO 6000 SFT bf16 11.061 8.084 -26.9% branch vs main 0.022/0.041
A100-40 (sm80) SFT default 9.888 8.791 -11.1% 0.038/0.078 / 0.051/0.107
A100-40 SFT bf16 19.130 12.440 -35.0% branch vs main 0.023/0.055
L4 (sm89) SFT default 35.028 29.446 -15.9% 0.063/0.143 / 0.061/0.129
L4 Base default 34.765 29.377 -15.5%
L4 SFT bf16 74.59 50.19 -32.7% branch vs main 0.016/0.035

Per-change ablations: each row turns one change off against the full branch on the same VM, SFT default.

change RTX PRO 6000 A100-40 L4
INT8 attention route -10.6% -3.8% -6.3%
shared Q/K/V quant -3.9% -4.1% not run
SiTU-GLU in the act quant -1.1% +2.2% (so off on sm80) -4.3%

Image change per fix (mean LPIPS, against the arm with that fix off, RTX PRO 6000 / A100 / L4):

  • INT8 attention: 0.018 / 0.033 / 0.019
  • shared Q/K/V: 0.025 / 0.046 (L4 not run)
  • SiTU: 0.021 / 0.032 / 0.044
  • bf16 compiling from render 1: 0.022 / 0.023 / 0.016

L4 profile: bf16 cuDNN attention took 1495 ms per 4-step render before, and the INT8 attention kernel takes 807 ms after. No graph breaks and no recompiles.

Testing

  • CPU: test_diffusion_moss_speed_routes.py covers the attention route, the shared quant, the compile-from-first-render rule, the SiTU gate per arch and the kill switch.
    • 15 errors on main; passes on this branch.
    • MOSS, dispatch and prequant suites: 294 passed. Broader diffusion suites: 1043 passed.
  • GPU: the A/B rows above, on Colab RTX PRO 6000, A100-40 and L4. The SiTU kernel output also matches the compiled stock path on a B200; that run checks correctness only, not timing.

Not covered

  • T4: MOSS-Image INT8 still fails to load on a Colab T4, on main and on this branch alike. The likely cause is the fp32 promotion on sm75 running before the INT8 quant and exhausting host RAM; that is unconfirmed and left for a separate fix.
  • GGUF: the sd.cpp route was not measured.
  • Peak VRAM: unchanged. The text encoder stays on 40 GB+ cards.
  • SiTU on other archs: only sm120, sm89 and sm80 were timed. Every other arch defaults to off.

… share one q/k/v activation quant

The in-tree MOSS-Image blocks now call their module's dispatch_attention_fn
(read per call), carry head_dim and mark their processor, so the INT8
attention dispatch route can take their unmasked joint attention. MOSS-Image
joins AUTO_DITS, raced per card like the other families, but only when its
own Linears are quantized (AUTO_QUANTIZED_ONLY): a bf16 load keeps its
attention as the accuracy reference.

On a ConvRot int8 checkpoint the three projections read the same input, so
q / k / v now run on one rotation + activation quant through the int8 GEMM
swap's shared_rotq_linears instead of three (the same op on the same tensor;
the compiled graph around it changes, so images match within compile noise).
Kill switch UNSLOTH_DIFFUSION_MOSS_SHARED_QKV=0.
A dense MOSS-Image load with speed unset ran the deferred profile: two
exact eager images, compiled from the third. The eager port is host-bound
(1024 px bf16: 14.5 s vs 8.0 s compiled per image on an RTX PRO 6000,
27.5 s vs 12.9 s on an A100), so the second and third images ran about
1.8x slower than the compiled path. A new FamilyProfile field,
compile_from_render_one, carries that measured reason; a family that sets
it engages the default speed tier from render 1, as the INT8 default and
the small-DiT bf16 rule already do. An explicit speed request still wins.
…inside the act quant

On a ConvRot int8 checkpoint the compiled block wrote the bf16 SiTU-GLU
product (4 tanh(g / 4) sigmoid(g) * 25 tanh(v / 25)) to memory, then the
down projection's rotation + act quant read it back. rotq_h16x, which
already builds the SwiGLU product from the projection's two halves, takes
an ACT constexpr for SiTU-GLU (fp32 math, one bf16 rounding, as the
compiled chain), behind its own per-device probe (codes_close vs the
stock product + rotation + quant) and op
(unsloth_studio::convrot_situ_act_quant_int8). The MOSS block calls
int8_gemm.situ_linear when the down projection is a swapped ConvRot
Linear and falls back to the stock product and projection otherwise.
Kill switch UNSLOTH_DIFFUSION_MOSS_SITU_FUSED=0 (and the producer switch).
…measured faster

Building the SiTU-GLU product inside the down projection's act quant saves
about 1% per image on sm120 (RTX PRO 6000, 4.914 -> 4.859 s at 1024 px) but
costs about 2% on sm80 (A100, 8.791 -> 8.985 s). Arm it on sm120 only; other
cards keep the compiled stock SiTU. UNSLOTH_DIFFUSION_MOSS_SITU_FUSED=1 still
forces it on any card whose probe passes.
On an L4 the fused SiTU-GLU act quant takes 30.76 -> 29.45 s per 1024 px
image (s/step 1.170 -> 1.134; repeated head run 29.90 s, 1.139), so sm89
joins sm120 in the default set. sm80 stays off.
@danielhanchen danielhanchen self-assigned this Nov 17, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T07:28:56.167726Z 7652597 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 Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-17T06:13:31.216912Z
We couldn’t complete this review. Comment @codex review to try again.
797d91a PR opened
ℹ️ 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. Keep it up!

Reviewed commit: 797d91a24e

ℹ️ 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 Nov 17, 2026
…t alias (teardown errors since #18080) (#18097)

Since #18080 the Studio CPU shards (`rest`, `g-l`, `m-r`) error at
teardown on any worker that imported `torch._dynamo` earlier. Seen on
the staging runs of #18003, #18080 and #18090, e.g.:

```
ERROR at teardown of test_torchao_export_rejected_early_when_stubbed[torchao_fp8]
  torch/overrides.py:1778: has_torch_function = _add_docstr(
  RuntimeError: function '_has_torch_function' already has a docstring
ERROR at teardown of test_versions_are_checked_before_imports
  AssertionError: imported a package the version check already rejected
ERROR at teardown of test_a_broken_torch_import_is_retried_once_per_detection_epoch
  OSError: libcudart.so.12: cannot open shared object file
```

## Cause

The `_restore_dynamo_process_knobs` fixture from #18080 snapshots and
restores `cache_size_limit`. On torch 2.6+ that entry is an alias of
`recompile_limit`, and torch resolves an alias read with
`importlib.import_module("torch._dynamo.config")`. Several tests take
torch out of `sys.modules` and block imports to exercise the no-torch
paths (`test_torchao_select.py`, `test_mlx_stack_blockers.py`,
`test_polled_probe_costs.py`, `test_warm_window_review_fixes.py`,
`test_hub_download_transport_auto.py`, `test_mlx_repair.py`). The
fixture teardown runs before their monkeypatches are undone, so the
alias read re-imported torch under the blocker.

Those tests are correct, and the reset itself is still wanted; only the
alias read was wrong.

## Fix

`_dynamo_own_knobs(cfg)` keeps only the config's own entries
(`_config[k].alias is None`). Restoring `recompile_limit` already covers
`cache_size_limit` on new torch, and on older torch, where
`cache_size_limit` is a real entry, it is still restored. Reading an own
entry never imports.

New `test_conftest_teardown_imports_nothing.py` imports `torch._dynamo`,
takes torch out of `sys.modules` and makes `importlib.import_module`
fail for torch, so any conftest teardown that imports torch errors.

## Verification

Run locally with `torch._dynamo` already imported on the worker, as in
CI:

- main conftest: 14 teardown errors across `test_torchao_select.py`,
`test_mlx_stack_blockers.py`, `test_polled_probe_costs.py`,
`test_warm_window_review_fixes.py`,
`test_hub_download_transport_auto.py` and `test_mlx_repair.py` (the CI
ones), and the new test errors too.
- this branch: all pass (196 and 188 passed in the two runs).
- `test_process_state_reset_between_tests.py` still passes, so the knob
reset still works.
# Conflicts:
#	studio/backend/core/inference/diffusion_int8_attention_dispatch.py
#	studio/backend/tests/test_diffusion_int8_attention_dispatch.py

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 7652597a23

ℹ️ 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 277fe7d into main Nov 17, 2026
@danielhanchen
danielhanchen deleted the studio-moss-image-speed branch November 17, 2026 08:35
danielhanchen added a commit that referenced this pull request Nov 22, 2026
…he fused SwiGLU (#18798)

On the MiniMax-H3-Image streamed tier, every MLP ran the stock forward.
This is the tier 24 GB cards get (L4, 3090, a capped Ada). `_install` in
`diffusion_minimax_h3_image_speed.py` installed the fused SwiGLU only
when the levers held the exact string `"h3_int8_gemm"`, but the streamed
route records `"h3_int8_gemm_streamed"`. So on those cards `value *
silu(gate)` was written out in bf16, then rotated and quantised again
for the down projection, in every block and every step. The resident
tiers have fused this since #18090 / #18284.

## Change

- The fused SwiGLU now installs on both int8 GEMM routes.
- This is safe on the streamed route for three reasons:
- The pinned streamer keeps the swapped Linears and copies into their
parameters. That is the condition `int8_gemm_route` already requires
before it picks the streamed GEMM.
- `producer_linear` returns `None`, which falls back to the stock MLP,
for any call whose weight is not on the card.
- `_ready` falls back as well whenever the SwiGLU module or the down
Linear has a hook. The input projection still runs its own forward, so
its hooks are kept.
- The pageable streamer, which swaps parameters per call, still gets
neither the GEMM nor the fused MLP.
- `UNSLOTH_H3_FUSED_SWIGLU=0` keeps the stock MLP, as before.
- Q / K / V fusion stays resident-only. Merging the three weights into
one buffer would leave the streamer copying into parameters the GEMM no
longer reads.

## Tests

- `test_the_fused_swiglu_installs_on_every_int8_gemm_route` (CPU, 5
cases): pinned streamed installs it, resident installs it,
`UNSLOTH_H3_FUSED_SWIGLU=0`, `UNSLOTH_H3_IMAGE_INT8_GEMM_STREAMED=0` and
the pageable streamer do not.
- The streamed-pinned case fails on main and passes here
(`test_differential.py`: DISCRIMINATING, no head failures).
- `tests/test_diffusion_minimax_h3_image_speed.py`,
`tests/test_h3_fused_swiglu.py`,
`tests/test_h3_image_resident_route.py`: 62 passed.

## GPU (Colab L4, 22.5 GB: the streamed tier)

H3-Image at 1024x1024, 6 steps, `streamed` placement. Main and this
branch ran in fresh processes on one VM, sharing one Studio home, with 2
warm-up cells and then 3 interleaved reps per arm. The batch ran twice,
on two VMs, after an infrastructure retry.

| VM | main s/step | branch s/step | delta |
|---|---|---|---|
| 1 | 0.8628 | 0.8472 | -1.8% |
| 2 | 0.8652 | 0.8518 | -1.5% |

Medians of 9 steady renders per arm.

- **Fused-SwiGLU calls:** 1500 to 2100 per cell here, 0 on main.
- **Images:** identical to main, with LPIPS 0.000 and `torch.equal` on
all 4 prompts on both VMs. Every arm also repeats itself bit for bit.
- **Peak allocated memory:** 17.98 GiB on both arms.

## Not proven here

- sm86 (3090) is not a Colab tier.
- No larger-card resident-tier comparison ran. It is not needed for
accuracy, because the streamed images match main bit for bit.
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