Repository: pytorch/vision

Issue:
Cannot be installed from source in debug mode if torch was built without CUDA
## 🐛 Bug

Cannot be installed from source in `DEBUG=1` mode if `torch` was built without CUDA.

## To Reproduce

1. Install `torch` without CUDA, e.g. from nightly pre-builds with CPU only.
2. Run `DEBUG=1 python setup.py develop`

```
No CUDA runtime is found, using CUDA_HOME='/opt/cuda'
Building wheel torchvision-0.9.0a0+b266c2f
Compile in debug mode
Traceback (most recent call last):
  File "setup.py", line 472, in <module>
    ext_modules=get_extensions(),
  File "setup.py", line 213, in get_extensions
    extra_compile_args['cxx'].append("-g")
KeyError: 'cxx'
```

## Expected behavior

Install without error.

## Environment

```
PyTorch version: 1.9.0.dev20210301+cpu
Is debug build: False
CUDA used to build PyTorch: Could not collect
ROCM used to build PyTorch: N/A

OS: Arch Linux (x86_64)
GCC version: (GCC) 10.2.0
Clang version: 11.1.0
CMake version: version 3.19.6

Python version: 3.6 (64-bit runtime)
Is CUDA available: False
CUDA runtime version: 11.2.142
GPU models and configuration: GPU 0: GeForce GTX 1080
Nvidia driver version: 460.56
cuDNN version: Probably one of the following:
/usr/lib/libcudnn.so.8.1.0
/usr/lib/libcudnn_adv_infer.so.8.1.0
/usr/lib/libcudnn_adv_train.so.8.1.0
/usr/lib/libcudnn_cnn_infer.so.8.1.0
/usr/lib/libcudnn_cnn_train.so.8.1.0
/usr/lib/libcudnn_ops_infer.so.8.1.0
/usr/lib/libcudnn_ops_train.so.8.1.0
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] torch==1.9.0.dev20210301+cpu
[conda] torch                     1.9.0.dev20210301+cpu          pypi_0    pypi
```

## Additional context

This is happens because we don't check if `cxx` is in `extra_compile_args` before we append to it:

https://github.com/pytorch/vision/blob/b266c2f1a5c10f5caf22f5aea7418acc392a5075/setup.py#L212-L214

We could either add this check or maybe add `cxx=[]` as default. If compiled with CUDA this is done

https://github.com/pytorch/vision/blob/b266c2f1a5c10f5caf22f5aea7418acc392a5075/setup.py#L199-L202

and it seems to be fine.


Discussion:
The proposed fix sounds good to me, can you send a PR for it?

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -181,7 +181,7 @@ def get_extensions():
 
     define_macros = []
 
-    extra_compile_args = {}
+    extra_compile_args = {'cxx': []}
     if (torch.cuda.is_available() and ((CUDA_HOME is not None) or is_rocm_pytorch)) \
             or os.getenv('FORCE_CUDA', '0') == '1':
         extension = CUDAExtension
@@ -196,15 +196,11 @@ def get_extensions():
         else:
             define_macros += [('WITH_HIP', None)]
             nvcc_flags = []
-        extra_compile_args = {
-            'cxx': [],
-            'nvcc': nvcc_flags,
-        }
+        extra_compile_args["nvcc"] = nvcc_flags
 
     if sys.platform == 'win32':
         define_macros += [('torchvision_EXPORTS', None)]
 
-        extra_compile_args.setdefault('cxx', [])
         extra_compile_args['cxx'].append('/MP')
 
     debug_mode = os.getenv('DEBUG', '0') == '1'


Test patch: