Skip to content

[MEM] MACaQ MXFP4 exponent screen materializes every code/index plane as int64 (values 0..14): screen_block_exponents 4.2 GiB on an 8B down_proj, 2.1 GiB with int32/uint8 (bitwise-identical); 8B layer peak 12.9 -> 10.7 GiB #4820

Closed

Description

@Qubitium

Found on main @ dba142d, RTX 4090 (CUDA 13.2, torch 2.15).

What is wrong

MACaQ's MXFP4 exponent screen builds every per-weight intermediate as int64, although the values are 0..14:

  • _e2m1_code_from_scaled (quantization/macaq.py:172-197) builds a 7-deep torch.where tree from Python ints and ends in .long();
  • mxfp4_rtn_signed_index (:200-238) then builds signed_index with two more int64 torch.wheres.

screen_block_exponents (:241-286) does this for 3 exponent offsets and keeps best_index int64 too. On an 8B down_proj (4096 x 14336), each int64 plane is 448 MiB. The screen alone peaks at about 4.1 GiB above its inputs. The default processor runs a layer's modules concurrently, so this is a large part of MACaQ's 8B layer peak: 12.9 GiB, 3.2x GPTQ's 4.0 GiB on the same layers.

This is separate from #4801, the compiled flip-sweep frame cycle that retains memory after the call. Here the waste is the live working set of the screen itself.

Measured

screen_block_exponents on a random 4096x14336 weight (Llama-3.1-8B down_proj shape), extra peak above inputs:

current (int64 codes/indices)                           4195 MiB, 0.25 s, result 679 MiB
int32 signed index, uint8 accumulation of the 7 thresholds 2123 MiB, 0.12 s, result 455 MiB
-> best_index, best_log2 and best_scale are bitwise equal (indices compared after .long())

End to end, Llama-3.1-8B-Instruct truncated to 2 layers, MacaqConfig(), 64 C4 samples. The patch only changes mxfp4_rtn_signed_index, and the screen result is cast back to int64 so nothing downstream changes:

main:     peak allocated 12944 MiB, peak reserved 13930 MiB
patched:  peak allocated 10656 MiB (-2.2 GiB, -18%), peak reserved 12630 MiB

Minimal repro

import torch
from gptqmodel.quantization.macaq import screen_block_exponents
w = torch.randn(4096, 14336, device="cuda") * 0.02
hd = torch.rand(14336, device="cuda") + 0.1
a0 = torch.cuda.memory_allocated(); torch.cuda.reset_peak_memory_stats()
idx, log2, scale = screen_block_exponents(w, hd)
print(idx.dtype, (torch.cuda.max_memory_allocated() - a0) / 2**20)   # torch.int64 4195.0

Suggested fix (the measured variant)

def _e2m1_code_from_scaled(abs_scaled, *, tie_high=False):
    code = torch.zeros(abs_scaled.shape, dtype=torch.uint8, device=abs_scaled.device)
    for t in (0.25, 0.75, 1.25, 1.75, 2.5, 3.5, 5.0):
        code += (abs_scaled >= t) if tie_high else (abs_scaled > t)
    return code
# mxfp4_rtn_signed_index:
code = _e2m1_code_from_scaled((grouped / scale).abs_(), tie_high=tie_high).to(torch.int32)
signed = torch.where(grouped < 0, 7 - code, 7 + code); signed.masked_fill_(code == 0, 7)

grid[signed_index] accepts int32 indices. Going further, the following would cut the screen to well under 1 GiB at this shape:

  • keep signed_index int32 (or uint8 with a cast at the gather) through flip_refine_levels;
  • return scale_element as the broadcast view of [out, groups, 1] instead of a contiguous fp32 [out, in], since it is 4 B/weight per offset.

Test that should have caught it

tests/test_macaq*.py: assert that screen_block_exponents on a 1024x4096 weight stays under about 1.5x of (fp32 weight bytes + int32 index bytes) of extra peak, and that its output is bitwise equal to a reference.

Severity

Medium-low: about 2.2 GiB (18%) of MACaQ's 8B per-layer peak, with no numeric change and a 2x faster screen.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-11788dff99fd finished this issue.

    Pull request: #4840

    Reproducing the int64 exponent screen; will move the code/index planes to uint8/int32 and prove bitwise-identical output plus a lower peak

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Fixed by #4840 (merged). _e2m1_code_from_scaled now accumulates the four-bit code in uint8, mxfp4_rtn_signed_index widens once to int32, and the screen's index/exponent/scale stay bitwise identical.

    Measured on the same host for screen_block_exponents: 4096x14336 extra peak 4195 -> 2011 MiB (-52%), retained result 679 -> 455 MiB; 1024x4096 extra peak 300 -> 144 MiB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions