Repository: pytorch
Issue: Functionalize crashes on train_step GraphModule
Labels: triaged,module: functionalization

### 🐛 Describe the bug

When apply functionalize to `train_step` with a single linear forward, backward and an SGD optimizer, it crashes with the following error:

1. with `torch._enable_functionalization(reapply_views=True)`:

```
  File "/fsx/users/shenli/pytorch/test/distributed/_spmd/test_tracing.py", line 533, in _test_optimizer                                                           
    train_step(mod, opt, inp)                                                                                                                                     
  File "/fsx/users/shenli/pytorch/torch/distributed/_spmd/api.py", line 313, in wrapper                                                                           
    gm = make_fx(                                                                                                                                                 
  File "/fsx/users/shenli/pytorch/torch/fx/experimental/proxy_tensor.py", line 739, in wrapped                                                                    
    args = pytree.tree_map(wrap_fn_map[tracing_mode], args)                                                                                                       
  File "/fsx/users/shenli/pytorch/torch/utils/_pytree.py", line 196, in tree_map                                                                                  
    return tree_unflatten([fn(i) for i in flat_args], spec)                                                                                                       
  File "/fsx/users/shenli/pytorch/torch/utils/_pytree.py", line 196, in <listcomp>                                                                                
    return tree_unflatten([fn(i) for i in flat_args], spec)                                                                                                       
  File "/fsx/users/shenli/pytorch/torch/fx/experimental/proxy_tensor.py", line 728, in wrap_fake                                                                  
    return fake_tensor_mode.from_tensor(x, source=source)  # type: ignore[attr-defined]                       
  File "/fsx/users/shenli/pytorch/torch/_subclasses/fake_tensor.py", line 1398, in from_tensor                                                                    
    return self.fake_tensor_converter(                                                                                                                            
  File "/fsx/users/shenli/pytorch/torch/_subclasses/fake_tensor.py", line 314, in __call__                                                                        
    return self.from_real_tensor(                                                                                                                                 
  File "/fsx/users/shenli/pytorch/torch/_subclasses/fake_tensor.py", line 272, in from_real_tensor                                                                
    out = self.meta_converter(                                                                                                                                    
  File "/fsx/users/shenli/pytorch/torch/_subclasses/meta_utils.py", line 501, in __call__                                                                         
    r = self.meta_tensor(                                                                                                                                         
  File "/fsx/users/shenli/pytorch/torch/_subclasses/meta_utils.py", line 364, in meta_tensor                                                                      
    r = callback(                                                                                                                                                 
  File "/fsx/users/shenli/pytorch/torch/_subclasses/fake_tensor.py", line 267, in mk_fake_tensor                                                                  
    make_meta_t(),                                                                                                                                                
  File "/fsx/users/shenli/pytorch/torch/_subclasses/meta_utils.py", line 365, in <lambda>                                                                         
    lambda: torch.empty_strided(                                                                                                                                  
RuntimeError: Cannot call storage_offset() on tensor with symbolic sizes/strides                                                                                  
Exception raised from storage_offset_default at /fsx/users/shenli/pytorch/c10/core/TensorImpl.h:770 (most recent call first):                                     
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) + 0x6c (0x7f6e977fd1ac in /fsx/u
sers/shenli/pytorch/torch/lib/libc10.so)     
```

2. with `torch._functorch.eager_transforms.functionalize`

```
  File "/fsx/users/shenli/pytorch/torch/distributed/_spmd/api.py", line 256, in wrapper                                                                           
    gm = functionalize(make_fx(                                                  
  File "/fsx/users/shenli/pytorch/torch/_functorch/vmap.py", line 39, in fn                                                                                       
    return f(*args, **kwargs)                                                    
  File "/fsx/users/shenli/pytorch/torch/_functorch/eager_transforms.py", line 1600, in wrapped                                                                    
    func_outputs = func(*func_args, **func_kwargs)                               
  File "/fsx/users/shenli/pytorch/torch/fx/experimental/proxy_tensor.py", line 715, in wrapped                                                                    
    args = pytree.tree_map(wrap_fn_map[tracing_mode], args)                      
  File "/fsx/users/shenli/pytorch/torch/utils/_pytree.py", line 196, in tree_map                                                                                  
    return tree_unflatten([fn(i) for i in flat_args], spec)                                                                                                       
  File "/fsx/users/shenli/pytorch/torch/utils/_pytree.py", line 196, in <listcomp>                                                                                
    return tree_unflatten([fn(i) for i in flat_args], spec)                                                                                                       
  File "/fsx/users/shenli/pytorch/torch/fx/experimental/proxy_tensor.py", line 704, in wrap_fake                                                                  
    return fake_tensor_mode.from_tensor(x, source=source)  # type: ignore[attr-defined]                                                                           
  File "/fsx/users/shenli/pytorch/torch/_subclasses/fake_tensor.py", line 1398, in from_tensor                                                                    
    return self.fake_tensor_converter(                                                                                                                            
  File "/fsx/users/shenli/pytorch/torch/_subclasses/fake_tensor.py", line 314, in __call__                                                                        
    return self.from_real_tensor(                                                                                                                                 
  File "/fsx/users/shenli/pytorch/torch/_subclasses/fake_tensor.py", line 280, in from_real_tensor                                                                
    raise UnsupportedFakeTensorException("meta converter nyi")                   
torch._subclasses.fake_tensor.UnsupportedFakeTensorException: meta converter nyi  
```

