Skip to content

Add IO layer - #10

Merged
zoeyzyhu merged 3 commits into
mainfrom
cli/add_file_io
Jul 5, 2026
Merged

zoeyzyhu merged 3 commits into
mainfrom
cli/add_file_io

Conversation

@zoeyzyhu

@zoeyzyhu zoeyzyhu commented Jul 5, 2026

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a new distributed IO layer (IOContext) on top of ProcessGroupUCX, plus a dedicated shared library target (commux_io) to ship this functionality separately from the core backend.

Changes:

  • Introduces commux::IOContext with barrier, p2p byte send/recv, broadcast/gather helpers, and coordinated file ops (open/close/fsync/write_at).
  • Adds make_tcp_io_context() helper to bootstrap an IO context via c10d::TCPStore.
  • Updates CMake to build and install libcommux_io.so.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 14 comments.

File Description
src/io.cpp Implements the IOContext IO/collective helpers and TCPStore bootstrap function.
include/commux/io.hpp Declares the IOContext API and make_tcp_io_context() factory.
CMakeLists.txt Adds the commux_io shared library target and installs it into the wheel/lib layout.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/commux/io.hpp
Comment on lines +3 to +6
#include <c10/util/intrusive_ptr.h>

#include <cstdint>
#include <memory>
Comment thread src/io.cpp
Comment on lines +129 to +132
auto size_t = i64_tensor(nbytes);
std::vector<at::Tensor> size_vec{size_t};
pg_->send(size_vec, r, kBroadcastSizeTag)->wait();
send_bytes(bytes.data(), bytes.size(), r, kBroadcastDataTag);
Comment thread src/io.cpp
Comment on lines +135 to +138
auto size_t = torch::zeros({1}, torch::kInt64);
std::vector<at::Tensor> size_vec{size_t};
pg_->recv(size_vec, root, kBroadcastSizeTag)->wait();
nbytes = tensor_i64(size_t);
Comment thread src/io.cpp
Comment on lines +158 to +161
auto size_t = torch::zeros({1}, torch::kInt64);
std::vector<at::Tensor> size_vec{size_t};
pg_->recv(size_vec, r, kGatherSizeTag)->wait();
auto nbytes = tensor_i64(size_t);
Comment thread src/io.cpp
Comment on lines +173 to +176
auto size_t = i64_tensor(static_cast<std::int64_t>(bytes.size()));
std::vector<at::Tensor> size_vec{size_t};
pg_->send(size_vec, root, kGatherSizeTag)->wait();
send_bytes(bytes.data(), bytes.size(), root, kGatherDataTag);
Comment thread src/io.cpp
opts.reduceOp = c10d::ReduceOp::SUM;
pg_->allreduce(tensors, opts)->wait();
if (tensor_i64(status) != 0)
throw std::runtime_error("commux::IOContext sync_file failed");
Comment thread src/io.cpp
Comment on lines +50 to +54
void IOContext::send_bytes(const void* data, std::size_t nbytes, int dst,
int tag) {
auto t = byte_tensor_from_copy(data, nbytes);
std::vector<at::Tensor> tensors{t};
pg_->send(tensors, dst, tag)->wait();
Comment thread src/io.cpp
Comment on lines +57 to +61
void IOContext::recv_bytes(void* data, std::size_t nbytes, int src, int tag) {
auto t = byte_tensor(nbytes);
std::vector<at::Tensor> tensors{t};
pg_->recv(tensors, src, tag)->wait();
if (nbytes != 0) std::memcpy(data, t.data_ptr(), nbytes);
Comment thread src/io.cpp
Comment on lines +120 to +125
void IOContext::broadcast_bytes(std::vector<std::uint8_t>& bytes, int root) {
if (root < 0 || root >= size_)
throw std::invalid_argument(
"commux::IOContext broadcast root out of range");

std::int64_t nbytes = static_cast<std::int64_t>(bytes.size());
Comment thread src/io.cpp
Comment on lines +120 to +145
void IOContext::broadcast_bytes(std::vector<std::uint8_t>& bytes, int root) {
if (root < 0 || root >= size_)
throw std::invalid_argument(
"commux::IOContext broadcast root out of range");

std::int64_t nbytes = static_cast<std::int64_t>(bytes.size());
if (rank_ == root) {
for (int r = 0; r < size_; ++r) {
if (r == root) continue;
auto size_t = i64_tensor(nbytes);
std::vector<at::Tensor> size_vec{size_t};
pg_->send(size_vec, r, kBroadcastSizeTag)->wait();
send_bytes(bytes.data(), bytes.size(), r, kBroadcastDataTag);
}
} else {
auto size_t = torch::zeros({1}, torch::kInt64);
std::vector<at::Tensor> size_vec{size_t};
pg_->recv(size_vec, root, kBroadcastSizeTag)->wait();
nbytes = tensor_i64(size_t);
if (nbytes < 0)
throw std::runtime_error("commux::IOContext negative broadcast size");
bytes.resize(static_cast<std::size_t>(nbytes));
recv_bytes(bytes.data(), bytes.size(), root, kBroadcastDataTag);
}
barrier();
}
@zoeyzyhu
zoeyzyhu merged commit f553f6a into main Jul 5, 2026
3 checks passed
@zoeyzyhu
zoeyzyhu deleted the cli/add_file_io branch July 5, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants