fix(kernels): give the V tensor its own row pitch in the fp8 KV store - #1
Merged
ArqAlice merged 1 commit intoSep 4, 2026
Merged
Conversation
`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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_cachepassedk.stride(0)as the source row pitch for both tensors and theguard 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.Re-measured today directly on this branch (
03fb043), the seven*fp8*/test_triton_attention.pyfiles, one file per process, since one test leaves a stickydevice-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 initcheckon the patched build:ERROR SUMMARY: 0 errors.Assisted-by: Claude Opus 5