To reproduce, wrap the following line with functionalize.

https://github.com/pytorch/pytorch/blob/91166ef7e75fc5fb94fb56ec6679a1744cf0f28c/torch/distributed/_spmd/api.py#L311

Then run `pytest test/distributed/_spmd/test_tracing.py -k test_sgd -vs`

### Versions

PyTorch version: 2.1.0a0+gitff807e7
Is debug build: False              
CUDA used to build PyTorch: 11.6  
ROCM used to build PyTorch: N/A   
                                                                                 
OS: Ubuntu 20.04.5 LTS (x86_64)   
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: 10.0.0-4ubuntu1                                                   
CMake version: version 3.24.1     
Libc version: glibc-2.31                                                         
                                                                                 
Python version: 3.9.12 (main, Apr  5 2022, 06:56:58)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.15.0-1019-aws-x86_64-with-glibc2.31
Is CUDA available: True                                                          
CUDA runtime version: 11.6.112                                                   
CUDA_MODULE_LOADING set to: LAZY                                                 
GPU models and configuration:                                                    
GPU 0: NVIDIA A100-SXM4-40GB                                                                                                                                      
GPU 1: NVIDIA A100-SXM4-40GB                                                     
GPU 2: NVIDIA A100-SXM4-40GB                                                     
GPU 3: NVIDIA A100-SXM4-40GB                                                     
GPU 4: NVIDIA A100-SXM4-40GB                                                                                                                                      
GPU 5: NVIDIA A100-SXM4-40GB                                                     
GPU 6: NVIDIA A100-SXM4-40GB                                                                                                                                      
GPU 7: NVIDIA A100-SXM4-40GB  

Nvidia driver version: 525.85.12                                                                                                                 
cuDNN version: Could not collect                                                                                                                                  
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
Byte Order:                      Little Endian
Address sizes:                   46 bits physical, 48 bits virtual
CPU(s):                          96
On-line CPU(s) list:             0-95
Thread(s) per core:              2
Core(s) per socket:              24
Socket(s):                       2
NUMA node(s):                    2
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           85
Model name:                      Intel(R) Xeon(R) Platinum 8275CL CPU @ 3.00GHz
Stepping:                        7
CPU MHz:                         2999.998
BogoMIPS:                        5999.99 
Hypervisor vendor:               KVM
Virtualization type:             full
L1d cache:                       1.5 MiB 
L1i cache:                       1.5 MiB                           
L2 cache:                        48 MiB                                          
L3 cache:                        71.5 MiB                                
NUMA node0 CPU(s):               0-23,48-71                                                                                                                       
NUMA node1 CPU(s):               24-47,72-95                                  
Vulnerability Itlb multihit:     KVM: Mitigation: VMX unsupported        
Vulnerability L1tf:              Mitigation; PTE Inversion               
Vulnerability Mds:               Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
Vulnerability Meltdown:          Mitigation; PTI     
Vulnerability Mmio stale data:   Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
Vulnerability Retbleed:          Vulnerable
Vulnerability Spec store bypass: Vulnerable
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Retpolines, STIBP disabled, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
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 arch_perfmon rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq 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 invpcid_single pti fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves ida arat pku ospke

Versions of relevant libraries:
[pip3] flake8==3.8.2
[pip3] flake8-bugbear==20.1.4
[pip3] flake8-comprehensions==3.3.0
[pip3] flake8-executable==2.0.4
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-pyi==20.5.0
[pip3] mypy==0.960
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.23.1
[pip3] torch==2.1.0a0+gitff807e7
[pip3] torchdynamo==1.13.0.dev0
[pip3] torchmetrics==0.9.3
[pip3] torchrec==0.2.0
[pip3] torchvision==0.13.0a0+ecbff88
[pip3] torchx-nightly==2022.9.28
[pip3] triton==2.0.0.dev20221202
[pip3] vit-pytorch==0.33.2
[conda] mkl                       2022.0.1           h06a4308_117  
[conda] mkl-include               2022.0.1           h06a4308_117  
[conda] numpy                     1.23.1                   pypi_0    pypi
[conda] torch                     2.1.0a0+gitff807e7           dev_0    <develop>
[conda] torchdynamo               1.13.0.dev0               dev_0    <develop>
[conda] torchmetrics              0.9.3                    pypi_0    pypi
[conda] torchrec                  0.2.0                    pypi_0    pypi
[conda] torchvision               0.13.0a0+ecbff88          pypi_0    pypi
[conda] torchx-nightly            2022.9.28                pypi_0    pypi
[conda] triton                    2.0.0.dev20221202          pypi_0    pypi

cc @bdhirsh @ezyang @soumith