Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/device/ep/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ nixl_ep_sources = [

nixl_ep_inc_dirs = [
nixl_inc_dirs,
nixl_device_inc_dirs,
nixl_device_api_inc_dirs,
include_directories('csrc'),
include_directories('csrc/kernels'),
torch_inc_dirs,
Expand Down
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ nixl_inc_dirs = include_directories('src/api/cpp', 'src/api/cpp/backend', 'src/i
nixl_device_inc_dirs = include_directories('src/api/device')
plugins_inc_dirs = include_directories('src/plugins')
utils_inc_dirs = include_directories('src/utils')
# The generic GPU API includes the proxy wire protocol as
# <device/proxy/proxy_protocol.h>. Keep the narrower device include set for
# consumers that only need headers such as <gpu/device_types.cuh>.
nixl_device_api_inc_dirs = [nixl_device_inc_dirs, utils_inc_dirs]

subdir('src')
if get_option('build_tests') and get_option('buildtype') != 'release'
Expand Down
39 changes: 37 additions & 2 deletions src/api/device/gpu/device_types.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

/**
Comment thread
tomerdav marked this conversation as resolved.
Resolved
* 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;
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in eccd56e7 (#2464).

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. */
Expand Down
130 changes: 123 additions & 7 deletions src/api/device/gpu/impl/device_dispatch.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &) {
Expand All @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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();
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 96a807f9 (#2463): a fixed mode dispatches with if constexpr, so the other backend isn't instantiated, and only DYNAMIC branches on the tag; the same for atomicAdd, getXferStatus and getPtr. In the DYNAMIC branch I kept the NIXL_ERR_INVALID_PARAM result (nullptr from getPtr) for an untagged view instead of the assert, since a zero tag means an invalid view, not a backend.

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
Expand Down
83 changes: 83 additions & 0 deletions src/api/device/gpu/impl/level_utils.cuh
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
Loading
Loading