Skip to content

ROCm: eager batch-1 NF4 decode takes the Triton GEMV (1.8x decode on gfx1151) - #14440

Merged
danielhanchen merged 3 commits into
mainfrom
nf4-hip-triton-gemv
Oct 21, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
nf4-hip-triton-gemv

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

On ROCm, eager batch-1 NF4 decode (fast_gemv in unsloth/kernels/utils.py) still calls bitsandbytes' ctypes GEMV, which on gfx1151 is about 3x slower per call than Unsloth's own Triton NF4 GEMV on the large projections.

The cause is the gate triton_gemv_eager() in unsloth/kernels/nf4_gemv.py:

  • It returns torch.version.hip is None or triton >= 3.7. It came in with Fused Triton NF4 dequant and GEMV: bit-exact, stream-safe, torch.compile traceable #12113 ("keep bitsandbytes' GEMV for eager decode before Triton 3.7") for every device.
  • CUDA was switched back to the Triton GEMV once the int32 words kernel beat bitsandbytes there, but HIP kept the old gate and was never re-measured.
  • No correctness reason excludes HIP. The GEMV tests passed on gfx1151, and compiled code already takes the Triton GEMV on HIP.

Change

  • On HIP with Triton 3.6+, eager batch-1 decode takes the Triton GEMV. Below 3.6 HIP keeps bitsandbytes (not measured there). Decode batches of 2+ on HIP still dequantize, as before.
  • New UNSLOTH_TRITON_GEMV_EAGER: 0 keeps bitsandbytes' GEMV, 1 takes the Triton GEMV on any GPU and Triton. Unset, CUDA routing is unchanged.
  • test_planned_rows_are_reused_without_an_out was previously skipped on HIP and now runs there. It skips when the direct launcher (triton_launch, CUDA only) is off: plans are only kept with it, and the multi-row path it forces is not taken on HIP.

Validation

tests/test_nf4_triton_gemv.py adds 38 cases; the 14 gate and routing cases fail on main:

  • the gate per platform, Triton version and switch value;
  • routing on a faked ROCm with Triton 3.6 (batch 1 takes the planned Triton GEMV with zero ctypes calls, the switch restores ctypes, rows still dequantize);
  • numerical checks of the route fast_gemv takes vs dequantize + fp32 matmul on Llama-8B / Qwen shapes, fp16 / bf16, nested and flat absmax.

On B200, test_nf4_triton_gemv.py + test_bnb_integration_compile.py + test_triton_fp_fusion.py: 403 passed.

AMD gfx1151 (Strix Halo, ROCm, torch 2.11, Triton 3.6), main vs this branch on the same runner, each also with UNSLOTH_TRITON_GEMV_EAGER=0. Every call took the Triton route on the branch and ctypes on main and with the switch off; parity vs fp64 is within the existing test tolerance.

Decode loop over real layer shapes, ms per token (host time included):

model shapes main this PR switch off
Llama-3-8B, 8 layers 9.85 5.34 (1.84x) 9.85
Llama-3.2-1B, 16 layers 6.93 3.86 (1.80x) 7.00

Per call, ms (CUDA events, first of the two AMD runs):

shape (N x K) main (ctypes) this PR (Triton)
up 14336 x 4096 0.293 0.097
down 4096 x 14336 0.276 0.099
Qwen up 0.244 0.082
q 4096 x 4096 0.085 0.033
kv 1024 x 4096 0.030 0.019
256 x 256 0.014 0.019
  • The smallest shape is slower per call because HIP has no direct launcher, so each Triton call pays the full launch cost. That does not show at the decode-loop level.
  • The tests on the runner: main 326 passed / 35 skipped, this PR 385 passed / 14 skipped, 0 failed.

Not covered:

  • Other AMD architectures (MI300 / gfx942, RDNA3), HIP with Triton below 3.6, and Windows ROCm.
  • bnb.matmul_4bit measured at or slightly below the Triton GEMV per call on gfx1151 (0.073 to 0.089 ms on the large shapes) but is not used here; routing HIP to it would be a separate change.

…on 3.6

On gfx1151 with Triton 3.6 the words kernel ran Llama 8B decode shapes 1.5x to
3.4x faster per call than bitsandbytes' ctypes GEMV, which HIP kept since the
gate predates the words kernel. UNSLOTH_TRITON_GEMV_EAGER=0 keeps bitsandbytes'
GEMV, =1 takes the Triton GEMV on any GPU and Triton. CUDA routing is unchanged.
@chatgpt-codex-connector

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 🔄 Running since 2026-10-21T03:28:36.508257Z 12bc96b 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

chatgpt-codex-connector Bot commented Oct 21, 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-21T04:38:59.188995Z 7e1742e Manual request
🔒 Security Review ⚠️ Failed 2026-10-21T03:28:40.946644Z
We couldn’t complete this review. Comment @codex security review to try again.
12bc96b 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 7e1742e453

ℹ️ 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 1f2e266 into main Oct 21, 2026
@danielhanchen
danielhanchen deleted the nf4-hip-triton-gemv branch October 21, 2026 04:41
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