Skip to content

perf(macaq): keep the MXFP4 exponent screen off int64 planes (#4820) - #4840

Merged
Qubitium merged 1 commit into
mainfrom
fix/macaq-narrow-screen-index
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/macaq-narrow-screen-index

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

MACaQ's MXFP4 exponent screen materialized every per-weight plane as int64 for values that only span 0..14:

  • _e2m1_code_from_scaled built a 7-deep torch.where tree and ended in .long();
  • mxfp4_rtn_signed_index then built signed_index with two more int64 torch.wheres;
  • screen_block_exponents runs that for three exponent offsets and keeps an int64 best_index.

On an 8B down_proj (4096 x 14336) each plane is 448 MiB, so the screen alone peaked at 4195 MiB above its inputs and retained a 679 MiB result.

What Changed

  • gptqmodel/quantization/macaq.py
    • _e2m1_code_from_scaled accumulates the four-bit code in uint8 across the same seven thresholds (same > / >= tie rule, same NaN/inf behavior) instead of a nested torch.where ending in .long().
    • mxfp4_rtn_signed_index builds the signed grid in uint8 and widens once to int32 — the narrowest dtype PyTorch accepts as a gather index. (uint8 cannot be used directly: PyTorch reads it as a boolean mask.)
  • tests/test_macaq.py — 4 new nodes: mxfp4_rtn_signed_index and screen_block_exponents bitwise against an independent int64 reference over random / NaN-inf-±0-extremes / exact-threshold fixtures in both tie modes, plus a CUDA peak-memory guard.

No numeric change: the screen's index, E8M0 exponent and scale element are bitwise identical, and the returned index dtype is narrower but still a valid gather index everywhere it flows (flip_refine_levels, pack_mxfp4_from_signed_index, grid[signed_index]).

Tests

cd tests
python -m pytest test_macaq.py -q -ra
# 95 passed, 5 skipped
#   skips are pre-existing: compiled flips unqualified on this GPU (4),
#   and the 6 GiB row-offset case

python -m pytest test_macaq.py test_macaq_forward_dtype_4217.py \
    test_macaq_fp32_reduce_4221.py test_macaq_rotation_serialization_4225.py \
    test_macaq_single_output_4472.py test_macaq_triton_forward_dtype_4315.py -q -ra
# 147 passed, 5 skipped

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

Red / green evidence

Reverting only the macaq.py hunk, keeping the new tests:

Test node Broken code With fix
test_mxfp4_rtn_signed_index_matches_the_int64_reference[False] FAIL assert torch.int64 is torch.int32 PASS
test_mxfp4_rtn_signed_index_matches_the_int64_reference[True] FAIL same PASS
test_screen_block_exponents_matches_the_int64_reference FAIL same PASS
test_screen_block_exponents_peak_memory_stays_bounded FAIL same PASS

Red run: 4 failed. Green run: 4 passed (and 95 passed, 5 skipped for the file).

The bitwise comparisons themselves pass on both sides by design — they are the safety net proving the narrowing is a no-op numerically. The red assertions are the int64→int32 plane dtype and the peak-memory bound.

Measured (same host, RTX-class CUDA, screen_block_exponents)

weight shape before after
4096 x 14336 (8B down_proj) 4195 MiB extra peak, 679 MiB result 2011 MiB extra peak (−52%), 455 MiB result
1024 x 4096 300 MiB extra peak, 48 MiB result 144 MiB extra peak (−52%), 32 MiB result

The 1024x4096 numbers are what the CUDA test asserts (extra_peak < 200 MiB).

Kernel Accuracy Requirements

Bitwise, not approximate: the reference in tests/test_macaq.py implements the OCP MXFP4 round-to-nearest definition independently (own thresholds, own E2M1 grid, own signed-grid build) and the comparison is allclose(rtol=0, atol=0, equal_nan=True) on the index, the E8M0 exponent and the scale element. Fixtures cover random weights, exact threshold values in both tie modes, and a row with 0.0, -0.0, 1e-12, ±65504, NaN and inf.

The 4096x14336 and 1024x4096 peaks above are real-device measurements, not estimates.

Logic Flow Documentation

Not applicable: a localized dtype change inside two functions. No stage, dispatch, selection order or tensor ownership changes — the screen still builds one candidate per offset and keeps the minimum-error one.

Model Support / Module Plan

Not applicable — no MODEL_MAP entry, definition or fusion config is touched.

Review Requirements

Notes

  • int32 rather than the issue's conservative "cast back to int64" variant: it also shrinks the retained best_index (679 -> 455 MiB) and works everywhere the index flows (verified by indexing E2M1_VALUES with int32 on this torch build).
  • Not addressed here: scale_element is still a contiguous fp32 [out, in] per offset. Returning the [out, groups, 1] broadcast view would save a further 4 B/weight per offset but changes what callers receive, so it is left for a follow-up.

_e2m1_code_from_scaled built a 7-deep torch.where tree ending in .long(), and
mxfp4_rtn_signed_index built the signed grid in int64, for values that only
span 0..14. The exponent screen runs that for three offsets and keeps an int64
best_index, so on an 8B down_proj every plane was 448 MiB.

Accumulate the four-bit codes in uint8, build the signed grid in uint8, and
widen once to int32 - the narrowest dtype PyTorch accepts as a gather index.
Bitwise identical output; extra peak above the inputs on 4096x14336 drops from
4195 to 2011 MiB and the retained result from 679 to 455 MiB.
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.

1 participant