Skip to content

fix(kernels): give the V tensor its own row pitch in the fp8 KV store - #1

Merged
ArqAlice merged 1 commit into
ArqAlice:feat/fp8-quantizationfrom
MT-z:fix/kv-fp8-vstore-pitch
Sep 4, 2026
Merged

fix(kernels): give the V tensor its own row pitch in the fp8 KV store#1
ArqAlice merged 1 commit into
ArqAlice:feat/fp8-quantizationfrom
MT-z:fix/kv-fp8-vstore-pitch

Conversation

@MT-z

@MT-z MT-z commented Sep 4, 2026

Copy link
Copy Markdown

The fix offered in
FlashML-org#354 (comment) -- root cause,
mechanism and the severity reading are in that comment, along with @Kaempferia's independent
reproduction on sm_120.

quantize_kv_to_cache passed k.stride(0) as the source row pitch for both tensors and the
guard above it checks only the inner strides, so V is read at K's pitch. This passes
v.stride(0) as its own argument and loads each tensor with its own pitch.

The test that covers it already exists on this branch and flips with the change, so no new
test is added:
tests/kernels/test_kv_fp8.py::test_codes_match_the_reference_quantizer_and_reconstruction_is_close
(2684 of 3072 elements mismatched in V before, 0 after).

Tested on: RTX 4090 (sm_89), Intel i9-14900KF, driver 595.84, CUDA 13.3, torch 2.11.0+cu130,
triton 3.6.0. No checkpoint -- the test builds its tensors with torch.randn.

python -m pytest tests/kernels/test_kv_fp8.py -q     # and the other 6 files, each on its own

Re-measured today directly on this branch (03fb043), the seven *fp8* /
test_triton_attention.py files, one file per process, since one test leaves a sticky
device-side assert that otherwise inflates the count:

11 failures before, 10 after. The only one that changes state is the test named above;
the remaining 10 are the items from my first comment and this patch does not touch them.

(My earlier report said 12 -> 11. That run was on a branch I had built as "main at 6eca2d7 +
this PR", which is four commits behind the base this PR actually sits on, and one of those
four fixes a test. The delta and the flipping test are the same either way.)

compute-sanitizer --tool initcheck on the patched build: ERROR SUMMARY: 0 errors.

Assisted-by: Claude Opus 5

`quantize_kv_to_cache` passed `k.stride(0)` as the only source pitch and
`_kv_quant_scatter_kernel` used it for both tensors:

    src = t * stride_xs + h * D + d
    xk = tl.load(k_src + src, ...)
    xv = tl.load(v_src + src, ...)

The guard above it checks only the inner stride (`k.stride(1) == 1 and
v.stride(1) == 1`), never `k.stride(0) == v.stride(0)`, so the kernel carries an
undocumented contract: K and V must share one row pitch.

When they do not, V is read at K's pitch. In the failing test K is a view of the
qkv slice (pitch 1152) while V is materialised by `.clamp()` (pitch 384), so with
8 tokens of 3072 elements:

    token 0     reads 0                 correct by coincidence
    token 1-2   reads 1152, 2304        in range, WRONG rows
    token 3-7   reads 3456 .. 8064      past the initialised data

2684 of 3072 codes wrong, all in V, K byte-perfect. Deterministic addressing;
only the contents of the uninitialised tail vary with allocator history, which is
why the mismatch count drifts (2684 / 2663 / 2676 across runs) while the
mismatching positions do not -- the in-range half is exactly 764 every time.

Found with `compute-sanitizer --tool initcheck` (TRITON_DISABLE_LINE_INFO=0),
which named `kv_quant.py:110`. `memcheck` reports 0 errors because PyTorch's
caching allocator rounds allocations up and the bad read stays inside the pooled
segment; `racecheck` reports 0 hazards because it is not a race.

Fix: pass `v.stride(0)` as its own kernel argument and load each tensor with its
own pitch.

Verified on RTX 4090 (sm_89): tests/kernels/test_kv_fp8.py 2 failed -> 1 failed,
the flip being test_codes_match_the_reference_quantizer_and_reconstruction_is_close;
five consecutive standalone runs give K 0/3072 and V 0/3072 with got.sort() ==
exp.sort(); compute-sanitizer initcheck reports 0 errors on the patched build.
Independently confirmed on RTX 5090 D (sm_120) by @Kaempferia: same single flip,
same multiset property, 5 runs clean.

Note for reviewers: the test's SECOND assertion (dequantised error <= 0.08)
passes at 0.035 while V is wrong, so a reconstruction-level check does not catch
this class. Only the exact-code assertion does.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.

2 participants