Repository: pytorch
Issue: TORCHDYNAMO_REPRO_AFTER=dynamo TORCHDYNAMO_REPRO_LEVEL=4 for accuracy debugging fails on extremely simple example
Labels: triaged,oncall: pt2,module: minifier

### 🐛 Describe the bug

Hello again. During debugging NaN issues with torch.compile, I've found out that simple nn.Sequential inheritance with "forward" redefinition throws error.

### Error logs

/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_inductor/compile_fx.py:150: UserWarning: TensorFloat32 tensor cores for float32 matrix multiplication available but not enabled. Consider setting `torch.set_float32_matmul_precision('high')` for better performance.
  warnings.warn(
W0816 18:40:55.872000 139890329966400 torch/fx/experimental/symbolic_shapes.py:4075] [0/0] Failing guard allocated at: 
W0816 18:40:55.872000 139890329966400 torch/fx/experimental/symbolic_shapes.py:4075] [0/0] 
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0] Error while creating guard:
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0] Name: ''
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     Source: shape_env
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     Create Function: SHAPE_ENV
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     Guard Types: None
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     Code List: None
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     Object Weakref: None
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     Guarded Class Weakref: None
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0] Traceback (most recent call last):
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_guards.py", line 260, in create
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     return self.create_fn(builder, self)
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/guards.py", line 1717, in SHAPE_ENV
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     guards = output_graph.shape_env.produce_guards(
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py", line 4084, in produce_guards
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     issue_guard(guard)
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py", line 4048, in issue_guard
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     guard_expr = ShapeGuardPrinter(symbol_to_source, source_ref, self.var_to_sources).doprint(expr)
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/printer.py", line 292, in doprint
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     return self._str(self._print(expr))
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/printer.py", line 331, in _print
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     return printmethod(expr, **kwargs)
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/str.py", line 778, in _print_Relational
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     return '%s %s %s' % (self.parenthesize(expr.lhs, precedence(expr)),
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/str.py", line 38, in parenthesize
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     return self._print(item)
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/printer.py", line 331, in _print
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     return printmethod(expr, **kwargs)
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/str.py", line 364, in _print_Mul
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     a_str = [self.parenthesize(x, prec, strict=False) for x in a]
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/str.py", line 364, in <listcomp>
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     a_str = [self.parenthesize(x, prec, strict=False) for x in a]
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/str.py", line 38, in parenthesize
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     return self._print(item)
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/printer.py", line 331, in _print
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     return printmethod(expr, **kwargs)
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py", line 1491, in _print_Symbol
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0]     assert self.symbol_to_source.get(expr), (
E0816 18:40:55.873000 139890329966400 torch/_guards.py:262] [0/0] AssertionError: s0 (could be from ['__meta_utils_unknown_tensor10.size()[0]']) not in {s1: [], s0: []}.  If this assert is failing, it could be due to the issue described in https://github.com/pytorch/pytorch/pull/90665
E0816 18:40:55.875000 139890329966400 torch/_guards.py:264] [0/0] Created at:
E0816 18:40:55.875000 139890329966400 torch/_guards.py:264] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 564, in transform
E0816 18:40:55.875000 139890329966400 torch/_guards.py:264] [0/0]     tracer = InstructionTranslator(
E0816 18:40:55.875000 139890329966400 torch/_guards.py:264] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2360, in __init__
E0816 18:40:55.875000 139890329966400 torch/_guards.py:264] [0/0]     output=OutputGraph(
E0816 18:40:55.875000 139890329966400 torch/_guards.py:264] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/output_graph.py", line 313, in __init__
E0816 18:40:55.875000 139890329966400 torch/_guards.py:264] [0/0]     self.init_ambient_guards()
E0816 18:40:55.875000 139890329966400 torch/_guards.py:264] [0/0]   File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/output_graph.py", line 452, in init_ambient_guards
E0816 18:40:55.875000 139890329966400 torch/_guards.py:264] [0/0]     self.guards.add(ShapeEnvSource().make_guard(GuardBuilder.SHAPE_ENV))
Traceback (most recent call last):
  File "/home/user//test.py", line 21, in <module>
    out = model(x)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1551, in _wrapped_call_impl
    return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 433, in _fn
    return fn(*args, **kwargs)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 1116, in __call__
    return self._torchdynamo_orig_callable(
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 948, in __call__
    result = self._inner_convert(
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 472, in __call__
    return _compile(
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_utils_internal.py", line 84, in wrapper_function
    return StrobelightCompileTimeProfiler.profile_compile_time(
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_strobelight/compile_time_profiler.py", line 129, in profile_compile_time
    return func(*args, **kwargs)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 817, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 231, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 726, in compile_inner
    check_fn = CheckFunctionManager(
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/guards.py", line 2130, in __init__
    guard.create(builder)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_guards.py", line 260, in create
    return self.create_fn(builder, self)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/_dynamo/guards.py", line 1717, in SHAPE_ENV
    guards = output_graph.shape_env.produce_guards(
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py", line 4084, in produce_guards
    issue_guard(guard)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py", line 4048, in issue_guard
    guard_expr = ShapeGuardPrinter(symbol_to_source, source_ref, self.var_to_sources).doprint(expr)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/printer.py", line 292, in doprint
    return self._str(self._print(expr))
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/printer.py", line 331, in _print
    return printmethod(expr, **kwargs)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/str.py", line 778, in _print_Relational
    return '%s %s %s' % (self.parenthesize(expr.lhs, precedence(expr)),
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/str.py", line 38, in parenthesize
    return self._print(item)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/printer.py", line 331, in _print
    return printmethod(expr, **kwargs)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/str.py", line 364, in _print_Mul
    a_str = [self.parenthesize(x, prec, strict=False) for x in a]
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/str.py", line 364, in <listcomp>
    a_str = [self.parenthesize(x, prec, strict=False) for x in a]
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/str.py", line 38, in parenthesize
    return self._print(item)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/sympy/printing/printer.py", line 331, in _print
    return printmethod(expr, **kwargs)
  File "/home/user/anaconda3/envs/python310/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py", line 1491, in _print_Symbol
    assert self.symbol_to_source.get(expr), (
AssertionError: s0 (could be from ['__meta_utils_unknown_tensor10.size()[0]']) not in {s1: [], s0: []}.  If this assert is failing, it could be due to the issue described in https://github.com/pytorch/pytorch/pull/90665


You can suppress this exception and fall back to eager by setting:
    import torch._dynamo
    torch._dynamo.config.suppress_errors = True

### Minified repro

```
import torch

device = torch.device("cuda:0")

# this one works fine
# class Test(torch.nn.Sequential):
#     pass

class Test(torch.nn.Sequential):
    def forward(self, x):
        for layer in self:
            x = layer(x)
        return x
    

model = Test(torch.nn.Linear(64, 64)).to(device)
model.compile()

x = torch.randn(4, 16, 64, device=device)
out = model(x)
```

### Versions

Collecting environment information...
PyTorch version: 2.4.0+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: 14.0.0-1ubuntu1.1
CMake version: version 3.30.0
Libc version: glibc-2.35

Python version: 3.10.13 | packaged by conda-forge | (main, Dec 23 2023, 15:36:39) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-5.4.210-39.1-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.4.99
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA A800-SXM4-80GB
GPU 1: NVIDIA A800-SXM4-80GB
GPU 2: NVIDIA A800-SXM4-80GB
GPU 3: NVIDIA A800-SXM4-80GB
GPU 4: NVIDIA A800-SXM4-80GB
GPU 5: NVIDIA A800-SXM4-80GB
GPU 6: NVIDIA A800-SXM4-80GB
GPU 7: NVIDIA A800-SXM4-80GB

Nvidia driver version: 550.54.14
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
Address sizes:                   48 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          120
On-line CPU(s) list:             0-119
Vendor ID:                       AuthenticAMD
Model name:                      AMD EPYC 7662 64-Core Processor
CPU family:                      23
Model:                           49
Thread(s) per core:              1
Core(s) per socket:              1
Socket(s):                       120
Stepping:                        0
BogoMIPS:                        3992.43
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core ssbd ibrs ibpb stibp vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 clzero xsaveerptr wbnoinvd arat npt nrip_save umip rdpid arch_capabilities
Virtualization:                  AMD-V
L1d cache:                       7.5 MiB (120 instances)
L1i cache:                       7.5 MiB (120 instances)
L2 cache:                        60 MiB (120 instances)
L3 cache:                        1.9 GiB (120 instances)
NUMA node(s):                    4
NUMA node0 CPU(s):               0-29
NUMA node1 CPU(s):               30-59
NUMA node2 CPU(s):               60-89
NUMA node3 CPU(s):               90-119
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   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; Retpolines, IBPB conditional, IBRS_FW, STIBP disabled, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] flake8==5.0.4
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.3
[pip3] open-clip-torch==2.24.0
[pip3] pytorch-warmup==0.1.1
[pip3] torch==2.4.0+cu124
[pip3] torch-model-archiver==0.11.1
[pip3] torch-tb-profiler==0.4.3
[pip3] torch-workflow-archiver==0.2.14
[pip3] torchaudio==2.4.0+cu124
[pip3] torchdata==0.7.1
[pip3] torchmetrics==1.4.0.post0
[pip3] torchserve==0.11.1
[pip3] torchvision==0.19.0+cu124
[pip3] triton==3.0.0
[conda] numpy                     1.26.3                   pypi_0    pypi
[conda] open-clip-torch           2.24.0                   pypi_0    pypi
[conda] torch                     2.4.0+cu124              pypi_0    pypi
[conda] torch-model-archiver      0.11.1                   pypi_0    pypi
[conda] torch-tb-profiler         0.4.3                    pypi_0    pypi
[conda] torch-workflow-archiver   0.2.14                   pypi_0    pypi
[conda] torchaudio                2.4.0+cu124              pypi_0    pypi
[conda] torchdata                 0.7.1                    pypi_0    pypi
[conda] torchmetrics              1.4.0.post0              pypi_0    pypi
[conda] torchserve                0.11.1                   pypi_0    pypi
[conda] torchvision               0.19.0+cu124             pypi_0    pypi
[conda] triton                    3.0.0                    pypi_0    pypi

cc @ezyang @chauhang @penguinwu