pytorch - ✅(Solved) Fix Gradcheck uses gradcheck_nondet_tol=0.0 for non-deterministic index_reduce variants (mean/prod) [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#179562Fetched 2026-04-08 03:00:23
View on GitHub
Comments
0
Participants
1
Timeline
31
Reactions
0
Author
Participants
Timeline (top)
mentioned ×12subscribed ×12labeled ×5added_to_project_v2 ×1

Error Message

Gradcheck is intended to validate derivative correctness, not bitwise reentrancy for known non-deterministic kernels. With gradcheck_nondet_tol=0.0, expected nondeterministic noise (often tiny, near-LSB) is interpreted as an error even when gradients are correct.

Root Cause

Problem

index_reduce includes reduction variants whose backward path is non-reentrant because accumulation order can vary between runs (for example, due to atomics). In this situation, repeated backward/gradgrad executions can be numerically valid but not bitwise-identical.

Fix Action

Fix / Workaround

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 46 bits physical, 57 bits virtual Byte Order: Little Endian CPU(s): 48 On-line CPU(s) list: 0-47 Vendor ID: GenuineIntel Model name: Intel(R) Xeon(R) Gold 6342 CPU @ 2.80GHz CPU family: 6 Model: 106 Thread(s) per core: 2 Core(s) per socket: 24 Socket(s): 1 Stepping: 6 CPU max MHz: 3500.0000 CPU min MHz: 800.0000 BogoMIPS: 5600.00 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 1.1 MiB (24 instances) L1i cache: 768 KiB (24 instances) L2 cache: 30 MiB (24 instances) L3 cache: 36 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-47 Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Retbleed: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

PR fix notes

PR #178589: Set gradcheck nondeterminism tolerance for index_reduce mean/prod.

Description (problem / solution / changelog)

Fix for: #179562

Summary

This change updates OpInfo for index_reduce so gradgrad checks allow nondeterminism tolerance for the reduction variants that are not reentrant in backward.

Specifically:

  • Enable gradcheck_nondet_tol for index_reduce with reduction=mean and reduction=prod
  • Keep strict nondeterminism tolerance (0.0) for reduction=amin and reduction=amax

Problem

The index_reduce operator is marked as not-deterministic by both CUDA and XPU, yet the tolerance is set to 0.0 for this operator. CUDA: CUDA IMPLEMENTATION FRAGMENT XPU: XPU IMPLEMENTATION FRAGMENT

The index_reduce with reduction=prod and reduction=mean on XPU use atomic operations that are not associative and thus non-deterministic. So, for identical inputs, the same kernel can return slightly different results.

Currently the tolerance is set to 0.0 (default gradcheck_nondet_tol value). So even the least significant bit change can cause the assertion error.

Reasoning

The failure is a classic gradcheck nondeterminism case: numerics are correct, but repeated backward passes are not bitwise/reentrant-identical on XPU for these reductions. The proper PyTorch testing-side remedy is to declare nondeterminism tolerance in OpInfo for exactly those variants.

Setting

gradcheck_nondet_tol=GRADCHECK_NONDET_TOL

is a common and broadly used approach in many places of the common_methods_invocations.py file.

Risk and scope

  • It only affects the gradcheck tests for index_reduce_prod and index_reduce_mean.
  • OpInfo is shared across backends, so this tolerance applies globally for these two variants - as there is no simple mechanism to apply this gradcheck_nondet_tol only to one device type.

Changed files

  • torch/testing/_internal/common_methods_invocations.py (modified, +1/-0)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Problem

index_reduce includes reduction variants whose backward path is non-reentrant because accumulation order can vary between runs (for example, due to atomics). In this situation, repeated backward/gradgrad executions can be numerically valid but not bitwise-identical.

However, the current OpInfo setup effectively uses strict nondeterminism tolerance (gradcheck_nondet_tol=0.0) for these variants. This makes gradcheck treat expected tiny run-to-run differences as failures.

The index_reduce operator is marked as not-deterministic by both CUDA and XPU, yet the tolerance is set to 0.0 for this operator.
CUDA: CUDA IMPLEMENTATION FRAGMENT
XPU: XPU IMPLEMENTATION FRAGMENT

On XPU it makes the tests to fail: https://github.com/intel/torch-xpu-ops/issues/2359

Affected Case

index_reduce variants:

  • reduction="mean"
  • reduction="prod"

By contrast, reduction="amin" and reduction="amax" can remain strict (0.0) when they are reentrant in backward for the tested paths.

Why This Is a Bug in Test Configuration

Gradcheck is intended to validate derivative correctness, not bitwise reentrancy for known non-deterministic kernels. With gradcheck_nondet_tol=0.0, expected nondeterministic noise (often tiny, near-LSB) is interpreted as an error even when gradients are correct.

Proposed fix

PR with proposal of changes: https://github.com/pytorch/pytorch/pull/178589

Versions

<details> <summary>Versions</summary>

Collecting environment information... PyTorch version: N/A Is debug build: N/A CUDA used to build PyTorch: N/A ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64) GCC version: (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0 Clang version: Could not collect CMake version: version 3.31.6 Libc version: glibc-2.35

Python version: 3.10.19 | packaged by conda-forge | (main, Oct 22 2025, 22:29:10) [GCC 14.3.0] (64-bit runtime) Python platform: Linux-5.15.0-73-generic-x86_64-with-glibc2.35 Is CUDA available: N/A CUDA runtime version: Could not collect CUDA_MODULE_LOADING set to: N/A GPU models and configuration: Could not collect Nvidia driver version: Could not collect cuDNN version: Could not collect Is XPU available: N/A HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: N/A

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 46 bits physical, 57 bits virtual Byte Order: Little Endian CPU(s): 48 On-line CPU(s) list: 0-47 Vendor ID: GenuineIntel Model name: Intel(R) Xeon(R) Gold 6342 CPU @ 2.80GHz CPU family: 6 Model: 106 Thread(s) per core: 2 Core(s) per socket: 24 Socket(s): 1 Stepping: 6 CPU max MHz: 3500.0000 CPU min MHz: 800.0000 BogoMIPS: 5600.00 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities Virtualization: VT-x L1d cache: 1.1 MiB (24 instances) L1i cache: 768 KiB (24 instances) L2 cache: 30 MiB (24 instances) L3 cache: 36 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-47 Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Retbleed: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence Vulnerability Srbds: Not affected Vulnerability Tsx async abort: Not affected

Versions of relevant libraries: [pip3] dpcpp-cpp-rt==2025.2.1 [pip3] impi-rt==2021.16.1 [pip3] intel-cmplr-lib-rt==2025.2.1 [pip3] intel-cmplr-lib-ur==2025.2.1 [pip3] intel-cmplr-lic-rt==2025.2.1 [pip3] intel-opencl-rt==2025.2.1 [pip3] intel-openmp==2025.2.1 [pip3] intel-pti==0.13.1 [pip3] intel-sycl-rt==2025.2.1 [pip3] mkl==2025.2.0 [pip3] mypy==1.16.0 [pip3] mypy_extensions==1.1.0 [pip3] numpy==1.22.4 [pip3] oneccl==2021.16.1 [pip3] oneccl-devel==2021.16.1 [pip3] onemkl-sycl-blas==2025.2.0 [pip3] onemkl-sycl-dft==2025.2.0 [pip3] onemkl-sycl-lapack==2025.2.0 [pip3] onemkl-sycl-rng==2025.2.0 [pip3] onemkl-sycl-sparse==2025.2.0 [pip3] onnx==1.19.1 [pip3] onnx-ir==0.1.12 [pip3] onnxscript==0.5.4 [pip3] optree==0.13.0 [pip3] pytorch-triton-xpu==3.5.0+git1b0418a9 [pip3] tbb==2022.2.0 [pip3] tcmlib==1.4.0 [pip3] torch==2.10.0.dev20251110+xpu [pip3] torchaudio==2.10.0.dev20251111+xpu [pip3] torchvision==0.25.0.dev20251111+xpu [pip3] umf==0.11.0 [conda] dpcpp-cpp-rt 2025.2.1 pypi_0 pypi [conda] impi-rt 2021.16.1 pypi_0 pypi [conda] intel-cmplr-lib-rt 2025.2.1 pypi_0 pypi [conda] intel-cmplr-lib-ur 2025.2.1 pypi_0 pypi [conda] intel-cmplr-lic-rt 2025.2.1 pypi_0 pypi [conda] intel-opencl-rt 2025.2.1 pypi_0 pypi [conda] intel-openmp 2025.2.1 pypi_0 pypi [conda] intel-pti 0.13.1 pypi_0 pypi [conda] intel-sycl-rt 2025.2.1 pypi_0 pypi [conda] mkl 2025.2.0 pypi_0 pypi [conda] numpy 1.22.4 pypi_0 pypi [conda] oneccl 2021.16.1 pypi_0 pypi [conda] oneccl-devel 2021.16.1 pypi_0 pypi [conda] onemkl-sycl-blas 2025.2.0 pypi_0 pypi [conda] onemkl-sycl-dft 2025.2.0 pypi_0 pypi [conda] onemkl-sycl-lapack 2025.2.0 pypi_0 pypi [conda] onemkl-sycl-rng 2025.2.0 pypi_0 pypi [conda] onemkl-sycl-sparse 2025.2.0 pypi_0 pypi [conda] optree 0.13.0 pypi_0 pypi [conda] pytorch-triton-xpu 3.5.0+git1b0418a9 pypi_0 pypi [conda] tbb 2022.2.0 pypi_0 pypi [conda] tcmlib 1.4.0 pypi_0 pypi [conda] torch 2.10.0.dev20251110+xpu pypi_0 pypi [conda] torchaudio 2.10.0.dev20251111+xpu pypi_0 pypi [conda] torchvision 0.25.0.dev20251111+xpu pypi_0 pypi [conda] umf 0.11.0 pypi_0 pypi

</details>

cc @ezyang @albanD @gqchen @nikitaved @soulitzer @Varal7 @bobrenjc93 @mruberry @gujinghui @EikanWang @fengyuan14 @guangyey

extent analysis

TL;DR

Increase the gradcheck_nondet_tol value to allow for small, expected differences in gradients due to non-deterministic kernels.

Guidance

  • Review the index_reduce operator implementation to understand the sources of non-determinism, particularly for reduction="mean" and reduction="prod".
  • Consider increasing the gradcheck_nondet_tol value to a small, non-zero value (e.g., 1e-6) to account for expected numerical differences.
  • Verify that the proposed fix (PR #178589) addresses the issue by testing with the updated gradcheck_nondet_tol value.
  • Be cautious when adjusting the tolerance value, as it may mask actual gradient computation errors.

Example

No specific code example is provided, as the issue is related to the configuration of the gradcheck_nondet_tol value rather than a code implementation.

Notes

The issue is specific to the index_reduce operator with non-deterministic kernels, and the proposed fix aims to address the overly strict tolerance value. However, the optimal tolerance value may depend on the specific use case and requirements.

Recommendation

Apply a workaround by increasing the gradcheck_nondet_tol value to a small, non-zero value, as this allows for expected numerical differences while still validating gradient correctness.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING