llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

CUDA: mul_mat is not batch-invariant for IQ quants at n=5 and n=8 on SM120

Open cuhawk opened this issue 3 months ago • 5 comments

Summary

On SM120, mul_mat with IQ quants is not batch-invariant for n of 5 and 8: evaluating n columns in one call gives a bitwise different result from n calls of one column. n of 1 through 4 agree.

This matters for speculative decoding, which is lossless under greedy sampling by construction — the target model ratifies every drafted token — so the verification pass must agree with what one-token decoding would have computed. Where it does not, enabling speculation changes the output.

This is separate from #28108, which covers the same class of problem in flash attention. This one I have not fixed.

Reproduction

The test below is not in the tree; it is the one I used, and I am happy to open it as a PR if that is useful — I have kept it out of #28108 deliberately, since a test that fails on master would turn CI red with no accompanying fix.

It builds one batched ggml_mul_mat and a serial chain of single-column ggml_mul_mat calls over the same weights, concatenates both, and compares with memcmp at max_err() = 0. Shapes are Qwen3.8 dense projections.

MUL_MAT_BATCH_INVARIANCE(type_a=iq3_s,m=17408,n=1,k=5120): OK
MUL_MAT_BATCH_INVARIANCE(type_a=iq4_xs,m=10240,n=1,k=5120): OK
MUL_MAT_BATCH_INVARIANCE(type_a=iq4_xs,m=5120,n=1,k=17408): OK
... n=2, n=3, n=4 all OK ...
MUL_MAT_BATCH_INVARIANCE(type_a=iq3_s,m=17408,n=5,k=5120): FAIL
MUL_MAT_BATCH_INVARIANCE(type_a=iq4_xs,m=10240,n=5,k=5120): FAIL
MUL_MAT_BATCH_INVARIANCE(type_a=iq4_xs,m=5120,n=5,k=17408): FAIL
MUL_MAT_BATCH_INVARIANCE(type_a=iq3_s,m=17408,n=8,k=5120): FAIL
MUL_MAT_BATCH_INVARIANCE(type_a=iq4_xs,m=10240,n=8,k=5120): FAIL
MUL_MAT_BATCH_INVARIANCE(type_a=iq4_xs,m=5120,n=8,k=17408): FAIL
12/18 tests passed

All three geometries fail at exactly n=5 and n=8, and ERR is 1.0 against a tolerance of 0, i.e. the results differ at all rather than by a small amount.

What I checked

The obvious suspect is the MMVQ to MMQ crossover, so I varied it with the runtime override. It is not the cause, and lowering the threshold makes things worse rather than better:

GGML_CUDA_MMVQ_MAX equivalent result
default 12/18
4 12/18, unchanged
1 3/18

Lowering it routes the batched side to MMQ while the serial reference stays on MMVQ, so the two sides run different kernels and disagree everywhere. That suggests the divergence at n=5/n=8 is within MMVQ itself — a per-ncols specialization changing accumulation order — rather than a kernel switch.

Worth noting alongside #26079, which added per-hardware tuning of that crossover for throughput: the crossover point also determines this correctness property, and nothing currently tests it.

Environment

  • RTX 5080 Laptop, SM120, CMAKE_CUDA_ARCHITECTURES=120a-real, Release, CUDA 12.9, Windows / MSVC
  • master at 41ef91f7c, unmodified apart from the added test
  • Reproduces identically with and without the flash-attention change in #28108, so the two are independent

cuhawk avatar Aug 31 '26 18:08 cuhawk

SYCL has a similar issue with MMVQ and MMQ.

mndodd avatar Sep 01 '26 01:09 mndodd

Please open a PR for MUL_MAT_BATCH_INVARIANCE. I'm tracking down the issue on the SYCL side.

mndodd avatar Sep 01 '26 13:09 mndodd

I retested this on the current master (207bdab950) and the issue is still reproducible on an RTX 5090 (SM 120).

By doing the same serial-vs-batched MUL_MAT invariance test, I get:

IQ3_S, m=17408, k=5120: n=1..4 PASS, n=5..8 FAIL IQ4_XS, m=10240, k=5120: n=1..4 PASS, n=5..8 FAIL IQ4_XS, m=5120, k=17408: n=1..4 PASS, n=5..8 FAIL

