Skip to content

fix(awq): address wide-prefill output and activation offsets in 64 bits - #5149

Merged
Qubitium merged 1 commit into
mainfrom
fix/5091-awq-gemv-int64
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5091-awq-gemv-int64

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Summary

The AWQ GEMV (gemmv2), GEMV_FAST prefill and split-K reduce kernels computed
several element offsets as 32-bit products. Once one long-context prefill
produced more than 2^31 output elements (rows x out_features), or the
activation itself passed rows x in_features > 2^31, the store wrapped to a
negative element offset.

Confirmed on current main before the fix (each case in its own process, since
cudaErrorIllegalAddress is sticky):

kernel shape result before
gemmv2_forward (split_k 1, 2) 262200 x 8192 cudaErrorIllegalAddress
gemmv2_forward (split_k 1) 262200 x 8192, fp16 tail rows unwritten (illegal address from the same store in other configs)
awq_fast_gemm_forward_prefill 262200 x 8192 cudaErrorIllegalAddress
gemmv2_forward K-side rows=262145, K=8192, N=128 cudaErrorIllegalAddress / wrong activation rows
gemm_forward fused split-K reduce 262200 x 8192 cudaErrorIllegalAddress

The realistic trigger is a single long-context prefill through one MLP
projection: for Qwen2.5-7B (intermediate 18944) 113,400 tokens suffice, which is
inside the advertised 128K context.

What Changed

  • gptqmodel_ext/awq/quantization/gemm_cuda_gen.cu
    • gemmv2_forward_4bit_cuda_m128n64k32: C store and A row offset in 64-bit.
    • gemm_forward_4bit_cuda_m16n128k32 / m16n64k32: A row offset and C store.
    • group_gemm_forward_4bit_cuda_m16nXk32: token_id * OC store offset.
    • reduce_splitk_fp32_to_output_kernel and
      fused_reduce_splitk_fp32_to_output: total_elements, idx and stride
      are 64-bit, so the fused reduce addresses the right partial above 2^31.
  • gptqmodel_ext/awq/quantization_new/gemm/gemm_cuda.cu
    • gemm_w4a16_T1 / gemm_w4a16_T2: A tile loads
      ((ld_row + cta_offset_m) * K) and the C store (write_row * N).

Shapes below 2^31 keep identical arithmetic, dispatch and numerics; the change
only widens the products. No API, configuration or storage change. Sibling
paths already correct above 2^31 were left alone: gemm_forward at
split_k_iters = 1 (the split-K partial base was already 64-bit) and the
non-AWQ backends. Out of scope: chunking M in the Python wrappers — the issue
offers that as an alternative, and widening the offsets keeps the kernels usable
at full prefill length.

Fixes #5091

Tests

New regressions build rows x out_features just above 2^31 with a small K
(256) and only the final rows nonzero, so each case stays inside a single
output allocation while still crossing the boundary. Oracles are the existing
independent float64 dequantize-then-matmul references (GEMV/GEMV_FAST) and the
repo's dequantize_gemm dense reference (fused reduce).

Red evidence, each node run in its own process against the unpatched base:

python -m pytest "tests/kernels/test_awq_gemv_splitk.py::test_gemmv2_stores_every_row_above_int32_element_count[1]" -q
# 1 failed - torch.AcceleratorError: CUDA error: an illegal memory access was encountered
python -m pytest "tests/kernels/test_awq_gemv_splitk.py::test_gemmv2_stores_every_row_above_int32_element_count[2]" -q
# 1 failed - torch.AcceleratorError: CUDA error: an illegal memory access was encountered
python -m pytest "tests/kernels/test_awq_gemv_splitk.py::test_gemv_fast_prefill_stores_every_row_above_int32_element_count" -q
# 1 failed - torch.AcceleratorError: CUDA error: an illegal memory access was encountered
python -m pytest "tests/kernels/test_awq_gemv_splitk.py::test_gemmv2_reads_activations_above_int32_row_stride_product" -q
# 1 failed - torch.AcceleratorError: CUDA error: an illegal memory access was encountered
python -m pytest "tests/kernels/test_awq_cuda_fp32_reduce.py::test_awq_cuda_fused_splitk_reduce_indexes_above_int32_element_count" -q
# 1 failed - torch.AcceleratorError: CUDA error: an illegal memory access was encountered

Green with the fix, same nodes, same device:

python -m pytest tests/kernels/test_awq_gemv_splitk.py -q
# 108 passed
python -m pytest tests/kernels/test_awq_cuda_fp32_reduce.py -q
# 7 passed
python -m pytest tests/kernels/test_awq_gemv_splitk.py tests/kernels/test_awq_cuda_fp32_reduce.py -q
# 115 passed
bash format/check.sh
# ruff 0.14.2 / All checks passed!

Extra manual probes at the issue's shapes (262200 x 8192 and 130112 x 16512,
plus 262144 x 8192 and 130048 x 16512 as controls) show relative error
2.7e-4..3.6e-4 against the float64 oracle for both gemmv2 and GEMV_FAST, and
2.1e-4 for the fused-reduce GEMM.

Skips: none in the runs above. Only NVIDIA sm_89 was qualified. The same
widened products serve the ROCm build, but the GEMV/GEMV_FAST paths are
CUDA-only (AWQ_GEMV_ROCM_UNSUPPORTED) and the AWQ GEMM was not re-run on AMD
hardware here.

Model Support / Module Plan

Not applicable: no model type, alias, definition, module plan or fusion
eligibility change.

The AWQ GEMV, GEMV_FAST prefill and split-K reduce kernels computed several
element offsets as 32-bit products. Once a single long-context prefill produced
more than 2^31 output elements (rows x out_features), or the activation itself
passed rows x in_features > 2^31, the store wrapped to a negative offset:

- gemmv2_forward_4bit_cuda_m128n64k32 stored with row_offset * OC; the sm_80+
  build silently dropped the tail rows, older arch builds faulted with
  cudaErrorIllegalAddress.
- gemm_w4a16_T2 (GEMV_FAST prefill) and gemm_w4a16_T1 (split-K) stored with
  write_row * N, and read the A tile with (row + cta_offset_m) * K.
- gemm_forward_4bit_cuda_m16n{128,64}k32 shared the same A and C products.
- group_gemm_forward_4bit_cuda_m16nXk32 indexed C with token_id * OC.
- fused_reduce_splitk_fp32_to_output narrowed result.numel() to int and walked
  the partials with 32-bit idx/stride, so the fused reduce addressed the wrong
  elements above 2^31.

Widen every affected product to int64_t. Shapes below 2^31 keep identical
arithmetic and numerics.

Adds guarded regressions in tests/kernels/test_awq_gemv_splitk.py and
tests/kernels/test_awq_cuda_fp32_reduce.py that build rows x out_features just
above 2^31 with a small K and only the tail rows nonzero, so each test stays
inside one output allocation while still crossing the boundary.

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

Labels

None yet

Projects

None yet

2 participants