Repository navigation
[HIP] [Bugfix] scaled_silu_and_mul: device-side scale, fp8 saturation, row bounds - #6641
Conversation
…, 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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
ChuanLi1101
left a comment
There was a problem hiding this comment.
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.
Motivation
aiter.scaled_silu_and_mulhas three independent bugs:v_cvt_pk_fp8_f32turns |x| >= 248 into 0x80, which is NaN in e4m3fnuz, instead of saturating the way.to(float8_e4m3fnuz)and AITER's quant kernels do.dis 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:
Technical Details
Root causes in
csrc/kernels/activation_kernels.cuon 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 withopus::cast<DTYPE_O>without clamping.423-428: the output buffer range isdrounded up to a dword, and the loop stores whole vectors up tod, so the tail vector writes past the row.401-402and423also assume packed rows (token_idx * 2 * d,token_idx * d).Changes, all in
scaled_act_and_mul_kerneland its launcher:1 / *scalein the kernel.fmed3f) before the cast. This covers fp8, and int8 with its own bounds.d / VEC_SIZE * VEC_SIZE, and store the tail element by element.outhas shape[..., d], and thatscaleis a single fp32 element on the GPU.test_scaled_silu_and_mul_edge_casestoop_tests/test_activation.py.The unscaled
silu_and_mulhas the same row overrun for oddd(bf16d= 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_activationrebuilt.activation_kernels.cuis identical in v0.1.25 and main. The file also compiles for gfx950, gfx1100 and gfx1201.Correctness suite, 864 cases:
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 wrongoutshape.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
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:
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
scalenow raisesscale must be a single fp32 element on the GPU. Before, it worked because the launcher read the scale on the host.Not tested:
op_tests), so there is no end-to-end or gsm8k run.Submission Checklist