Repository: pytorch
Issue: Inductor does not check input SymInt invariant on GraphModules passed in
Labels: good first issue,triaged,oncall: pt2,module: dynamic shapes,module: inductor

See https://github.com/pytorch/pytorch/issues/104102#issuecomment-1804754101 for update

### 🐛 Describe the bug

Repro script:

```

import torch
from torch import tensor, device
import torch.fx as fx
from torch._dynamo.testing import rand_strided
from math import inf
import torch._inductor.inductor_prims

import torch._dynamo.config
import torch._inductor.config
import torch._functorch.config

torch._inductor.config.fallback_random = True
torch._inductor.config.generate_intermediate_hooks = True
torch._inductor.config.triton.cudagraphs = True



isolate_fails_code_str = None



# torch version: 2.1.0a0+git2232cce
# torch cuda version: 12.0
# torch git version: 2232cce69c450db3163751e9238e1f34a6a2477e


# CUDA Info: 
# nvcc: NVIDIA (R) Cuda compiler driver 
# Copyright (c) 2005-2023 NVIDIA Corporation 
# Built on Fri_Jan__6_16:45:21_PST_2023 
# Cuda compilation tools, release 12.0, V12.0.140 
# Build cuda_12.0.r12.0/compiler.32267302_0 

# GPU Hardware Info: 
# NVIDIA PG509-210 : 8 


from torch.nn import *
class Repro(torch.nn.Module):
    def __init__(self):
        super().__init__()

    
    
    def forward(self, arg5_1):
        sym_size = torch.ops.aten.sym_size(arg5_1, 2);  arg5_1 = None
        mul_2 = 1024 * sym_size;  sym_size = None
        return (mul_2,)
        
def load_args(reader):
    buf0 = reader.storage('d5d3fbeeaccb2bf4d8e4ba44de52bd663bfe4e41', 1572864, device=device(type='cuda', index=0), dtype_hint=torch.float16)
    reader.tensor(buf0, (4, 256, 24, 32), dtype=torch.float16, is_leaf=True)  # arg5_1
load_args._version = 0
mod = Repro()
if __name__ == '__main__':
    from torch._dynamo.repro.after_aot import run_repro
    run_repro(mod, load_args, accuracy=False, command='run', save_dir='/data/users/ezyang/b/pytorch/torch_compile_debug/run_2023_06_23_06_17_09_326738-pid_4066193/minifier/checkpoints', tracing_mode='real')
```

Run with `python repro.py run --tracing-mode=symbolic`

Fails with

```
Traceback (most recent call last):
  File "/data/users/ezyang/b/pytorch/repro.py", line 58, in <module>
    run_repro(mod, load_args, accuracy=False, command='run', save_dir='/data/users/ezyang/b/pytorch/torch_compile_debug/run_2023_06_23_06_17_09_326738-pid_4066193/minifier/checkpoints', tracing_mode='real')
  File "/data/users/ezyang/b/pytorch/torch/_dynamo/repro/after_aot.py", line 908, in run_repro
    COMMAND_FNS[options.command](options, mod, load_args)
  File "/data/users/ezyang/b/pytorch/torch/_dynamo/repro/after_aot.py", line 696, in repro_run
    ref = compiled(args)
  File "/data/users/ezyang/b/pytorch/torch/_inductor/codecache.py", line 313, in __call__
    return self.get_current_callable()(inputs)
  File "/data/users/ezyang/b/pytorch/torch/_inductor/compile_fx.py", line 510, in run
    return old_compiled_artifact(new_inputs)
  File "/data/users/ezyang/b/pytorch/torch/_inductor/cudagraph_trees.py", line 359, in deferred_cudagraphify
    fn, out = cudagraphify(model, inputs, static_input_idxs, *args, **kwargs)
  File "/data/users/ezyang/b/pytorch/torch/_inductor/cudagraph_trees.py", line 383, in cudagraphify
    return manager.add_function(
  File "/data/users/ezyang/b/pytorch/torch/_inductor/cudagraph_trees.py", line 1853, in add_function
    return fn, fn(inputs)
  File "/data/users/ezyang/b/pytorch/torch/_inductor/cudagraph_trees.py", line 1673, in run
    out = self._run(new_inputs, function_id)
  File "/data/users/ezyang/b/pytorch/torch/_inductor/cudagraph_trees.py", line 1714, in _run
    return self.run_eager(new_inputs, function_id)
  File "/data/users/ezyang/b/pytorch/torch/_inductor/cudagraph_trees.py", line 1829, in run_eager
    return node.run(new_inputs)
  File "/data/users/ezyang/b/pytorch/torch/_inductor/cudagraph_trees.py", line 571, in run
    out = self.wrapped_function.model(new_inputs)
  File "/data/users/ezyang/b/pytorch/torch/_inductor/codecache.py", line 340, in _run_from_cache
    return compiled_graph.compiled_artifact(inputs)
  File "/tmp/torchinductor_ezyang/tz/ctzcpl3bpwtj5hhfuecve6mdgpbgkxmuazrhludfbutwb6oxbu6t.py", line 28, in call
    return (1024*s2, )
NameError: name 's2' is not defined
```

Generated code is

```

from ctypes import c_void_p, c_long
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile

from torch import empty_strided, as_strided, device
from torch._inductor.codecache import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels

aten = torch.ops.aten
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
async_compile = AsyncCompile()


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 256, 24, 32), (196608, 768, 32, 1))
    return (1024*s2, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    arg0_1 = rand_strided((4, 256, 24, 32), (196608, 768, 32, 1), device='cuda:0', dtype=torch.float16)
    return print_performance(lambda: call([arg0_1]), times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.utils import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)
```

Repro was extracted from yolov3 although... it's not the actual bug I was trying to hit LOL.

### Versions

main

cc @msaroufim @wconstab @bdhirsh @anijain2305 @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @ngimel @yf225 @chenyang78