Skip to content

[BUG] Regression from #4657: Marlin returns wrong, nondeterministic output tiles (GPTQ MarlinLinear bf16 on sm_120, |err| 2-5 vs 0.1; AWQ fp16 on sm_89); stable at bf5adeb6f^ #4735

Closed

Description

@Qubitium

Found on main @ b225a43, RTX 5090 (sm_120, CUDA 13.2, torch 2.15). A/B against bf5adeb6f^ on the same card.

What is wrong

Since #4657 (bf5adeb6f, "fix(marlin): keep the checkpoint bias out of the tile layout ..."), the Marlin GEMM sometimes returns wrong output tiles. On sm_120 this hits GPTQ MarlinLinear in bf16, the AUTO kernel for GPTQ checkpoints on CUDA. The same input gives a different result from call to call. In a bad call a few 16x64 (or 64x64) output tiles are off by 2-5 absolute on outputs with |y|max ~30-45. The kernel's normal error against an fp64 dequant reference is ~0.1 in bf16. The wrong tiles appear with no bias and no other process on the GPU.

At bf5adeb6f^ the identical script gives 1 distinct output in 30 calls, with max error 0.09-0.15, for every shape below. bf5adeb6f is the only commit since then that touches gptqmodel_ext/marlin, utils/marlin.py or the Marlin qlinear modules.

#4657 widened the bias stage in marlin_template.h to FP32: sh_bias_size = thread_n_blocks * 16 / 4 (was / 8), plus two cp_async4 per group. That grows sh_b_red_bias_size and shifts every shared-memory region after it. The host-side size/validity model (get_kernel_cache_size() / is_valid_config() in gptq_marlin.cu) was not changed with it. A thread config the host thinks fits can therefore overrun its dynamic shared-memory allocation, or alias the reduction/scale regions. That would explain why the bad tiles depend on scheduling, and why they happen even when has_bias is false. This is a hypothesis; I did not patch and verify it.

Repro (module level)

The module is built the way the loader builds it: MarlinLinear(register_buffers=True), v1 GPTQ buffers copied in, post_init(). Codes are random 4-bit sym, g128, desc_act=False, and there is no bias. The input is fixed and forward is called 30 times back to back.

import torch, hashlib
# m = MarlinLinear(bits=4, group_size=128, desc_act=False, sym=True, in_features=K, out_features=N,
#                  bias=False, register_buffers=True, dtype=torch.bfloat16); copy qweight/qzeros/scales/g_idx; m.cuda().post_init()
x = torch.randn(M, K, device="cuda", dtype=torch.bfloat16)
with torch.inference_mode():
    outs = [m(x) for _ in range(30)]
print(len({hashlib.sha1(o.float().cpu().numpy().tobytes()).hexdigest() for o in outs}))
ref = x.double() @ W_dequant_fp64            # independent fp64 oracle from the integer codes
print(max((o.double() - ref).abs().max().item() for o in outs))

Distinct outputs out of 30 identical calls, and max |out - fp64 ref|:

K x N (g128) M main bf16 bf5adeb6f^ bf16 main fp16
2048 x 8192 64 / 256 / 1000 2 / 12 / 28 distinct, err 2.98 / 3.00 / 3.94 1 / 1 / 1, err 0.099 / 0.098 / 0.095 1 (M=512/2048), err 0.008-0.013
4096 x 4096 64 / 256 / 1000 3 / 15 / 28, err 2.73 / 3.54 / 3.70 1 / 1 / 1, err 0.137 / 0.152 / 0.145 -
896 x 4864 (Qwen2.5-0.5B MLP) 64 / 256 / 1000 1 / 2 / 11, err 0.067 / 2.30 / 3.17 1 / 1 / 1, err 0.067 / 0.078 / 0.082 1 / 1 (M=256/1000), err 0.008
2048 x 2048 512 / 2048 7 / 24, err 4.04 / 3.82 - 1 / 1
8192 x 2048 256 / 1000 14 / 28, err 3.42 / 4.46 - -
1536 x 8960 64 / 256 / 1000 3 / 8 / 29, err 2.84 / 3.40 / 4.68 - -
4864 x 896 64 / 256 / 1000 1 / 1 / 1 (clean) - -

The bad cells are always whole tiles. For example, at M=1000 on 896x4864 the rows 128-191 of column block 75 are wrong in one call and correct in the next.

On sm_120, AwqMarlinLinear (kU4 + zero points) was stable in both dtypes for 2048x2048 and 2048x8192 at M=64/512/2048, and GPTQ fp16 was stable. The thread configs that sm_120 selects for kU4B8/bf16 are the ones that break. A parallel audit on this host sees the same commit break the other combination on RTX 4090 (sm_89): AwqMarlinLinear fp16, M>=512, N>=2048. There the new tests/kernels/test_awq_marlin_determinism_4683.py fails at HEAD (5-6 failed) and is stable at bf5adeb6f^. That is likely the same root cause and explains why #4683 got worse after its regression test landed.

