mirror of
https://github.com/kvcache-ai/Mooncake.git
synced 2026-11-05 01:17:55 +00:00
[GH-ISSUE #3200] [RFC]: Decouple Mooncake PG from PyTorch #819
Labels
No labels
ascend
auto-closed
bug
bug
CI/Build
Common
documentation
enhancement
feature request
good first issue
help wanted
high priority
MetaX
nightly-failure
pull-request
PyTorch Backend
question
RFC
Roadmap
stale
Store
Tests
Transfer Engine
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
kvcache-ai/Mooncake#819
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @caozhanhao on GitHub (Jul 30, 2026).
Original GitHub issue: https://github.com/kvcache-ai/Mooncake/issues/3200
Originally assigned to: @caozhanhao on GitHub.
Summary
This RFC proposes decoupling the Mooncake PG into two distinct layers:
libmooncake_pg.so): A separate C++ shared library built alongside the Transfer Engine via the top-level CMake project. It contains zero dependencies on PyTorch.c10dand Python interfaces by acting as an adapter on top of PG Core.PG Core will expose a preliminary C API (
mooncake_pg.h) for native C++ tests, sanitizers, micro-benchmarks, and non-PyTorch runtime integrations. The initial API is experimental and does not guarantee source or ABI compatibility.Motivation
Currently, Mooncake PG is compiled as a PyTorch extension, whereas the Transfer Engine is compiled directly by the top-level CMake build system. Because these two components are built under different build contexts, shared header-only dependencies can be selected or configured inconsistently across the build boundary. This leads to incompatible template instantiations and subtle ABI mismatches at runtime (such as the YLT header mismatch addressed in #3164).
Furthermore, the existing build architecture tightly couples the entire PG implementation to the PyTorch C++ ABI. Consequently:
By migrating the core communication logic into the top-level CMake build, PG Core and the Transfer Engine will share a single, unified dependency graph. PyTorch ABI-sensitive code is strictly isolated to the thin
PG Torchadapter layer.Architecture & Design
Layer Boundary (
mooncake_pg.h)at::Tensor,c10dclasses, PyTorchStore, or Python C API bindings.at::Tensorobjects, extracts device pointers (data_ptr()) and active stream handles, mapsc10doptions to core C enums, invokes the PG Core C API, and wraps core completion handles or GPU stream events intoc10d::Workinstances. It does not re-implement any control plane or collective algorithms.Key Abstractions
MooncakeCommunicator: The primary C++ class inside PG Core that encapsulates the state for a single CPU or GPU communication group (rank mappings, device context, buffers, worker handles, and membership view). The C API exposes this class as an opaque handle:mooncakePgComm_t.MooncakeBackend: Thec10d::ProcessGroupimplementation remaining inside PG Torch, serving as the adapter for an underlyingmooncakePgComm_thandle.Build System Integration
The top-level CMake build compiles
libmooncake_pg.soalongside the Transfer Engine. PG Torch is built as a lightweight PyTorch extension once per target PyTorch ABI, linking dynamically againstlibmooncake_pg.so.Why a C API?
PG Core and PG Torch are compiled by different toolchains. In particular, PG Torch must follow PyTorch's
_GLIBCXX_USE_CXX11_ABIsetting, which may differ from the ABI used by the top-level CMake build. Exposing C++ types such asstd::string,std::vector, exceptions, or class layouts across this boundary would couple PG Core to each PyTorch C++ ABI and require rebuilding it for different PyTorch distributions.The C API acts as an ABI boundary by exposing only opaque handles and C-compatible values.
MooncakeCommunicatorremains the internal C++ implementation, while PG Torch and other external users call it throughmooncake_pg.h. The initial C API may still evolve, but it prevents PyTorch's C++ ABI from leaking into PG Core.C API
The preliminary C API header lives at
mooncake-pg/include/mooncake_pg.h.Types
mooncakePgContext_t,mooncakePgComm_t,mooncakePgCompletion_t,mooncakePgStream_tmooncakePgResult_tmooncakePgDataType_t,mooncakePgReduceOp_tmooncakePgDeviceType_t,mooncakePgIdResolvePolicy_t,mooncakePgCommConfig_tmooncakePgProposalStatus_t,mooncakePgProposalResponse_t,mooncakePgSyncAfterFailureStatus_t,mooncakePgSyncAfterFailureResponse_tFunctions
Error Handling
mooncakePgGetErrorStringmooncakePgGetLastErrorContext Lifecycle & Global Options
mooncakePgContextCreate/mooncakePgContextDestroymooncakePgContextInitializemooncakePgContextLaunchCoordinator/mooncakePgContextConnectCoordinatormooncakePgContextSetHostIpmooncakePgContextSetTransferEnginemooncakePgContextSetDeviceFiltermooncakePgContextSetCollectiveTimeout/mooncakePgContextSetP2PTimeoutmooncakePgContextSetFaultReconciliationWindowCommunicator Management
mooncakePgCommCreate/mooncakePgCommDestroymooncakePgCommGetRank/mooncakePgCommGetSize/mooncakePgCommGetMaxGroupSizeCollective Operations
Each collective provides separate CPU and GPU entry points, named
mooncakePg<Operation>CpuandmooncakePg<Operation>Gpu, respectively:Broadcast,AllReduce,AllGather,ReduceScatter,AllToAll,Reduce,Gather,Scatter,BarrierPoint-to-Point Communication
mooncakePgSendCpu/mooncakePgRecvCpumooncakePgSendGpu/mooncakePgRecvGpuAsynchronous Completion Tracking
mooncakePgCompletionIsCompletedmooncakePgCompletionWaitmooncakePgCompletionDestroySemantics: CPU collectives and all P2P operations return an asynchronous
mooncakePgCompletion_thandle. GPU collectives execute asynchronously on the caller-provided hardware stream without returning a completion handle. Callers are responsible for maintaining memory buffer validity and stream lifecycle until work execution completes on the hardware.Elastic Membership & Topology
mooncakePgCommGetActiveRanksmooncakePgCommGetPeerStatemooncakePgCommActivateRanks/mooncakePgCommDeactivateRanksmooncakePgCommJoinmooncakePgCommSyncAfterFailuremooncakePgCommGetEpochmooncakePgCommGetNumSyncedRanksmooncakePgCommGetPreferredHcaAlternatives Considered
PyProcessGroup).This approach would eliminate the C++ dependency on PyTorch entirely. However,
PyProcessGroupis a newer and less mature integration path. Changing both the core boundary and the PyTorch integration model at the same time would introduce unnecessary compatibility risk.The proposed split preserves the established C++
c10d::ProcessGroupintegration while removing PyTorch from PG Core. It also leaves open the option of migrating toPyProcessGrouponce that integration path is sufficiently mature across the supported PyTorch versions.@github-actions[bot] commented on GitHub (Jul 30, 2026):
Thanks for opening this issue, @caozhanhao!
64004730A maintainer will triage this when possible. To help us respond faster, please include:
Useful links: Documentation · Contributing guide
@UNIDY2002 commented on GitHub (Jul 31, 2026):
LGTM