Skip to content

[PyTorch] Match TE's TF32 FP32 GEMM precision in test_numerics references - #3931

Closed
rlaope wants to merge 1 commit into
NVIDIA:mainfrom
rlaope:tf32-numerics-ref
Closed

rlaope wants to merge 1 commit into
NVIDIA:mainfrom
rlaope:tf32-numerics-ref

Conversation

@rlaope

@rlaope rlaope commented Nov 24, 2026

Copy link
Copy Markdown

Description

Without an NGC container, tests/pytorch/test_numerics.py gives 72 FP32 failures on correct code. TE runs pure FP32 GEMMs with CUBLAS_COMPUTE_32F_FAST_TF32 (cublaslt_gemm.cu). The PyTorch reference modules in the test (torch.nn.Linear, TorchLayerNormLinear, TorchLayerNormMLP) use TF32 only when torch.backends.cuda.matmul.allow_tf32 is set. NGC PyTorch images set TORCH_ALLOW_TF32_CUBLAS_OVERRIDE=1, so both sides run TF32 and CI passes. On a plain PyTorch install allow_tf32 defaults to False, so TE's TF32 output is compared against an IEEE FP32 reference with FP32 tolerances.

#3014 and #3797 set NVIDIA_TF32_OVERRIDE=0 in the QA launchers. That disables TF32 for TE as well, and in #3014 it was pointed out that this moves the test away from what TE actually runs. This PR does what #3797 offered as the alternative: it changes the reference side of the test and keeps TE's TF32 path. The result is the same with or without the env override, and with direct pytest runs that skip qa/*.sh.

The failing cases are all dtype0 (FP32): test_linear_accuracy (8), test_layernorm_linear_accuracy (32), test_layernorm_mlp_accuracy (32). One example: Outputs not close enough in tensor at idx=0. Maximum difference at location [50] with -3.5490684509277344 vs -3.55558705329895. That is the same message #3014 reported on its setup.

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

  • tests/pytorch/test_numerics.py: add an autouse fixture that sets torch.backends.cuda.matmul.allow_tf32 = True for each test and restores the previous value afterwards. It sits next to the existing disable_bf16_reduced_precision_reduction fixture, which aligns PyTorch with TE in the same way for BF16. FP16/BF16 cases are not affected. test_linear_tight_dims still turns TF32 off inside its own body for its FP32 case, so its behaviour does not change.

Validation

Hardware: RTX PRO 6000 Blackwell Max-Q (SM 12.0), driver 590.48.01, CUDA 13.0.88, PyTorch 2.14.1+cu130 (pip, not NGC), TE source build at a6393d0.
Command: PYTORCH_JIT=0 NVTE_TORCH_COMPILE=0 NVTE_ALLOW_NONDETERMINISTIC_ALGO=0 NVTE_FUSED_ATTN=0 python -m pytest tests/pytorch/test_numerics.py (the L0_pytorch_unittest line)

Environment main this PR
no override (plain PyTorch default) 72 failed, 1279 passed, 120 skipped 1351 passed, 120 skipped
TORCH_ALLOW_TF32_CUBLAS_OVERRIDE=1 (NGC default) 1351 passed, 120 skipped 1351 passed, 120 skipped
NVIDIA_TF32_OVERRIDE=0 1351 passed, 120 skipped 1351 passed, 120 skipped

Only tested on SM 12.0. The mismatch comes from the TF32 setting and is not specific to the architecture (#3797 reports the same failures on B200).

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

TE runs pure FP32 GEMMs with CUBLAS_COMPUTE_32F_FAST_TF32, while the
PyTorch reference modules in test_numerics.py use TF32 only when
torch.backends.cuda.matmul.allow_tf32 is set. NGC containers set it via
TORCH_ALLOW_TF32_CUBLAS_OVERRIDE=1, so CI passes; on a plain PyTorch
install it is False and 72 FP32 cases of test_linear_accuracy,
test_layernorm_linear_accuracy and test_layernorm_mlp_accuracy fail.

Enable TF32 matmul for the PyTorch references through an autouse
fixture, next to the existing BF16 reduced-precision fixture, and
restore the previous value afterwards.

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 24, 2026
@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge.

Summary

Adds an autouse fixture in tests/pytorch/test_numerics.py that enables TF32 for PyTorch references to match TE’s FP32 GEMMs.

  • PyTorch numerical tests match TF32 precision for FP32 references.

Reviews (1) · Last reviewed commit: "[PyTorch] Match TE TF32 FP32 GEMM precis..." · Reviewed by Greptile

@pggPL pggPL mentioned this pull request Nov 24, 2026
8 of 13 tasks
@pggPL

pggPL commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Thank you for the contribution! This fix has been included in #3935 (with original commit authorship preserved) so we can run CI and land several small community fixes together. Closing this PR in favor of #3935.

@pggPL pggPL closed this Nov 24, 2026
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.

2 participants