Repository navigation
utils: add the proxy memory-view manager [CPU Proxy part 2] #2226
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 all commits
f47c0c0
de241d3
d47867a
2e610a2
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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * 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_UTILS_DEVICE_PROXY_PROXY_HOST_VIEW_H | ||
| #define NIXL_SRC_UTILS_DEVICE_PROXY_PROXY_HOST_VIEW_H | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "backend_aux.h" | ||
| #include "device/device_ops.h" | ||
| #include "nixl_types.h" | ||
|
|
||
| namespace nixl { | ||
|
|
||
| /** | ||
| * The manager's device memview: kernels read the token and direct pointers through | ||
| * it. The same type as nixlMemViewH; the name says which of its meanings is meant. | ||
| */ | ||
| using proxy_view_handle_t = nixlMemViewH; | ||
|
|
||
| /** | ||
| * The host side of one prepared memory view. The manager creates and owns host views; | ||
| * a ring command names one by its address (the 64-bit host token), and | ||
| * resolveSubmission() reads it through that token without a lookup. | ||
| */ | ||
| struct proxyHostView { | ||
| struct storedDesc { | ||
| /** metadataP is borrowed and must outlive submissions. */ | ||
| nixlMetaDesc desc; | ||
| bool usable = true; | ||
| }; | ||
|
|
||
| proxy_view_handle_t proxy_memview = nullptr; | ||
| deviceMem proxy_memview_mem; | ||
| bool remote = false; | ||
| std::vector<storedDesc> descs; | ||
| }; | ||
|
|
||
| } // namespace nixl | ||
|
|
||
| #endif // NIXL_SRC_UTILS_DEVICE_PROXY_PROXY_HOST_VIEW_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| #include "proxy_memview_manager.h" | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| #include "nixl_log.h" | ||
| #include "nixl_types.h" | ||
|
|
||
| namespace nixl { | ||
|
|
||
| proxyMemViewManager::proxyMemViewManager(deviceOps &allocator, | ||
| const nixlProxyDeviceContextData *device_context) | ||
| : allocator_(allocator), | ||
| device_context_(device_context) {} | ||
|
|
||
| template<typename DlistT> | ||
| nixl_status_t | ||
| proxyMemViewManager::createView(const DlistT &dlist, | ||
| const std::vector<void *> &direct_ptrs, | ||
| proxyHostView *&out) { | ||
| out = nullptr; | ||
|
|
||
| // CPU: the host view, and the device memview's contents, which name the host view by | ||
| // its address. | ||
| auto host_view = std::make_unique<proxyHostView>(); | ||
| host_view->remote = std::is_same_v<DlistT, nixl_remote_meta_dlist_t>; | ||
| fillDescs(dlist, host_view->descs); | ||
| const nixlProxyDeviceMemView memview_contents{ | ||
| static_cast<uint64_t>(reinterpret_cast<uintptr_t>(host_view.get())), | ||
| device_context_, | ||
| static_cast<uint32_t>(direct_ptrs.size())}; | ||
|
|
||
| // GPU: allocate the device memview and copy the contents and direct pointers into it. | ||
| deviceMem device_memview_mem; | ||
| if (allocator_.allocDeviceMem(nixlProxyDeviceMemViewBytes(direct_ptrs.size()), | ||
| device_memview_mem) != NIXL_SUCCESS) { | ||
| NIXL_ERROR << "proxyMemViewManager: failed to allocate device memview"; | ||
| return NIXL_ERR_BACKEND; | ||
|
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 code seems to negate the point of using statuses in the I'm still not sure if using statuses in deviceOps methods makes more sense than exceptions.
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. Agreed; the follow-up returns |
||
| } | ||
| auto *device_memview = static_cast<nixlProxyDeviceMemView *>(device_memview_mem.get()); | ||
| nixl_status_t copy_status = allocator_.copy(device_memview, | ||
| &memview_contents, | ||
| sizeof(memview_contents), | ||
| deviceOps::copyDirection::HostToDevice); | ||
| if (copy_status == NIXL_SUCCESS && !direct_ptrs.empty()) { | ||
| copy_status = allocator_.copy(nixlProxyDeviceMemViewDirectPtrs(device_memview), | ||
| direct_ptrs.data(), | ||
| direct_ptrs.size() * sizeof(void *), | ||
| deviceOps::copyDirection::HostToDevice); | ||
| } | ||
| if (copy_status != NIXL_SUCCESS) { | ||
| NIXL_ERROR << "proxyMemViewManager: failed to initialize device memview"; | ||
| return NIXL_ERR_BACKEND; | ||
| } | ||
|
|
||
| // Link: the host view owns its device memview, and the map owns the host view. | ||
| host_view->proxy_memview = device_memview; | ||
| host_view->proxy_memview_mem = std::move(device_memview_mem); | ||
| out = host_view.get(); | ||
| views_.emplace(device_memview, std::move(host_view)); | ||
| return NIXL_SUCCESS; | ||
| } | ||
|
|
||
| nixl_status_t | ||
| proxyMemViewManager::prepLocal(const nixl_meta_dlist_t &dlist, proxy_view_handle_t &out) { | ||
| proxyHostView *host_view = nullptr; | ||
| const nixl_status_t status = createView(dlist, {}, host_view); | ||
| if (status != NIXL_SUCCESS) { | ||
| return status; | ||
| } | ||
|
|
||
| out = host_view->proxy_memview; | ||
| NIXL_DEBUG << "proxyMemViewManager::prepLocal: host_view=" << host_view | ||
| << " descs=" << dlist.descCount(); | ||
| return NIXL_SUCCESS; | ||
| } | ||
|
|
||
| nixl_status_t | ||
| proxyMemViewManager::prepRemote(const nixl_remote_meta_dlist_t &dlist, | ||
| const std::vector<void *> &direct_ptrs, | ||
| proxy_view_handle_t &out) { | ||
| if (dlist.getType() != VRAM_SEG) { | ||
| NIXL_ERROR << "proxyMemViewManager::prepRemote: unsupported mem type " << dlist.getType(); | ||
| return NIXL_ERR_INVALID_PARAM; | ||
| } | ||
|
|
||
| proxyHostView *host_view = nullptr; | ||
| const nixl_status_t status = createView(dlist, direct_ptrs, host_view); | ||
| if (status != NIXL_SUCCESS) { | ||
| return status; | ||
| } | ||
|
|
||
| out = host_view->proxy_memview; | ||
| NIXL_DEBUG << "proxyMemViewManager::prepRemote: host_view=" << host_view | ||
| << " descs=" << dlist.descCount() << " direct_ptrs=" << direct_ptrs.size(); | ||
| return NIXL_SUCCESS; | ||
| } | ||
|
|
||
| nixl_status_t | ||
| proxyMemViewManager::release(proxy_view_handle_t proxy_memview) { | ||
|
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. Why do we need a return status in a release method? I think issue a warning is enogh here.
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. In #2327 the backend's |
||
| const auto it = views_.find(proxy_memview); | ||
| if (it == views_.end()) { | ||
| return NIXL_ERR_INVALID_PARAM; | ||
| } | ||
|
|
||
| views_.erase(it); | ||
| return NIXL_SUCCESS; | ||
| } | ||
|
|
||
| template<typename DlistT> | ||
| void | ||
| proxyMemViewManager::fillDescs(const DlistT &dlist, std::vector<proxyHostView::storedDesc> &out) { | ||
|
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. Maybe just return
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. Will do in a follow-up PR after #2327, which rewrites this file. |
||
| out.clear(); | ||
| out.reserve(dlist.descCount()); | ||
| for (const auto &desc : dlist) { | ||
| proxyHostView::storedDesc stored{desc}; | ||
| if constexpr (std::is_same_v<DlistT, nixl_remote_meta_dlist_t>) { | ||
| // A hole (the null agent's descriptor) has no metadata, and nothing without metadata | ||
| // can be posted, so the metadata alone decides. | ||
| stored.usable = desc.metadataP != nullptr; | ||
| } | ||
| out.push_back(std::move(stored)); | ||
| } | ||
| } | ||
|
|
||
| } // namespace nixl | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,74 @@ | ||||||
| /* | ||||||
| * 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_UTILS_DEVICE_PROXY_PROXY_MEMVIEW_MANAGER_H | ||||||
| #define NIXL_SRC_UTILS_DEVICE_PROXY_PROXY_MEMVIEW_MANAGER_H | ||||||
|
|
||||||
| #include <memory> | ||||||
| #include <unordered_map> | ||||||
| #include <vector> | ||||||
|
|
||||||
| #include "backend_aux.h" | ||||||
| #include "device/device_ops.h" | ||||||
| #include "proxy_protocol.h" | ||||||
| #include "proxy_host_view.h" | ||||||
|
|
||||||
| namespace nixl { | ||||||
|
|
||||||
| /** | ||||||
|
tomerdav marked this conversation as resolved.
Resolved
|
||||||
| * Creates, owns and releases the proxy's prepared memory views. A view is a host view | ||||||
| * (its descriptors) plus a device memview that the GPU reads and that carries the host | ||||||
| * view's address as its token. Host views never move, so a token stays valid until its | ||||||
| * view is released. Not thread-safe: the caller serializes every call. | ||||||
| */ | ||||||
| class proxyMemViewManager { | ||||||
| public: | ||||||
| proxyMemViewManager(deviceOps &allocator, const nixlProxyDeviceContextData *device_context); | ||||||
|
|
||||||
| proxyMemViewManager(const proxyMemViewManager &) = delete; | ||||||
| proxyMemViewManager & | ||||||
| operator=(const proxyMemViewManager &) = delete; | ||||||
|
|
||||||
| [[nodiscard]] nixl_status_t | ||||||
| prepLocal(const nixl_meta_dlist_t &dlist, proxy_view_handle_t &out); | ||||||
|
|
||||||
| [[nodiscard]] nixl_status_t | ||||||
| prepRemote(const nixl_remote_meta_dlist_t &dlist, | ||||||
| const std::vector<void *> &direct_ptrs, | ||||||
| proxy_view_handle_t &out); | ||||||
|
|
||||||
| /** Releases the view after all GPU/CPU operations using it have completed. */ | ||||||
| [[nodiscard]] nixl_status_t | ||||||
| release(proxy_view_handle_t proxy_memview); | ||||||
|
|
||||||
| private: | ||||||
| template<typename DlistT> | ||||||
| [[nodiscard]] nixl_status_t | ||||||
| createView(const DlistT &dlist, const std::vector<void *> &direct_ptrs, proxyHostView *&out); | ||||||
|
|
||||||
| template<typename DlistT> | ||||||
| static void | ||||||
| fillDescs(const DlistT &dlist, std::vector<proxyHostView::storedDesc> &out); | ||||||
|
|
||||||
| deviceOps &allocator_; | ||||||
|
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.
Suggested change
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. Will do in a follow-up PR after #2327, which rewrites this file.
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 |
||||||
| const nixlProxyDeviceContextData *device_context_; | ||||||
|
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.
Suggested change
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. Will do in a follow-up PR after #2327, which rewrites this file.
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 |
||||||
| /** Workers never access this ownership map. */ | ||||||
| std::unordered_map<proxy_view_handle_t, std::unique_ptr<proxyHostView>> views_; | ||||||
| }; | ||||||
|
|
||||||
| } // namespace nixl | ||||||
|
|
||||||
| #endif // NIXL_SRC_UTILS_DEVICE_PROXY_PROXY_MEMVIEW_MANAGER_H | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use camelCase here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do in a follow-up PR after #2327, which rewrites this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in
dfab94b0(#2465).