Repository navigation
Description
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
Qubitium commentedon Nov 17, 2026
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):bf5adeb6f^bf5adeb6f(exact commit, fresh JIT build)mainmain+ host-side fix suggested above:get_kernel_cache_size()int sh_bias_size = tb_n * 4;(wastb_n * 2)sh_bias_sizeinget_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.sh_bias/sh_g_idx/sh_zp/sh_s/sh_anow start at different offsets, and thesh_red/sh_boverlap thatsh_b_red_bias_sizeis meant to cover.has_bias: no bias tensor is passed here.tests/kernels/test_awq_marlin_determinism_4683.pyfails 3/3 on sm_89 at HEAD. The failing cases aretest_repeated_launches_are_bitwise_identical[...-dtype0]for (512,2048,2048), (512,8192,2048), (512,2048,8192), (2048,2048,2048), plustest_alternating_batch_shapes_keep_their_own_result, and oncetest_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 fixedx, and count the distinct outputs.🤖 Generated with Claude Code
Qubitium commentedon Nov 17, 2026
The fault is localized to the kernel sources, not the Python side of #4657. Measured on RTX 4090 (sm_89),
main@ b225a43.AwqMarlinLinearfp16 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 toAWQ_TORCHon the same checkpoint. Values are max |err| per call (min / median / max over calls):12c1273c2(pre-#4657)mainmain+gptqmodel/nn_modules/qlinear/marlin_awq.pyfrom12c1273c2main+gptqmodel_ext/marlin/from12c1273c2(Python unchanged)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-sidesh_bias_size = tb_n * 4patch and got the same result: it is still wrong (100/100 repeated calls differ on q_proj M=2048).gate_projM=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-InstructAWQ asym checkpoint on AUTO gives 12 distinct first-window logits in 12 forwards onmain, with max |dlogit| up to 0.57. On12c1273c2it gives 5 of 6 bitwise equal to the round-13 reference.🤖 Generated with Claude Code
Qubitium commentedon Nov 17, 2026
Agent claim:
agent-root-4735-marlinis 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
Qubitium commentedon Nov 17, 2026
Taking this.
Qubitium commentedon Nov 17, 2026
Agent release:
agent-root-4735-marlinis releasing this claim.Findings from
main@dba142d2fon RTX 5090 (sm_120, CUDA 13.4, torch 2.15.0+cu132), Marlin JIT rebuilt for sm_120:(K,N)in(2048,8192),(4096,4096),(896,4864),(2048,2048),(128,128),(512,512)at M in1/64/256/1000always gave 1 distinct output. No 2/12/28-distinct pattern.Nin 256/512/768/1280 atK=2048, every output column carried its own bias in fp16 and bf16, M=1 and M=64. The FP32sh_biasstage addresses and reads back the right columns; I also confirmed by instrumenting the kernel that the staged vector holds the right values for everyslice_col.get_kernel_cache_size()for a fewtn=256configs (sh_biasistb_n*2bytes host-side vstb_nbytes 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 withcompute-sanitizer --tool memcheck, since a tile-shaped error that depends on scheduling is otherwise indistinguishable from an uninitialised-read race.Qubitium commentedon Nov 17, 2026
Verification of #4819 on RTX 4090 (sm_89),
main@ 5cded90, A/B against0d9396e59^(each tree with a fresh JIT build). Real checkpoints,BACKEND.MARLIN, fixed input, repeated calls:0d9396e59^maintests/kernels/test_marlin_tile_determinism_4735.py+test_awq_marlin_determinism_4683.pyPerformance: 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
Qubitium commentedon Nov 18, 2026
Agent claim:
agent-root-4735-marlin-accountingis 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()ingptq_marlin.cu) versus the kernel's actualsh_bias_size, plus a GPTQMarlinLinearbf16 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.test(marlin): derive the shared-memory budget invariant from the sour…
Qubitium commentedon Nov 18, 2026
Agent release:
agent-root-4735-marlin-accountingis releasing this claim.Status: fixed and closed. The runtime fix was already on
mainas #4819 (0d9396e59):sh_bias_size = tb_n * 4bytes,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 atsh + 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 bothm_block_size_8values), and assertshost dynamic shared memory >= kernel declared extent, plusspan == union + biasand byte-exact union/bias/g_idx regions. A second test replays the oldtb_n * 2term and asserts it under-counts.Accounting numbers (bytes). Kernel needs vs host reserves, before/after:
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):
0d9396e59^withassert 512 == 1024(the pre-fix host bias term vs the kernel's FP32 stage) and passes onmain.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.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 alltest_q4_marlin.pyImportError and reproduce identically on0d9396e59^; the pre-fix tree additionally fails the eighttest_marlin_scratch_cuda.pyN=200 padded cases, which pass here. No new failures.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.
fix(marlin): size the FP32 bias stage and keep it out of the reduction (