Skip to content

Commit f626041

Browse files
authored
Fix CUDA backend Windows build under C++20 (pytorch#23135)
### Summary All \`test-model-cuda-windows-e2e\` jobs on main fail to build: \`\`\` backends\cuda\runtime\platform\platform.cpp(63,20): error C2440: 'initializing': cannot convert from 'std::basic_string<char8_t,...>' to 'std::basic_string<char,...>' \`\`\` pytorch#22793 made \`executorch_core\` export \`cxx_std_20\`, so \`cuda_platform\` now compiles as C++20, where \`std::filesystem::path::u8string()\` returns \`std::u8string\`, which no longer converts to \`std::string\`. Fix: load the library with \`LoadLibraryW(path.c_str())\` (which is also correct for non-ASCII paths), and build a UTF-8 \`std::string\` only for the error log. The conversion compiles under both C++17 and C++20. **Second fix:** once platform.cpp built, `aoti_cuda_shims` failed next: ``` c10/util/safe_numerics.h(71): error : namespace "std" has no member "countl_zero" ``` The `.cu` shims are compiled by nvcc, and `backends/cuda/CMakeLists.txt` pinned `CMAKE_CUDA_STANDARD 17`. `cxx_std_20` on `executorch_core` only raises the CXX language, so it never reaches nvcc. The CUDA backend now uses C++20 for both CXX and CUDA on Windows (matching the top-level rule) and stays on C++17 elsewhere. ### Test plan CI: \`Test CUDA Windows Export and E2E\` (triggered by the \`backends/cuda/**\` path filter).
1 parent 82c3bc0 commit f626041

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎backends/cuda/CMakeLists.txt‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@
1616
#
1717
cmake_minimum_required(VERSION 3.26)
1818

19-
set(CMAKE_CXX_STANDARD 17)
19+
# The .cu shims include vendored c10's safe_numerics.h, whose MSVC-only branch
20+
# needs C++20 (see the top-level CMakeLists.txt). cxx_std_20 on executorch_core
21+
# does not reach nvcc, so the CUDA standard has to be raised here too.
22+
if(WIN32)
23+
set(CMAKE_CXX_STANDARD 20)
24+
set(CMAKE_CUDA_STANDARD 20)
25+
else()
26+
set(CMAKE_CXX_STANDARD 17)
27+
set(CMAKE_CUDA_STANDARD 17)
28+
endif()
2029
set(CMAKE_CXX_STANDARD_REQUIRED ON)
21-
set(CMAKE_CUDA_STANDARD 17)
2230
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
2331

2432
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

‎backends/cuda/runtime/platform/platform.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ namespace cuda {
6060
executorch::runtime::Result<void*> load_library(
6161
const std::filesystem::path& path) {
6262
#ifdef _WIN32
63-
std::string utf8 = path.u8string();
64-
auto lib_handle = LoadLibrary(utf8.c_str());
63+
auto lib_handle = LoadLibraryW(path.c_str());
6564
if (lib_handle == NULL) {
6665
const DWORD err = GetLastError();
66+
// u8string() returns std::u8string under C++20, so copy into std::string.
67+
const auto u8 = path.u8string();
68+
const std::string utf8(u8.begin(), u8.end());
6769
ET_LOG(
6870
Error,
6971
"Failed to load %s with error %lu: %s",

0 commit comments

Comments
 (0)