Skip to content

test(marlin): derive the shared-memory budget invariant from the kernel sources (#4735) - #5058

Merged
Qubitium merged 1 commit into
mainfrom
root/marlin-shmem-bias-4735
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/marlin-shmem-bias-4735

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

What this is

#4735 reports that #4657 (bf5adeb6f) widened the Marlin bias epilogue stage to
FP32 without updating the host shared-memory model, so a config the host accepts
can overrun its dynamic shared-memory allocation.

The production fix is already on main: #4819 (0d9396e59) sets
sh_bias_size = tb_n * 4, tmp_size = max(sh_b, sh_red) + sh_bias_size,
sh_g_idx_size = stages * tb_k * 4, and moves sh_bias to
sh + sh_size_b_red_max. This branch is based on af2ab180a, which contains it;
re-deriving both models from source found nothing left to patch.

What #4819 did not add is the load-bearing part of the issue's own bar: a test
that recomputes the kernel's declared regions and the host's budget and
proves the budget covers them. The test that landed with #4819 compares source
strings ("tb_n * 4" in host_bias), so it cannot tell whether the host budget
covers the whole layout, and it would keep passing if both sides drifted to the
same wrong number.

This PR adds that derived check to
tests/kernels/test_marlin_tile_determinism_4735.py.

The test

test_host_shared_memory_budget_covers_the_kernel_layout translates the real
declarations in marlin_template.h and gptq_marlin.cu into Python (a small
C-expression translator handles the ternaries, sizeof(int), &&/! and
truncating /), then evaluates them over the real matrix:

  • the four unique thread_config_t entries parsed from gptq_marlin.cu,
  • thread_m_blocks 1..4 and both m_block_size_8 values,
  • num_bits 4/8, group_size 128/64/32/-1, has_act_order, and the
    has_zp / is_zp_float modes,

i.e. 768 configs and 1536 evaluated layouts. For every one it asserts

host dynamic shared memory >= kernel declared extent

and additionally that the three regions #4657 touched match byte for byte:
the B/reduce union, the FP32 bias, and g_idx, plus span == union + bias
(the bias has its own region after the union; it is not carved out of it).

test_the_pre_4657_bias_budget_would_under_count replays the old
sh_bias_size = tb_n * 2 term through the same model and asserts it
under-counts, so the invariant cannot be satisfied by two models that agree on
the wrong number.

Accounting numbers

Kernel requirement vs host reservation, bytes, from the two sources (the
"old" column is the pre-#4819 host model):

thread_k thread_n M blocks bits group act_order zp kernel needs host now host before 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 matrix (768 configs, m_block_size_8=False): the pre-#4819
model under-counts 560 of them, by up to 2176 B (e.g.
thread_k=128, thread_n=128, thread_m_blocks=4, 8-bit, g128, act_order:
256 B of FP32 bias plus 1920 B of g_idx). The current model is exact for
every non-act-order config and over-reserves only for the act-order chunk
scales path (by design, load_groups = max(..., 32)).

Verification

Measurement host: 8x NVIDIA PG506 (sm_80, 124 SMs), torch
2.15.0.dev20260928+cu130, CUDA 13.x, CUDA_VISIBLE_DEVICES 5/7.

  • Static tests on this branch: 4 passed.
  • Same file on 0d9396e59^ (pre-fix main): the new invariant test fails with
    assert 512 == 1024 (host bias term 512 B vs the kernel's 1024 B); the two
    string checks fail with tb_n * 2 / stages * tb_k / 4. The sensitivity
    test passes there, which is the point.
  • tests/kernels/test_marlin_tile_determinism_4735.py (real JIT kernel,
    GPTQ bf16 + AWQ fp16, 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,
    test_awq_marlin_determinism_4683.py): 274 passed, 2 skipped, 10 failed
    on this branch. The 10 failures are all tests/test_q4_marlin.py
    (ImportError from the prebuilt q4 extension) and reproduce identically on
    0d9396e59^. The pre-fix tree additionally fails the eight
    test_marlin_scratch_cuda.py N=200 padded cases; none of them fail here.
    No new failures.

sm_80 reproduction: not reproduced

The 30-launch GPTQ bf16 sweep (2048x8192 and 4096x4096, g128, M in
{64,256,1000}) and the AWQ fp16 sweep (2048x2048 and 8192x2048, M in
{512,2048}) are deterministic with one distinct output and within the FP64
oracle bound both on this branch and on 0d9396e59^. This matches the
#4819 report: the wrong-tile failure is architecture-specific and does not
appear on sm_80. The static accounting invariant is the load-bearing evidence
here; no sm_120 or sm_89 measurement is claimed.

Not verified here

  • sm_120 (RTX 5090) and sm_89 (RTX 4090) hardware is not available on this
    host, so the reported nondeterministic tile failure itself was not observed.
  • The test_q4_marlin.py ImportError is environmental and pre-existing; it is
    not related to this change.

Closes #4735 (the runtime fix is #4819; this PR adds the missing invariant
test).

…ces (#4735)

#4657 widened the Marlin bias stage to FP32 (thread_n_blocks * 16 / 4 int4)
and #4819 fixed the host-side model to match it. The regression test that
landed with #4819 only compares source *strings*; it never recomputes the
extent the kernel declares, so it cannot show that the host budget covers the
whole layout (B/reduce union, FP32 bias, g_idx, zero points, scales, A) for
the configs is_valid_config() accepts.

Add a derived check that translates the real declarations in
marlin_template.h and gptq_marlin.cu into Python, evaluates them over the
real thread-config matrix (4 thread configs x thread_m_blocks 1..4 x bits
{4,8} x group {128,64,32,-1} x act_order x zero-point modes, 2880 cases), and
asserts host dynamic shared memory >= the kernel requirement. It also pins
that the FP32 bias sits after the union (span == union + bias) and that the
bias and g_idx regions match byte for byte.

A second test replays the pre-#4657 tb_n * 2 bias term through the same
model and asserts it under-counts, so the invariant cannot be satisfied by
two models that agree on the wrong number.

On 0d9396e^ (pre-fix) the new invariant test fails with
``assert 512 == 1024`` for the FP32 bias stage; on current main all four
static checks pass.
@Qubitium
Qubitium merged commit 2caa7af into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the root/marlin-shmem-bias-4735 branch November 18, 2026 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant