Skip to content

fix(awq): cover every split-K tile and bound gemmv2 k-tiles (#4054) - #4058

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4054-awqgemv
Nov 14, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4054-awqgemv

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Part of #4054

This PR fixes items 1 and 2 of #4054 only (the two AWQ CUDA kernel bugs). Items 3-7 (CPU AUTO group size, torch.compile addmm, W4AFP8 compile, device_map CPU entries, max_memory) are owned by a separate change.

TL;DR

Two native AWQ split-K kernels partitioned K incorrectly. gemm_w4a16_T1 (prefill, SPLITK=2) dropped tail K tiles when K/128 was odd, and gemmv2_forward_4bit_cuda_m128n64k32 trimmed its last split by comparing a tile index with a column count, reading past the activation row. Net diff: 2 kernel files, +18/-3; one new regression test file; one docs page. All 104 new tests pass after the fix and 47 of them fail on the old kernels.

1. The problem

Item 1 - prefill split-K drops K tiles for odd K/128

gemm_forward_cuda_prefill takes the M <= 32 branch with SPLITK = 2, CTA_K = 128 (gptqmodel_ext/awq/quantization_new/gemm/gemm_cuda.cu:957). The kernel derived its split offset from raw columns and its per-split tile count with integer division:

  • gemm_cuda.cu:341: int cta_offset_k = blockIdx_z * (K / SPLITK);
  • gemm_cuda.cu:351: int gemm_iters = (K + CTA_K - 1) / CTA_K / SPLITK;

For K = 896, ceil(896/128) = 7 tiles and 7/2 = 3 iterations, so each split computed 3 tiles. Two K ranges were never computed.

flowchart LR
    subgraph old["Old prefill split, K=896, SPLITK=2"]
        A["z=0: offset 0<br/>3 tiles: columns 0..383"]
        G["never computed<br/>columns 384..447 and 832..895"]
        B["z=1: offset 448<br/>3 tiles: columns 448..831"]
        A --> G --> B
    end
    classDef bad fill:#ffdddd,stroke:#cc0000,color:#000
    class G bad
Loading

AwqGEMVFastLinear declares SUPPORTS_IN_FEATURES_DIVISIBLE_BY = [64] and group_size = 128, so validation accepts the shape. gemv_fast_awq.py sends every prompt of <= 32 tokens, and any decode step with batch >= 8, to this kernel.

Item 2 - gemmv2 reads past the activation row

gemmv2_forward_4bit_cuda_m128n64k32 (gptqmodel_ext/awq/quantization/gemm_cuda_gen.cu:628) compared a 32-wide k-tile index against the column count:

int k_bound = make_divisible(IC / 32, split_k_iters);
if ((k_bound - 1) * 32 + blockIdx_z >= IC) k_bound -= 1;   // tile index vs columns

For IC = 896 and split_k_iters = 8, IC/32 = 28 tiles and k_bound = 4; the check never fires, so splits z = 4..7 processed tiles 28..31 past the row end.

flowchart TD
    A["k_bound = ceil((IC / 32) / split_k_iters)"] --> B["trim if (k_bound - 1) * 32 + blockIdx_z >= IC"]
    B --> C["compares tile index * 32 with column count IC"]
    C --> D["IC=896, split_k_iters=8: never trims"]
    D --> E["z=4..7 read tiles 28..31 past the row end"]
    classDef bad fill:#ffdddd,stroke:#cc0000,color:#000
    class E bad
Loading

The zero-padded scale row makes the over-read harmless while the following memory is finite, so the result is usually right by accident. When the memory after the input holds a non-finite value, 0 * NaN poisons the row. The sibling m16n128k32, m16n64k32, and MoE kernels already used the tile-count form; gemmv2 was the outlier.

2. The new flow

Prefill: partition whole tiles, last split takes the remainder

flowchart TD
    A["num_k_tiles = ceil(K / CTA_K)"] --> B["tiles_per_split = ceil(num_k_tiles / SPLITK)"]
    B --> C["k_tile_begin = min(z * tiles_per_split, num_k_tiles)"]
    C --> D["k_tile_end = min(k_tile_begin + tiles_per_split, num_k_tiles)"]
    D --> E["cta_offset_k = k_tile_begin * CTA_K<br/>gemm_iters = k_tile_end - k_tile_begin"]
    E --> F["SPLITK semaphore reduction<br/>last split rounds to output dtype"]
Loading

cta_offset_k is now always a multiple of CTA_K, so the group-scale index (cta_offset_k + iter * CTA_K) / G stays aligned with the loaded tile. A split that receives no tile runs zero iterations and contributes a zero partial, so the existing semaphore order is unchanged.

gemmv2: trim on tile count

flowchart TD
    A["k_bound = ceil((IC / 32) / split_k_iters)"] --> B["trim if (k_bound - 1) * split_k_iters + blockIdx_z >= IC / 32"]
    B --> C["compares tile index with tile count IC / 32"]
    C --> D["IC=896, split_k_iters=8: z=4..7 stop at tile 27"]
    D --> E["no read at or past column IC"]
Loading

The host requires num_in_channels % group_size == 0 with group_size 64 or 128, so IC/32 is an exact tile count and at most the last tile of a split is out of range.

3. Failure and ambiguity contract

Situation Before After
K/128 odd, M <= 32 prefill tail K ranges silently dropped every tile covered once
split receives no tile negative/zero iteration edge cases zero iterations, zero partial
IC/32 not divisible by split_k_iters last split reads past the row last split trimmed on tile count
memory after activation row is non-finite 0 * NaN poisons the output row never read

4. Change inventory

Area File +/- Purpose
Prefill split-K gptqmodel_ext/awq/quantization_new/gemm/gemm_cuda.cu +15/-2 tile-aligned split offsets and remainder-aware iteration count
Decode-batch bound gptqmodel_ext/awq/quantization/gemm_cuda_gen.cu +5/-1 compare the tile index with IC/32
Tests tests/kernels/test_awq_gemv_splitk.py new fp64 oracle + NaN-sentinel + real-model greedy decode
Docs docs/awq-kernel-splitk.md new contracts, before/after, measured numbers
Docs link README.md +1 link the new reference page

5. Tests / guarantees

Test Invariant
test_gemv_fast_prefill_computes_every_k_tile K in 896/1024/1152/1408/4864 and M in 1/7/8/16/32/33/100 match an fp64 oracle within 2e-3
test_gemv_fast_prefill_ignores_nonfinite_memory_after_input NaN in the storage after the activation rows never reaches the output
test_gemmv2_matches_fp64_oracle_across_k_tiles M > 8 matches an fp64 oracle across K
test_gemmv2_never_reads_past_the_activation_row NaN sentinel is never read, including split_k_iters values that do not divide IC/32
test_real_qwen25_awq_gemv_greedy_decode_stays_finite real Qwen2.5-0.5B (896/4864) quantized to FORMAT.GEMV decodes greedily with finite scores

The oracle dequantizes the packed codes, scales, and zero offsets in float64 and performs its own matmul; it never calls the kernel under test.

6. Validation

Hardware: NVIDIA RTX 4090 (Ada, sm_89), GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42; CUDA 13.0, torch 2.16.0.dev20261102+cu130. ROCm check: HIP_VISIBLE_DEVICES=4, torch 2.16.0.dev+rocm10.1.

Before the fix (kernels reverted, same test file):

$ ... -m pytest tests/kernels/test_awq_gemv_splitk.py -k "not real_qwen" -q
47 failed, 56 passed, 1 deselected

After the fix:

$ ... -m pytest tests/kernels/test_awq_gemv_splitk.py -k "not real_qwen" -q
103 passed, 1 deselected

$ ... -m pytest tests/kernels/test_awq_gemv_splitk.py -k "real_qwen" -q
1 passed, 103 deselected

$ ruff check gptqmodel docs/eora tests setup.py
All checks passed!

ROCm (native AWQ extension is CUDA-only, so every case skips cleanly):

$ HIP_VISIBLE_DEVICES=4 ... -m pytest tests/kernels/test_awq_gemv_splitk.py -q
104 skipped

Related suites:

$ ... -m pytest tests/kernels/test_awq_cuda_fp32_reduce.py tests/test_awq_gemv_fast_jit.py -q
6 passed, 6 failed

The 6 test_awq_gemv_fast_jit.py failures are pre-existing on origin/main (in_features: 32 must be divisible by [64]); I reproduced the identical 6 failures in a clean origin/main worktree.

$ ... -m pytest tests/test_awq_dequant_layouts.py tests/test_awq_affine_range.py tests/test_awq_cuda_contract.py -q
24 passed, 2 skipped

Measured numerics (fp64 oracle, N=128, group_size=128)

Item 1 relative error, prefill SPLITK=2:

K M=1 before M=7 before M=32 before M=33 before after (all M)
896 0.797 0.615 0.569 2.8e-4 2.8e-4 .. 3.3e-4
1024 2.6e-4 2.9e-4 2.8e-4 2.9e-4 2.6e-4 .. 2.9e-4
1152 0.611 0.609 0.606 2.9e-4 2.8e-4 .. 3.2e-4
1408 0.486 0.578 0.551 2.8e-4 2.8e-4 .. 2.9e-4
4864 3.0e-4 3.0e-4 2.9e-4 2.9e-4 2.9e-4 .. 3.0e-4

Item 2 NaN sentinel, M=9:

K non-finite before after
896 128 / 1152 0 / 1152
1152 128 / 1152 0 / 1152
1408 128 / 1152 0 / 1152
4864 0 / 1152 0 / 1152

7. Reviewer notes

  • Scope is items 1-2 only; the issue closes when the items 3-7 PR also lands.
  • The prefill fix keeps the SPLITK=2 launch for M <= 32; it does not fall back to SPLITK=1, so no shape becomes unusable and no validated model changes kernel.
  • gemmv2 matches the bound style already used by the sibling kernels.
  • The real-model test quantizes the local Qwen2.5-0.5B-Instruct (hidden 896 / intermediate 4864) to FORMAT.GEMV; it is marked slow/model and skips when the model is absent.
  • Reference page: docs/awq-kernel-splitk.md (linked from README.md).

Part of #4054 (items 1-2).

Item 1: gemm_w4a16_T1 (prefill, SPLITK=2, CTA_K=128) derived its split
offset from K/SPLITK and its per-split iteration count from
ceil(K/CTA_K)/SPLITK with integer division. For an odd K/128 tile count
(K=896, 1152, 1408) each split ran floor(tiles/2) tiles, leaving whole K
ranges uncomputed and producing ~0.5-0.8 relative error for M<=32.

Partition whole tiles instead: give each split ceil(tiles/SPLITK)
consecutive tiles and let the last split take the remainder. The offset is
now tile-aligned so group-scale indexing stays correct, and a split with no
tile runs zero iterations and contributes a zero partial.

Item 2: gemmv2_forward_4bit_cuda_m128n64k32 trimmed its last split with
(k_bound - 1) * 32 + blockIdx_z >= IC, comparing a 32-wide tile index with
the column count. For IC=896 and split_k_iters=8 splits 4..7 read tiles
28..31 past the row end; a non-finite value after the activation row then
poisoned the output through 0*NaN. Compare against the tile count IC/32, as
the sibling m16n128k32/m16n64k32 kernels already do.

Tests: tests/kernels/test_awq_gemv_splitk.py compares both kernels against
an independent fp64 dequantize-then-matmul oracle and places a NaN sentinel
in the storage immediately after the activation rows. 47 cases fail on the
old kernels; all 104 pass after the fix (103 kernel cases + the real
Qwen2.5-0.5B GEMV greedy-decode integration test).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant