Skip to content

Commit fc21064

Browse files
Give the CUDA extension the toolkit and the runtime it needs (pytorch#23100)
The CUDA extension could not build in two configurations. In both cases it used something it never asked for. ## It links the toolkit without looking for it It worked anyway, because the Torch package found a few lines above in the parent defines that target, but only when Torch itself is a CUDA build. Anywhere that is not true, the configure step stops and says the target was not found. The extension already looks for its accelerator package on the other branch of the same condition, and both backends that link the same library make the call for themselves. This adds the matching call. ## It needs the runtime, and in some builds had nothing to link it against The guard logs, so the extension needs the ExecuTorch runtime. It took that through a helper which does nothing when a build produces no shared runtime, so in those builds the reference had nothing to resolve against. Where one shared runtime exists, the helper still retains it, so a process keeps a single registry. Where it does not, the extension links the static core, which is already defined long before this directory is configured. That leaves the library holding a copy of the platform layer, and a copy that nothing has started aborts the first time anything reads a timestamp. So the guard's factory starts it on the way in. The factory is the only way to make a guard, the call is safe to repeat, and the kernel registry already does the same thing at its own entry point for the same reason. Where the program around it already carries the runtime, the two resolve to one copy and the call changes nothing. ## Testing I reproduced the missing toolkit target two ways: with a small project whose subdirectory links the runtime library without looking for the toolkit, and in this tree by configuring an accelerator build against a processor only Torch. Both configure cleanly once the call is there. For the runtime, I built the library with unresolved symbols rejected, which is how it links where there is no shared runtime to defer to. It fails without this change and links with it. A log running before the platform layer is started aborts, and starting it at the entry point stops that. The private default constructor makes the factory the only entry, so nothing reaches a log first. Co-authored-by: PyTorch Bot <pytorchbot@users.noreply.github.com>
1 parent b253af8 commit fc21064

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎extension/cuda/CMakeLists.txt‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ endif()
1818

1919
if(EXECUTORCH_BUILD_ROCM)
2020
find_package(hip CONFIG REQUIRED)
21+
else()
22+
# Found here rather than relied on from the parent scope. The Torch package
23+
# found above defines this target only when Torch itself is a CUDA build, so
24+
# the target was there by luck: a processor only Torch, or a platform where
25+
# nothing else defines it, leaves the link below asking the linker for a file
26+
# named after the target.
27+
find_package(CUDAToolkit REQUIRED)
2128
endif()
2229

2330
# SHARED on purpose: the caller-stream thread-local must have a single
@@ -44,11 +51,15 @@ if(EXECUTORCH_BUILD_ROCM)
4451
else()
4552
target_link_libraries(extension_cuda PUBLIC CUDA::cudart)
4653
endif()
47-
# device_guard.cpp logs, so this needs the ExecuTorch runtime. Taken through the
48-
# helper rather than as an ordinary dependency, because CMake would order the
49-
# static core ahead of the shared runtime and leave this library with its own
50-
# copy of the registries.
51-
executorch_target_link_shared_runtime(extension_cuda)
54+
# device_guard.cpp logs, so this needs the ExecuTorch runtime. Where one shared
55+
# runtime exists, the helper retains it, so a process has a single registry.
56+
# Where it does not, the helper does nothing, and this library carries the parts
57+
# of the static core it needs instead.
58+
if(EXECUTORCH_BUILD_SHARED)
59+
executorch_target_link_shared_runtime(extension_cuda)
60+
else()
61+
target_link_libraries(extension_cuda PRIVATE executorch_core)
62+
endif()
5263
target_include_directories(extension_cuda PUBLIC ${_common_include_directories})
5364
target_compile_options(
5465
extension_cuda PUBLIC "$<$<COMPILE_LANGUAGE:CXX>:${_common_compile_options}>"

‎extension/cuda/device_guard.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <executorch/extension/cuda/device_guard.h>
1010

1111
#include <executorch/runtime/platform/log.h>
12+
#include <executorch/runtime/platform/platform.h>
1213

1314
#if defined(EXECUTORCH_USE_HIP)
1415
#include <executorch/extension/cuda/runtime_api.h>
@@ -22,6 +23,11 @@ using ::executorch::runtime::Error;
2223
using ::executorch::runtime::Result;
2324

2425
Result<CUDAGuard> CUDAGuard::create(int device_index) {
26+
// Where a build makes no shared runtime, this library carries its own copy of
27+
// the platform layer, and the log below would be the first thing to touch it.
28+
// Safe to call more than once, which is why the kernel registry does the same
29+
// at its own entry point.
30+
::et_pal_init();
2531
CUDAGuard guard;
2632
ET_CHECK_OK_OR_RETURN_ERROR(guard.set_index(device_index));
2733
return guard;

0 commit comments

Comments
 (0)