Skip to content

GradcheckError: Backward is not reentrant #2359

@daisyden

Description

@daisyden

🐛 Describe the bug

Cases:
op_ut,test.test_ops_gradients.TestBwdGradientsXPU,test_fn_gradgrad_index_reduce_mean_xpu_float64
op_ut,test.test_ops_gradients.TestBwdGradientsXPU,test_fn_gradgrad_index_reduce_prod_xpu_float64
op_ut,test.test_ops_gradients.TestBwdGradientsXPU,test_inplace_gradgrad_index_reduce_mean_xpu_float64
op_ut,test.test_ops_gradients.TestBwdGradientsXPU,test_inplace_gradgrad_index_reduce_prod_xpu_float64

pytest_command:
PYTORCH_TEST_WITH_SLOW=1 pytest -v test/test_ops_gradients.py -k test_fn_gradgrad_index_reduce_mean_xpu_float64
PYTORCH_TEST_WITH_SLOW=1 pytest -v test/test_ops_gradients.py -k test_fn_gradgrad_index_reduce_prod_xpu_float64
PYTORCH_TEST_WITH_SLOW=1 pytest -v test/test_ops_gradients.py -k test_inplace_gradgrad_index_reduce_mean_xpu_float64
PYTORCH_TEST_WITH_SLOW=1 pytest -v test/test_ops_gradients.py -k test_inplace_gradgrad_index_reduce_prod_xpu_float64

Trace Example:

Command: PYTORCH_TEST_WITH_SLOW=1 pytest -v test/test_ops_gradients.py -k test_inplace_gradgrad_index_reduce_prod_xpu_float64
Traceback (most recent call last):
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/testing/_internal/common_device_type.py", line 1151, in test_wrapper
    return test(*args, **kwargs)
  File "/home/daisyden/upstream/gradient_test/test/test_ops_gradients.py", line 100, in test_inplace_gradgrad
    self._check_helper(
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/testing/_internal/common_utils.py", line 5622, in _check_helper
    self.assertTrue(gradgradcheck(fn, gradcheck_args, **kwargs))
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/testing/_internal/common_utils.py", line 5207, in gradgradcheck
    return torch.autograd.gradgradcheck(fn, inputs, grad_outputs, **kwargs)
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/autograd/gradcheck.py", line 2291, in gradgradcheck
    return gradcheck(
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/autograd/gradcheck.py", line 2086, in gradcheck
    return _gradcheck_helper(**args)
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/autograd/gradcheck.py", line 2115, in _gradcheck_helper
    _gradcheck_real_imag(
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/autograd/gradcheck.py", line 1518, in _gradcheck_real_imag
    gradcheck_fn(
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/autograd/gradcheck.py", line 1952, in _fast_gradcheck
    analytical_vJu = _get_analytical_vJu_backward_mode(
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/autograd/gradcheck.py", line 828, in _get_analytical_vJu_backward_mode
    all_vJ = _check_analytical_jacobian_attributes(
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/autograd/gradcheck.py", line 814, in _check_analytical_jacobian_attributes
    raise GradcheckError(
torch.autograd.gradcheck.GradcheckError: Backward is not reentrant, i.e., running backward with same input and grad_output multiple times gives different values, although analytical gradient matches numerical gradient.The tolerance for nondeterminism was 0.0.

NOTE: If your op relies on non-deterministic operations i.e., it is listed here:
https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html
this failure might be expected.

If you are adding a new operator, please file an issue and then use one of the
workarounds. The workaround depends on how your test invokes gradcheck/gradgradcheck.
If the test
- manually invokes gradcheck/gradgradcheck, then call gradcheck/gradgradcheck
  with `nondet_tol=<tol>` as a keyword argument.
- is OpInfo-based (e.g., in test_ops_gradients.py), then modify the OpInfo for the test
  to have `gradcheck_nondet_tol=<tol>`.
- is a Module test (e.g., in common_nn.py), then modify the corresponding
  module_test entry to have `gradcheck_nondet_tol=<tol>`


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/testing/_internal/common_utils.py", line 3334, in wrapper
    method(*args, **kwargs)
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/testing/_internal/common_device_type.py", line 428, in instantiated_test
    result = test(self, **param_kwargs)
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/testing/_internal/common_utils.py", line 1741, in wrapper
    fn(*args, **kwargs)
  File "/home/daisyden/miniforge3/envs/gradient_test/lib/python3.10/site-packages/torch/testing/_internal/common_device_type.py", line 1163, in test_wrapper
    raise e_tracked from e
Exception: Backward is not reentrant, i.e., running backward with same input and grad_output multiple times gives different values, although analytical gradient matches numerical gradient.The tolerance for nondeterminism was 0.0.

NOTE: If your op relies on non-deterministic operations i.e., it is listed here:
https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html
this failure might be expected.

If you are adding a new operator, please file an issue and then use one of the
workarounds. The workaround depends on how your test invokes gradcheck/gradgradcheck.
If the test
- manually invokes gradcheck/gradgradcheck, then call gradcheck/gradgradcheck
  with `nondet_tol=<tol>` as a keyword argument.
- is OpInfo-based (e.g., in test_ops_gradients.py), then modify the OpInfo for the test
  to have `gradcheck_nondet_tol=<tol>`.
- is a Module test (e.g., in common_nn.py), then modify the corresponding
  module_test entry to have `gradcheck_nondet_tol=<tol>`


Caused by sample input at index 6: SampleInput(input=Tensor[size=(5, 5, 5), device="xpu:0", dtype=torch.float64], args=(1,Tensor[size=(10,), device="xpu:0", dtype=torch.int64],Tensor[size=(5, 10, 5), device="xpu:0", dtype=torch.float64],'prod'), kwargs={'include_self': 'True'}, broadcasts_input=False, name='')

To execute this test, run the following from the base repo dir:
    PYTORCH_OPINFO_SAMPLE_INPUT_INDEX=6 PYTORCH_TEST_WITH_SLOW=1 python test/test_ops_gradients.py TestBwdGradientsXPU.test_inplace_gradgrad_index_reduce_prod_xpu_float64

This message can be suppressed by setting PYTORCH_PRINT_REPRO_ON_FAILURE=0```
No duplicate issues found with RAG in xpu_issues.

No duplicate issues generated.


### Versions

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions