Repository: pytorch
Issue: Bug with torch.compile + fwAD
Labels: triaged,module: forward ad,oncall: pt2

### 🐛 Describe the bug

Hello,
I tried using the beta forward-mode automatic differentiation, but I ran into an issue when trying to compile my forward pass. I wonder if it was an error on my part or if it was an actual bug in the PyTorch code. Here is a minimal example that generates the error. 


```
import torch
import torch.nn as nn
import torch.autograd.forward_ad as fwAD

device = "cuda:0"

@torch.compile()
def step(model, x, device):
    x = x.to(device)
    tangent = torch.zeros_like(x, device=device)
    with fwAD.dual_level():
        dual_input = fwAD.make_dual(x, tangent)
        dual_output = model(dual_input)
    return None

lamb = 0.001  # Regularization parameter

x = torch.randn(2,3)
model = nn.Sequential(nn.Linear(3,3),
                      nn.BatchNorm1d(3),
                      nn.Linear(3,1)).to(device)
model.train()


step(model, x, device)
```

I get an error like such: 
```
torch._dynamo.exc.TorchRuntimeError: Failed running call_function <function batch_norm at 0x788edb8ac9a0>(*(FakeTensor(..., device='cuda:0', size=(2, 3), grad_fn=<AddmmBackward0>,
           tangent=FakeTensor(..., device='cuda:0', size=(2, 3), grad_fn=<AddBackward0>)), FakeTensor(..., device='cuda:0', size=(3,)), FakeTensor(..., device='cuda:0', size=(3,)), FakeTensor(..., device='cuda:0', size=(3,), requires_grad=True), FakeTensor(..., device='cuda:0', size=(3,), requires_grad=True), True, 0.1, 1e-05), **{}):
InferenceMode::is_enabled() && self.is_inference() INTERNAL ASSERT FAILED at "../aten/src/ATen/native/VariableMethodStubs.cpp":66, please report a bug to PyTorch. Expected this method to only be reached in inference mode and when all the inputs are inference tensors. You should NOT call this method directly as native::_fw_primal. Please use the dispatcher, i.e., at::_fw_primal. Please file an issue if you come across this error otherwise.

from user code:
   File "/home/enzo/Documents/git/LieEquiv/minimal_example.py", line 13, in step
    dual_output = model(dual_input)
  File "/home/enzo/mambaforge/envs/up_to_date/lib/python3.12/site-packages/torch/nn/modules/container.py", line 250, in forward
    input = module(input)
  File "/home/enzo/mambaforge/envs/up_to_date/lib/python3.12/site-packages/torch/nn/modules/batchnorm.py", line 193, in forward
    return F.batch_norm(
```

### Versions

PyTorch version: 2.5.1+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
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.22.1
Libc version: glibc-2.35

Python version: 3.12.5 | packaged by conda-forge | (main, Aug  8 2024, 18:36:51) [GCC 12.4.0] (64-bit runtime)
Python platform: Linux-6.8.0-47-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.6.20
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 4090
Nvidia driver version: 550.120
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture :                          x86_64
Mode(s) opératoire(s) des processeurs : 32-bit, 64-bit
Address sizes:                          46 bits physical, 48 bits virtual
Boutisme :                              Little Endian
Processeur(s) :                         32
Liste de processeur(s) en ligne :       0-31
Identifiant constructeur :              GenuineIntel
Nom de modèle :                         13th Gen Intel(R) Core(TM) i9-13900K
Famille de processeur :                 6
Modèle :                                183
Thread(s) par cœur :                    2
Cœur(s) par socket :                    24
Socket(s) :                             1
Révision :                              1
Vitesse maximale du processeur en MHz : 5800,0000
Vitesse minimale du processeur en MHz : 800,0000
BogoMIPS :                              5990.40
Drapaux :                               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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr ibt flush_l1d arch_capabilities
Virtualisation :                        VT-x
Cache L1d :                             896 KiB (24 instances)
Cache L1i :                             1,3 MiB (24 instances)
Cache L2 :                              32 MiB (12 instances)
Cache L3 :                              36 MiB (1 instance)
Nœud(s) NUMA :                          1
Nœud NUMA 0 de processeur(s) :          0-31
Vulnerability Gather data sampling:     Not affected
Vulnerability Itlb multihit:            Not affected
Vulnerability L1tf:                     Not affected
Vulnerability Mds:                      Not affected
Vulnerability Meltdown:                 Not affected
Vulnerability Mmio stale data:          Not affected
Vulnerability Reg file data sampling:   Mitigation; Clear Register File
Vulnerability Retbleed:                 Not affected
Vulnerability Spec rstack overflow:     Not affected
Vulnerability Spec store bypass:        Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:               Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:               Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI BHI_DIS_S
Vulnerability Srbds:                    Not affected
Vulnerability Tsx async abort:          Not affected

Versions of relevant libraries:
[pip3] numpy==2.0.2
[pip3] nvidia-cublas-cu12==12.4.5.8
[pip3] nvidia-cuda-cupti-cu12==12.4.127
[pip3] nvidia-cuda-nvrtc-cu12==12.4.127
[pip3] nvidia-cuda-runtime-cu12==12.4.127
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu12==11.2.1.3
[pip3] nvidia-curand-cu12==10.3.5.147
[pip3] nvidia-cusolver-cu12==11.6.1.9
[pip3] nvidia-cusparse-cu12==12.3.1.170
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.4.127
[pip3] nvidia-nvtx-cu12==12.4.127
[pip3] pytorch-lightning==2.4.0
[pip3] torch==2.5.1
[pip3] torchaudio==2.4.1
[pip3] torchmetrics==1.4.2
[pip3] torchvision==0.20.1
[pip3] triton==2.0.0
[conda] cuda-cudart               12.4.127                      0    nvidia
[conda] cuda-cupti                12.4.127                      0    nvidia
[conda] cuda-libraries            12.4.0                        0    nvidia
[conda] cuda-nvrtc                12.4.127                      0    nvidia
[conda] cuda-nvtx                 12.4.127                      0    nvidia
[conda] cuda-opencl               12.6.68                       0    nvidia
[conda] cuda-runtime              12.4.0                        0    nvidia
[conda] cudnn                     9.3.0.75             h93bb076_0    conda-forge
[conda] libcublas                 12.4.2.65                     0    nvidia
[conda] libcufft                  11.2.0.44                     0    nvidia
[conda] libcurand                 10.3.7.68                     0    nvidia
[conda] libcusolver               11.6.0.99                     0    nvidia
[conda] libcusparse               12.3.0.142                    0    nvidia
[conda] libmagma                  2.8.0                h0af6554_0    conda-forge
[conda] libmagma_sparse           2.8.0                h0af6554_0    conda-forge
[conda] libnvjitlink              12.4.99                       0    nvidia
[conda] libtorch                  2.4.1           cuda120_h1d34654_302    conda-forge
[conda] mkl                       2023.2.0         h84fe81f_50496    conda-forge
[conda] mkl-devel                 2023.2.0         ha770c72_50496    conda-forge
[conda] mkl-include               2023.2.0         h84fe81f_50496    conda-forge
[conda] nccl                      2.23.4.1             h52f6c39_0    conda-forge
[conda] numpy                     2.0.2           py312h58c1407_0    conda-forge
[conda] nvidia-cublas-cu12        12.4.5.8                 pypi_0    pypi
[conda] nvidia-cuda-cupti-cu12    12.4.127                 pypi_0    pypi
[conda] nvidia-cuda-nvrtc-cu12    12.4.127                 pypi_0    pypi
[conda] nvidia-cuda-runtime-cu12  12.4.127                 pypi_0    pypi
[conda] nvidia-cudnn-cu12         9.1.0.70                 pypi_0    pypi
[conda] nvidia-cufft-cu12         11.2.1.3                 pypi_0    pypi
[conda] nvidia-curand-cu12        10.3.5.147               pypi_0    pypi
[conda] nvidia-cusolver-cu12      11.6.1.9                 pypi_0    pypi
[conda] nvidia-cusparse-cu12      12.3.1.170               pypi_0    pypi
[conda] nvidia-nccl-cu12          2.21.5                   pypi_0    pypi
[conda] nvidia-nvjitlink-cu12     12.4.127                 pypi_0    pypi
[conda] nvidia-nvtx-cu12          12.4.127                 pypi_0    pypi
[conda] pytorch-cuda              12.4                 hc786d27_6    pytorch
[conda] pytorch-lightning         2.4.0              pyhd8ed1ab_0    conda-forge
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torch                     2.5.1                    pypi_0    pypi
[conda] torchaudio                2.4.1               py312_cu124    pytorch
[conda] torchmetrics              1.4.2              pyhd8ed1ab_0    conda-forge
[conda] torchvision               0.20.1                   pypi_0    pypi
[conda] triton                    3.1.0                    pypi_0    pypi

cc @ezyang @chauhang @penguinwu