| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import shutil |
| 5 | import subprocess |
| 6 | import sys |
| 7 | from pathlib import Path |
| 8 | |
| 9 | |
| 10 | def _get_cmake_bin() -> str: |
| 11 | cmake = shutil.which("cmake") |
| 12 | if cmake: |
| 13 | return cmake |
| 14 | venv_cmake = Path(sys.executable).parent / "cmake" |
| 15 | if venv_cmake.is_file(): |
| 16 | return str(venv_cmake) |
| 17 | return "cmake" |
| 18 | |
| 19 | |
| 20 | def test_exact_family_arch_precedes_generic_family_fallback(tmp_path: Path): |
| 21 | repo_root = Path(__file__).parents[1] |
| 22 | script = tmp_path / "test_cuda_archs.cmake" |
| 23 | script.write_text( |
| 24 | f""" |
| 25 | cmake_minimum_required(VERSION 3.26) |
| 26 | include("{repo_root / "cmake" / "utils.cmake"}") |
| 27 | cuda_archs_loose_intersection( |
| 28 | actual "10.0f;10.7f" "10.7") |
| 29 | if(NOT "${{actual}}" STREQUAL "10.7f") |
| 30 | message(FATAL_ERROR "Expected 10.7f, got '${{actual}}'") |
| 31 | endif() |
| 32 | """ |
| 33 | ) |
| 34 | |
| 35 | subprocess.run([_get_cmake_bin(), "-P", script], check=True) |
| 36 | |
| 37 | |
| 38 | def test_extract_archs_prefers_sass_target_over_corrupted_virtual_arch( |
| 39 | tmp_path: Path, |
| 40 | ): |
| 41 | """Torch's autodetection can emit a bogus arch=compute_* half (e.g. |
| 42 | capability 12.1 corrupted to arch=compute_20,code=sm_121); the SASS |
| 43 | target must win, while PTX-only entries keep the virtual arch.""" |
| 44 | repo_root = Path(__file__).parents[1] |
| 45 | script = tmp_path / "test_extract_archs.cmake" |
| 46 | script.write_text( |
| 47 | f""" |
| 48 | cmake_minimum_required(VERSION 3.26) |
| 49 | include("{repo_root / "cmake" / "utils.cmake"}") |
| 50 | extract_unique_cuda_archs_ascending(actual |
| 51 | "-gencode arch=compute_20,code=sm_121;\ |
| 52 | -gencode arch=compute_80,code=sm_80;\ |
| 53 | -gencode arch=compute_80,code=compute_80") |
| 54 | if(NOT "${{actual}}" STREQUAL "8.0;12.1") |
| 55 | message(FATAL_ERROR "Expected '8.0;12.1', got '${{actual}}'") |
| 56 | endif() |
| 57 | """ |
| 58 | ) |
| 59 | |
| 60 | subprocess.run([_get_cmake_bin(), "-P", script], check=True) |
| 61 | |
| 62 | |
| 63 | def test_clear_cuda_gencode_flags(tmp_path: Path): |
| 64 | repo_root = Path(__file__).parents[1] |
| 65 | script = tmp_path / "test_clear_flags.cmake" |
| 66 | script.write_text( |
| 67 | f""" |
| 68 | cmake_minimum_required(VERSION 3.26) |
| 69 | include("{repo_root / "cmake" / "utils.cmake"}") |
| 70 | set(CMAKE_CUDA_FLAGS "-Wall -gencode arch=compute_80,code=sm_80") |
| 71 | clear_cuda_gencode_flags(CUDA_ARCH_FLAGS) |
| 72 | if(NOT "${{CMAKE_CUDA_FLAGS}}" STREQUAL "-Wall ") |
| 73 | message(FATAL_ERROR "Expected '-Wall ', got '${{CMAKE_CUDA_FLAGS}}'") |
| 74 | endif() |
| 75 | if(NOT "${{CUDA_ARCH_FLAGS}}" STREQUAL "-gencode arch=compute_80,code=sm_80") |
| 76 | message(FATAL_ERROR "Expected '-gencode arch=compute_80,code=sm_80', " |
| 77 | "got '${{CUDA_ARCH_FLAGS}}'") |
| 78 | endif() |
| 79 | """ |
| 80 | ) |
| 81 | |
| 82 | subprocess.run([_get_cmake_bin(), "-P", script], check=True) |