Repository: pytorch/pytorch

Issue:
Compatibility issue of torch._inductor.triton_ops.conv_perf_model and latest nightly triton
### 🐛 Describe the bug

In <https://github.com/openai/triton/commit/20100a7254e62efd0fced864b52a877c520fc38a#diff-e15e164b247309ce1b9a886812ae86b7043b4350af85f1b7cffe9a1f4dc45c05L361> (`python/src/triton.cc`, `init_triton_runtime()`), triton removed many attributes of `triton._C.libtriton.triton.runtime` such as `cc`, `max_shared_memory`, which is called in `triton_ops/conv_perf_model` in inductor. This issue can be triggered by setting `torch._inductor.config.triton.convolution` to `triton` and compiling a model with conv.

Pinning triton to a version prior to this change (such as `2.0.0.dev20221030`) workarounds this issue.

### Error logs

```
Traceback (most recent call last):
  File "/home/username/pytorch2/test3.py", line 34, in <module>
    print(model(torch.randn(1, 3, 224, 224).cuda()))
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 82, in __call__
    return self.dynamo_ctx(self._orig_mod.__call__)(*args, **kwargs)
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 215, in _fn
    return fn(*args, **kwargs)
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/username/pytorch2/test3.py", line 11, in forward
    def forward(self, x):
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 215, in _fn
    return fn(*args, **kwargs)
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 2821, in forward
    return compiled_fn(full_args)
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 1222, in g
    return f(*args)
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 2388, in debug_compiled_function
    return compiled_function(*args)
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 1900, in runtime_wrapper
    all_outs = call_func_with_args(
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 1247, in call_func_with_args
    out = normalize_as_list(f(args))
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 1222, in g
    return f(*args)
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/autograd/function.py", line 506, in apply
    return super().apply(*args, **kwargs)  # type: ignore[misc]
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 2153, in forward
    fw_outs = call_func_with_args(
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 1247, in call_func_with_args
    out = normalize_as_list(f(args))
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_inductor/compile_fx.py", line 250, in run
    return model(new_inputs)
  File "/tmp/torchinductor_username/xf/cxfscyw26zjrfhp4tamvn7hmhsnr6qtiq232xccma7l6sdo3u7dr.py", line 90, in call
    buf2 = triton_ops.conv(buf1, primals_1, None, (1, 1), (0, 0), (1, 1), False, (0, 0), 1)
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_inductor/triton_ops/conv.py", line 732, in forward
    return _conv._call(
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_inductor/triton_ops/conv.py", line 606, in _call
    _kernel_delta_x[grid](
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/triton/runtime/autotuner.py", line 75, in run
    pruned_configs = self.prune_configs(kwargs)
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/triton/runtime/autotuner.py", line 95, in prune_configs
    pruned_configs = self.early_config_prune(self.configs, self.nargs)
  File "/staff/username/miniconda3/envs/pytorch2/lib/python3.10/site-packages/torch/_inductor/triton_ops/conv_perf_model.py", line 100, in early_config_prune
    cc = _triton.runtime.cc(backend, device)
AttributeError: module 'triton._C.libtriton.triton.runtime' has no attribute 'cc'
```

### Minified repro

```python
# it seems that minifier failed to generate a minified code with `TORCHDYNAMO_REPRO_AFTER="aot"`
# so I will just put my MRE here.
import torch
import torch._dynamo
import torch._inductor.config
import logging

class ModuleTest2(torch.nn.Module):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.weight = torch.nn.Parameter(torch.randn(1, 3, 224, 224))
    
    def forward(self, x):
        x = x + torch.ones_like(x)
        res = torch.nn.functional.conv2d(x, self.weight)
        res = res + torch.ones_like(res)
        return res

torch._dynamo.config.log_level = logging.DEBUG
torch._dynamo.config.output_code = True
torch._dynamo.config.verbose = True
torch._inductor.config.triton.convolution = "triton"
torch._inductor.config.debug = True

model = ModuleTest2().cuda()

model = torch.compile(model)
print(model)
print(model(torch.randn(1, 3, 224, 224).cuda()))
```

### Versions

```
Collecting environment information...
PyTorch version: 2.0.0.dev20230228
Is debug build: False
CUDA used to build PyTorch: 11.8
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: 15.0.1 (https://github.com/exaloop/llvm-project 55b0b8fa1c9f9082b535628fc9fa6313280c0b9a)
CMake version: version 3.25.2
Libc version: glibc-2.31

Python version: 3.10.9 (main, Jan 11 2023, 15:21:40) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.4.0-137-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA A100-PCIE-40GB
Nvidia driver version: 465.19.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.8.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.8.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.8.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.8.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.8.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.8.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.8.0
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, 57 bits virtual
CPU(s):                          128
On-line CPU(s) list:             0-127
Thread(s) per core:              2
Core(s) per socket:              32
Socket(s):                       2
NUMA node(s):                    2
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           106
Model name:                      Intel(R) Xeon(R) Platinum 8358 CPU @ 2.60GHz
Stepping:                        6
Frequency boost:                 enabled
CPU MHz:                         900.000
CPU max MHz:                     3400.0000
CPU min MHz:                     800.0000
BogoMIPS:                        5200.00
Virtualization:                  VT-x
L1d cache:                       3 MiB
L1i cache:                       2 MiB
L2 cache:                        80 MiB
L3 cache:                        96 MiB
NUMA node0 CPU(s):               0-31,64-95
NUMA node1 CPU(s):               32-63,96-127
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:          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; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
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 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 pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local wbnoinvd dtherm ida arat pln pts avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq rdpid md_clear pconfig flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.23.5
[pip3] torch==2.0.0.dev20230228
[pip3] torchaudio==2.0.0.dev20230301
[pip3] torchvision==0.15.0.dev20230228
[conda] blas                      1.0                         mkl  
[conda] mkl                       2021.4.0           h06a4308_640  
[conda] mkl-service               2.4.0           py310h7f8727e_0  
[conda] mkl_fft                   1.3.1           py310hd6ae3a3_0  
[conda] mkl_random                1.2.2           py310h00e6091_0  
[conda] numpy                     1.23.5          py310hd5efca6_0  
[conda] numpy-base                1.23.5          py310h8e6c178_0  
[conda] pytorch                   2.0.0.dev20230228 py3.10_cuda11.8_cudnn8.7.0_0    pytorch-nightly
[conda] pytorch-cuda              11.8                 h7e8668a_3    pytorch-nightly
[conda] pytorch-mutex             1.0                        cuda    pytorch-nightly
[conda] torchaudio                2.0.0.dev20230301     py310_cu118    pytorch-nightly
[conda] torchvision               0.15.0.dev20230228     py310_cu118    pytorch-nightly
```

cc @ezyang @gchanan @zou3519 @soumith @msaroufim @wconstab @ngimel @bdhirsh


Discussion:
Reproduced this error with v2.0.0 release candidates as well.  The versions are below: 
pytorch-triton     2.0.0+b8b470bc59
torch              2.0.0+cu117
torchaudio         2.0.0+cu117
torchvision        0.15.0+cu117

As @taoky noted, this bug will be triggered if the source code include "torch._inductor.config.triton.convolution = "triton"". 
I'm not sure whether this bug should block the release, i.e. is the torch._inductor.config.triton.convolution = "triton" a commonly used configuration? 
Would be good to get @ezyang and @albanD input here. 

cc @seemethere @malfet  @atalman 
Removing `releng` label as it has nothing to do with release engineering activities.

As name suggests, `torch._inductor` is an internal package, as so no all permutations of tweaks available there  are guaranteed to work. But if this can be triggered via `torch.compile` kwargs then indeed it a a release blocking issue.
cc: @jansel @ngimel 
This will be fixed by https://github.com/pytorch/pytorch/pull/95556 for nightlies.  For the release, I'd suggest just removing the flag.
https://github.com/pytorch/pytorch/pull/95840 and https://github.com/pytorch/pytorch/pull/95842 for removing the config.

Resolving patch:
diff --git a/torch/_inductor/codegen/wrapper.py b/torch/_inductor/codegen/wrapper.py
--- a/torch/_inductor/codegen/wrapper.py
+++ b/torch/_inductor/codegen/wrapper.py
@@ -294,15 +294,6 @@ def __init__(self):
                 """
             )
 
-            if config.triton.convolution != "aten":
-                self.header.splice(
-                    """
-                    from torch._inductor.triton_ops.conv_perf_model import early_config_prune
-                    from torch._inductor.triton_ops.conv_perf_model import estimate_conv_time
-                    from torch._inductor.triton_ops.autotune import conv_heuristics
-                    """
-                )
-
         self.write_prefix()
 
         for name, value in V.graph.constants.items():
diff --git a/torch/_inductor/config.py b/torch/_inductor/config.py
--- a/torch/_inductor/config.py
+++ b/torch/_inductor/config.py
@@ -153,9 +153,6 @@ class triton:
     # Synchronize after every kernel launch, to help pinpoint bugs
     debug_sync_kernel = False
 
-    # choose conv backend, "aten" or "triton"
-    convolution = "aten"
-
     # Always load full blocks (rather than broadcasting inside the block)
     dense_indexing = False
 
diff --git a/torch/_inductor/ir.py b/torch/_inductor/ir.py
--- a/torch/_inductor/ir.py
+++ b/torch/_inductor/ir.py
@@ -2184,20 +2184,10 @@ def simplify_and_reorder(self):
             for reads_name in body.reads_name2expr.keys()
         ]
         priority_idx = []
-        if config.triton.convolution == "aten":
-            memory_addrs = [
-                *body.reads_name2expr.values(),
-                *body.writes_name2expr.values(),
-            ]
-        else:
-            # prioritize reads layout/loop_ordering over writes
-            if len(body.reads_name2expr.values()) > 0:
-                memory_addrs = [*body.reads_name2expr.values()]
-            else:
-                memory_addrs = [*body.writes_name2expr.values()]
-            for i, reads_buf in enumerate(reads_bufs):
-                if isinstance(reads_buf, Convolution):
-                    priority_idx.append(i)
+        memory_addrs = [
+            *body.reads_name2expr.values(),
+            *body.writes_name2expr.values(),
+        ]
         index_vars = []
         reduce_vars = []
         index_size = []
@@ -3140,12 +3130,8 @@ def create(
             )
             req_stride_order = get_stride_order(output.stride())
 
-        if config.triton.convolution == "aten":
-            weight = cls.require_stride_order(weight, req_stride_order)
-            x = cls.require_stride_order(x, req_stride_order)
-        else:
-            x = cls.require_stride1(cls.realize_input(x))
-            weight = cls.require_stride1(cls.realize_input(weight))
+        weight = cls.require_stride_order(weight, req_stride_order)
+        x = cls.require_stride_order(x, req_stride_order)
 
         stride = tuple(stride_)
         padding = tuple(padding_)
@@ -3163,7 +3149,7 @@ def create(
         _, _, *kernel_size = weight_shape
 
         # choose runtime kernel
-        config_conv = config.triton.convolution
+        config_conv = "aten"
         if (
             config_conv == "aten"
             or len(kernel_size) != 2  # triton conv only supports conv2d
@@ -3196,7 +3182,7 @@ def create(
             )
 
         # for conv2d or conv3d, prefer channels last format
-        transform_x_layout = config.triton.convolution != "aten"
+        transform_x_layout = False
         if kernel == "triton_ops.conv":
             output_layout_str = "torch.channels_last"
         else:


Test patch: