Skip to content

feat(pytorch): backport MXFP4 weight QAT - #1

Open
guapisolo wants to merge 1 commit into
miles-mainfrom
codex/mxfp4-weight-qat
Open

guapisolo wants to merge 1 commit into
miles-mainfrom
codex/mxfp4-weight-qat

Conversation

@guapisolo

Copy link
Copy Markdown
Collaborator

Summary

Add MXFP4 weight QAT using the existing MXFP8 and block-scaled FP8 compute paths.

Motivation

Training on unprojected weights does not expose the optimizer to MXFP4 weight rounding. This brings MXFP4 weight projection to miles-main while retaining high-precision master parameters and the host recipe's activation and gradient handling.

Usage

import transformer_engine.pytorch as te
from transformer_engine.common.recipe import MXFP4QATMXFP8BlockScaling

with te.autocast(enabled=True, recipe=MXFP4QATMXFP8BlockScaling()):
    loss = model(inputs).sum()
loss.backward()

Use BF16/FP32 master weights. Modules also support MXFP4QATFloat8BlockScaling; the operations API retains its MXFP8-only QAT support boundary.

Design Notes

  • Mental model: The recipe enables module/ops weight preparation to project each master onto the 1x32 MXFP4 grid before host quantization and GEMM; autograd returns gradients to the master while activations keep host quantization.
  • high_precision backward intentionally uses the unprojected master; FSDP2 replay reconstructs the projected operand for other modes.
  • Recipe snapshots invalidate stale workspaces on in-place MXFP8 QAT toggles; fusion keeps one recipe signature.

Adapted from NVIDIA/TransformerEngine#3264 by @xiuhu17 at 3a472d9b, including mxfp4_fake_quantize, recipes, FSDP2 replay and tests. GTP integration is omitted because this fork has no DistributedWeight protocol. The commit retains the original author's Co-authored-by credit.

Verification

  • CPU probes executing extracted production metadata/fuser methods passed, reproducing both stale-cache transitions upstream before verifying their repair; CUDA allocation/ops were stand-ins, proving control flow only.
  • Changed Python: AST parsing, Black 24.4.2, Ruff F,E9, production Pylint 3.3.1 passed.
  • Changed C++/CUDA: clang-format 18.1.6 and cpplint passed.
  • Repository license, shell syntax and whitespace checks passed.
  • GPU cache-toggle/rejection regressions plus kernel/module/ops/FSDP2 suites are included in QA but not executed here; CUDA build, GPU numerics, FSDP2, TP/Userbuffers remain unverified on this host without CUDA.

Upstream's reported GPU passes do not validate this port.

Review Focus

  • Cached weights and FSDP2 replay must preserve forward projection while keeping the optimizer master intact.
  • Validate CUDA numerics and the supported module/ops matrix on Blackwell before merge.

Project master weights onto the MXFP4 grid before existing MXFP8 or
block-scaled FP8 compute, preserving high-precision optimizer parameters
and the host recipe's backward policy. Adapt module/FSDP2 and ops fusion
paths to miles-main without the absent upstream GTP protocol.

Invalidate cached weight workspaces when a mutable MXFP8 recipe changes
its QAT flag. Keep a single recipe signature for fusion state, and cover
in-place cache transitions plus the existing ops block-scaling rejection.

Adapted from NVIDIA#3264 at
3a472d9.

Co-authored-by: zhihaow6 <zhihaow6@illinois.edu>
Signed-off-by: Jiajun Li <guapisolo@gmail.com>
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