Skip to content

[HIP] [Bugfix] scaled_silu_and_mul: device-side scale, fp8 saturation, row bounds - #6641

Merged
vgokhale merged 2 commits into
ROCm:mainfrom
AndreasKaratzas:akaratza/scaled-silu-and-mul-fix
Oct 19, 2026
Merged

vgokhale merged 2 commits into
ROCm:mainfrom
AndreasKaratzas:akaratza/scaled-silu-and-mul-fix

Conversation

@AndreasKaratzas

Copy link
Copy Markdown
Contributor

Motivation

aiter.scaled_silu_and_mul has three independent bugs:

  1. The scale is read on the host at launch. Under CUDA/HIP graphs the captured value is frozen, so changing the scale in place and replaying gives 0.5x, 0.125x or 2x the correct output. A scale written by a kernel that is still running on the same stream is also read stale (10 of 10 launches in the repro).
  2. No saturation. On gfx942, v_cvt_pk_fp8_f32 turns |x| >= 248 into 0x80, which is NaN in e4m3fnuz, instead of saturating the way .to(float8_e4m3fnuz) and AITER's quant kernels do.
  3. Row overrun. When d is not a multiple of the vector size (e.g. 1, 3, 4098, 14338), each row's last vector store spills into the first columns of the next row. For the last row it writes past the end of the tensor. Strided rows are not supported either.

Repro on MI300X:

import torch, aiter
from aiter import dtypes
x = torch.randn(64, 2 * 14336, device="cuda", dtype=torch.bfloat16)
s = torch.tensor([1.0], device="cuda")
o = torch.empty(64, 14336, device="cuda", dtype=dtypes.fp8)
g = torch.cuda.CUDAGraph()
with torch.cuda.graph(g):
    aiter.scaled_silu_and_mul(o, x, s)
s.fill_(0.5); g.replay()
e = torch.empty_like(o); aiter.scaled_silu_and_mul(e, x, s)
print("graph replay / eager:", (o.float().abs().sum() / e.float().abs().sum()).item())

s.fill_(0.01)  # outputs far beyond the fp8 range
aiter.scaled_silu_and_mul(e, (x * 8).contiguous(), s)
print("NaN after saturation:", e.float().isnan().sum().item())

d, M = 3, 7
buf = torch.full((M + 1, d), 0x5A, device="cuda", dtype=torch.uint8)  # last row is a guard
s.fill_(1.0)
aiter.scaled_silu_and_mul(buf[:M].view(dtypes.fp8), torch.randn(M, 2 * d, device="cuda", dtype=torch.bfloat16), s)
print("bytes written past the tensor:", (buf[M] != 0x5A).sum().item())
main this PR
graph replay / eager 0.500 1.0
NaN after saturation 421094 of 917504 (varies by run) 0
bytes written past the tensor 1 0

Technical Details

Root causes in csrc/kernels/activation_kernels.cu on main:

  • 1325, 1334: 1.0f / *scale.data_ptr() is evaluated on the host and the result is passed to the kernel by value.
  • 468, 475: the result is cast with opus::cast<DTYPE_O> without clamping.
  • 423-428: the output buffer range is d rounded up to a dword, and the loop stores whole vectors up to d, so the tail vector writes past the row. 401-402 and 423 also assume packed rows (token_idx * 2 * d, token_idx * d).

Changes, all in scaled_act_and_mul_kernel and its launcher:

  • Pass the scale pointer and compute 1 / *scale in the kernel.
  • Clamp to the output type's finite range (fmed3f) before the cast. This covers fp8, and int8 with its own bounds.
  • Run the vector loop only up to d / VEC_SIZE * VEC_SIZE, and store the tail element by element.
  • Use the input and output row strides, and check that the last dim is contiguous, that out has shape [..., d], and that scale is a single fp32 element on the GPU.
  • Add test_scaled_silu_and_mul_edge_cases to op_tests/test_activation.py.

The unscaled silu_and_mul has the same row overrun for odd d (bf16 d = 3, 4097, 14337 write into a guard row). That is a different kernel and is not changed here.

Test Plan

All runs used MI300X (gfx942) on the AITER v0.1.25 runtime with only module_activation rebuilt. activation_kernels.cu is identical in v0.1.25 and main. The file also compiles for gfx950, gfx1100 and gfx1201.

  • Correctness suite, 864 cases:

    • fp8 and int8 outputs; bf16, fp16 and fp32 inputs;
    • contiguous, 3-D, and strided rows (pad 8 and pad 3);
    • d in {1, 2, 3, 4, 6, 8, 1000, 4098, 8192, 14336, 14338, 28672}, M in {1, 7, 64, 4096} (int8 output, fp32 input and 3-D use d in {3, 1000, 4098, 14336} and M in {1, 64});
    • each case once in a normal and once in a saturating range.

    A case passes when it has no NaN, every value is within one quantization step of a clamped fp32 reference, and the sentinel bytes in the padding columns and in a guard row after the tensor are unchanged. I ran the suite 10 times (seeds 101-110) on this commit, and 10 times (seed 0) on an earlier revision that differed only in how the tail loop was structured.

  • Bit-exact comparison with main on the 288 cases where main is correct: contiguous rows, d % vec_size == 0, nothing saturating.

  • Graph replay after an in-place scale change, bf16/fp16 x (d, M) in {(14336, 64), (4098, 7), (28672, 1)}. Also a scale updated in-stream behind four 8192x8192 matmuls, 10 launches.

  • test_scaled_silu_and_mul_edge_cases, plus the argument checks: a CPU scale, a non-contiguous last dim, and a wrong out shape.

  • Latency: bf16 in, fp8 out. A CUDA graph of 50 launches, median of 21 replays per run, then the median (min-max) over 6 runs on main and 5 with this PR.

