Repository: pytorch
Issue: list index out of range in cpp_extension.py
Labels: module: build,triaged

### 🐛 Describe the bug

I am trying to build a pip package that depends on torch to build, with

```
 cat pyproject.toml 
[build-system]

requires = ["torch"]
```

The package builds a CUDA C++ extension, and calls torck/utils/cpp_extension.py, but hits a list index out of range error:

```
Processing /home/jacob/dev/fused-ssim
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: fused_ssim
  Building wheel for fused_ssim (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for fused_ssim (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [94 lines of output]
      /var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/torch/_subclasses/functional_tensor.py:258: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at ../torch/csrc/utils/tensor_numpy.cpp:84.)
        cpu = _conversion_method_template(device=torch.device("cpu"))
      /var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/torch/cuda/__init__.py:128: UserWarning: CUDA initialization: Unexpected error from cudaGetDeviceCount(). Did you run some cuda functions before calling NumCudaDevices() that might have already set an error? Error 803: system has unsupported display driver / cuda driver combination (Triggered internally at ../c10/cuda/CUDAFunctions.cpp:108.)
        return torch._C._cuda_getDeviceCount() > 0
      No CUDA runtime is found, using CUDA_HOME='/opt/cuda'
      running bdist_wheel
      running build
      running build_py
      copying fused_ssim/__init__.py -> build/lib.linux-x86_64-cpython-312/fused_ssim
      running build_ext
      /var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/torch/utils/cpp_extension.py:414: UserWarning: The detected CUDA version (12.6) has a minor version mismatch with the version that was used to compile PyTorch (12.1). Most likely this shouldn't be a problem.
        warnings.warn(CUDA_MISMATCH_WARN.format(cuda_str_version, torch.version.cuda))
      /var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/torch/utils/cpp_extension.py:424: UserWarning: There are no g++ version bounds defined for CUDA version 12.6
        warnings.warn(f'There are no {compiler_name} version bounds defined for CUDA version {cuda_str_version}')
      building 'fused_ssim_cuda' extension
      /var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/torch/utils/cpp_extension.py:1965: UserWarning: TORCH_CUDA_ARCH_LIST is not set, all archs for visible cards are included for compilation.
      If this is not desired, please set os.environ['TORCH_CUDA_ARCH_LIST'].
        warnings.warn(
      /var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/torch/cuda/__init__.py:654: UserWarning: Can't initialize NVML
        warnings.warn("Can't initialize NVML")
      Traceback (most recent call last):
        File "/home/jacob/dev/vertigo-ai/train/env/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/home/jacob/dev/vertigo-ai/train/env/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/jacob/dev/vertigo-ai/train/env/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 251, in build_wheel
          return _build_backend().build_wheel(wheel_directory, config_settings,
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 421, in build_wheel
          return self._build_with_temp_dir(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 403, in _build_with_temp_dir
          self.run_setup()
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 503, in run_setup
          super().run_setup(setup_script=setup_script)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 318, in run_setup
          exec(code, locals())
        File "<string>", line 4, in <module>
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/__init__.py", line 117, in setup
          return distutils.core.setup(**attrs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/core.py", line 183, in setup
          return run_commands(dist)
                 ^^^^^^^^^^^^^^^^^^
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/core.py", line 199, in run_commands
          dist.run_commands()
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/dist.py", line 954, in run_commands
          self.run_command(cmd)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/dist.py", line 950, in run_command
          super().run_command(command)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/dist.py", line 973, in run_command
          cmd_obj.run()
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/command/bdist_wheel.py", line 398, in run
          self.run_command("build")
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/cmd.py", line 316, in run_command
          self.distribution.run_command(command)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/dist.py", line 950, in run_command
          super().run_command(command)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/dist.py", line 973, in run_command
          cmd_obj.run()
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/command/build.py", line 135, in run
          self.run_command(cmd_name)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/cmd.py", line 316, in run_command
          self.distribution.run_command(command)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/dist.py", line 950, in run_command
          super().run_command(command)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/dist.py", line 973, in run_command
          cmd_obj.run()
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/command/build_ext.py", line 98, in run
          _build_ext.run(self)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/command/build_ext.py", line 359, in run
          self.build_extensions()
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/torch/utils/cpp_extension.py", line 866, in build_extensions
          build_ext.build_extensions(self)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/command/build_ext.py", line 476, in build_extensions
          self._build_extensions_serial()
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/command/build_ext.py", line 502, in _build_extensions_serial
          self.build_extension(ext)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/command/build_ext.py", line 263, in build_extension
          _build_ext.build_extension(self, ext)
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/setuptools/_distutils/command/build_ext.py", line 557, in build_extension
          objects = self.compiler.compile(
                    ^^^^^^^^^^^^^^^^^^^^^^
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/torch/utils/cpp_extension.py", line 670, in unix_wrap_ninja_compile
          cuda_post_cflags = unix_cuda_flags(cuda_post_cflags)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/torch/utils/cpp_extension.py", line 569, in unix_cuda_flags
          cflags + _get_cuda_arch_flags(cflags))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/var/tmp/pip-build-env-43s_9fyc/overlay/lib/python3.12/site-packages/torch/utils/cpp_extension.py", line 1985, in _get_cuda_arch_flags
          arch_list[-1] += '+PTX'
          ~~~~~~~~~^^^^
      IndexError: list index out of range
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for fused_ssim
Failed to build fused_ssim
ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (fused_ssim)
```

Looking at the code, it looks like it is possible for arch_list to be empty, which will explain why the code crashes here.


### Versions

Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Arch Linux (x86_64)
GCC version: (GCC) 14.2.1 20240910
Clang version: 18.1.8
CMake version: version 3.30.4
Libc version: glibc-2.40

Python version: 3.12.6 (main, Sep  8 2024, 13:18:56) [GCC 14.2.1 20240805] (64-bit runtime)
Python platform: Linux-6.9.8-arch1-1-x86_64-with-glibc2.40
Is CUDA available: N/A
CUDA runtime version: 12.6.68
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Probably one of the following:
/usr/lib/libcudnn.so.9.2.1
/usr/lib/libcudnn_adv.so.9.2.1
/usr/lib/libcudnn_cnn.so.9.2.1
/usr/lib/libcudnn_engines_precompiled.so.9.2.1
/usr/lib/libcudnn_engines_runtime_compiled.so.9.2.1
/usr/lib/libcudnn_graph.so.9.2.1
/usr/lib/libcudnn_heuristic.so.9.2.1
/usr/lib/libcudnn_ops.so.9.2.1
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

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):                               12
On-line CPU(s) list:                  0-11
Vendor ID:                            AuthenticAMD
Model name:                           AMD Ryzen 5 3600 6-Core Processor
CPU family:                           23
Model:                                113
Thread(s) per core:                   2
Core(s) per socket:                   6
Socket(s):                            1
Stepping:                             0
Frequency boost:                      enabled
CPU(s) scaling MHz:                   66%
CPU max MHz:                          4208.2031
CPU min MHz:                          2200.0000
BogoMIPS:                             7203.16
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 xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 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 ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd 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 sev sev_es
Virtualization:                       AMD-V
L1d cache:                            192 KiB (6 instances)
L1i cache:                            192 KiB (6 instances)
L2 cache:                             3 MiB (6 instances)
L3 cache:                             32 MiB (2 instances)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-11
Vulnerability Gather data sampling:   Not affected
Vulnerability Itlb multihit:          Not affected
Vulnerability L1tf:                   Not affected
Vulnerability Mds:                    Not affected
Vulnerability Meltdown:               Not affected
Vulnerability Mmio stale data:        Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed:               Mitigation; untrained return thunk; SMT enabled with STIBP protection
Vulnerability Spec rstack overflow:   Mitigation; Safe RET
Vulnerability Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl
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; BHI Not affected
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Not affected

Versions of relevant libraries:
[pip3] numpy==2.1.1
[pip3] triton==3.0.0
[conda] Could not collect

cc @malfet @seemethere