Skip to content

[PyTorch] Handle unpadded dim-0 MXFP8 scales in copy_ - #3992

Open
VikingDeng wants to merge 2 commits into
NVIDIA:mainfrom
VikingDeng:fix/mxfp8-new-zeros-scale-padding
Open

VikingDeng wants to merge 2 commits into
NVIDIA:mainfrom
VikingDeng:fix/mxfp8-new-zeros-scale-padding

Conversation

@VikingDeng

@VikingDeng VikingDeng commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4021.

MXFP8Tensor.new_zeros intentionally keeps dim 0 of _rowwise_scale_inv and _columnwise_scale_inv unpadded for FSDP2 (matching the unpadded-input swizzle path from #3768), while aten.split.Tensor and MXFP8Quantizer.make_empty allocate dim-0-padded scale buffers. When first_dim is not a multiple of 128 (such as [64, 128] FSDP2 shards from fully_shard(te.Linear(128, 128)) on 2 GPUs), sharded_param.new_zeros(...).copy_(sharded_param) in FSDP2 _init_sharded_param fails with a scale shape mismatch in aten.copy_.default.

Changes

  • transformer_engine/pytorch/tensor/mxfp8_tensor.py: keep aten.new_zeros.default unpadded along dim 0, and update aten.copy_.default for unswizzled MXFP8Tensor copies to copy the active dim-0 scale prefix ([:active_rows]) and zero any trailing destination pad rows when dim-0 padding differs. Validate both scale buffers before writing any destination buffers, and route FP8 dtype or logical shape mismatches through dequantize -> expand_as -> quantize_.
  • tests/pytorch/test_quantized_tensor.py: add CPU tests covering padded/unpadded new_zeros and copy_, the torch.chunk -> new_zeros -> copy_ FSDP2 shard initialization sequence, non-mutating rejection of incompatible scale shapes, and dtype/shape mismatch fallback.

Verification

  • pytest tests/pytorch/test_quantized_tensor.py -k "test_mxfp8_new_zeros_copies_unpadded_scales or test_mxfp8_copy_preserves_equal_shape_scale_padding or test_mxfp8_fsdp_shard_copy_from_padded_scales or test_mxfp8_copy_rejects_incompatible_scale_shapes or test_mxfp8_copy_falls_back_for_incompatible_tensors": 20 passed (PyTorch 2.14.1, CPU).
  • pre-commit run --files transformer_engine/pytorch/tensor/mxfp8_tensor.py tests/pytorch/test_quantized_tensor.py: passed.
  • pylint==4.1.3 --rcfile=pylintrc transformer_engine/pytorch/tensor/mxfp8_tensor.py: 10.00/10.

@VikingDeng
VikingDeng marked this pull request as ready for review December 3, 2026 05:09
@VikingDeng
VikingDeng requested a review from ksivaman as a code owner December 3, 2026 05:09
@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The changes affect PyTorch callers that copy MXFP8 tensors.

Keep scale padding for swizzled tensors before merging.

Summary

The PR changes how MXFP8Tensor.copy_ works with padded and unpadded scales.

  • MXFP8 copies accept compatible scale buffers with different row padding.
  • MXFP8 copies convert values when formats or shapes differ.

Reviews (2) · Last reviewed commit: "[PyTorch] Handle unpadded dim-0 MXFP8 sc..." · Reviewed by Greptile

@vthumbe1503 vthumbe1503 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First Dimension not being padded is intended for FSDP2. Swizzling kernel can directly consume unpadded scales and produce padded swizzled scales needed for the GEMMs.

This is done so that padding will avoid the additional performance penalty

Here is the PR that added that support #3768

@VikingDeng VikingDeng changed the title [PyTorch] Pad MXFP8Tensor.new_zeros scale tensors along dim 0 [PyTorch] Handle unpadded dim-0 MXFP8 scales in copy_ Dec 5, 2026
@VikingDeng

Copy link
Copy Markdown
Contributor Author

Thanks for the context on #3768, @vthumbe1503. That makes sense.

In #4021, FSDP2 _init_sharded_param still hits a shape mismatch on [64, 128] shards because aten.split.Tensor returns dim-0-padded scales ([128, 4] / [4, 128], which test_chunk still expects for dequantize), new_zeros returns unpadded scales ([64, 4] / [2, 128]), and aten.copy_.default assumed identical scale shapes.

I pushed a follow-up commit (9dfe36ca) that restores unpadded dim 0 in new_zeros and instead updates aten.copy_.default to copy the active dim-0 scale rows (and zero any trailing destination pad rows) for unswizzled MXFP8Tensor copies. Ready for another look whenever you have time.

@greptile-apps

greptile-apps Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Comments Outside Diff

These findings could not be posted inline.

  • P1 Swizzled tensor copies fail transformer_engine/pytorch/tensor/mxfp8_tensor.py:619 ▶

    new_zeros now removes scale padding even when the source has _with_gemm_swizzled_scales=True. For a supported (64, 128) source, it allocates scales shaped (64, 4) and (2, 128) instead of (128, 4) and (4, 128), but keeps the swizzled flag.

    As a result, src.new_zeros(src.shape).copy_(src) raises Incompatible MXFP8 scale_inv shapes: the new copy path only permits different scale sizes for unswizzled tensors. Keep the padded sizes when the swizzled flag is set.

This branch has not been deployed

No deployments
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.

FSDP2 initialization fails when MXFP8Tensor.new_zeros allocates shorter scale buffers than split

2 participants