Skip to content

Improve the dimension checks for the FP8 recipes - #2894

Merged
ptrendx merged 5 commits into
NVIDIA:mainfrom
ptrendx:pr_relax_dim_checks
Nov 6, 2026
Merged

ptrendx merged 5 commits into
NVIDIA:mainfrom
ptrendx:pr_relax_dim_checks

Conversation

@ptrendx

@ptrendx ptrendx commented Apr 16, 2026

Copy link
Copy Markdown
Member

Description

Improved the dimension checks for the FP8 recipes. The old one was too restrictive and did not have any recipe-aware logic.

Dimension check changes

Layer Recipe Before After
Python assert_dim_for_fp8_exec (utils.py) all FP8/FP4 M % 8 == 0 ∧ last_dim % 16 == 0, run before the recipe is known Removed — guard was recipe-agnostic and stricter than any underlying kernel required
C++ MXFP8Quantizer::create_tensor / get_scale_shape MXFP8 first_dim % 32 ∧ last_dim % 32 last_dim % 16 only (TMA 16 B alignment). Scale tensor uses DIVUP for partial trailing blocks
C++ NVFP4Quantizer::create_tensor / get_scale_shape NVFP4 first_dim % 32 ∧ last_dim % 32 last_dim % 32 always; first_dim % 16 only when RHT is enabled (Hadamard block size)
C++ swizzle_block_scaling.cu kernel Float8 1×128 block data_rows % 4 == 0 Unchanged — real kernel requirement; error message clarified with actual data_rows
C++ cublaslt_gemm.cu MXFP8 path MXFP8 No explicit check — cuBLAS returns opaque CUBLAS_STATUS_NOT_SUPPORTED` / "no algo found" lda % 16 == 0 ∧ ldb % 16 == 0 via min_alignment_elements(dtype) using typeToNumBits (16 B alignment for FP8)
C++ cublaslt_gemm.cu NVFP4 path NVFP4 No explicit check — opaque cuBLAS error lda % 32 == 0 ∧ ldb % 32 == 0 (16 B alignment for FP4 = 32 elements)
C++ cublaslt_gemm.cu 1D block-scaled FP8 path Float8BlockScaling 1×128 No explicit check n % 8 == 0 for the native 1D block-scaling path
Python is_quantizable → supports_quantized_allgather (mxfp8_tensor.py) MXFP8 Both-dim % 32 Renamed. last_dim % 16; also first_dim % 32 when columnwise usage (block boundary across ranks)
Python is_quantizable → supports_quantized_allgather (nvfp4_tensor.py) NVFP4 Both-dim % 32 Renamed. last_dim % 32; also first_dim % 16 when columnwise usage

Fixes # (issue)

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

  • Change A
  • Change B

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@ptrendx
ptrendx requested a review from timmoon10 April 16, 2026 20:26
@greptile-apps

greptile-apps Bot commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes affect low-precision GEMM and distributed gather workflows across several recipes.

The PR still needs the remaining new_zeros scale padding fixed before merging.

Findings

  1. P1 Zero tensors read past scales ▶

Summary

The PR replaces shared FP8 dimension checks with recipe-specific checks.

  • FP8 recipes check dimensions at the GEMM they use.
  • MXFP8 tensors keep scale data for partial blocks.
  • NVFP4 tensors keep scale data for partial blocks.
  • Quantized gathers check whether shard shapes fit their scale blocks.

Reviews (20) · Last reviewed commit: "Move the test" · Reviewed by Greptile

Comment thread tests/pytorch/test_numerics.py Resolved
@ptrendx

ptrendx commented Apr 16, 2026

Copy link
Copy Markdown
Member Author

/te-ci

Comment thread tests/pytorch/test_numerics.py Resolved
Comment thread tests/pytorch/test_numerics.py Resolved
Comment thread tests/pytorch/test_numerics.py Resolved
Comment thread tests/pytorch/test_numerics.py Resolved
@pggPL

pggPL commented May 15, 2026

Copy link
Copy Markdown
Collaborator

Please update the docs also

@ptrendx
ptrendx requested a review from ksivaman as a code owner May 21, 2026 00:20
@kainzhong

Copy link
Copy Markdown
Collaborator

Merged main and resolved conflicts for @ptrendx

@ptrendx

ptrendx commented Aug 31, 2026

Copy link
Copy Markdown
Member Author

/te-ci

@kainzhong kainzhong 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.

CC told me that in this branch tests/pytorch/test_recipe.py:593-594 still uses the deleted is_quantizable() method. Other parts seem fine.

@ptrendx

ptrendx commented Sep 17, 2026

Copy link
Copy Markdown
Member Author

/te-ci

1 similar comment
@ptrendx

ptrendx commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

/te-ci

@greptile-apps

greptile-apps Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Comments Outside Diff

These findings could not be posted inline.

  • P0 flat first dim - Residual NameError after removing the old assertion block. The PR removed flat first dim = math.prod shape -1 along with the old assertions, but this expression at line 344 was left unreplaced. When self.row scaled nvfp4 is True and self.rowwise usage is therefore also True , so this branch is reached , Python evaluates the true-branch of the ternary and raises NameError name 'flat first dim' is not defined . The shape 2d occurrence at line 356 was fixed in this PR; this one was not. transformer_engine/pytorch/tensor/nvfp4_tensor.py:344 ▶

    flat_first_dim - Residual NameError after removing the old assertion block. The PR removed flat_first_dim = math.prod(shape[:-1]) along with the old assertions, but this expression at line 344 was left unreplaced. When self.row_scaled_nvfp4 is True (and self.rowwise_usage is therefore also True, so this branch is reached), Python evaluates the true-branch of the ternary and raises NameError: name 'flat_first_dim' is not defined. The shape_2d occurrence at line 356 was fixed in this PR; this one was not.

                # Allocate global amax metadata. Row-scaled NVFP4 stores one value per row.
                amax_rows = math.prod(shape[:-1]) if self.row_scaled_nvfp4 else 1
    
  • P0 flat first dim undefined - NameError at runtime transformer_engine/pytorch/tensor/nvfp4_tensor.py:340 ▶

    flat_first_dim is referenced here but is no longer defined in make_empty(). The old code set it just before the removed assertion:

    # OLD (removed):
    flat_first_dim = math.prod(shape[:-1])
    assert flat_first_dim % NVFP4_BLOCK_SCALING_SIZE == 0, ...

    The new code dropped the whole block, including the assignment, so any call to make_empty() with self.columnwise_usage = True will raise NameError: name 'flat_first_dim' is not defined.

                shape_2d = tuple([math.prod(shape[:-1]), shape[-1]])
    
  • P1 Floor-div in fsdp pre all gather inconsistent with ceildiv scale allocation transformer_engine/pytorch/tensor/mxfp8_tensor.py:676 ▶

    This line computes the unpadded columnwise scale-inv dim-0 using floor-division, but make_empty and get_scale_shape were changed in this PR to use ceildiv. When first_dim % 32 ≠ 0 (e.g. first_dim = 16, now allowed by the relaxed constraint), the slice target becomes 16 // 32 = 0 - an empty tensor - corrupting FSDP2 + columnwise MXFP8 training.

                flattened_in_shape0 = math.ceil(math.prod(shape[:-1]) / MXFP8_BLOCK_SCALING_SIZE)
    
  • P1 get scale shape has same floor-division bug as make empty transformer_engine/pytorch/tensor/mxfp8_tensor.py:204 ▶

    get_scale_shape is called by grouped tensor storage, utilities (tensor/utils.py lines 449, 488, 1012), and cast.cpp extensions - all of which would receive zero-sized shapes when last_dim = 16 or flat_first_dim < 32. The fix mirrors what NVFP4Quantizer.get_scale_shape already does: replace // with math.ceil(... / ...).

            if columnwise:
                return (
                    round_up_to_nearest_multiple(
                        math.ceil(math.prod(shape[:-1]) / MXFP8_BLOCK_SCALING_SIZE), 4
                    ),
                    round_up_to_nearest_multiple(shape[-1], 128),
                )
            return (
                round_up_to_nearest_multiple(math.prod(shape[:-1]), 128),
                round_up_to_nearest_multiple(
                    math.ceil(shape[-1] / MXFP8_BLOCK_SCALING_SIZE), 4
                ),
            )
    
  • P1 Integer division breaks scale shape when last dim = 16 transformer_engine/pytorch/tensor/mxfp8_tensor.py:143 ▶

    make_empty uses // (floor division) to compute scale-inv dimensions, but the dimension constraint was relaxed to % 16. When shape[-1] = 16 and MXFP8_BLOCK_SCALING_SIZE = 32, both 16 // 32 = 0 and math.prod(shape[:-1]) // 32 = 0 (for flat_first_dim < 32), producing a zero-sized scale tensor. The C++ counterpart was correctly updated to ceildiv, and NVFP4Quantizer.get_scale_shape was similarly updated to math.ceil - but MXFP8Quantizer.make_empty was not. The distributed all-gather path in distributed.py calls quantizer.make_empty(out_shape, ...) directly, so this would silently allocate a broken output buffer.

                scale_inv = torch.empty(
                    round_up_to_nearest_multiple(math.prod(shape[:-1]), 128),
                    round_up_to_nearest_multiple(math.ceil(shape[-1] / MXFP8_BLOCK_SCALING_SIZE), 4),
                    dtype=torch.uint8,
                    device=device,
                    pin_memory=pin_memory,
                )
    

    The columnwise case at line 143 needs the same fix:

    round_up_to_nearest_multiple(math.ceil(math.prod(shape[:-1]) / MXFP8_BLOCK_SCALING_SIZE), 4),
  • P2 new zeros dispatch falls back for shapes that are now valid transformer_engine/pytorch/tensor/mxfp8_tensor.py:569 ▶

    The guard still requires both first_dim % MXFP8_BLOCK_SCALING_SIZE == 0 and last_dim % MXFP8_BLOCK_SCALING_SIZE == 0 (i.e. % 32). A valid MXFP8 tensor with last_dim = 16 now satisfies the relaxed constraint but fails this check, silently falling back to a standard new_zeros and losing FP8 scale data. The fallback condition and the scale-shape calculation below it both need the same ceildiv treatment applied elsewhere in this PR.

@ptrendx

ptrendx commented Sep 28, 2026

Copy link
Copy Markdown
Member Author

/te-ci

@ptrendx

ptrendx commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

/te-ci

ptrendx added a commit to ptrendx/TransformerEngine that referenced this pull request Nov 4, 2026
Integrate recipe-aware FP8 and FP4 dimension checks while preserving
main's current NVFP4 scale-swizzling helper.

Signed-off-by: Przemek Tredak <ptredak@nvidia.com>
Signed-off-by: Przemek Tredak <ptredak@nvidia.com>
@ptrendx
ptrendx force-pushed the pr_relax_dim_checks branch from 5f85ea7 to 99bcbd6 Compare November 4, 2026 19:41
@ptrendx

ptrendx commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

/te-ci

Comment on lines 567 to 573
rowwise_scale_inv_shape = [
first_dim,
ceil_div(last_dim, MXFP8_BLOCK_SCALING_SIZE),
]
columnwise_scale_inv_shape = [
first_dim // MXFP8_BLOCK_SCALING_SIZE,
ceil_div(first_dim, MXFP8_BLOCK_SCALING_SIZE),
last_dim,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Zero tensors read past scales

The relaxed new_zeros branch returns MXFP8 tensors with unpadded scale buffers. For (16, 16), it allocates rowwise scales of shape (16, 1). But MXFP8 dequantization uses a padded stride of 4 and reads scales for a full tile. Calling .dequantize() on this newly accepted result can read past the scale buffer.

Allocate scales with get_scale_shape, including its padding, rather than using only ceil-division.

timmoon10
timmoon10 previously approved these changes Nov 4, 2026

@timmoon10 timmoon10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall LGTM, with several nits.

- Float8BlockScaling swizzle requires data_rows (first dim) % 4.
- NVFP4 RHT requires input first dim % 16 (subsumed by 32-elt alignment for training).
"""
if recipe_obj.delayed() or recipe_obj.float8_current_scaling():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we also test BF16 GEMMs?

Suggested change
if recipe_obj.delayed() or recipe_obj.float8_current_scaling():
if recipe is None:
return (1, 8, 8, 8, 8, 8)
if recipe_obj.delayed() or recipe_obj.float8_current_scaling():

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why? This whole PR is about the quantized gemms.

Comment thread tests/pytorch/test_quantized_tensor.py Resolved
Signed-off-by: Przemek Tredak <ptredak@nvidia.com>
ptrendx and others added 2 commits November 4, 2026 17:15
Co-authored-by: Tim Moon <4406448+timmoon10@users.noreply.github.com>
Signed-off-by: Przemyslaw Tredak <ptrendx@gmail.com>
Co-authored-by: Tim Moon <4406448+timmoon10@users.noreply.github.com>
Signed-off-by: Przemyslaw Tredak <ptrendx@gmail.com>
Signed-off-by: Przemek Tredak <ptredak@nvidia.com>
@ptrendx

ptrendx commented Nov 5, 2026

Copy link
Copy Markdown
Member Author

/te-ci

1 similar comment
@ptrendx

ptrendx commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

/te-ci

@ptrendx
ptrendx merged commit 89a4a3f into NVIDIA:main Nov 6, 2026
37 of 38 checks passed
bhargavchintam added a commit to bhargavchintam/TransformerEngine that referenced this pull request Nov 8, 2026
Since NVIDIA#2894, MXFP8 accepts a last dimension that is a multiple of 16,
and a partial last block keeps its own scale, so rowwise tensors carry
ceil(K / 32) scale columns. export_components now returns all of them
instead of K // 32. Tests cover K = 48 and K = 16, including rows that
are not a multiple of 32 and GEMM-swizzled scales.

Signed-off-by: Bindu Bhargava R Chintam <bindubhargavareddy@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.

4 participants