Skip to content

fix(macaq): int64 row offsets, device-bound Triton launches, sm_120 K tiles (#4090) - #4099

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4090-macaq
Nov 15, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4090-macaq

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4090

TL;DR

Seven MACaQ MXFP4 W4A4 defects: 32-bit row offsets in three Triton kernels,
transformers silently random-initializing MACaQ (and ParoQuant/QQQ/NF12/
TT-blockfloat) checkpoints, MACaQ launches ignoring the operand's device
(permanent Torch fallback), the sm_120 BLOCK_K=32 compile failure, the
unread MoE fallback, one-element exp_offsets, and dynamic={pattern: False}.
Core MACaQ math is unchanged.

Root cause and fix

1 (high) — int32 row offsets overflow once rows*K >= 2^31

gptqmodel/utils/macaq_triton.py: _quant_mxfp4_kernel, _rot_scale_quant_kernel
and _dequant_w4_to_bf16_kernel used row = tl.program_id(0) (i32) and computed
row * stride_xr / row * stride_pr / row * (K // 2) in i32. The dense GEMM
already widened offs_m (:200). One large prefill through a wide down_proj
(K=18944 overflows at 113,359 tokens) wrapped the row address, read the wrong
row on CUDA, and faulted the HIP context on ROCm.

Fix: row = tl.program_id(0).to(tl.int64) in all three kernels
(gptqmodel/utils/macaq_triton.py:143,178,258).

2 (medium) — HF loads MACaQ checkpoints as random dense weights

config.json carries quant_method: "macaq". Transformers 5.22's
AutoHfQuantizer.supports_quant_method only warns Unknown quantization type
and sets pre_quantized=False, so from_pretrained returns a plain model whose
packed tensors were randomly initialized. Only gptqmodel_gguf had a refusing
quantizer (gptqmodel/__init__.py:131-156).

Fix: one table, REFUSED_HF_QUANT_METHODS in gptqmodel/_quant_methods.py:21,
registers GPTQModelRefusingHfQuantizer/GPTQModelRefusingQuantizationConfig
(gptqmodel/utils/hf_refusing_quantizer.py) for macaq, paroquant, qqq,
nf12, tt-blockfloat
. GGUF keeps its dedicated quantizer. The table is the
single extension point for the sibling #4091 item 5 fix.

Before (origin/main, tiny packed checkpoint re-labelled macaq):

LOADED modules: {... 'Linear': 8 ...}
packed qweight keys after load: []
linear weights random (no qweight restored)

After:

RAISED ValueError GPT-QModel MACaQ MXFP4 checkpoints cannot be loaded by
`transformers`; load them with `GPTQModel.load` or vLLM. (quant_method: 'macaq')

3 (medium) — Triton launches ignored the tensor's GPU; modules latched to Torch

quantize_mxfp4, rot_scale_quantize_mxfp4, dequant_w4_to_bf16 and
mxfp4_w4a4_linear launched into the current context. A layer on cuda:1
while cuda:0 was current raised ValueError: Pointer argument cannot be accessed from Triton, and MacaqLinear.forward set _triton_disabled=True
permanently (gptqmodel/nn_modules/qlinear/macaq.py:279-287).

Fix: every launch is wrapped in _launch_device(<operand>.device)
(gptqmodel/utils/macaq_triton.py:56, same guard as GGUF/W4A), and the catch-all
now latches only permanent failures: _triton_failure_is_transient
(gptqmodel/nn_modules/qlinear/macaq.py:41,297) keeps Triton enabled after a
transient OOM.

flowchart TD
    A["MacaqLinear.forward"] --> B{"_can_use_triton"}
    B -->|no| T["_forward_torch"]
    B -->|yes| C["with torch.cuda.device(x.device)<br/>Triton launch"]
    C -->|ok| O["output"]
    C -->|OOM or 'out of memory'| T2["_forward_torch<br/>Triton stays enabled"]
    C -->|compile or pointer error| L["_triton_disabled = True<br/>_forward_torch"]
    classDef error fill:#fdd,stroke:#c00,color:#000;
    class L error;
Loading

Measured cliff the latch protected, decode shape M=1, N=896, K=4864
(300 iterations, CUDA events):

Device Triton W4A4 Torch fallback Ratio
RTX 4090 59.0 µs 863.4 µs 14.6×
R9700 gfx1201 83.7 µs 1547.2 µs 18.5×

4 (medium) — sm_120 BLOCK_K=32 fails to compile

_pick_tiles fell back to block_k=32 when no candidate divided K
(gptqmodel/utils/macaq_triton.py:370). On sm_120, tl.dot_scaled lowers to
the native mma.sync...kind::mxf4nvf4.block_scale instruction, which requires a
K tile of at least 64 → RuntimeError: PassManager::run failed, then the
catch-all disabled Triton.

Fix: _pick_tiles keeps the largest measured tile and the public entry point
shrinks it only down to 64 (:440-443). The existing kb_mask/ks_mask tails
make a non-divisor tile correct; no divisor requirement remains.
test_triton_dense_kernel_masks_k_tail now also covers K=96/288/160.

The sm_120 compile path itself was not re-verified here (no sm_120 card is
available; the RTX 5090 is off-limits). Correctness of the 64-wide tile with the
K tails was verified against the fp64-derived Torch oracle on sm_89 and
gfx1201.

5 (medium) — MoE expert with no calibration tokens crashed; fallback never read

MacaqProcessor.preprocess set task.fallback
(gptqmodel/looper/macaq_processor.py:114), but Macaq.quantize always raised
ValueError: ... no usable calibration signal (gptqmodel/quantization/macaq.py:706).

Fix (gptqmodel/quantization/macaq.py:715-760): consult
should_use_fallback(self.fallback, nsamples, expected_nsamples). When it
matches, or the Hessian is unusable but a fallback threshold is configured, run
data-independent MXFP4 RTN with an identity metric, no D1, no exponent screen and
no flips; record fallback=True in the result stats. Without a configured
threshold the original actionable ValueError remains.

flowchart TD
    E["Macaq.add_batch<br/>capture Hessian"] --> U{"finite and nonzero<br/>diagonal?"}
    U -->|no| F{"fallback threshold<br/>matches observed tokens?"}
    U -->|yes| G{"observed tokens below<br/>fallback threshold?"}
    F -->|no| X["ValueError<br/>no usable calibration signal"]
    F -->|yes| R["MXFP4 RTN<br/>identity H, no D1"]
    G -->|yes| R
    G -->|no| Q["calibrated MACaQ<br/>D1 + screen + flip"]
    classDef error fill:#fdd,stroke:#c00,color:#000;
    class X error;
Loading

6 (low) — one non-zero exp_offsets entry was ignored

macaq_quantize_weight only screened len(exp_offsets) > 1, so (-1,) and
(2,) fell through to mxfp4_rtn_signed_index(..., offset=0)
(gptqmodel/quantization/macaq.py:533). Fix: pass the single offset
(:540).

7 (low) — dynamic={pattern: False} crashed MacaqConfig

The documented exclusion form (docs/macaq.md:319) was accepted by
MacaqConfig.__post_init__ but the shared normalizer calls .items() on the
False payload (gptqmodel/quantization/config.py:4571/4719). Fix: translate
False to the base -:pattern rule with an empty override before shared
normalization (gptqmodel/quantization/config.py:6244-6256).

Before / after

# Fail before (origin/main @ 718256f) After
1 rows past 2^31/K: packed[-1] all zero bytes (wrong row); source check fails exact bytes match the independent oracle
2 LOADED plain Linear x8, no qweight, random weights ValueError naming GPTQModel.load/vLLM
3 launch-context spy records []; transient OOM sets _triton_disabled=True spy records the operand device; OOM leaves Triton enabled and retries
4 _pick_tiles(1,96)[2] == 32, (8,160) == 32 both >= 64
5 ValueError: no usable calibration signal completes, fallback=True, pre_scale=None, bytes equal independent RTN oracle
6 (-1,)/(2,) produce offset-0 packed bytes match the independent oracle and differ from offset 0
7 AttributeError: 'bool' object has no attribute 'items' dynamic_get(...) is False, round-trips as -:.*down_proj

Tests

New regressions in tests/test_macaq.py; the ParoQuant registration guard in
tests/test_paroquant.py was updated (it previously asserted the name was
unknown, which is exactly the silent-load path).

# CUDA (RTX 4090, GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae)
CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae PYTHONPATH=$PWD \
  /opt/venvs/cu130t/bin/python -m pytest tests/test_macaq.py -q
# 66 passed, 1 skipped (two-visible-device test)

# ROCm (R9700 gfx1201, HIP_VISIBLE_DEVICES=3 only)
HIP_VISIBLE_DEVICES=3 PYTHONPATH=$PWD \
  /opt/venvs/rocm101t/bin/python -m pytest tests/test_macaq.py -q
# 65 passed, 2 skipped (two-device test; NVIDIA free-threaded compiled-flip check)

CUDA_VISIBLE_DEVICES=... PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_checkpoint_compat.py -q
# 19 passed
CUDA_VISIBLE_DEVICES=... PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_paroquant.py -q -k not_aliased
# 1 passed
CUDA_VISIBLE_DEVICES=... PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_hf_gptqmodel_load_bridge.py tests/test_qqq_config.py \
  tests/test_quant_bits_validation.py tests/test_nf12_codec.py \
  tests/qcfg/test_config_serialization.py -q
# 292 passed, 9 skipped
HIP_VISIBLE_DEVICES=3 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest \
  tests/test_rocm_kernel_contracts.py -q -k macaq
# 13 passed

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

The fail-before numbers above were produced in a clean
git worktree of origin/main @ 718256fd7 with the same test file:
16 failed, 3 passed for the new regressions. The two-device cross-device test
is skipped in both runs.

Hardware: RTX 4090 (sm_89, Torch 2.16.0.dev+cu130, Triton 3.9) and Radeon AI
PRO R9700 (gfx1201, Torch 2.16.0.dev+rocm10.1).

Limitations

  • Item 3 two-device measurement: the pinned environment exposes exactly one
    CUDA device and one HIP device (HIP_VISIBLE_DEVICES=3). CUDA/HIP reject
    duplicate UUID/index entries (device_count() == 0), the RTX 5090 is
    off-limits, and the other 4090s belong to parallel workers. The committed
    test_macaq_module_on_non_current_gpu_stays_on_triton runs the
    cuda:0 vs cuda:1 output/disabled-module comparison whenever two devices are
    visible; it is the skipped test here. The launch-guard regression fails before
    and passes after, and the decode cliff is quantified above.
  • Item 4 sm_120 compile path was not re-verified on sm_120 hardware (see
    above).
  • Item 2 scope: macaq, paroquant, qqq, nf12, tt-blockfloat (plus
    the existing GGUF refusal). exl3 and the bare bitsandbytes name are also
    absent from transformers' mapping but are outside this issue's enumerated set.

Docs

docs/macaq.md gains the runtime/edge-case contracts, the updated calibration
fallback flow, the launch/latch diagram, the BLOCK_K >= 64 contract, the
dynamic=False translation, and the transformers refusal table. It is already
linked from README.md (MACaQ links to docs/macaq.md).

… tiles (#4090)

- widen the row index to int64 in the activation quantizer, fused
  rotate+quantize, and weight dequantizer kernels; rows*K can exceed 2^31
- enter the operand's device for every MACaQ Triton launch and only latch
  _triton_disabled for permanent failures, not transient OOMs
- keep BLOCK_K >= 64 and rely on the existing K-tail masks so sm_120's
  native MXFP4 dot_scaled compiles
- honor the configured fallback threshold with data-independent MXFP4 RTN
  (identity H, no D1) when an expert receives no calibration tokens
- apply a one-element exp_offsets entry on the RTN path
- translate dynamic={pattern: False} into a `-:` exclusion
- register refusing HF quantizers for macaq, paroquant, qqq, nf12 and
  tt-blockfloat so transformers cannot silently random-initialize them
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