Repository navigation
test(marlin): derive the shared-memory budget invariant from the kernel sources (#4735) - #5058
Merged
Merged
Conversation
…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.
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.
What this is
#4735 reports that #4657 (
bf5adeb6f) widened the Marlin bias epilogue stage toFP32 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) setssh_bias_size = tb_n * 4,tmp_size = max(sh_b, sh_red) + sh_bias_size,sh_g_idx_size = stages * tb_k * 4, and movessh_biastosh + sh_size_b_red_max. This branch is based onaf2ab180a, 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 budgetcovers 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_layouttranslates the realdeclarations in
marlin_template.handgptq_marlin.cuinto Python (a smallC-expression translator handles the ternaries,
sizeof(int),&&/!andtruncating
/), then evaluates them over the real matrix:thread_config_tentries parsed fromgptq_marlin.cu,thread_m_blocks1..4 and bothm_block_size_8values,num_bits4/8,group_size128/64/32/-1,has_act_order, and thehas_zp/is_zp_floatmodes,i.e. 768 configs and 1536 evaluated layouts. For every one it asserts
and additionally that the three regions #4657 touched match byte for byte:
the B/reduce union, the FP32 bias, and
g_idx, plusspan == 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_countreplays the oldsh_bias_size = tb_n * 2term through the same model and asserts itunder-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):
Across the test's matrix (768 configs,
m_block_size_8=False): the pre-#4819model 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 forevery 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_DEVICES5/7.0d9396e59^(pre-fixmain): the new invariant test fails withassert 512 == 1024(host bias term 512 B vs the kernel's 1024 B); the twostring checks fail with
tb_n * 2/stages * tb_k / 4. The sensitivitytest 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.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 failedon this branch. The 10 failures are all
tests/test_q4_marlin.py(
ImportErrorfrom the prebuilt q4 extension) and reproduce identically on0d9396e59^. The pre-fix tree additionally fails the eighttest_marlin_scratch_cuda.pyN=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
host, so the reported nondeterministic tile failure itself was not observed.
test_q4_marlin.pyImportError is environmental and pre-existing; it isnot related to this change.
Closes #4735 (the runtime fix is #4819; this PR adds the missing invariant
test).