Skip to content

[BUG] Kernels: non-contiguous input wrong/crash (BitBLAS, EXL3, ParoQuant), GGUF multi-GPU crash + current-device leak + stream race, int32 overflow, Hadamard M=0 crash, per-M Triton recompiles #4042

Closed

Description

@Qubitium

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 in forward (~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 raise ValueError: Pointer argument cannot be accessed from Triton, which breaks multi-GPU device_map. Fix: with torch.cuda.device(x.device); the W4A Triton file's _launch_device helper 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 later torch.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 from x.data_ptr() on ggml's own stream with no event or sync. Under torch.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.

  • EXL3 Hadamard 32-bit offset: exllamav3/quant/hadamard.cu:19-20, 34-35, 53-58, 76-81 computes 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: use size_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 with invalid 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 check
  • torch.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_kernel declares N and ELEMENTS=M*N as tl.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_gemm declares M constexpr (0.36-0.74 s per new M). The NVFP4 kernels take M / PADDED_M as constexpr too.

Fix: pass them as runtime ints.

Unverified:

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions