Repository navigation
device_api: run memory views through the CPU proxy from GPU kernels [CPU Proxy part 5] #2326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
b50b773
183291b
31f7f40
8be25bf
78a98cb
cf9c92c
1bdbfb8
03f006a
8251aa5
dc0959c
dcf2f55
616a1e2
d52c5c3
4e2a465
8de562b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,16 +39,48 @@ struct xferStatusH { | |
| alignas(16) unsigned char storage[64] = {}; /**< Backend-owned request payload */ | ||
| }; | ||
|
|
||
| constexpr size_t xfer_status_payload_size = 64; | ||
| /** | ||
| * How much of xferStatusH::storage a backend implementation may use. The | ||
| * remaining bytes hold the execution-mode tag written by the dispatch layer, | ||
| * which is what lets a later poll on this handle find its way back to the | ||
| * implementation that produced it. | ||
| */ | ||
| constexpr size_t xfer_status_payload_size = 60; | ||
| constexpr size_t xfer_status_mode_size = 4; | ||
|
|
||
| static_assert(xfer_status_payload_size == sizeof(xferStatusH)); | ||
| static_assert(xfer_status_payload_size + xfer_status_mode_size == sizeof(xferStatusH)); | ||
|
|
||
| /** | ||
| * @enum level_t | ||
| * @brief An enumeration of different levels for GPU transfer requests. | ||
| */ | ||
| enum class level_t : uint64_t { THREAD = 0, WARP = 1, BLOCK = 2, GRID = 3 }; | ||
|
|
||
| /** | ||
| * Which implementation executes a prepared memory view, chosen per view by | ||
| * the backend that prepared it, not per build. Zero is deliberately not a | ||
| * valid mode, so an untagged handle reads as invalid rather than as the first | ||
| * backend. | ||
| * | ||
| * DYNAMIC, the device API's default mode argument, reads the mode from the | ||
| * view. A fixed mode must match the views (otherwise undefined; asserted in | ||
| * debug builds), and its statuses must be polled with the same mode. | ||
| */ | ||
| enum class exec_mode_t : uint8_t { | ||
| UCX_DIRECT = 1, | ||
| PROXY = 2, | ||
| DYNAMIC = 0xff, | ||
| }; | ||
|
|
||
| /** | ||
| * What a prepared memory-view handle actually points at: the mode, plus the | ||
| * backend's own handle. Device code unwraps it on the dispatch path. | ||
| */ | ||
| struct memViewWrapper { | ||
| exec_mode_t execution_mode; | ||
| nixlMemViewH backend_memview; | ||
| }; | ||
|
|
||
| namespace flags { | ||
| /** @brief Defer the operation instead of flushing immediately. */ | ||
| constexpr uint64_t defer = 1; | ||
|
|
@@ -80,7 +112,10 @@ using nixl_gpu_level_t = nixl::gpu::level_t; | |
| * @see nixl::gpu::memViewElem | ||
| */ | ||
| using nixlMemViewElem = nixl::gpu::memViewElem; | ||
| using nixl_device_exec_mode_t = nixl::gpu::exec_mode_t; | ||
| using nixlDeviceMemViewWrapper = nixl::gpu::memViewWrapper; | ||
|
Comment on lines
+109
to
+110
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove it. We added aliases for the old C-style names. We do not need aliases for new names and types.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in |
||
| constexpr size_t nixl_gpu_xfer_status_payload_size = nixl::gpu::xfer_status_payload_size; | ||
| constexpr size_t nixl_gpu_xfer_status_mode_size = nixl::gpu::xfer_status_mode_size; | ||
|
|
||
| namespace nixl_gpu_flags { | ||
| /** @brief Defer the operation instead of flushing immediately. */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,13 +17,22 @@ | |
| #ifndef NIXL_SRC_API_DEVICE_GPU_IMPL_DEVICE_DISPATCH_CUH | ||
| #define NIXL_SRC_API_DEVICE_GPU_IMPL_DEVICE_DISPATCH_CUH | ||
|
|
||
| #include <cassert> | ||
|
|
||
| #include <gpu/device_types.cuh> | ||
| #include <gpu/impl/level_utils.cuh> | ||
|
|
||
| #if __has_include(<ucp/api/device/ucp_device_impl.h>) | ||
| #include <gpu/impl/ucx/device_ucx.cuh> | ||
|
|
||
| namespace nixl::gpu::impl::ucx { | ||
| inline constexpr bool available = true; | ||
| } // namespace nixl::gpu::impl::ucx | ||
| #else | ||
| namespace nixl::gpu::impl::ucx { | ||
|
|
||
| inline constexpr bool available = false; | ||
|
|
||
| template<level_t level> | ||
| __device__ nixl_status_t | ||
| getXferStatus(xferStatusH &) { | ||
|
|
@@ -50,38 +59,145 @@ getPtr(nixlMemViewH, size_t) { | |
| } // namespace nixl::gpu::impl::ucx | ||
| #endif | ||
|
|
||
| // The proxy implementation needs only CUDA, so unlike the UCX arm it is always | ||
| // present and needs no capability gate. | ||
| #include "proxy/device_proxy.cuh" | ||
|
|
||
| namespace nixl::gpu::impl { | ||
|
|
||
| template<level_t level> | ||
| namespace detail { | ||
|
|
||
| __device__ __forceinline__ const memViewWrapper * | ||
| asDeviceMemView(nixlMemViewH handle) { | ||
| return static_cast<const memViewWrapper *>(handle); | ||
| } | ||
|
|
||
| __device__ __forceinline__ exec_mode_t | ||
| loadExecutionMode(const xferStatusH &status) { | ||
| uint32_t execution_mode = 0; | ||
| memcpy(&execution_mode, status.storage + xfer_status_payload_size, sizeof(execution_mode)); | ||
| return static_cast<exec_mode_t>(execution_mode); | ||
| } | ||
|
|
||
| template<exec_mode_t mode> | ||
| inline constexpr bool buildable = mode != exec_mode_t::UCX_DIRECT || ucx::available; | ||
|
|
||
| template<exec_mode_t mode> | ||
| __device__ __forceinline__ exec_mode_t | ||
| executionMode(const memViewWrapper &view) { | ||
| static_assert(buildable<mode>, "UCX_DIRECT needs UCX's GPU device API"); | ||
| if constexpr (mode == exec_mode_t::DYNAMIC) { | ||
| return view.execution_mode; | ||
| } else { | ||
| assert(view.execution_mode == mode); | ||
| return mode; | ||
| } | ||
| } | ||
|
|
||
| template<exec_mode_t mode> | ||
| __device__ __forceinline__ exec_mode_t | ||
| executionMode(const xferStatusH &status) { | ||
| static_assert(buildable<mode>, "UCX_DIRECT needs UCX's GPU device API"); | ||
| if constexpr (mode == exec_mode_t::DYNAMIC) { | ||
| return loadExecutionMode(status); | ||
| } else { | ||
| return mode; | ||
| } | ||
| } | ||
|
|
||
| /** Tag accepted (NIXL_IN_PROG) submissions for a DYNAMIC getXferStatus. */ | ||
| template<level_t level, exec_mode_t mode> | ||
| __device__ __forceinline__ void | ||
| writeExecutionMode(xferStatusH *status, | ||
| nixl_status_t submission_status, | ||
| exec_mode_t execution_mode) { | ||
| if constexpr (mode == exec_mode_t::DYNAMIC) { | ||
| if (status != nullptr && submission_status >= 0 && isLeader<level>()) { | ||
| const uint32_t tag = static_cast<uint32_t>(execution_mode); | ||
| memcpy(status->storage + xfer_status_payload_size, &tag, sizeof(tag)); | ||
| } | ||
| // Collective pollers share the leader's tag. | ||
| sync<level>(); | ||
| } | ||
| } | ||
|
|
||
| } // namespace detail | ||
|
|
||
| template<level_t level, exec_mode_t mode> | ||
| __device__ nixl_status_t | ||
| getXferStatus(xferStatusH &xfer_status) { | ||
| return ucx::getXferStatus<level>(xfer_status); | ||
| switch (detail::executionMode<mode>(xfer_status)) { | ||
| case exec_mode_t::UCX_DIRECT: | ||
| return ucx::getXferStatus<level>(xfer_status); | ||
| case exec_mode_t::PROXY: | ||
| return proxy::getXferStatus<level>(xfer_status); | ||
| default: | ||
| return NIXL_ERR_INVALID_PARAM; | ||
| } | ||
| } | ||
|
|
||
| template<level_t level> | ||
| template<level_t level, exec_mode_t mode> | ||
| __device__ nixl_status_t | ||
| put(const memViewElem &src, | ||
| const memViewElem &dst, | ||
| size_t size, | ||
| unsigned channel_id, | ||
| uint64_t flags, | ||
| xferStatusH *xfer_status) { | ||
| return ucx::put<level>(src, dst, size, channel_id, flags, xfer_status); | ||
| const auto *src_view = detail::asDeviceMemView(src.mvh); | ||
| const auto *dst_view = detail::asDeviceMemView(dst.mvh); | ||
| const memViewElem backend_src{src_view->backend_memview, src.index, src.offset}; | ||
| const memViewElem backend_dst{dst_view->backend_memview, dst.index, dst.offset}; | ||
|
|
||
| // The destination decides: it is the view that names the peer. | ||
| const exec_mode_t execution_mode = detail::executionMode<mode>(*dst_view); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a suboptimal use of a template variable. it should be smth like: if constexpr (mode == exec_mode_t::UCX_DIRECT) {
return ucx::put();
} else if constexpr (mode == exec_mode_t::PROXY) {
return proxy::put();
} else {
if (runtime_mode == exec_mode_t::UCX_DIRECT) {
return ucx::put();
}
assert(runtime_mode == exec_mode_t::PROXY);
return proxy::put();
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in |
||
| nixl_status_t status; | ||
| if (execution_mode == exec_mode_t::UCX_DIRECT) { | ||
| status = ucx::put<level>(backend_src, backend_dst, size, channel_id, flags, xfer_status); | ||
| } else if (execution_mode == exec_mode_t::PROXY) { | ||
| status = proxy::put<level>(backend_src, backend_dst, size, channel_id, flags, xfer_status); | ||
| } else { | ||
| status = NIXL_ERR_INVALID_PARAM; | ||
| } | ||
| detail::writeExecutionMode<level, mode>(xfer_status, status, execution_mode); | ||
| return status; | ||
| } | ||
|
|
||
| template<level_t level> | ||
| template<level_t level, exec_mode_t mode> | ||
| __device__ nixl_status_t | ||
| atomicAdd(uint64_t value, | ||
| const memViewElem &counter, | ||
| unsigned channel_id, | ||
| uint64_t flags, | ||
| xferStatusH *xfer_status) { | ||
| return ucx::atomicAdd<level>(value, counter, channel_id, flags, xfer_status); | ||
| const auto *view = detail::asDeviceMemView(counter.mvh); | ||
| const memViewElem backend_counter{view->backend_memview, counter.index, counter.offset}; | ||
|
|
||
| const exec_mode_t execution_mode = detail::executionMode<mode>(*view); | ||
| nixl_status_t status; | ||
| if (execution_mode == exec_mode_t::UCX_DIRECT) { | ||
| status = ucx::atomicAdd<level>(value, backend_counter, channel_id, flags, xfer_status); | ||
| } else if (execution_mode == exec_mode_t::PROXY) { | ||
| status = proxy::atomicAdd<level>(value, backend_counter, channel_id, flags, xfer_status); | ||
| } else { | ||
| status = NIXL_ERR_INVALID_PARAM; | ||
| } | ||
| detail::writeExecutionMode<level, mode>(xfer_status, status, execution_mode); | ||
| return status; | ||
| } | ||
|
|
||
| template<exec_mode_t mode> | ||
| __device__ inline void * | ||
| getPtr(nixlMemViewH mvh, size_t index) { | ||
| return ucx::getPtr(mvh, index); | ||
| const auto *view = detail::asDeviceMemView(mvh); | ||
| switch (detail::executionMode<mode>(*view)) { | ||
| case exec_mode_t::UCX_DIRECT: | ||
| return ucx::getPtr(view->backend_memview, index); | ||
| case exec_mode_t::PROXY: | ||
| return proxy::getPtr(view->backend_memview, index); | ||
| default: | ||
| return nullptr; | ||
| } | ||
| } | ||
|
|
||
| } // namespace nixl::gpu::impl | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #ifndef NIXL_SRC_API_DEVICE_GPU_IMPL_LEVEL_UTILS_CUH | ||
| #define NIXL_SRC_API_DEVICE_GPU_IMPL_LEVEL_UTILS_CUH | ||
|
|
||
| #include <cooperative_groups.h> | ||
|
|
||
| #include <gpu/device_types.cuh> | ||
|
|
||
| namespace nixl::gpu::impl { | ||
|
|
||
| /** This thread's index among the threads that make one call at `level`. */ | ||
| template<level_t level> | ||
| __device__ __forceinline__ uint32_t | ||
| laneId() { | ||
| uint32_t lane_id = 0; | ||
| if constexpr (level == level_t::WARP) { | ||
| lane_id = threadIdx.x % warpSize; | ||
| } else if constexpr (level == level_t::BLOCK) { | ||
| lane_id = threadIdx.x; | ||
| } else if constexpr (level == level_t::GRID) { | ||
| lane_id = threadIdx.x + blockIdx.x * blockDim.x; | ||
| } | ||
| return lane_id; | ||
| } | ||
|
|
||
| /** Whether this thread acts for all the threads that make one call at `level`. */ | ||
| template<level_t level> | ||
| __device__ __forceinline__ bool | ||
| isLeader() { | ||
| return laneId<level>() == 0; | ||
| } | ||
|
|
||
| /** Barrier across the threads that make one call at `level`; a grid needs a cooperative launch. */ | ||
| template<level_t level> | ||
| __device__ __forceinline__ void | ||
| sync() { | ||
| if constexpr (level == level_t::WARP) { | ||
| __syncwarp(); | ||
| } else if constexpr (level == level_t::BLOCK) { | ||
| __syncthreads(); | ||
| } else if constexpr (level == level_t::GRID) { | ||
| cooperative_groups::this_grid().sync(); | ||
| } | ||
| } | ||
|
|
||
| /** A barrier that also hands every thread the leader's status; only a barrier at grid level. */ | ||
| template<level_t level> | ||
| __device__ __forceinline__ nixl_status_t | ||
| broadcastStatus(nixl_status_t status) { | ||
| if constexpr (level == level_t::WARP) { | ||
| status = static_cast<nixl_status_t>(__shfl_sync(0xffffffff, static_cast<int>(status), 0)); | ||
| } else if constexpr (level == level_t::BLOCK) { | ||
| __shared__ nixl_status_t s_status; | ||
| if (threadIdx.x == 0) { | ||
| s_status = status; | ||
| } | ||
| __syncthreads(); | ||
| status = s_status; | ||
| __syncthreads(); | ||
| } else if constexpr (level == level_t::GRID) { | ||
| sync<level>(); | ||
| } | ||
| return status; | ||
| } | ||
|
|
||
| } // namespace nixl::gpu::impl | ||
|
|
||
| #endif // NIXL_SRC_API_DEVICE_GPU_IMPL_LEVEL_UTILS_CUH |
Uh oh!
There was an error while loading. Please reload this page.