-
Notifications
You must be signed in to change notification settings - Fork 90
Export mscclpp GpuBuffer to dlpack format #492
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c79b101
update
Binyang2014 5f06612
update
Binyang2014 965b89d
fix
Binyang2014 cedd9b4
update
Binyang2014 1db77ee
WIP
Binyang2014 6a7b8d8
Merge branch 'main' into binyli/export_dlpack
Binyang2014 f52ed95
update lint
Binyang2014 b7fa947
fix
Binyang2014 9d17885
fix
Binyang2014 e4a7134
update
Binyang2014 4d5ce34
fix comments
Binyang2014 fe1f13c
update
Binyang2014 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,116 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| #include <dlpack/dlpack.h> | ||
| #include <nanobind/nanobind.h> | ||
| #include <nanobind/stl/shared_ptr.h> | ||
| #include <nanobind/stl/string.h> | ||
| #include <nanobind/stl/vector.h> | ||
|
|
||
| #include <mscclpp/gpu_data_types.hpp> | ||
| #include <mscclpp/gpu_utils.hpp> | ||
|
|
||
| namespace nb = nanobind; | ||
| using namespace mscclpp; | ||
|
|
||
| constexpr int BYTE_BITS = 8; | ||
|
|
||
| static DLDeviceType getDeviceType() { | ||
| #if defined(__HIP_PLATFORM_AMD__) | ||
| return kDLROCM; | ||
| #else | ||
| return kDLCUDA; | ||
| #endif | ||
| } | ||
|
|
||
| static DLDataType getDlType(std::string type) { | ||
| if (type == "torch.float") { | ||
| return DLDataType{kDLFloat, 32, 1}; | ||
| } else if (type == "torch.int") { | ||
| return DLDataType{kDLInt, 32, 1}; | ||
| } else if (type == "torch.uint32") { | ||
| return DLDataType{kDLUInt, 32, 1}; | ||
| } else if (type == "torch.bfloat16") { | ||
| return DLDataType{kDLBfloat, 16, 1}; | ||
| } else if (type == "torch.float16") { | ||
| return DLDataType{kDLFloat, 16, 1}; | ||
| } else { | ||
| throw Error("Unsupported type: " + type, ErrorCode::InvalidUsage); | ||
| } | ||
| } | ||
|
|
||
| static nb::capsule toDlpack(GpuBuffer<char> buffer, std::string dataType, std::vector<int64_t>& shape, | ||
| std::vector<int64_t>& strides) { | ||
| DLDataType dtype = getDlType(dataType); | ||
| int64_t* tensorShape = shape.size() > 0 ? new int64_t[shape.size()] : new int64_t[1]; | ||
| int64_t* tensorStrides = strides.size() > 0 ? new int64_t[strides.size()] : nullptr; | ||
| if (shape.size() == 0) { | ||
| tensorShape[0] = (int64_t)(buffer.nelems() / ((dtype.bits * dtype.lanes + 7) / BYTE_BITS)); | ||
| } else { | ||
| for (size_t i = 0; i < shape.size(); ++i) { | ||
| tensorShape[i] = shape[i]; | ||
| } | ||
| } | ||
| for (size_t i = 0; i < strides.size(); ++i) { | ||
| tensorStrides[i] = strides[i]; | ||
| } | ||
|
|
||
| DLManagedTensor* dlManagedTensor = new DLManagedTensor(); | ||
| dlManagedTensor->dl_tensor.data = buffer.data(); | ||
| dlManagedTensor->dl_tensor.device.device_type = getDeviceType(); | ||
| dlManagedTensor->dl_tensor.device.device_id = buffer.deviceId(); | ||
| dlManagedTensor->dl_tensor.ndim = shape.size() == 0 ? 1 : shape.size(); | ||
| dlManagedTensor->dl_tensor.strides = tensorStrides; | ||
| dlManagedTensor->dl_tensor.shape = tensorShape; | ||
| dlManagedTensor->dl_tensor.byte_offset = 0; | ||
| dlManagedTensor->dl_tensor.dtype = dtype; | ||
| dlManagedTensor->manager_ctx = new GpuBuffer<char>(buffer); | ||
| dlManagedTensor->deleter = [](DLManagedTensor* self) { | ||
| delete static_cast<GpuBuffer<char>*>(self->manager_ctx); | ||
| self->manager_ctx = nullptr; | ||
| self->dl_tensor.data = nullptr; | ||
| if (self->dl_tensor.shape != nullptr) { | ||
| delete[] self->dl_tensor.shape; | ||
| self->dl_tensor.shape = nullptr; | ||
| if (self->dl_tensor.strides) { | ||
| delete[] self->dl_tensor.strides; | ||
| self->dl_tensor.strides = nullptr; | ||
| } | ||
| } | ||
| delete self; | ||
| }; | ||
|
|
||
| PyObject* dlCapsule = PyCapsule_New(static_cast<void*>(dlManagedTensor), "dltensor", [](PyObject* capsule) { | ||
| if (PyCapsule_IsValid(capsule, "used_dltensor")) { | ||
| return; | ||
| } | ||
| if (!PyCapsule_IsValid(capsule, "dltensor")) { | ||
| return; | ||
| } | ||
| DLManagedTensor* managedTensor = static_cast<DLManagedTensor*>(PyCapsule_GetPointer(capsule, "dltensor")); | ||
| if (managedTensor == nullptr) { | ||
| return; | ||
| } | ||
| if (managedTensor->deleter) { | ||
| managedTensor->deleter(managedTensor); | ||
| } | ||
| }); | ||
| return nb::steal<nb::capsule>(dlCapsule); | ||
| } | ||
|
|
||
| void register_gpu_utils(nb::module_& m) { | ||
| m.def("is_nvls_supported", &isNvlsSupported); | ||
|
|
||
| nb::class_<GpuBuffer<char>>(m, "RawGpuBuffer") | ||
| .def(nb::init<size_t>(), nb::arg("nelems")) | ||
| .def("nelems", &GpuBuffer<char>::nelems) | ||
| .def("bytes", &GpuBuffer<char>::bytes) | ||
| .def("data", [](GpuBuffer<char>& self) { return reinterpret_cast<uintptr_t>(self.data()); }); | ||
| .def("data", [](GpuBuffer<char>& self) { return reinterpret_cast<uintptr_t>(self.data()); }) | ||
| .def("device_id", &GpuBuffer<char>::deviceId) | ||
| .def( | ||
| "to_dlpack", | ||
| [](GpuBuffer<char>& self, std::string dataType, std::vector<int64_t> shape, std::vector<int64_t> strides) { | ||
| return toDlpack(self, dataType, shape, strides); | ||
| }, | ||
| nb::arg("dataType"), nb::arg("shape") = std::vector<int64_t>(), nb::arg("strides") = std::vector<int64_t>()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.