Repository: pytorch
Issue: custom_op's backward changes can't invalidate `torch.compile` cache for backward
Labels: triaged,module: custom-operators,oncall: pt2,module: pt2-dispatcher

### 🐛 Describe the bug

(clean cache: `rm -r /tmp/torchinductor_root/*`)
First, run the following code
```python
import torch
from torch import Tensor

@torch.library.custom_op("mylib::foo", mutates_args=())
def foo(x: Tensor) -> Tensor:
    return x.clone()
    
@foo.register_fake
def _(x):
    return torch.empty_like(x)

def backward(ctx, grad):
    return 1.0 * grad

foo.register_autograd(backward)

x = torch.tensor(0., requires_grad=True)

@torch.compile
def bar(x):
    return torch.ops.mylib.foo(x)

bar(x).backward()
print(x.grad) # tensor(1.)
```
Then, change the code to
```python
import torch
from torch import Tensor

@torch.library.custom_op("mylib::foo", mutates_args=())
def foo(x: Tensor) -> Tensor:
    return x.clone()
    
@foo.register_fake
def _(x):
    return torch.empty_like(x)

def backward(ctx, grad):
    return 2.0 * grad

foo.register_autograd(backward)

x = torch.tensor(0., requires_grad=True)

@torch.compile
def bar(x):
    return torch.ops.mylib.foo(x)

bar(x).backward()
print(x.grad) # tensor(1.)
```
It will still print `tensor(1.)`.

Interestingly, if the "sequence" of backwards is
(clean cache: `rm -r /tmp/torchinductor_root/*`)
```python
def backward(ctx, grad):
    return grad
# tensor(1.)
```
```python
def backward(ctx, grad):
    return 2.0 * grad
# tensor(2.)
```
```python
def backward(ctx, grad):
    return grad
# tensor(2.)
```
It will print `tensor(1.)`, `tensor(2.)`, `tensor(2.)`.

I inspected the code generated by inductor, and found it didn't change after `1.0` changed to `2.0`
```python
# /tmp/torchinductor_root/5g/c5gahzddocrqqegxwc4iud6jjufbxmvx6rwvify7r4bkdc5tec6v.py
# other lines omitted
cpp_fused_mul_0 = async_compile.cpp_pybinding(['const float*', 'float*'], '''
#include "/tmp/torchinductor_root/db/cdb7hyptwxpzukwd42x4ajfjlgrpum4a4htdd6lhb65apclsmno4.h"
extern "C"  void kernel(const float* in_ptr0,
                       float* out_ptr0)
{
    {
        {
            {
                auto tmp0 = in_ptr0[static_cast<int64_t>(0L)];
                auto tmp1 = static_cast<float>(1.0);
                auto tmp2 = decltype(tmp0)(tmp0 * tmp1);
                out_ptr0[static_cast<int64_t>(0L)] = tmp2;
            }
        }
    }
}
''')


async_compile.wait(globals())
del async_compile

def call(args):
    tangents_1, = args
    args.clear()
    assert_size_stride(tangents_1, (), ())
    buf0 = empty_strided_cpu((), (), torch.float32)
    cpp_fused_mul_0(tangents_1, buf0)
    del tangents_1
    return (buf0, )
```
And deleting this generated file (`/tmp/torchinductor_root/5g/c5gahzddocrqqegxwc4iud6jjufbxmvx6rwvify7r4bkdc5tec6v.py`) can't solve the problem -- an identical file will be generated.

### Versions

PyTorch version: 2.7.0.dev20250107+cu126
Is debug build: False
CUDA used to build PyTorch: 12.6
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.30.4
Libc version: glibc-2.35

Python version: 3.12.8 (main, Dec 19 2024, 14:33:20) [Clang 18.1.8 ] (64-bit runtime)
Python platform: Linux-5.4.250-2-velinux1u1-amd64-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.6.77
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA H100 80GB HBM3
GPU 1: NVIDIA H100 80GB HBM3
GPU 2: NVIDIA H100 80GB HBM3
GPU 3: NVIDIA H100 80GB HBM3
GPU 4: NVIDIA H100 80GB HBM3
GPU 5: NVIDIA H100 80GB HBM3
GPU 6: NVIDIA H100 80GB HBM3
GPU 7: NVIDIA H100 80GB HBM3

Nvidia driver version: 535.129.03
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_graph.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.9.5.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops.so.9.5.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Address sizes:                   52 bits physical, 57 bits virtual
Byte Order:                      Little Endian
CPU(s):                          168
On-line CPU(s) list:             0-161
Off-line CPU(s) list:            162-167
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Xeon(R) Platinum 8457C
CPU family:                      6
Model:                           143
Thread(s) per core:              2
Core(s) per socket:              42
Socket(s):                       2
Stepping:                        8
BogoMIPS:                        5199.99
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 wbnoinvd arat avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid cldemote movdiri movdir64b md_clear arch_capabilities
Hypervisor vendor:               KVM
Virtualization type:             full
L1d cache:                       3.9 MiB (84 instances)
L1i cache:                       2.6 MiB (84 instances)
L2 cache:                        168 MiB (84 instances)
L3 cache:                        195 MiB (2 instances)
NUMA node(s):                    2
NUMA node0 CPU(s):               0-83
NUMA node1 CPU(s):               84-167
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Unknown: No mitigations
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:   Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] numpy==2.2.1
[pip3] nvidia-cublas-cu12==12.6.4.1
[pip3] nvidia-cuda-cupti-cu12==12.6.80
[pip3] nvidia-cuda-nvrtc-cu12==12.6.77
[pip3] nvidia-cuda-runtime-cu12==12.6.77
[pip3] nvidia-cudnn-cu12==9.5.1.17
[pip3] nvidia-cufft-cu12==11.3.0.4
[pip3] nvidia-curand-cu12==10.3.7.77
[pip3] nvidia-cusolver-cu12==11.7.1.2
[pip3] nvidia-cusparse-cu12==12.5.4.2
[pip3] nvidia-cusparselt-cu12==0.6.3
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.6.85
[pip3] nvidia-nvtx-cu12==12.6.77
[pip3] pytorch-triton==3.2.0+git0d4682f0
[pip3] torch==2.7.0.dev20250107+cu126
[conda] Could not collect

cc @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @aakhundov @BoyuanFeng @zou3519 @bdhirsh