Test Result

  • Suite: main fails 708 of 864 cases. This PR passes all 864 in all 10 runs (and so did the earlier revision in its 10). Roughly 800 of 3.59e9 output elements per run differ from the reference, all by at most one quantization step.
  • Bit-exact: identical to main on all 288 cases where main is correct.
  • Graphs and stream order: graph replay matches eager in all 6 configs (main: 0.5x, 0.125x, 2x). The stale in-stream scale goes from 10/10 to 0/10.
  • Edge-case test: fails on main (d=3: NaN instead of saturation) and passes with this PR. Each of the three bad arguments raises its new error message; on main all three were accepted silently.

Latency, us:

d M main this PR ratio
14336 1 4.54 (4.51-4.58) 4.71 (4.68-4.76) 1.04
14336 64 4.79 (4.75-4.86) 4.95 (4.94-5.00) 1.03
14336 256 5.51 (5.47-5.69) 5.84 (5.66-5.96) 1.06
14336 1024 21.31 (20.88-21.86) 22.19 (22.10-22.34) 1.04
14336 4096 89.00 (82.50-95.16) 87.35 (87.02-88.06) 0.98
14336 16384 322.23 (320.41-379.83) 331.73 (331.50-331.86) 1.03
28672 1 7.80 (7.78-7.84) 8.09 (8.09-8.17) 1.04
28672 64 8.27 (8.26-8.30) 8.57 (8.57-8.60) 1.04
28672 256 11.79 (11.60-11.87) 12.08 (12.05-12.10) 1.02
28672 1024 46.39 (45.38-46.65) 47.92 (46.96-48.27) 1.03
28672 4096 169.09 (168.03-189.13) 173.74 (173.08-174.28) 1.03
28672 16384 711.75 (705.92-819.50) 719.22 (717.55-720.97) 1.01

At M <= 256 the cost is +0.15-0.5 us per call (2-6%), from the scale load and the clamp. At M >= 512 the medians differ by -4% to +4%, mostly +1-4%. Main's own min-max range at those sizes spans up to 25% (d=14336, M=8192: 162.6-202.8 us).

Behavior change: a CPU scale now raises scale must be a single fp32 element on the GPU. Before, it worked because the launcher read the scale on the host.

Not tested:

  • gfx950/MI355. Compile only.
  • Real workloads. There is no in-tree caller in either vLLM or AITER (only op_tests), so there is no end-to-end or gsm8k run.

Submission Checklist

AndreasKaratzas and others added 2 commits October 16, 2026 02:14
…, row bounds

scaled_act_and_mul_kernel had three defects:

- The scale was dereferenced on the host at launch. Under CUDA/HIP graph
  capture its value was frozen, and a scale written by a still-running
  kernel on the same stream was read stale. Pass the pointer and compute
  1/scale in the kernel.
- Values beyond the output range were converted unclamped, and
  v_cvt_pk_fp8_f32 turns |x| >= 248 into NaN on gfx942. Saturate to the
  output type's finite range (fp8 or int8) before the cast.
- When d was not a multiple of the vector size, the last vector store of
  each row spilled into the next row (and past the end of the tensor for
  the last row), and rows were assumed to be packed. Store the partial
  last vector element-wise and honour the input/output row strides.

Also check the scale/out arguments, and add a regression test.

Co-Authored-By: Claude <[email protected]>
Signed-off-by: Andreas Karatzas <[email protected]>
Fill the guard row and padding columns with 0x5A instead of zeros so an
out-of-bounds fp8 zero write is caught, and check the guard before the
value asserts so an overrun is reported as such.

Co-Authored-By: Claude <[email protected]>
Signed-off-by: Andreas Karatzas <[email protected]>
@AndreasKaratzas
AndreasKaratzas requested a review from a team October 17, 2026 04:52
@github-actions github-actions Bot added the HIP label Oct 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 6641 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@ChuanLi1101 ChuanLi1101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scale is read on the device, so a CUDA graph replay sees a new scale. Values past the fp8 range are clamped before the cast, so they stay in range. The tail of a row is stored one element at a time, so a short last vector no longer writes into the next row. A scale on the CPU now raises. Keep it on the GPU.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants