-
Notifications
You must be signed in to change notification settings - Fork 407
[TransferEngine]feat: add tensor transfer Read/Write API for transfer-engine #703
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <pybind11/stl.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| auto torch = py::module_::import("torch"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #ifdef USE_MNNVL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "transport/nvlink_transport/nvlink_transport.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static void *allocateMemory(size_t size) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -53,6 +55,72 @@ TransferEnginePy::~TransferEnginePy() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| large_buffer_list_.clear(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| py::array TransferEnginePy::create_typed_array(char *exported_data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| py::capsule free_when_done( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exported_data, [](void *p) { delete[] static_cast<char *>(p); }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return py::array_t<T>({static_cast<ssize_t>(total_length / sizeof(T))}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (T *)(exported_data + offset), free_when_done); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using ArrayCreatorFunc = std::function<py::array(char *, size_t, size_t)>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static const std::array<ArrayCreatorFunc, 11> array_creators = {{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<float>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, // FLOAT32 = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<double>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, // FLOAT64 = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<int8_t>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, // INT8 = 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<uint8_t>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, // UINT8 = 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<int16_t>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, // INT16 = 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<uint16_t>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, // UINT16 = 5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<int32_t>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, // INT32 = 6 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<uint32_t>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, // UINT32 = 7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<int64_t>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, // INT64 = 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<uint64_t>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, // UINT64 = 9 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [](char* data, size_t offset, size_t total_length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<bool>(data, offset, total_length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return TransferEnginePy{}.create_typed_array<bool>(data, offset, total_length); | |
| return TransferEnginePy::create_typed_array<float>(data, offset, total_length); | |
| }, // FLOAT32 = 0 | |
| [](char* data, size_t offset, size_t total_length) { | |
| return TransferEnginePy::create_typed_array<double>(data, offset, total_length); | |
| }, // FLOAT64 = 1 | |
| [](char* data, size_t offset, size_t total_length) { | |
| return TransferEnginePy::create_typed_array<int8_t>(data, offset, total_length); | |
| }, // INT8 = 2 | |
| [](char* data, size_t offset, size_t total_length) { | |
| return TransferEnginePy::create_typed_array<uint8_t>(data, offset, total_length); | |
| }, // UINT8 = 3 | |
| [](char* data, size_t offset, size_t total_length) { | |
| return TransferEnginePy::create_typed_array<int16_t>(data, offset, total_length); | |
| }, // INT16 = 4 | |
| [](char* data, size_t offset, size_t total_length) { | |
| return TransferEnginePy::create_typed_array<uint16_t>(data, offset, total_length); | |
| }, // UINT16 = 5 | |
| [](char* data, size_t offset, size_t total_length) { | |
| return TransferEnginePy::create_typed_array<int32_t>(data, offset, total_length); | |
| }, // INT32 = 6 | |
| [](char* data, size_t offset, size_t total_length) { | |
| return TransferEnginePy::create_typed_array<uint32_t>(data, offset, total_length); | |
| }, // UINT32 = 7 | |
| [](char* data, size_t offset, size_t total_length) { | |
| return TransferEnginePy::create_typed_array<int64_t>(data, offset, total_length); | |
| }, // INT64 = 8 | |
| [](char* data, size_t offset, size_t total_length) { | |
| return TransferEnginePy::create_typed_array<uint64_t>(data, offset, total_length); | |
| }, // UINT64 = 9 | |
| [](char* data, size_t offset, size_t total_length) { | |
| return TransferEnginePy::create_typed_array<bool>(data, offset, total_length); |
Copilot
AI
Aug 8, 2025
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.
[nitpick] Using -1 as a sentinel value for unused shape dimensions is unclear. Consider using 0 or defining a named constant to make the intent more explicit.
| metadata.shape[i] = -1; | |
| metadata.shape[i] = UNUSED_DIMENSION; |
Copilot
AI
Aug 8, 2025
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.
The condition metadata.shape[i] > 0 excludes dimensions with size 0, but zero-sized dimensions are valid in PyTorch tensors. This could cause incorrect tensor reconstruction for tensors with empty dimensions.
| } | |
| shape_vec.push_back(metadata.shape[i]); |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |||||||
| #include <gflags/gflags.h> | ||||||||
| #include <glog/logging.h> | ||||||||
| #include <pybind11/pybind11.h> | ||||||||
| #include <pybind11/numpy.h> | ||||||||
| #include <sys/time.h> | ||||||||
|
|
||||||||
| #include <cstdlib> | ||||||||
|
|
@@ -31,6 +32,7 @@ | |||||||
| #include "transport/transport.h" | ||||||||
|
|
||||||||
| using namespace mooncake; | ||||||||
| namespace py = pybind11; | ||||||||
|
|
||||||||
| const static size_t kDefaultBufferCapacity = 2ull * 1024 * 1024 * 1024; | ||||||||
| const static size_t kSlabSizeKBTabLen = 16; | ||||||||
|
|
@@ -40,6 +42,17 @@ const static size_t kSlabSizeKB[] = { | |||||||
| 512, 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024, | ||||||||
| 32 * 1024, 64 * 1024, 128 * 1024, 256 * 1024}; | ||||||||
|
|
||||||||
| enum class TensorDtype : int32_t { | ||||||||
| FLOAT32 = 0, FLOAT64 = 1, INT8 = 2, UINT8 = 3, INT16 = 4, UINT16 = 5, | ||||||||
| INT32 = 6, UINT32 = 7, INT64 = 8, UINT64 = 9, BOOL = 10, UNKNOWN = -1 | ||||||||
| }; | ||||||||
|
|
||||||||
| struct TensorMetadata { | ||||||||
| int32_t dtype; | ||||||||
| int32_t ndim; | ||||||||
| int32_t shape[4]; | ||||||||
| } __attribute__((packed)); | ||||||||
|
||||||||
| } __attribute__((packed)); | |
| std::vector<int32_t> shape; | |
| }; |
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.
The global torch module import at file scope could cause issues if torch is not available when the module is loaded. Consider importing torch lazily within functions that need it, with proper error handling for cases where torch is not installed.