So we still observe that the batch-invariance failure starts at n >= 5. Forcing the number of warps at 2 or 4 irrespective of n fixes the issue (restores invariance) but it affects the performance of this kernel. So I don't yet have a root cause to address both the invariance and performance at the same time but still continuing to investigate.

siavashnorouzi avatar Oct 02 '26 06:10 siavashnorouzi

I tried to recreate this issue on the latest git build and it still reproduces on my RTX 5060ti 8gb card. (M=10240, K=5120)

The solution of just forcing generic's (blackwell) nwarp count to 2 does work without a penalty (but i have not tested it in much detail to identify some hidden costs that may occur with some varying kernel sizes)

I also tried emulating a nwarp count 4 tmp_shared reduction tree when nwarp count was 2, to test a common reduction tree, it works! but with a penalty (though it is be my implementation that is causing the slowdown)

I am trying restructure the loop to derive a reference tree width from calc_nwarps. So that every reduction at any nwarp gets a same reduction tree in mmvq.

I will write and show results (for any penalty too) for a "canonical" reduction tree if i am able to make it happen.

Incorrectone avatar Oct 17 '26 20:10 Incorrectone

Following the earlier Blackwell results, I investigated whether a similar approach would work on a GTX 1650 (SM75, CUDA 13.2).

On master (22b99122), I added a temporary diagnostic switch that pins MMVQ nwarps to each quantization type's N = 1 value: 2 for Q4_K/Q5_K and 4 for IQ3_S/IQ4_XS/Q8_0.

The initial test grid covered these five types, M = 256/1024, K = 1024/4096/5120 and N = 1–10, with additional chunked checks extending to N = 512.

Pinning nwarps alone was sufficient to make N = 2–8 bit-exact against serial execution across the tested shapes. I did not need to pin rows_per_block, small_k or halve_iters.

The N = 1 output remained bit-identical to default decode.

The remaining mismatches at N >= 9 came from the MMVQ-to-MMQ dispatch boundary. Splitting src1 into MMVQ calls of at most eight columns also produced bit-exact results through N = 512 for the tested shapes.

These tests covered plain MUL_MAT only, not MUL_MAT_ID, fused paths or flash attention.

Performance

At M = K = 4096, fixed-warp MMVQ took 0.96–1.08x the default execution time within the MMVQ dispatch range (N <= 8). The largest overhead was approximately 8% for IQ4_XS, while the K-quants were approximately 3–4% faster at N = 2–4.

I also measured chunked fixed-warp MMVQ at larger batch sizes. The following are execution-time ratios relative to default MMQ (lower is better):

Type N=16 N=32 N=64 N=128 N=256 N=512
IQ3_S 1.13 1.21 1.29 1.34 1.25 1.26
IQ4_XS 0.88 0.95 0.98 1.00 0.94 0.94
Q4_K 0.83 0.89 0.92 0.95 0.89 0.89
Q5_K 0.84 0.92 0.96 0.98 0.92 0.92
Q8_0 0.82 0.92 0.99 1.02 0.95 0.95

Measurements were interleaved with one warmup and three measured rounds per arm, on AC power, with the GPU at P0 and a 6120 MHz memory clock. Spread was at most 0.9% except for IQ3_S at N = 16 (5%).

IQ3_S was the notable exception, consistent with its existing iq_slow_turing classification. The other four types showed no more than 2% overhead relative to default MMQ at any measured batch size through N = 512.

Compared with forced MMQ in the #32081 build, chunked MMVQ took 0.68–0.94x the execution time across the tested prefill sizes and types.

For example, Q4_K at N = 512:

Configuration Execution time
Default MMQ 14.8 ms
Chunked fixed-warp MMVQ 13.1 ms
Forced MMQ (#32081) 17.1 ms

Limitations

These performance results are hardware-specific. The GTX 1650 lacks tensor cores but still takes the Turing MMQ path according to the dispatch code. I would expect the relative performance to differ on tensor-core-equipped GPUs, particularly at larger batch sizes, but I have not tested those devices.

The main findings on this hardware are that pinning nwarps alone was sufficient within the MMVQ path, that the remaining divergence could be addressed by avoiding the MMQ dispatch boundary, and that this preserved the existing serial decode output with relatively little overhead for four of the five tested quantization types.

This was a diagnostic experiment rather than a proposed implementation. I can share the diagnostic diff and benchmark details if useful.

mathesonlj16-eng avatar Nov 24 '26 23:11 mathesonlj16-eng