Repository navigation
Description
Kernel audit at main @ 8f2efe5, RTX 4090 (sm_89), with cross-device runs on two GPUs.
Method: each kernel compared against an fp64 dequant-then-matmul reference.
- M ∈ {0, 1, 2, 7, 16, 33, 64, 255, 1024}
- K/N tails, act-order, sym/asym, fp16/bf16, bias, 3-D and non-contiguous inputs
- cuda:1 while cuda:0 is current
Clean:
- Numerics pass: AllSpark GPTQ/AWQ (incl. M×N > 2³¹), VPTQ (all codebook options), NF12, ParoQuant CUDA/Triton, GGUF Triton/ggml (Q4_0/Q8_0/Q4_K/Q5_K/Q6_K), QQQ-Triton (bit-exact), NVFP4 helpers.
- Source review only: Swordfish (needs sm100/103/110) and Machete (needs SM90).
1. Non-contiguous activations: silently wrong output or crash
The usual trigger is hidden_states[:, -1:, :] with batch > 1, which every batched generate step produces: reshape(-1, K) then returns a strided view.
| kernel | where | result |
|---|---|---|
| BitBLAS (fp16) | nn_modules/qlinear/bitblas.py:865 x.reshape(-1, K), then _invoke_bitblas_matmul (:889-899) passes data_ptr() via ctypes with no stride handling |
rel err 1.30 vs 2.2e-4 after .contiguous(); column slices and strided 2-D give 0.9-1.6 for every bits/group/sym config. bf16 is unaffected (its .to(fp16) copies) |
| EXL3 (fp16) | exllamav3/modules/quant/exl3.py:135 x.view(-1, in_features); had_r_128 (exllamav3/quant/hadamard.cu:100-106), hgemm (hgemm.cu:29-36) and the bc GEMM assume packed rows |
b=4, s=7: rel err 1.36 (0.0006 after .contiguous()); b=8, s=40: 1.22; b=40, s=3 (hgemm): 1.20. #3740 fixed only bf16/fp32 |
| ParoQuant CUDA + Triton (default gs=128, krot 1/8) | paroquant.py _rotate_inputs (:345-378) → apply_paroquant_rotation feeds a strided x_flat to the fused op |
RuntimeError: ParoQuant rotation launch config expects contiguous floating-point input (gptqmodel_ext/paroquant/rotation.cu:1016, 1066) |
Fix: .reshape(-1, K).contiguous() in each, plus TORCH_CHECK(x.is_contiguous()) in the EXL3 kernels. Machete's forward probably has the same problem (unverified, no SM90).
2. GGUF on multiple GPUs and streams
- Triton kernels crash when the module is on a non-current GPU (high):
gguf_triton.py_launch/_launch_with_meta(~L979-1020) and the cast/restore launches inforward(~L2080-2120) have no device guard. With module and input on cuda:1 while cuda:0 is current, Q4_K, Q5_K, Q6_K and Q1_0 all raiseValueError: Pointer argument cannot be accessed from Triton, which breaks multi-GPUdevice_map. Fix:with torch.cuda.device(x.device); the W4A Triton file's_launch_devicehelper already does this. - ggml CUDA path changes the process's current GPU (medium):
gguf_cpp.py~L429-444, ~L771-816. After one forward on cuda:1,torch.cuda.current_device()goes from 0 to 1 and stays there, so latertorch.empty(device="cuda")lands on the wrong GPU. Fix: save and restore the current device. - ggml CUDA path isn't ordered with PyTorch's current stream (high when it triggers):
run_quantized_matmul_cuda_plan(~L694-728) copies the input fromx.data_ptr()on ggml's own stream with no event or sync. Undertorch.cuda.stream(s), 10/10 runs give wrong results (0/10 on the default stream). Fix: make ggml's stream wait on an event recorded on the current stream, or sync before the copy.
3. Index overflow and launch limits on very large inputs (medium)
-
Triton int32 offsets: illegal memory access once rows×K or rows×N > 2³¹ (it passes just below):
- qqq_triton
_quantize_rows(X + row*K) - w4a_triton
_token_fp8_quant,_grouped_fp8_gemm - gguf_triton
_gguf_cast_wide_input_kernel - the GGUF Q4_K/Q6_K fused matmuls (
offs_m*stride_xm, ~L726, and the output offsets)
Fix:
.to(tl.int64)on the row index before multiplying. - qqq_triton
-
EXL3 Hadamard 32-bit offset:
exllamav3/quant/hadamard.cu:19-20, 34-35, 53-58, 76-81computes the offset in unsigned 32-bit and wraps at rows×cols = 2³² (e.g. K=4096 with M ≥ 2²⁰). Rows past the wrap are garbage (rel 1.0 vs 0.0037). Fix: usesize_t. -
tt_blockfloat fp32 linear:
tt_blockfloat_cuda.cu:904(launch_linear_f32) sets grid y = ceil(m/32) > 65535 with no row loop, so m=2,097,152 fails withinvalid argument. The fused fallback at L982 fails at ~8.4M rows. fp16 is fine.
4. Hadamard ext crashes on empty input (medium)
All five ops in gptqmodel_ext/hadamard/hadamard.cpp launch a zero-size grid and raise CUDA error: invalid argument at M=0. The torch fallback (quantization/rotation/hadamard_utils.py:141) also fails, because reshape(..., -1, 2, stride) is ambiguous with 0 elements.
This is reached from:
NF12Linear._forward_impl(nf12.py:451-455), which rotates before its empty-rows checktorch.py:448,tritonv2.py:161/166,hooked_linear.py:138
Fix: return early on numel() == 0 in the bindings and in apply_online_hadamard, and move NF12's empty check before the rotation.
5. A full Triton recompile for every new token count (performance)
- GGUF:
_gguf_restore_wide_output_kerneldeclaresNandELEMENTS=M*Nastl.constexpr, and it runs on every bf16 GGUF forward without bias. The first call at a new M costs 40-340 ms vs 2.3 ms warm. - W4A:
_grouped_fp8_gemmdeclaresMconstexpr (0.36-0.74 s per new M). The NVFP4 kernels takeM/PADDED_Mas constexpr too.
Fix: pass them as runtime ints.
Unverified:
- Machete and EXL3 fp16 add the bias after rounding (same class as [BUG] Inference fp16 overflow: QQQ & BnB-int8 route bf16 through fp16 (NaN), several kernels add bias after fp16 rounding (inf) #3977 / [BUG] Follow-up on #3999/#4001: stale uniform-group flag after load_state_dict (M=2..64 wrong), TorchFused GPTQ still overflows, bias forces 7x (ExLlamaV2) and 2.6-21x (CPU) slower paths, AwqExLlamaV2+LoRA returns fp32 #4028).
- BitBLAS 8-bit g=-1 segfaults inside BitBLAS's TVM parser on this toolchain (likely environmental).
- The ggml CUDA path doesn't check input width against
in_features.
🤖 Generated with Claude Code
Activity
fix(kernels): pack strided activations, bind devices, widen offsets, …