End to end

Llama-3.2-1B-Instruct-GPTQ-4bits-gp128, dtype=torch.bfloat16, wikitext-2 40x512. Then 20 identical full-model forwards of one 512-token window:

AUTO (MarlinLinear)  bf16  ppl=22.6844  distinct logits over 20 forwards = 20
GPTQ_TORCH           bf16  ppl=22.6802  distinct logits over 20 forwards = 1
AUTO (MarlinLinear)  fp16  ppl=22.7141  distinct logits over 20 forwards = 1

So every bf16 GPTQ model on a 5090 now has run-to-run varying logits with occasional wrong tiles, on the default kernel. The PPL effect on this model is small, but a wrong tile is a silent correctness error. Qwen/Llama-3 checkpoints default to bf16.

Suggested fix

Make the host-side shared-memory accounting (get_kernel_cache_size, is_valid_config, and the max_shared_mem_new budget) use the same FP32 bias stage size as the kernel (thread_n_blocks * 16 / 4 int4). Alternatively, give the FP32 bias its own region that does not shift the B/reduce/scale layout. Then rerun the determinism sweep for kU4B8 and kU4 in fp16/bf16 on sm_89 and sm_120.

Tests that should have caught it

test_awq_marlin_determinism_4683.py covers only AWQ and only the shapes/dtypes that fail on sm_89. A GPTQ MarlinLinear bf16 variant (repeat 30x, bitwise equal, plus an fp64 oracle check with a max-abs bound) on 2048x8192 / 4096x4096 at M in {64, 256, 1000} would have failed on sm_120. The #4657 tests check only bias numerics on small shapes.

Severity

