Repository navigation
perf(macaq): keep the MXFP4 exponent screen off int64 planes (#4820) - #4840
Merged
Merged
Conversation
_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.
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.
Summary
MACaQ's MXFP4 exponent screen materialized every per-weight plane as int64 for values that only span 0..14:
_e2m1_code_from_scaledbuilt a 7-deeptorch.wheretree and ended in.long();mxfp4_rtn_signed_indexthen builtsigned_indexwith two more int64torch.wheres;screen_block_exponentsruns that for three exponent offsets and keeps an int64best_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_scaledaccumulates the four-bit code in uint8 across the same seven thresholds (same>/>=tie rule, same NaN/inf behavior) instead of a nestedtorch.whereending in.long().mxfp4_rtn_signed_indexbuilds 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_indexandscreen_block_exponentsbitwise 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
ruff check gptqmodel docs/eora tests setup.py: All checks passed.Red / green evidence
Reverting only the
macaq.pyhunk, keeping the new tests:test_mxfp4_rtn_signed_index_matches_the_int64_reference[False]assert torch.int64 is torch.int32test_mxfp4_rtn_signed_index_matches_the_int64_reference[True]test_screen_block_exponents_matches_the_int64_referencetest_screen_block_exponents_peak_memory_stays_boundedRed run:
4 failed. Green run:4 passed(and95 passed, 5 skippedfor 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)down_proj)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.pyimplements the OCP MXFP4 round-to-nearest definition independently (own thresholds, own E2M1 grid, own signed-grid build) and the comparison isallclose(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 with0.0,-0.0,1e-12,±65504,NaNandinf.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_MAPentry, definition or fusion config is touched.Review Requirements
tests/test_macaq.py, which already builds independent MXFP4 oracles).E2M1_VALUES,flip_refine_levelsstill does+= 1/-= 1in place, andpack_mxfp4_from_signed_indexstill casts to uint8 for the nibbles.Notes
int32rather than the issue's conservative "cast back to int64" variant: it also shrinks the retainedbest_index(679 -> 455 MiB) and works everywhere the index flows (verified by indexingE2M1_VALUESwith int32 on this torch build).scale_elementis 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.