feat(megamoe): add fp4 group16 to group32 requant path

Add a utility and synthetic benchmark path for converting FP4 group16 tensors into group32 tensors that the existing SM100 block32 MegaMoE kernels can consume.

Document that this is a requantization path rather than a lossless metadata rewrite, so GLM-5.2 accuracy validation is still required before production use.

Tested: PYTHONPYCACHEPREFIX=/private/tmp/deepgemm_pycache python3 -m py_compile deep_gemm/utils/math.py tests/test_layout.py tests/test_mega_moe.py

Tested: git diff --check

Not-tested: CUDA build, SM100/B300 runtime, and GLM-5.2 accuracy validation are not available locally.
This commit is contained in:
LuminolT
2026-07-08 18:34:33 +08:00
parent 79fcfd6abf
commit 8ad348fb11
4 changed files with 72 additions and 7 deletions

View File

@@ -140,4 +140,25 @@ def cast_back_from_fp4(packed: torch.Tensor, sf: torch.Tensor, gran_k: int = 128
x_dequantized = _dequantize_from_fp4_e2m1(unpacked)
group_idx = torch.arange(n, device=packed.device) // gran_k
x_restored = x_dequantized * sf[:, group_idx]
return x_restored
return x_restored
def requant_fp4_to_gran_k(packed: torch.Tensor, sf: torch.Tensor, src_gran_k: int,
dst_gran_k: int, use_packed_ue8m0: bool = False) -> Tuple[torch.Tensor, torch.Tensor]:
assert packed.dtype == torch.int8
assert packed.dim() >= 2
assert sf.shape[:-1] == packed.shape[:-1]
original_packed_shape = packed.shape
original_sf_shape = sf.shape
packed_2d = packed.reshape(-1, packed.size(-1))
sf_2d = sf.reshape(-1, sf.size(-1))
restored = cast_back_from_fp4(packed_2d, sf_2d, src_gran_k, use_packed_ue8m0)
repacked, rescaled = per_token_cast_to_fp4(
restored, use_ue8m0=use_packed_ue8m0, gran_k=dst_gran_k,
use_packed_ue8m0=use_packed_ue8m0)
return (
repacked.reshape(original_packed_shape),
rescaled.reshape(*original_sf_shape[:-1], rescaled.size(-1)),
)