Skip to content

[BUG] AWQ GEMV (gemmv2, rows>8) and GEMV_FAST prefill CUDA kernels index the output with 32-bit int: illegal memory access once rows x out_features > 2^31 (long-context prefill) #5091

Closed

Description

@Qubitium

Found on main @ e75181d, RTX 4090 (sm_89), torch 2.15 + CUDA 13.2.

What is wrong

Two AWQ CUDA kernels compute the output element offset as a 32-bit int. Once the output has more than 2^31 elements (rows x out_features), the store wraps to a negative offset. Both kernels then fault with cudaErrorIllegalAddress, which is sticky: the CUDA context is dead, and so is the rest of the process.

  • AWQ_GEMV, rows > 8 (AwqGEMVLinear.forward -> awq_gemmv2_forward): gptqmodel_ext/awq/quantization/gemm_cuda_gen.cu:780, *(C_ptr + ax1_0 * 16 + row_offset * OC + ...). Both row_offset and OC are int. Only the split-K base (line 620) is promoted to long long.
  • AWQ_GEMV_FAST prefill (awq_fast_gemm_forward_prefill): gptqmodel_ext/awq/quantization_new/gemm/gemm_cuda.cu:551, int index = write_row * N + ..., and :920, C + write_row * N + ..., in gemm_w4a16_T1 / gemm_w4a16_T2.

The other AWQ and GPTQ CUDA kernels handle the same shapes correctly: AWQ GEMM, GEMM Triton, Marlin, ExLlamaV2, Torch, and GPTQ Marlin/ExLlamaV2/Triton/Torch all pass at 1.07 x 2^31 output elements.

The threshold is exact. The input stays small (K=256, so the input is 64 MiB):

kernel M x N M*N result
AWQ_GEMV / GEMV_FAST 262144 x 8192 2^31 OK (rel 3.7e-4)
AWQ_GEMV / GEMV_FAST 262200 x 8192 2^31 + 458752 illegal memory access
AWQ_GEMV / GEMV_FAST 130048 x 16512 2^31 - 131072 OK
AWQ_GEMV / GEMV_FAST 130112 x 16512 2^31 + 925696 illegal memory access
AWQ GEMM / Marlin / ExLlamaV2 / Torch 140000 x 16384 1.07 x 2^31 OK

Realistic trigger: a single long-context prefill through an MLP projection without chunking. For Qwen2.5-7B (intermediate 18944), 113,400 tokens is enough. For a Llama-3 MLP (14336), 149,800 tokens is enough. Both are inside the advertised 128K context. The scheduler does not split M for these kernels.

Repro

import torch
from gptqmodel.nn_modules.qlinear.gemv_awq import AwqGEMVLinear   # same with gemv_fast_awq.AwqGEMVFastLinear
K, N = 256, 8192
# m = AwqGEMVLinear(bits=4, group_size=128, sym=False, desc_act=False, in_features=K, out_features=N,
#                   bias=True, pack_dtype=torch.int32, dtype=torch.float16) packed from a dense nn.Linear, .cuda(), post_init()
for M in (262144, 262200):
    x = torch.randn(M, K, device="cuda", dtype=torch.float16)
    with torch.inference_mode():
        y = m(x); torch.cuda.synchronize()
    print(M, M * N > 2**31, "ok")
OK  awq:gemv float16 M=262144 K=256 N=8192 shape=(262144, 8192) rel_sampled=3.66e-04
ERR awq:gemv float16 M=262200 K=256 N=8192 AcceleratorError CUDA error: an illegal memory access was encountered
OK  awq:gemv_fast float16 M=262144 ...
ERR awq:gemv_fast float16 M=262200 ... AcceleratorError CUDA error: an illegal memory access was encountered

Each case runs in its own process, because the error poisons the context.

Suggested fix

Compute the store offsets in 64-bit, for example static_cast<int64_t>(row_offset) * OC and int64_t index = int64_t(write_row) * N + .... Do the same for the A-side row offsets (row * IC) in these kernels, which wrap once rows x in_features exceeds 2^31. #4042 applied this fix to the Triton kernels. Alternatively, have the Python wrappers split M into chunks below 2^31 / N rows.

Tests that should have caught it

Kernel sweeps stop at M=2048. A guarded large-shape test (rows x N just above 2^31, small K) for every CUDA GEMM entry point would catch this class. The #4042 report covered the Triton kernels only.

Severity

Medium. It is a hard crash that takes down the whole CUDA context, on a long but supported prefill. No wrong output escapes silently.

🤖 Generated with Claude Code

Activity

  1. ZX-ModelCloud commented on Nov 18, 2026

    @ZX-ModelCloud
    Collaborator

    Agent claim released: agent-283140e5a13d finished this issue.

    Pull request: #5149

    Assigned to this account; user directed the fix. Reproducing the AWQ GEMV/GEMV_FAST 32-bit output-offset overflow on CUDA and preparing a guarded large-shape fix

  2. added a commit that references this issue on Nov 18, 2026
    8585be2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions