Repository: pytorch
Issue: `torch.compile(dynamic=True)` compiles forever
Labels: module: performance,triaged,ezyang's list,oncall: pt2,module: dynamic shapes,module: startup-tracing-compile

### 🐛 Describe the bug


The following trivial example using `torch.compile(dynamic=True)` fails to compile. It never ends compiling, stalling forever on PyTorch 2.1.2.



```py
import torch
import torchvision.models as models

device = torch.device("cuda", 0)
model = models.densenet121().to(device)

# dynamic=True compiles forever
# dynamic=False compiles in ~ 90 seconds
# On PyTorch 2.3 nightly, fails with AttributeError
compiled_model = torch.compile(model, dynamic=True)

input = torch.randn(16, 3, 128, 128, device=device)
compiled_model(input)
input = torch.randn(8, 3, 128, 128, device=device)
compiled_model(input)

```

On the other hand, on PyTorch 2.3 nightly, it crashes with an AttributeError:
```
Traceback (most recent call last):
  File "/home/adrian/repositories/lightning/examples/compile_guide_dynamic.py", line 12, in <module>
    compiled_model(input)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 443, in _fn
    return fn(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 605, in catch_errors
    return callback(frame, cache_entry, hooks, frame_state)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 748, in _convert_frame
    result = inner_convert(frame, cache_entry, hooks, frame_state)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 390, in _convert_frame_assert
    return _compile(
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 650, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 248, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 531, in compile_inner
    out_code = transform_code_object(code, transform)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/bytecode_transformation.py", line 1033, in transform_code_object
    transformations(instructions, code_options)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 155, in _fn
    return fn(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 496, in transform
    tracer.run()
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2125, in run
    super().run()
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 787, in run
    and self.step()
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 750, in step
    getattr(self, inst.opname)(inst)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2244, in RETURN_VALUE
    self.output.compile_subgraph(
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/output_graph.py", line 918, in compile_subgraph
    self.compile_and_call_fx_graph(tx, list(reversed(stack_values)), root)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/output_graph.py", line 1089, in compile_and_call_fx_graph
    compiled_fn = self.call_user_compiler(gm)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 248, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/output_graph.py", line 1162, in call_user_compiler
    raise BackendCompilerFailed(self.compiler_fn, e).with_traceback(
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/output_graph.py", line 1143, in call_user_compiler
    compiled_fn = compiler_fn(gm, self.example_inputs())
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/repro/after_dynamo.py", line 117, in debug_wrapper
    compiled_gm = compiler_fn(gm, example_inputs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/__init__.py", line 1714, in __call__
    return compile_fx(model_, inputs_, config_patches=self.config)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/compile_fx.py", line 1214, in compile_fx
    return aot_autograd(
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/backends/common.py", line 55, in compiler_fn
    cg = aot_module_simplified(gm, example_inputs, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 891, in aot_module_simplified
    compiled_fn = create_aot_dispatcher_function(
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 248, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 604, in create_aot_dispatcher_function
    compiled_fn = compiler_fn(flat_fn, fake_flat_args, aot_config, fw_metadata=fw_metadata)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 434, in aot_wrapper_dedupe
    return compiler_fn(flat_fn, leaf_flat_args, aot_config, fw_metadata=fw_metadata)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 639, in aot_wrapper_synthetic_base
    return compiler_fn(flat_fn, flat_args, aot_config, fw_metadata=fw_metadata)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/jit_compile_runtime_wrappers.py", line 296, in aot_dispatch_autograd
    compiled_fw_func = aot_config.fw_compiler(fw_module, adjusted_flat_args)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 248, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/compile_fx.py", line 1142, in fw_compiler_base
    return inner_compile(
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/repro/after_aot.py", line 83, in debug_wrapper
    inner_compiled_fn = compiler_fn(gm, example_inputs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/debug.py", line 304, in inner
    return fn(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/compile_fx.py", line 345, in compile_fx_inner
    compiled_graph = fx_codegen_and_compile(
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/compile_fx.py", line 582, in fx_codegen_and_compile
    compiled_fn = graph.compile_to_fn()
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/graph.py", line 1181, in compile_to_fn
    return self.compile_to_module().call
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 248, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/graph.py", line 1133, in compile_to_module
    mod = PyCodeCache.load_by_key_path(
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/codecache.py", line 2046, in load_by_key_path
    exec(code, mod.__dict__, mod.__dict__)
  File "/tmp/torchinductor_adrian/a5/ca5kyge3jddrpgryty23rg4uqhyfx23654vwnlqsbas4gk2o6jzw.py", line 11629, in <module>
    async_compile.wait(globals())
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/codecache.py", line 2585, in wait
    scope[key] = result.result()
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/codecache.py", line 2393, in result
    kernel = self.kernel = _load_kernel(self.kernel_name, self.source_code)
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/codecache.py", line 2369, in _load_kernel
    kernel.precompile()
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/triton_heuristics.py", line 200, in precompile
    compiled_binary, launcher = self._precompile_config(
  File "/home/adrian/.conda/envs/lightning/lib/python3.10/site-packages/torch/_inductor/triton_heuristics.py", line 378, in _precompile_config
    (binary.metadata.num_ctas, *binary.metadata.cluster_dims)
torch._dynamo.exc.BackendCompilerFailed: backend='inductor' raised:
AttributeError: 'dict' object has no attribute 'num_ctas'

Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information


You can suppress this exception and fall back to eager by setting:
    import torch._dynamo
    torch._dynamo.config.suppress_errors = True
```

### Versions

Collecting environment information...
PyTorch version: 2.3.0.dev20240127+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.26.0
Libc version: glibc-2.35

Python version: 3.10.9 (main, Jan 11 2023, 15:21:40) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-75-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: Could not collect
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.125.06
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.5
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.5
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.5
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.5
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.5
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.5
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.5
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:                   43 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          256
On-line CPU(s) list:             0-254
Off-line CPU(s) list:            255
Vendor ID:                       AuthenticAMD
Model name:                      AMD EPYC 7742 64-Core Processor
CPU family:                      23
Model:                           49
Thread(s) per core:              2
Core(s) per socket:              64
Socket(s):                       2
Stepping:                        0
Frequency boost:                 enabled
CPU max MHz:                     2250.0000
CPU min MHz:                     0.0000
BogoMIPS:                        4499.83
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip rdpid overflow_recov succor smca sme sev sev_es
Virtualization:                  AMD-V
L1d cache:                       4 MiB (128 instances)
L1i cache:                       4 MiB (128 instances)
L2 cache:                        64 MiB (128 instances)
L3 cache:                        512 MiB (32 instances)
NUMA node(s):                    2
NUMA node0 CPU(s):               0-63,128-191
NUMA node1 CPU(s):               64-127,192-254
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Retbleed:          Mitigation; untrained return thunk; SMT enabled with STIBP protection
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, STIBP always-on, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] mypy==1.4.1
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.2
[pip3] onnx==1.12.0
[pip3] onnxruntime==1.14.1
[pip3] pytorch-lightning==2.0.6
[pip3] pytorch-triton==3.0.0+901819d2b6
[pip3] torch==2.3.0.dev20240127+cu121
[pip3] torch-tb-profiler==0.4.3
[pip3] torchaudio==2.2.0.dev20240127+cu121
[pip3] torchmetrics==1.0.3
[pip3] torchvision==0.18.0.dev20240127+cu121
[pip3] triton==2.1.0
[conda] numpy                     1.24.2                   pypi_0    pypi
[conda] pytorch-lightning         2.0.6                    pypi_0    pypi
[conda] pytorch-triton            3.0.0+901819d2b6          pypi_0    pypi
[conda] torch                     2.3.0.dev20240127+cu121          pypi_0    pypi
[conda] torch-tb-profiler         0.4.3                    pypi_0    pypi
[conda] torchaudio                2.2.0.dev20240127+cu121          pypi_0    pypi
[conda] torchmetrics              1.0.3                    pypi_0    pypi
[conda] torchvision               0.18.0.dev20240127+cu121          pypi_0    pypi
[conda] triton                    2.1.0                    pypi_0    pypi

cc @ezyang @msaroufim @bdhirsh @anijain2305 @zou3519 @chauhang