|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +#include <dlpack/dlpack.h> |
1 | 5 | #include <nanobind/nanobind.h> |
2 | 6 | #include <nanobind/stl/shared_ptr.h> |
| 7 | +#include <nanobind/stl/string.h> |
| 8 | +#include <nanobind/stl/vector.h> |
3 | 9 |
|
4 | 10 | #include <mscclpp/gpu_data_types.hpp> |
5 | 11 | #include <mscclpp/gpu_utils.hpp> |
6 | 12 |
|
7 | 13 | namespace nb = nanobind; |
8 | 14 | using namespace mscclpp; |
9 | 15 |
|
| 16 | +constexpr int BYTE_BITS = 8; |
| 17 | + |
| 18 | +static DLDeviceType getDeviceType() { |
| 19 | +#if defined(__HIP_PLATFORM_AMD__) |
| 20 | + return kDLROCM; |
| 21 | +#else |
| 22 | + return kDLCUDA; |
| 23 | +#endif |
| 24 | +} |
| 25 | + |
| 26 | +static DLDataType getDlType(std::string type) { |
| 27 | + if (type == "torch.float") { |
| 28 | + return DLDataType{kDLFloat, 32, 1}; |
| 29 | + } else if (type == "torch.int") { |
| 30 | + return DLDataType{kDLInt, 32, 1}; |
| 31 | + } else if (type == "torch.uint32") { |
| 32 | + return DLDataType{kDLUInt, 32, 1}; |
| 33 | + } else if (type == "torch.bfloat16") { |
| 34 | + return DLDataType{kDLBfloat, 16, 1}; |
| 35 | + } else if (type == "torch.float16") { |
| 36 | + return DLDataType{kDLFloat, 16, 1}; |
| 37 | + } else { |
| 38 | + throw Error("Unsupported type: " + type, ErrorCode::InvalidUsage); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +static nb::capsule toDlpack(GpuBuffer<char> buffer, std::string dataType, std::vector<int64_t>& shape, |
| 43 | + std::vector<int64_t>& strides) { |
| 44 | + DLDataType dtype = getDlType(dataType); |
| 45 | + int64_t* tensorShape = shape.size() > 0 ? new int64_t[shape.size()] : new int64_t[1]; |
| 46 | + int64_t* tensorStrides = strides.size() > 0 ? new int64_t[strides.size()] : nullptr; |
| 47 | + if (shape.size() == 0) { |
| 48 | + tensorShape[0] = (int64_t)(buffer.nelems() / ((dtype.bits * dtype.lanes + 7) / BYTE_BITS)); |
| 49 | + } else { |
| 50 | + for (size_t i = 0; i < shape.size(); ++i) { |
| 51 | + tensorShape[i] = shape[i]; |
| 52 | + } |
| 53 | + } |
| 54 | + for (size_t i = 0; i < strides.size(); ++i) { |
| 55 | + tensorStrides[i] = strides[i]; |
| 56 | + } |
| 57 | + |
| 58 | + DLManagedTensor* dlManagedTensor = new DLManagedTensor(); |
| 59 | + dlManagedTensor->dl_tensor.data = buffer.data(); |
| 60 | + dlManagedTensor->dl_tensor.device.device_type = getDeviceType(); |
| 61 | + dlManagedTensor->dl_tensor.device.device_id = buffer.deviceId(); |
| 62 | + dlManagedTensor->dl_tensor.ndim = shape.size() == 0 ? 1 : shape.size(); |
| 63 | + dlManagedTensor->dl_tensor.strides = tensorStrides; |
| 64 | + dlManagedTensor->dl_tensor.shape = tensorShape; |
| 65 | + dlManagedTensor->dl_tensor.byte_offset = 0; |
| 66 | + dlManagedTensor->dl_tensor.dtype = dtype; |
| 67 | + dlManagedTensor->manager_ctx = new GpuBuffer<char>(buffer); |
| 68 | + dlManagedTensor->deleter = [](DLManagedTensor* self) { |
| 69 | + delete static_cast<GpuBuffer<char>*>(self->manager_ctx); |
| 70 | + self->manager_ctx = nullptr; |
| 71 | + self->dl_tensor.data = nullptr; |
| 72 | + if (self->dl_tensor.shape != nullptr) { |
| 73 | + delete[] self->dl_tensor.shape; |
| 74 | + self->dl_tensor.shape = nullptr; |
| 75 | + if (self->dl_tensor.strides) { |
| 76 | + delete[] self->dl_tensor.strides; |
| 77 | + self->dl_tensor.strides = nullptr; |
| 78 | + } |
| 79 | + } |
| 80 | + delete self; |
| 81 | + }; |
| 82 | + |
| 83 | + PyObject* dlCapsule = PyCapsule_New(static_cast<void*>(dlManagedTensor), "dltensor", [](PyObject* capsule) { |
| 84 | + if (PyCapsule_IsValid(capsule, "used_dltensor")) { |
| 85 | + return; |
| 86 | + } |
| 87 | + if (!PyCapsule_IsValid(capsule, "dltensor")) { |
| 88 | + return; |
| 89 | + } |
| 90 | + DLManagedTensor* managedTensor = static_cast<DLManagedTensor*>(PyCapsule_GetPointer(capsule, "dltensor")); |
| 91 | + if (managedTensor == nullptr) { |
| 92 | + return; |
| 93 | + } |
| 94 | + if (managedTensor->deleter) { |
| 95 | + managedTensor->deleter(managedTensor); |
| 96 | + } |
| 97 | + }); |
| 98 | + return nb::steal<nb::capsule>(dlCapsule); |
| 99 | +} |
| 100 | + |
10 | 101 | void register_gpu_utils(nb::module_& m) { |
11 | 102 | m.def("is_nvls_supported", &isNvlsSupported); |
12 | 103 |
|
13 | 104 | nb::class_<GpuBuffer<char>>(m, "RawGpuBuffer") |
14 | 105 | .def(nb::init<size_t>(), nb::arg("nelems")) |
15 | 106 | .def("nelems", &GpuBuffer<char>::nelems) |
16 | 107 | .def("bytes", &GpuBuffer<char>::bytes) |
17 | | - .def("data", [](GpuBuffer<char>& self) { return reinterpret_cast<uintptr_t>(self.data()); }); |
| 108 | + .def("data", [](GpuBuffer<char>& self) { return reinterpret_cast<uintptr_t>(self.data()); }) |
| 109 | + .def("device_id", &GpuBuffer<char>::deviceId) |
| 110 | + .def( |
| 111 | + "to_dlpack", |
| 112 | + [](GpuBuffer<char>& self, std::string dataType, std::vector<int64_t> shape, std::vector<int64_t> strides) { |
| 113 | + return toDlpack(self, dataType, shape, strides); |
| 114 | + }, |
| 115 | + nb::arg("dataType"), nb::arg("shape") = std::vector<int64_t>(), nb::arg("strides") = std::vector<int64_t>()); |
18 | 116 | } |
0 commit comments