High: silent wrong outputs and non-reproducible logits from the default GPTQ kernel in bf16 on Blackwell consumer GPUs. It is a regression from #4657.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    More data from RTX 4090 (sm_89), main @ b225a43. AwqMarlinLinear, mock AWQ 4-bit g128 module, no bias. Counts are distinct outputs over 200 back-to-back launches with no host sync (and over 50 launches synced between calls):

    build fp16 M=512 N=2048 K=2048 fp16 M=2048 N=2048 K=2048 bf16 (same shapes), fp16 M<=128, fp16 N=512
    bf5adeb6f^ 1 / 1 1 / 1 1
    bf5adeb6f (exact commit, fresh JIT build) 28 / 11 80 / 26 1
    main 19-28 / 7-22 73 / 25 1
    main + host-side fix suggested above: get_kernel_cache_size() int sh_bias_size = tb_n * 4; (was tb_n * 2) 9 / 4 138 / 19 1
    • The suggested host-side accounting fix alone does not fix it. With only sh_bias_size in get_kernel_cache_size() widened to FP32, the fp16 AWQ shapes stay nondeterministic. So either the budget at launch (max_shared_mem_new) and the device-side layout differ in more places, or the cause is elsewhere in fix(marlin): keep the checkpoint bias out of the tile layout and the output dtype #4657's template change.
      • Candidates: sh_bias / sh_g_idx / sh_zp / sh_s / sh_a now start at different offsets, and the sh_red / sh_b overlap that sh_b_red_bias_size is meant to cover.
      • This does not depend on has_bias: no bias tensor is passed here.
    • Wrong variants are off by up to 1.7 against the AwqTorchLinear output, while the correct variant is off by 0.03.
    • In the repo, tests/kernels/test_awq_marlin_determinism_4683.py fails 3/3 on sm_89 at HEAD. The failing cases are test_repeated_launches_are_bitwise_identical[...-dtype0] for (512,2048,2048), (512,8192,2048), (512,2048,8192), (2048,2048,2048), plus test_alternating_batch_shapes_keep_their_own_result, and once test_marlin_matches_the_torch_awq_oracle.

    Repro: the test file above. Or build the module with its _marlin_and_torch(dev, in_features=K, out_features=N, dtype=torch.float16) helper, run 200 forwards of a fixed x, and count the distinct outputs.

    🤖 Generated with Claude Code

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    The fault is localized to the kernel sources, not the Python side of #4657. Measured on RTX 4090 (sm_89), main @ b225a43. AwqMarlinLinear fp16 from a real Llama-3.2-1B-Instruct AWQ 4-bit g128 asym checkpoint, layer 0, no bias. The input is fixed, there are 20 calls per case, and each call is compared to AWQ_TORCH on the same checkpoint. Values are max |err| per call (min / median / max over calls):

    build q_proj M=2048 gate_proj M=2048 down_proj M=512
    12c1273c2 (pre-#4657) 0.031 / 0.031 / 0.031 0.002 / 0.002 / 0.002 0.0039 / 0.0039 / 0.0039
    main 0.031 / 0.031 / 3.43 (8/40 calls > 0.5) 0.113 / 0.216 / 1.04 0.0039 / 0.011 / 0.078
    main + gptqmodel/nn_modules/qlinear/marlin_awq.py from 12c1273c2 0.031 / 0.031 / 3.31 0.055 / 0.200 / 1.04 0.0039 / 0.019 / 0.046
    main + gptqmodel_ext/marlin/ from 12c1273c2 (Python unchanged) 0.031 / 0.031 / 0.031 0.002 / 0.002 / 0.002 0.0039 / 0.0039 / 0.0039

    So reverting only gptqmodel_ext/marlin/ (gptq_marlin.cu + marlin_template.h) fully restores the old behaviour. The _kernel_bias / MODEL_DTYPE_BUFFERS = () changes are not involved. I independently tried the same host-side sh_bias_size = tb_n * 4 patch and got the same result: it is still wrong (100/100 repeated calls differ on q_proj M=2048).

    gate_proj M=2048 is wrong in the median call, not occasionally: median error 0.216 against a 0.002 baseline.

    At the model level, the bd922cd Llama-3.2-1B-Instruct AWQ asym checkpoint on AUTO gives 12 distinct first-window logits in 12 forwards on main, with max |dlogit| up to 0.57. On 12c1273c2 it gives 5 of 6 bitwise equal to the round-13 reference.

    🤖 Generated with Claude Code

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4735-marlin is working on this issue now.

    Started 2026-11-17T14:37:46Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.

    claiming: reproduce the #4657 shared-memory regression on sm_120, fix the host-side shmem model, PR when green

  4. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Taking this.

  5. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent release: agent-root-4735-marlin is releasing this claim.

    Findings from main @ dba142d2f on RTX 5090 (sm_120, CUDA 13.4, torch 2.15.0+cu132), Marlin JIT rebuilt for sm_120:

    • Could not reproduce the nondeterminism. 30 back-to-back forwards per shape (bf16 and fp16) over (K,N) in (2048,8192), (4096,4096), (896,4864), (2048,2048), (128,128), (512,512) at M in 1/64/256/1000 always gave 1 distinct output. No 2/12/28-distinct pattern.
    • The bias epilogue that fix(marlin): keep the checkpoint bias out of the tile layout and the output dtype #4657 widened is correct. A zero-weight checkpoint (all codes equal the zero point) makes the module's output exactly the bias, so it is an exact per-column oracle. For N in 256/512/768/1280 at K=2048, every output column carried its own bias in fp16 and bf16, M=1 and M=64. The FP32 sh_bias stage addresses and reads back the right columns; I also confirmed by instrumenting the kernel that the staged vector holds the right values for every slice_col.
    • Host vs kernel shared-memory model: I re-derived both from source and found a 256-512 byte under-count in get_kernel_cache_size() for a few tn=256 configs (sh_bias is tb_n*2 bytes host-side vs tb_n bytes of FP32 staging), but it is far below the guard, so it is not this bug.

    So the reporter's sm_120 bf16 symptom did not reproduce on this card/build. Closing my claim rather than shipping an unverified change. If it is still seen, the most useful next datum is the exact prob_m_split/thread config for a failing call, or a run with compute-sanitizer --tool memcheck, since a tile-shaped error that depends on scheduling is otherwise indistinguishable from an uninitialised-read race.

  6. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Verification of #4819 on RTX 4090 (sm_89), main @ 5cded90, A/B against 0d9396e59^ (each tree with a fresh JIT build). Real checkpoints, BACKEND.MARLIN, fixed input, repeated calls:

    case 0d9396e59^ main
    AWQ-Llama-3.2-1B-g128-gemm fp16, layer 0, distinct outputs over 50 calls: q_proj M=2048 / gate_proj M=2048 / down_proj M=512 30-34 / 50 / 8-45 (spread up to 7.8) 1 / 1 / 1
    same model fp16, 20 full-model forwards of a 512-token window 20 distinct logits 1
    200-call sweep, layers 0/5/15, {q,gate,up,down}_proj, M 1024-4096, AWQ fp16+bf16 and Llama-3.2-1B GPTQ g128 fp16+bf16 (72 cases, 14,400 calls) - 0 calls differ
    tests/kernels/test_marlin_tile_determinism_4735.py + test_awq_marlin_determinism_4683.py - 25 passed

    Performance: no regression from moving the bias to shared memory. AWQ fp16/bf16 module medians (CUDA events, min of 2 interleaved runs) are equal or faster at M = 1..2048 (geomean new/old 0.79). GPTQ bf16 prefill-2048 is 27.4 vs 27.8 ms.

    One caveat: in the first two runs, while another process was busy on the same GPU, I saw three isolated non-repeating events. These were 2 distinct of 50 on AWQ fp16 down_proj M=2048 (K=8192, N=2048) with |dev| 0.10, 2 of 50 on AWQ bf16 layer-7 gate_proj M=2048 with |dev| 0.16, and 2 distinct of 20 full-model bf16 forwards with |dlogit| 0.125. I could not reproduce any of them in three later model-level reruns or in the 14,400-call sweep, so they are not actionable from here. If a final sign-off is needed, a longer soak under concurrent GPU load on sm_89 and sm_120 is still worth running. The sm_120 (5090) half of the original report was not re-checked here.

    🤖 Generated with Claude Code

  7. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4735-marlin-accounting is working on this issue now.

    Started 2026-11-18T00:32Z. The claim expires 4.0h after this comment is last edited; it is released with the PR link or with a stand-down comment carrying evidence.

    Scope: the host-side shared-memory accounting for the FP32 bias stage (get_kernel_cache_size() / is_valid_config() in gptq_marlin.cu) versus the kernel's actual sh_bias_size, plus a GPTQ MarlinLinear bf16 determinism + fp64-oracle regression test. This host is sm_80 (PG506), not sm_120/sm_89, so the reproduction may not appear here; the claim will say exactly which part was measured and which part rests on the static accounting invariant.

  8. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent release: agent-root-4735-marlin-accounting is releasing this claim.

    Status: fixed and closed. The runtime fix was already on main as #4819 (0d9396e59): sh_bias_size = tb_n * 4 bytes, tmp_size = max(sh_b, sh_red) + sh_bias_size, sh_g_idx_size = stages * tb_k * 4, and the FP32 bias moved to its own region at sh + sh_size_b_red_max. I re-derived both models from source and found nothing left to patch, so the PR is test hardening, not a second fix.

    PR: #5058, merged as 2caa7af8b02650fab0ada4a66222e87ac631d04d.
    It adds the derived invariant #4819's string checks cannot prove: it parses marlin_template.h + gptq_marlin.cu, evaluates the kernel and host layouts over 768 accepted configs (1536 layouts with both m_block_size_8 values), and asserts host dynamic shared memory >= kernel declared extent, plus span == union + bias and byte-exact union/bias/g_idx regions. A second test replays the old tb_n * 2 term and asserts it under-counts.

    Accounting numbers (bytes). Kernel needs vs host reserves, before/after:

    config (thread_k, thread_n, M blocks, bits, group, act, zp) kernel needs host now host before #4819 short by
    64, 256, 4, 4, 128, no, no 69632 69632 69120 512
    64, 256, 1, 4, 128, no, no 44032 44032 43520 512
    128, 128, 1, 4, 128, yes, no 59904 59904 57728 2176
    64, 128, 4, 4, 128, no, yes 51968 51968 51712 256
    64, 128, 4, 8, 128, no, yes 67584 67584 67328 256
    128, 64, 4, 4, -1, no, no 82688 82688 82560 128

    Across the test's 768-config matrix the pre-#4819 model under-counts 560 configs, by up to 2176 B (e.g. 128/128/M4/8-bit/g128/act_order: 256 B FP32 bias + 1920 B g_idx). The current model is exact for every non-act-order config and over-reserves only the act-order chunk-scales path by design.

    Measured on this host (sm_80, 8x PG506, torch 2.15.0.dev20260928+cu130):

    • New static invariant fails on 0d9396e59^ with assert 512 == 1024 (the pre-fix host bias term vs the kernel's FP32 stage) and passes on main.
    • tests/kernels/test_marlin_tile_determinism_4735.py (real JIT kernel, 30 launches + FP64 oracle): 14 passed.
    • tests/kernels/test_awq_marlin_determinism_4683.py: 13 passed.
    • Marlin suites (tests/kernels/test_marlin_*.py, tests/test_marlin_scratch_cuda.py, tests/test_q4_marlin.py, the 4683 file): 274 passed, 2 skipped, 10 failed. The 10 are all test_q4_marlin.py ImportError and reproduce identically on 0d9396e59^; the pre-fix tree additionally fails the eight test_marlin_scratch_cuda.py N=200 padded cases, which pass here. No new failures.
    • The wrong/nondeterministic tile symptom did not reproduce on sm_80, before or after: the GPTQ bf16 sweep (2048x8192, 4096x4096, g128, M in {64,256,1000}) and AWQ fp16 sweep (2048x2048, 8192x2048, M in {512,2048}) each gave 1 distinct output in 30 calls and were within the FP64 bound on both trees.

    Not measured: sm_120 (RTX 5090) and sm_89 (RTX 4090) hardware is not available here, so the reported architecture-specific failure was not observed. The sm_80 numerical sweep being clean means the static accounting invariant is the load-bearing evidence for this issue, not a measured fix of the wrong tiles.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions