Repository navigation
fix(awq): cover every split-K tile and bound gemmv2 k-tiles (#4054) - #4058
Merged
Merged
Conversation
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).
This was referenced Nov 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.compileaddmm, W4AFP8 compile,device_mapCPU 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 whenK/128was odd, andgemmv2_forward_4bit_cuda_m128n64k32trimmed 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_prefilltakes theM <= 32branch withSPLITK = 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) = 7tiles and7/2 = 3iterations, 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 badAwqGEMVFastLineardeclaresSUPPORTS_IN_FEATURES_DIVISIBLE_BY = [64]andgroup_size = 128, so validation accepts the shape.gemv_fast_awq.pysends every prompt of<= 32tokens, 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:For
IC = 896andsplit_k_iters = 8,IC/32 = 28tiles andk_bound = 4; the check never fires, so splitsz = 4..7processed 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 badThe 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 * NaNpoisons the row. The siblingm16n128k32,m16n64k32, and MoE kernels already used the tile-count form;gemmv2was 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"]cta_offset_kis now always a multiple ofCTA_K, so the group-scale index(cta_offset_k + iter * CTA_K) / Gstays 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"]The host requires
num_in_channels % group_size == 0withgroup_size64 or 128, soIC/32is an exact tile count and at most the last tile of a split is out of range.3. Failure and ambiguity contract
K/128odd,M <= 32prefillIC/32not divisible bysplit_k_iters0 * NaNpoisons the output row4. Change inventory
gptqmodel_ext/awq/quantization_new/gemm/gemm_cuda.cugptqmodel_ext/awq/quantization/gemm_cuda_gen.cuIC/32tests/kernels/test_awq_gemv_splitk.pydocs/awq-kernel-splitk.mdREADME.md5. Tests / guarantees
test_gemv_fast_prefill_computes_every_k_tileKin 896/1024/1152/1408/4864 andMin 1/7/8/16/32/33/100 match an fp64 oracle within 2e-3test_gemv_fast_prefill_ignores_nonfinite_memory_after_inputtest_gemmv2_matches_fp64_oracle_across_k_tilesM > 8matches an fp64 oracle acrossKtest_gemmv2_never_reads_past_the_activation_rowsplit_k_itersvalues that do not divideIC/32test_real_qwen25_awq_gemv_greedy_decode_stays_finiteFORMAT.GEMVdecodes greedily with finite scoresThe 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):
After the fix:
ROCm (native AWQ extension is CUDA-only, so every case skips cleanly):
Related suites:
The 6
test_awq_gemv_fast_jit.pyfailures are pre-existing onorigin/main(in_features: 32 must be divisible by [64]); I reproduced the identical 6 failures in a cleanorigin/mainworktree.Measured numerics (fp64 oracle,
N=128,group_size=128)Item 1 relative error, prefill
SPLITK=2:Item 2 NaN sentinel,
M=9:7. Reviewer notes
SPLITK=2launch forM <= 32; it does not fall back toSPLITK=1, so no shape becomes unusable and no validated model changes kernel.gemmv2matches the bound style already used by the sibling kernels.FORMAT.GEMV; it is markedslow/modeland skips when the model is absent.docs/awq-kernel-splitk.md(linked fromREADME.md).