Skip to content

[PyTorch] Keep NVFP4 and MXFP8 tensor repr data-free under tracing - #3951

Open
rlaope wants to merge 1 commit into
NVIDIA:mainfrom
rlaope:fix-quantized-repr-dynamic-dims
Open

rlaope wants to merge 1 commit into
NVIDIA:mainfrom
rlaope:fix-quantized-repr-dynamic-dims

Conversation

@rlaope

@rlaope rlaope commented Nov 27, 2026

Copy link
Copy Markdown

Description

NVFP4Tensor.__repr__ and MXFP8Tensor.__repr__ dequantize their data. During torch.compile, AOTAutograd formats its forward metadata with repr() of the fake outputs whenever the structured trace logger (torch.__trace) has a handler. Under tracing that dequantize adds shape guards (Eq(s0, 128) from _FromNVFP4Func.forward). Dims marked dynamic get specialized and compilation fails:

torch.fx.experimental.symbolic_shapes.ConstraintViolationError: Constraints violated (L['inp'].size()[0])!
  - You marked L['inp'].size()[0] as dynamic but your code specialized it to be a constant (128).

Without mark_dynamic it costs one recompile per input shape instead (4 graphs for 4 row counts instead of 2).

The trace logger gets a handler from:

  • TORCH_TRACE=<dir> (tlparse), with plain Python and no pytest.
  • pytest >= 9.2. Its log capture now also attaches to non-propagating loggers, and torch.__trace is one of them. That is why test_te_ops_bias_nvfp4_layout[rht|2d|row-scaled] fails under pytest 9.2 and passes under the pinned pytest 8.4.2.

Float8Tensor already returns the metadata-only repr for fake or meta _scale_inv for the same reason (#3053). This PR applies the same check to NVFP4 and MXFP8, using the data buffer. Eager repr() is unchanged.

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

  • NVFP4Tensor.__repr__ and MXFP8Tensor.__repr__: return safe_quantized_repr when the data buffer is fake or meta (the same exact-class check Float8Tensor.__repr__ uses).
  • test_torch_compile.py: test_te_ops_bias_quantized_dynamic_with_trace_log[mxfp8|nvfp4] compiles the Bias backward custom op with an NVFP4/MXFP8 quantizer, a NullHandler on torch.__trace and both dims marked dynamic. It runs 2 shapes and checks bitwise equality against tex.bgrad_quantize and that only one graph is compiled. The test does not depend on the pytest version.

Validation

RTX PRO 6000 Blackwell Max-Q (SM 12.0), driver 590.48.01, torch 2.14.1+cu130, cuDNN 9.24, TE built from main 0c4a13f (NVTE_CUDA_ARCHS=120). The patch is Python-only, so both arms use the same libtransformer_engine.so.

python -m pytest -p no:randomly tests/pytorch/test_torch_compile.py:

pytest 9.2.0 pytest 8.4.2 (qa/L0_pytorch_unittest)
main 3 failed (test_te_ops_bias_nvfp4_layout[rht|2d|row-scaled]), 487 passed, 50 skipped, 1 xpassed, 40 errors 490 passed, 50 skipped, 1 xpassed
this PR 0 failed, 491 passed, 51 skipped, 1 xpassed, 40 errors 491 passed, 51 skipped, 1 xpassed

The new test adds one pass (nvfp4) and one skip (mxfp8). The failure sets differ only in the three layout tests. The 40 teardown errors (all test_te_ops_activation_mlp_compile, AttributeError: 'cuBLASModule' object has no attribute 'allow_tf32') under pytest 9.2 are unrelated to this PR. They come from a pytest 9.2.0 regression in monkeypatch.setattr undo for objects with __getattr__ (torch.backends.cuda.matmul), tracked in pytest-dev/pytest#15402 with a revert in pytest-dev/pytest#15403.

New test alone, main vs this PR (pytest 8.4.2): main nvfp4 FAILED (ConstraintViolationError), this PR passed. The mxfp8 case is skipped on SM 12.0 (is_mxfp8_available() is False there), so it ran only on the NVFP4 path here. The MXFP8 repr change was checked with a standalone script that calls the same custom op with TORCH_TRACE set: main raised ConstraintViolationError and this PR passed. Float8 current scaling and blockwise were unaffected on main.

Not run: other architectures (the change has no architecture-specific branch) and the full L0 suite.

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

When torch.__trace has a handler (TORCH_TRACE, or pytest's log capture,
which attaches to non-propagating loggers since pytest 9.2), AOTAutograd
formats its forward metadata with repr() of the fake outputs. NVFP4Tensor
and MXFP8Tensor reprs dequantize their data, which adds guards such as
Eq(s0, 128) and specializes dims marked dynamic, so compiling an op that
returns these tensors raises ConstraintViolationError.

Return the metadata-only repr for fake or meta data, as Float8Tensor
already does, and add a regression test that installs a trace handler.

Signed-off-by: rlaope <105429536+rlaope@users.noreply.github.com>
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Nov 27, 2026
@rlaope
rlaope marked this pull request as ready for review November 27, 2026 05:41
@rlaope
rlaope requested a review from ksivaman as a code owner November 27, 2026 05:41
@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Keeps NVFP4Tensor.__repr__ and MXFP8Tensor.__repr__ data-free for fake, functional, and meta buffers.

  • Quantized tensor reprs keep dynamic shapes during tracing.

No actionable issues found. Tests were not run during this review.

Reviews (1) · Last reviewed commit: "[PyTorch] Keep NVFP4 and MXFP8 tensor re..." · Reviewed by Greptile

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

community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant