Skip to content

Studio: judge the compiled ROCm W8A8 linear per card, not once per process - #17078

Merged
danielhanchen merged 5 commits into
mainfrom
studio/rocm-w8a8-per-device
Nov 8, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
studio/rocm-w8a8-per-device

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Problem

#16825 enabled the compiled int8 W8A8 linear on ROCm RDNA3 / RDNA4 only (gfx11xx / gfx12xx), and #17013 lets AUTO pick W8A8 on those cards. The architecture check read the current device, and its answer was cached for the whole process in one slot:

  • an RDNA3 card used first, then an RDNA2 / CDNA card: the second card got the same compiled function without its arch ever being read;
  • an RDNA2 / CDNA card used first: the refusal (ROCm gfx942) stuck for the process, so a later RDNA3 card stayed eager and AUTO never picked W8A8 for it;
  • a compile failure on one card also turned the compiled linear off for every other card.

So on a host with two different ROCm cards, which card ran first decided the path for both, even when each load was correctly placed on its own card.

Change

  • The compiled-linear check is now per card. _fused_w8a8(device) takes the input's device (the linear's forward passes x.device), reads that card's arch once, and caches the answer under that card. The compiled function itself is still built once.
  • A compile failure is recorded against the card it happened on. That card goes eager, and the other cards keep the compiled linear.
  • rocm_rdna_w8a8_ready(ordinal) answers for the target's card. AUTO W8A8 passes target.ordinal (current card when unset, as before).
  • fused_w8a8_state() keeps failed (the process-wide reason, else the first card's) and adds devices (each card's answer).
  • Nothing changes on a single-card host: same arch rule, same compiled function, same eager fallback, same UNSLOTH_NATIVE_INT8_FUSED=0 kill switch. NVIDIA keeps the compiled linear as before.
  • The forward still runs only outside torch.compile (not torch.compiler.is_compiling()), so the per-card lookup adds no guards. Its cost: 0.68 µs per call on main, 0.84 µs here (CPU, timeit, 3 x 5 x 200k calls).

Tests

test_diffusion_native_quant_fused.py (CPU, HIP arch and compile runtime mocked):

  • Two cards (gfx1100 + gfx1030), in both orders, by input device and by switching the current card: each card gets its own answer, and each arch is read once.
  • CDNA card first, then RDNA: rocm_rdna_w8a8_ready is True for the RDNA card and False for the CDNA one.
  • A compile failure on card 1 keeps card 0 on the compiled function.
arm test_diffusion_native_quant_fused.py + test_diffusion_native_quant.py
main (a174823) 190 passed, 7 failed (the 6 new per-card cases, plus 1 torchao test)
this PR 196 passed, 1 failed (the same torchao test)

The torchao failure (AffineQuantizedTensor has no qdata) comes from the torchao version on this host and fails the same way on main. The RDNA / AMD / W8A8 cases in test_diffusion_backend.py pass (13).

On gfx1151 (Strix Halo, ROCm)

Two jobs, base 49453f39a vs head 9e86484b7 on the same runner: test_diffusion_native_quant_fused.py + test_diffusion_native_quant.py. The GPU job presents the one real card as two HIP devices (a second ordinal on the same gfx1151), so the per-card bookkeeping runs against real HIP indices.

job base head verdict
suites (HIP masked) 190 passed, 1 failed 197 passed, 1 failed no regression; the failure is the torchao test above, on both
GPU (real gfx1151 + a second ordinal) 195 passed, 5 failed 203 passed, 5 failed, 0 skipped no regression; the same 5 fail on base

On the GPU job, test_each_real_card_runs_the_compiled_linear_bit_identical_to_eager ran on cuda:1 and cuda:0: the compiled linear engaged on each and matched eager bit for bit. The 5 failures on both arms (test_eager_w8a8_chunks_long_inputs_without_changing_the_result, two torchao offload tests, and the two ROCm test_cuda_engagement_tests_skip_where_production_stays_eager cases) pre-date this PR.

An earlier gfx1151 run of this branch caught a case the CPU tests missed. On a ROCm torch with no visible card, a CPU input was judged by the current card's arch (unreadable, so a refusal). Fixed in 9e86484b7: only the current card (no device given) and HIP inputs are judged.

Not covered

  • Two physically different ROCm cards in one host: the second ordinal on the gfx1151 runner is the same card, so mixed-arch routing is proven on the mocked-arch CPU tests only.

…ocess

The RDNA arch check read the current device and its refusal (or a compile failure) was cached
process-wide, so with two ROCm cards the first card decided for both: an RDNA2 / CDNA card could get the
compiled linear after an RDNA3 card used it, and an RDNA3 card stayed eager after another card refused.
Eligibility and failures are now keyed by the input's card (AUTO W8A8 reads the target's card), and a
compile failure on one card leaves the others on the compiled linear.
@danielhanchen danielhanchen self-assigned this Nov 8, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 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-08T03:55:56.450701Z 9e86484 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.

[skip ci]
The per-card check read the current card's arch for a CPU input too, so on a ROCm torch with no visible
card (HIP masked) every CPU call was refused as 'ROCm unknown arch'. Only the current card (no device) and
HIP inputs are judged now.

[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. 👍

Reviewed commit: 9e86484b70

ℹ️ 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".

[skip ci]
@danielhanchen
danielhanchen merged commit 0854cbe into main Nov 8, 2026
@danielhanchen
danielhanchen deleted the studio/rocm-w8a8-per-device branch November 8, 2026 20:53
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 9, 2026
… own arch (follow-up to unslothai#17078) (unslothai#17195)

## Summary

Follow-up to unslothai#17078. That PR made the compiled int8 W8A8 linear decide
per card on ROCm: an RDNA3 / RDNA4 card runs it, and any other arch (for
example CDNA gfx942) stays eager. The production routing is correct. The
real two-card test it added still assumed both cards run the compiled
linear:

- it checked readiness only on the current card
(`_require_the_compiled_linear()`)
- it required `calls + 1` on both cards
- it required `devices == {"cuda:0": None, "cuda:1": None}`

On a mixed ROCm host with an RDNA card 0 and a CDNA card 1, card 1
correctly stays eager. The call count then does not move, so the test
fails even though production behaves as intended. With a CDNA card 0 and
an RDNA card 1, the test is skipped entirely and the RDNA card is never
checked.

## Change (test only)

- The test asks production for each card's own decision
(`_fused_w8a8(torch.device("cuda", i))`). It skips only when neither
card can run the compiled linear.
- On CUDA, where no card is refused by arch, it still requires both
cards to be ready.
- For each card it expects `calls + 1` only where that card is ready.
Output must be bit-identical to eager on every card.
- The final per-card state must match: `None` for a ready card, a
refusal reason for the others.

No product code changes.

## Testing

- `pytest -o addopts= tests/test_diffusion_native_quant_fused.py` on
CPU: 48 passed, 5 skipped. The skipped tests, this one included, need
real cards.
- I had no two-card host here, mixed ROCm or otherwise, so the changed
test did not execute in this PR. The failure on main follows directly
from the old assertions and the routing in `_fused_w8a8`
(`diffusion_native_quant.py`, the `_rocm_fused_w8a8_refusal` branch),
which records a refusal for the non-RDNA card and returns no compiled
function there.
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