Skip to content

fix(marlin): seed g_idx with the canonical contiguous index (#4239) - #4250

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4239-marlin-gidx
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4239-marlin-gidx

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4239

Root cause

MarlinLinear.__init__ registered g_idx with torch.empty(K, dtype=int32). The
#4053 contiguity guard in post_init() reads g_idx to tell a real
activation-order checkpoint from a plain desc_act=False layer, so it consumed
uninitialised memory:

  • desc_act=False checkpoints that omit g_idx flipped to desc_act=True and
    decoded with a permutation built from garbage (e.g. [-1151343613, 5, 0, 0]),
    or read out of bounds;
  • tile-misaligned shapes raised NotImplementedError (K=288/N=200);
  • test_inference_fp16_overflow hit RuntimeError: For act_order, num_groups must be > 1.

Fix

Seed the placeholder with arange(K) // group_size — the same canonical mapping
GPTQQuantLinear stores for desc_act=False. It is the identity ordering, keeps
load_state_dict() shape-compatible, and leaves a checkpoint without g_idx on
the contiguous fast path. A checkpoint that really stores a permutation still
takes the activation-order path, so #4053 is preserved (covered by a dedicated
test).

Diff is 2 files touched minimally (marlin.py placeholder only, plus a short
docs/checkpoint-interop.md note) and 1 new test file.

Fail-before / pass-after (real output)

New suite on origin/main (7c7d704) with the test file copied in:

$ python -m pytest tests/test_marlin_g_idx_placeholder.py -q
FAILED tests/test_marlin_g_idx_placeholder.py::test_placeholder_g_idx_is_the_canonical_contiguous_index[256-32-False]
FAILED tests/test_marlin_g_idx_placeholder.py::test_placeholder_g_idx_is_the_canonical_contiguous_index[288-32-False]
FAILED tests/test_marlin_g_idx_placeholder.py::test_placeholder_g_idx_is_the_canonical_contiguous_index[128-128-False]
FAILED tests/test_marlin_g_idx_placeholder.py::test_placeholder_g_idx_is_the_canonical_contiguous_index[256--1-False]
FAILED tests/test_marlin_g_idx_placeholder.py::test_placeholder_g_idx_is_the_canonical_contiguous_index[256-64-True]
FAILED tests/test_marlin_g_idx_placeholder.py::test_post_init_without_checkpoint_g_idx_keeps_the_contiguous_path
FAILED tests/test_marlin_g_idx_placeholder.py::test_checkpoint_without_g_idx_decodes_contiguous_groups
FAILED tests/test_marlin_g_idx_placeholder.py::test_desc_act_module_without_checkpoint_g_idx_starts_from_identity_order
==================== 8 failed, 1 passed, 1 warning in 4.01s ====================

Representative failures on main:

E       assert False
E        +  where False = torch.equal(Parameter containing: tensor([ 757467119, 6, 0, 0, ...]), tensor([0, 0, ...]))
tests/test_marlin_g_idx_placeholder.py:121: AssertionError

E       NotImplementedError: GPTQ Marlin: checkpoint g_idx is non-contiguous but `desc_act` is False.
E       Activation-order decoding requires tile-aligned shapes; got K=288, N=200. Load with a non-Marlin backend.
gptqmodel/nn_modules/qlinear/marlin.py:381: NotImplementedError

E       assert True is False
E        +  where True = MarlinLinear().desc_act
tests/test_marlin_g_idx_placeholder.py:200: AssertionError

Same suite on this branch:

$ python -m pytest tests/test_marlin_g_idx_placeholder.py -q
9 passed, 1 warning in 4.18s

The one test that passes on both is
test_noncontiguous_checkpoint_g_idx_still_selects_the_act_order_path, which
pins the #4053 behaviour.

Pre-existing tests named in the issue, on main:

FAILED tests/test_marlin_jit.py::test_marlin_quant_linear_post_init_pads_weight_scales_and_bias
FAILED tests/test_marlin_jit.py::test_marlin_quant_linear_post_init_uses_compute_dtype_for_repack
FAILED tests/test_inference_fp16_overflow.py::test_marlin_bias_cancels_before_the_fp16_cast[gptq]
==================== 3 failed, 1 passed, 1 warning in 4.42s ====================

On this branch:

==================== 4 passed, 1 warning in 4.47s ====================

End-to-end repro from the issue (K=N=256, checkpoint state dict without g_idx):

main this branch
g_idx[:4] after ctor [-1151343613, 5, 0, 0] [0, 0, 0, 0]
desc_act after post_init() True (warn + act-order) False
max abs diff vs reference 0.020996 0.000244

Numerics

The new tests do not use any gptqmodel kernel as their own reference. They build
GPTQ int32 words and scales directly, then compare the module output with an
FP64 oracle computed in the test: x @ (scales[g_idx] * (codes - 8)).T.
Relative error is ~2.8e-4 (fp16 scale resolution) for the contiguous, identity
act-order, and permuted act-order paths.

Validation

$ ruff check gptqmodel docs/eora tests setup.py
All checks passed!

Marlin regression surface on this branch (test_marlin_jit,
test_inference_fp16_overflow, test_marlin_permute,
test_marlin_cpu_offload_post_init, test_marlin_device_profile,
test_marlin_import_min, test_marlin_scratch_interface,
test_marlin_scratch_regressions, the new file): 143 passed, 1 skipped, 5 failed. The 5 failures reproduce identically on origin/main in this
environment (test_marlin_cuda_smoke_build_and_forward: "already loaded in this
Python process"; 4x test_marlin_cpu_offload_refreshes_packed_weights:
unexpected qzeros_format key). tests/test_marlin_scratch_cuda.py also has 4
pre-existing AWQ-8bit failures on main. test_q4_marlin.py / test_awq_marlin.py
cannot be collected here (ModuleNotFoundError: parameterized).

Residual risk

`MarlinLinear.__init__` registered `g_idx` with `torch.empty`. The #4053
contiguity guard in `post_init()` reads that tensor to decide whether a
checkpoint stores a real activation-order index, so uninitialised memory was
interpreted as a permutation:

- a `desc_act=False` checkpoint that omits `g_idx` silently flipped to the
  activation-order path and decoded with an index built from garbage;
- tile-misaligned shapes (e.g. K=288/N=200) raised NotImplementedError;
- `test_inference_fp16_overflow` hit "For act_order, num_groups must be > 1".

Seed the placeholder with `arange(K) // group_size`, the same canonical mapping
the base class stores for `desc_act=False`. It is the identity ordering, it
keeps `load_state_dict()` shape-compatible, and it keeps a checkpoint without
`g_idx` on the contiguous fast path. A checkpoint that really stores a
permutation still takes the activation-order path, so #4053 is preserved.

Tests use an independent FP64 oracle for the dequantized matmul.
@Qubitium
Qubitium merged commit 7fa9677 into main Nov 16, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4239-marlin-gidx branch November 16, 2026 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][Marlin] MarlinLinear registers g_idx with torch.empty; the #4053 contiguity guard then treats uninitialised memory as an act-order index

1 participant