Repository navigation
Conversation
… modules
Add an MXFP4 quantized tensor type and a custom-recipe quantizer factory
that uses MXFP4 weights with MXFP8 activations and gradients in Linear,
LayerNormLinear and GroupedLinear:
* fprop: weight.row(MXFP4) x input.row(MXFP8)
* dgrad: weight.col(MXFP4) x grad_output.row(MXFP8)
* wgrad: input.col(MXFP8) x grad_output.col(MXFP8)
MXFP4 is represented as NVTE_MXFP8_1D_SCALING with kFloat4E2M1 data:
E2M1 values in blocks of 32 with E8M0 scales rounded up. Row-wise and
column-wise data are each quantized from the high-precision weight.
Common:
* MXFP4 quantize and dequantize kernels, dispatched from the MXFP8
scaling path by output dtype, with optional GEMM-swizzled scales.
* CUTLASS SM100 block-scaled GEMM for MXFP4 x MXFP8 (TN and NN),
selected in cublas_gemm; the GEMM reduction dimension and, for dgrad,
the weight input dimension must be multiples of 128.
* Tensor validation and scale swizzling for FP4 MXFP8-scaled tensors.
PyTorch:
* MXFP4Quantizer, MXFP4Tensor and MXFP4TensorStorage with C++
quantizer bindings.
* mxfp4_weight_mxfp8_activation_factory in quantizer_factory_zoo,
usable through CustomRecipe. With Megatron-LM:
--fp8-format e4m3 --fp8-recipe custom
--fp8-quantizer-factory transformer_engine.pytorch.custom_recipes.quantizer_factory_zoo.mxfp4_weight_mxfp8_activation_factory
* GroupedLinear falls back to per-GEMM execution for MXFP4 weights.
Tests cover bitwise-exact quantization, GEMM and module forward/backward
against Python references, CPU offload, backward_override, and TP/SP
distributed numerics.
Signed-off-by: Cael Ling <caell@nvidia.com>
eb4514f to
be50b44
Compare
[Medium impact] The change adds an opt-in mixed-precision training path for three PyTorch linear modules. Fix the early bias rounding before merging. Findings
SummaryAdds MXFP4 weights with MXFP8 activations and gradients for Linear modules.
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart LR
W[High-precision weight] --> WR[Row-wise MXFP4]
W --> WC[Column-wise MXFP4]
X[Input] --> XR[Row-wise MXFP8]
X --> XC[Column-wise MXFP8]
G[Output gradient] --> GR[Row-wise MXFP8]
G --> GC[Column-wise MXFP8]
WR --> F[Forward CUTLASS GEMM]
XR --> F
WC --> D[Input-gradient CUTLASS GEMM]
GR --> D
XC --> B[Weight-gradient cuBLASLt GEMM]
GC --> B
Reviews (7) · Last reviewed commit: "Merge branch 'main' into feat/w4a8-mxfp4..." · Reviewed by Greptile |
MXFP4Tensor inherited the storage view(), which returned a bare MXFP4TensorStorage without autograd and accepted only a single shape argument. Add view() and reshape() through an autograd function that returns an MXFP4Tensor and handle aten.view in __torch_dispatch__. copy_() between MXFP4 tensors copied raw buffers even when one side had GEMM-swizzled scales and the other compact scales, leaving the destination with scales in the wrong order. Requantize the destination when the scale layouts differ. Add tests for view/reshape with gradients and for copies across all combinations of scale layouts. Signed-off-by: Cael Ling <caell@nvidia.com>
|
@cael-ling thx for implementing this. Do you know when will this PR be merged? |
|
@crazyguitar Thanks for your interest. The PR is currently under review, and I'm working with the maintainers to get it merged. Out of curiosity, could you share a bit about your use case — e.g., which model / training setup you're planning to use this with, and roughly what scale? That would help us prioritize and make sure the feature covers what you need. |
Yeah, we are investigating open weight model + QAT like kimi or DSv4. Most of these models are using MXFP4 rather than NVFP4, so we are in favor of using MXFP4 for QAT. This is kimi k3 roadmap:
We are excited if NV can native support it. |
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Address MXFP4 review feedback
|
Thanks for the PR @cael-ling. I have merged in a commit to address a few minor issues. --> Move scale-layout copy tests to test_quantized_tensor.py. |
|
I think there are some merge conflicts we need to resolve. But could we also add tests for transformer_engine/common with respect to the nvte APIs for mxfp4 quantize/dequantize and the cutlass GGEMM? |
Signed-off-by: Cael Ling <caell@nvidia.com> # Conflicts: # tests/pytorch/test_quantized_tensor.py # transformer_engine/common/cast/dispatch/dequantize.cuh # transformer_engine/pytorch/cpp_extensions/gemm.py
Signed-off-by: Cael Ling <caell@nvidia.com>
Signed-off-by: Cael Ling <caell@nvidia.com>
Signed-off-by: Cael Ling <caell@nvidia.com>
for more information, see https://pre-commit.ci
Done in d557c33 for merge the upstream/main;
All tests pass on SM100. |
|
Hi @vthumbe1503, thanks for the review and for the fixes you pushed to the PR, I've addressed the remaining comments, all pushed to the branch. |
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Quantizing into an existing MXFP4 tensor of a different shape launched the kernels with the input dimensions and wrote past the destination data and scale buffers. Signed-off-by: Cael Ling <caell@nvidia.com>
|
cc @rybakov |
|
Is it possible to generalize GEMM dispatch to accept both operands as MXFP4? |
@negvet Yes, I think so. The CUTLASS SM100 kernel is already templated on the operand types, so most of it would carry over. I'd prefer to keep this PR scoped to W4A8, since it has already been reviewed in that form, and do MXFP4 x MXFP4 in a follow-up PR. Does that work for you? cc @vthumbe1503 |
|
@negvet / @cael-ling it seems like CUBLAS already supports MXFP4 GEMM and GGEMM https://docs.nvidia.com/cuda/cublas/index.html#using-the-cublaslt-api. So we can use that directly. But deferring that to different PR makes sense to me. |
|
/te-ci L0 L1 |
| __global__ void broadcast_bias_kernel(OType *out, const BiasType *bias, size_t rows, size_t cols) { | ||
| const size_t idx = blockIdx.x * static_cast<size_t>(blockDim.x) + threadIdx.x; | ||
| if (idx < rows * cols) { | ||
| out[idx] = static_cast<OType>(static_cast<float>(bias[idx % cols])); |
There was a problem hiding this comment.
Description
Adds W4A8 training: MXFP4 weights with MXFP8 activations and gradients in
Linear,LayerNormLinearandGroupedLinear, exposed as a quantizer factory forCustomRecipe.Motivation: RL pipelines whose rollout engine serves MXFP4 weights. Training with the same MXFP4 weight values removes the weight-side mismatch between the policy that generates samples and the policy that is trained. The weight is quantized independently for each GEMM direction from the high-precision parameter, and the row-wise MXFP4 data used by fprop is what a rollout engine would load.
Usage:
With Megatron-LM, no Megatron changes are required:
Type of change
Changes
Please list the changes introduced in this PR:
Checklist: