Repository: pytorch
Issue: Vendored FindCUDAToolkit.cmake deviates from upstream in splayed installation support
Labels: module: build,triaged

### 🐛 Describe the bug

Hi!

> CMake supports `CUDAToolkit_ROOT` being a list of directories to search so you can add each of the splayed directories to that CMake variable to find each subcomponent ( All <pkg>_ROOT variables support multiple values ).
> https://gitlab.kitware.com/cmake/cmake/-/issues/24663#note_1345852

As far as I can see, upstream locates CUDA components using `find_library` with `HINTS` derived (?) from `CUDAToolkit_ROOT` and `CUDAToolkit_INCLUDE_DIR`, whereas the copy uses `find_path` with custom (?) `${ComponentName}_INCLUDE_DIR` variables (introduced in https://github.com/pytorch/pytorch/pull/82695; [line](https://github.com/pytorch/pytorch/pull/82695/files#diff-da8417910b430cc5f7d50fdfa3e0f5a78c73b67e750b50bee47e6f25e178d7adR1048))? I didn't dig too deep though.

It would be really nice if pytorch supported the same interface as upstream's `FindCUDAToolkit.cmake`.

Symptoms:

```bash
$ cmakeFlags=-DCUDAToolkit_ROOT=/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvcc-11.8.89;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_cudart-11.8.89;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_cccl-11.8.89;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libnpp-11.8.0.86;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcublas-11.11.3.6;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcufft-10.9.0.58 ...
$ cmake -S . -B build/ $cmakeFlags
...
-- The CUDA compiler identification is NVIDIA 11.8.89
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Check for working CUDA compiler: /nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvcc-11.8.89/bin/nvcc - skipped
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
-- Unable to find cuda_runtime.h in "/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvcc-11.8.89/include" for CUDAToolkit_INCLUDE_DIR.
-- Unable to find cublas_v2.h in either "/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_cccl-11.8.89-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_cudart-11.8.89/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_cupti-11.8.87-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvcc-11.8.89-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvml_dev-11.8.86-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvrtc-11.8.89-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvtx-11.8.86-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcublas-11.11.3.6-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcufft-10.9.0.58-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcurand-10.3.0.86-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcusolver-11.4.1.48-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcusparse-11.7.5.86-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_profiler_api-11.8.86-dev/include" or "/nix/math_libs/include"
-- Found CUDAToolkit: /nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_cccl-11.8.89-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_cudart-11.8.89/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_cupti-11.8.87-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvcc-11.8.89-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvml_dev-11.8.86-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvrtc-11.8.89-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_nvtx-11.8.86-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcublas-11.11.3.6-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcufft-10.9.0.58-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcurand-10.3.0.86-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcusolver-11.4.1.48-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-libcusparse-11.7.5.86-dev/include;/nix/store/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-cuda_profiler_api-11.8.86-dev/include (found version "11.8.89") 
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
CMake Error at cmake/public/cuda.cmake:71 (message):
  Failed to find nvToolsExt
Call Stack (most recent call first):
  cmake/Dependencies.cmake:44 (include)
  CMakeLists.txt:722 (include)
$ rm cmake/Modules/FindCUDAToolkit.cmake
$ cmake -S . -B build/ $cmakeFlags
...
python3.11-torch> -- Configuring done (10.5s)                                         
python3.11-torch> -- Generating done (0.9s)                                                                                                                                                                                                                                                                                                                                                                                               
python3.11-torch> -- Build files have been written to: ...
```

### Versions

```
Collecting environment information...
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: NixOS 23.11 (Tapir) (x86_64)
GCC version: (GCC) 11.4.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.38

Python version: 3.11.5 (main, Aug 24 2023, 12:23:19) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-6.1.60-x86_64-with-glibc2.38
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090
Nvidia driver version: 545.23.06
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):                             24
On-line CPU(s) list:                0-23
Vendor ID:                          AuthenticAMD
Model name:                         AMD Ryzen 9 5900X 12-Core Processor
CPU family:                         25
Model:                              33
Thread(s) per core:                 2
Core(s) per socket:                 12
Socket(s):                          1
Stepping:                           0
Frequency boost:                    enabled
CPU(s) scaling MHz:                 75%
CPU max MHz:                        5619.1401
CPU min MHz:                        2200.0000
BogoMIPS:                           8399.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 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 erms invpcid 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 arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca fsrm
Virtualization:                     AMD-V
L1d cache:                          384 KiB (12 instances)
L1i cache:                          384 KiB (12 instances)
L2 cache:                           6 MiB (12 instances)
L3 cache:                           64 MiB (2 instances)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-23
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 Retbleed:             Not affected
Vulnerability Spec rstack overflow: Mitigation; safe RET, no microcode
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, IBRS_FW, STIBP always-on, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] numpy==1.25.2
[pip3] torch==2.0.1
[conda] Could not collect
```

cc @malfet @seemethere