Closed
[RFC]: HIXL支持Python API #461
LuoYangyangAdd on Jul 10Closed at Aug 14
Jul 10 Modify description of issue
Jul 10 Modify description of issue
Jul 10 Modify description of issue
Jul 10 Assign qq_43646600 as responsible person
liuhao
Aug 14 Comments:
Aug 14 Comments:
Aug 14 changed custom state from 进行中 to 已完成
Aug 14 Close issue
Aug 14 Added label: resolved
Sep 3 Associated pull request
Sep 3 Deleted associated pull request: 【docs】增加readme的中英文跳转链接。
Sep 3 Associated pull request


需求描述
1. 需求描述
1.1 背景与目标
HIXL(Huawei Xfer Library)是面向昇腾芯片的单边通信库,当前仅提供 C++ 接口。上层应用(vLLM、SGLang 等推理框架)和 LLM-DataDist 已具备 Python 绑定(
llm_datadist_wrapper),但 HIXL Engine 自身缺少独立的 Python 接口,导致:本需求为 HIXL Engine 新增 Python Binding,通过 pybind11 将 HIXL C++ 公开接口完整暴露为 Python 模块
hixl_engine_wrapper,使用户可以在 Python 中完成初始化、内存注册、建链、同步/异步传输、通知、能力查询等全部操作。功能要点
技术方案
采用 静态薄包装层(Static Thin Wrapper) 模式,与 llm_datadist_wrapper 保持一致的设计风格:
相关文档
src/python/hixl_wrapper/
├── CMakeLists.txt # pybind11 模块构建配置
├── hixl_engine_wrapper.h # HixlEngineWrapper 类声明
├── hixl_engine_wrapper.cc # HixlEngineWrapper 实现
└── hixl_wrapper.cc # PYBIND11_MODULE 入口 + 常量绑定
src/python/hixl_engine/
├── CMakeLists.txt # 独立 wheel 打包配置
└── pyproject.toml # 现代 wheel 构建配置
└── hixl_engine/
└── init.py # Python 包入口(导入 .so)
tests/python
├── test_hixl_engine_api.py # UT
└── test_hixl_e2e_multiproc.py # ST
4. 接口映射
4.1 方法映射
下表列出 Python 方法、Wrapper C++ 方法、底层 C++ Hixl API、Python 返回值的完整对应关系:
initialize(local_engine, options)HixlEngineWrapper::Initialize(local_engine, options)Hixl::Initialize(AscendString, map<AscendString, AscendString>)int(Status)finalize()HixlEngineWrapper::Finalize()Hixl::Finalize()(void)Noneregister_mem(mem_desc, mem_type)HixlEngineWrapper::RegisterMem(MemDescTuple, string)Hixl::RegisterMem(MemDesc, MemType, MemHandle&)tuple[int, int](Status, handle)deregister_mem(mem_handle)HixlEngineWrapper::DeregisterMem(uintptr_t)Hixl::DeregisterMem(MemHandle)int(Status)connect(remote_engine, timeout_ms=1000)HixlEngineWrapper::Connect(string, int32_t)Hixl::Connect(AscendString, int32_t)int(Status)disconnect(remote_engine, timeout_ms=1000)HixlEngineWrapper::Disconnect(string, int32_t)Hixl::Disconnect(AscendString, int32_t)int(Status)connect_async(remote_engine, timeout_ms=1000)HixlEngineWrapper::ConnectAsync(string, int32_t)Hixl::ConnectAsync(AscendString, int32_t)int(Status)disconnect_async(remote_engine, timeout_ms=1000)HixlEngineWrapper::DisconnectAsync(string, int32_t)Hixl::DisconnectAsync(AscendString, int32_t)int(Status)get_async_connect_status(remote_engine)HixlEngineWrapper::GetAsyncConnectStatus(string)Hixl::GetAsyncConnectStatus(AscendString, AsyncConnectStatus&)tuple[int, str](Status, 状态字符串)get_all_async_connect_status()HixlEngineWrapper::GetAllAsyncConnectStatus()Hixl::GetAsyncConnectStatus(map<AscendString, AsyncConnectStatus>&)tuple[int, list[tuple[str, str]]](Status, [(engine, status_str)...])transfer_sync(remote_engine, operation, op_descs, timeout_ms=1000)HixlEngineWrapper::TransferSync(string, string, vector<TransferOpDescTuple>, int32_t)Hixl::TransferSync(AscendString, TransferOp, vector<TransferOpDesc>, int32_t)int(Status)transfer_async(remote_engine, operation, op_descs)HixlEngineWrapper::TransferAsync(string, string, vector<TransferOpDescTuple>)Hixl::TransferAsync(AscendString, TransferOp, vector<TransferOpDesc>, TransferArgs, TransferReq&)tuple[int, int](Status, req_id)get_transfer_status(req_id)HixlEngineWrapper::GetTransferStatus(uintptr_t)Hixl::GetTransferStatus(TransferReq, TransferStatus&)tuple[int, str](Status, 状态字符串)get_transfer_status_batch(max_query_count, skip_waiting)HixlEngineWrapper::GetTransferStatusBatch(uint32_t, bool)Hixl::GetTransferStatus(GetTransferStatusArgs, vector<TransferResult>&)tuple[int, list[tuple[int, str]]](Status, [(req_id, status_str)...])send_notify(remote_engine, notify, timeout_ms=1000)HixlEngineWrapper::SendNotify(string, NotifyDescTuple, int32_t)Hixl::SendNotify(AscendString, NotifyDesc, int32_t)int(Status)get_notifies()HixlEngineWrapper::GetNotifies()Hixl::GetNotifies(vector<NotifyDesc>&)tuple[int, list[tuple[str, str]]](Status, [(name, msg)...])get_capability(feature_type)HixlEngineWrapper::GetCapability(string)Hixl::GetCapability(FeatureType, int32_t&)) statictuple[int, int](Status, 1=支持/0=不支持)4.2 常量与枚举映射
Status 状态码
kSuccesshixl::SUCCESSintkFailedhixl::FAILEDintkParamInvalidhixl::PARAM_INVALIDintkTimeouthixl::TIMEOUTintkNotConnectedhixl::NOT_CONNECTEDintkAlreadyConnectedhixl::ALREADY_CONNECTEDintkNotifyFailedhixl::NOTIFY_FAILEDintkUnsupportedhixl::UNSUPPORTEDintkResourceExhaustedhixl::RESOURCE_EXHAUSTEDint无作用域枚举(需
static_cast<int>传给py::int_())kMemDevicestatic_cast<int>(hixl::MEM_DEVICE)intkMemHoststatic_cast<int>(hixl::MEM_HOST)intkReadstatic_cast<int>(hixl::READ)intkWritestatic_cast<int>(hixl::WRITE)intkFeatureAutoConnectstatic_cast<int>(hixl::FeatureType::AUTO_CONNECT)intkFeatureClientServerCommstatic_cast<int>(hixl::FeatureType::CLIENT_SERVER_COMM)intkFeatureSupportedhixl::FEATURE_SUPPORTED(1)intkFeatureNotSupportedhixl::FEATURE_NOT_SUPPORTED(0)intenum class 值(需
static_cast<uint32_t>传给py::int_())kTransferStatusWaitingstatic_cast<uint32_t>(TransferStatus::WAITING)intkTransferStatusCompletedstatic_cast<uint32_t>(TransferStatus::COMPLETED)intkTransferStatusTimeoutstatic_cast<uint32_t>(TransferStatus::TIMEOUT)intkTransferStatusFailedstatic_cast<uint32_t>(TransferStatus::FAILED)intkAsyncConnectStatusNotConnectstatic_cast<uint32_t>(AsyncConnectStatus::NOT_CONNECT)intkAsyncConnectStatusConnectPendingstatic_cast<uint32_t>(AsyncConnectStatus::CONNECT_PENDING)intkAsyncConnectStatusConnectingstatic_cast<uint32_t>(AsyncConnectStatus::CONNECTING)intkAsyncConnectStatusConnectedstatic_cast<uint32_t>(AsyncConnectStatus::CONNECTED)intkAsyncConnectStatusConnectFailedstatic_cast<uint32_t>(AsyncConnectStatus::CONNECT_FAILED)intkAsyncConnectStatusDisconnectPendingstatic_cast<uint32_t>(AsyncConnectStatus::DISCONNECT_PENDING)intkAsyncConnectStatusDisconnectingstatic_cast<uint32_t>(AsyncConnectStatus::DISCONNECTING)int初始化选项常量
kOptionEnableUseFabricMemhixl::OPTION_ENABLE_USE_FABRIC_MEMstrkOptionRdmaTrafficClasshixl::OPTION_RDMA_TRAFFIC_CLASSstrkOptionRdmaServiceLevelhixl::OPTION_RDMA_SERVICE_LEVELstrkOptionBufferPoolhixl::OPTION_BUFFER_POOLstrkOptionGlobalResourceConfighixl::OPTION_GLOBAL_RESOURCE_CONFIGstrkOptionAutoConnecthixl::OPTION_AUTO_CONNECTstrkOptionLocalCommReshixl::OPTION_LOCAL_COMM_RESstr5. 类型桥接规则
5.1 基本类型
AscendStringstrAscendString(py_str.c_str());出:std::string(ascend_str.GetString())GetString()(null 安全,返回""而非nullptr)Status(uint32_t)intvoidNoneFinalize()uintptr_tintint32_t/uint32_t/size_tintboolboolskip_waitingMemHandle(void*)intreinterpret_cast<uintptr_t>(handle)出;reinterpret_cast<MemHandle>(uintptr_t)入TransferReq(void*)int5.2 枚举桥接
MemTypestr"npu"/"cpu"ParseMemType{PARAM_INVALID, MEM_DEVICE}+HIXL_LOGW警告TransferOpstr"READ"/"WRITE"ParseTransferOp{PARAM_INVALID, READ}+HIXL_LOGW警告TransferStatusstr"WAITING"/"COMPLETED"/"TIMEOUT"/"FAILED"TransferStatusToStrAsyncConnectStatusstr"CONNECTED"/"NOT_CONNECT"/...AsyncConnectStatusToStrFeatureTypestr"auto_connect"/"client_server_comm"ParseFeatureType{PARAM_INVALID, AUTO_CONNECT}+HIXL_LOGW警告5.3 结构体桥接(Python tuple ↔ C++ struct)
MemDesc {addr, len}tuple[int, int]UnpackMemDesc:std::get<0>→addr,std::get<1>→lenTransferOpDesc {local_addr, remote_addr, len}tuple[int, int, int]UnpackTransferOpDescs:逐条拆包NotifyDesc {name, notify_msg}tuple[str, str]UnpackNotifyDesc:.c_str()→AscendStringNotifyDesc {name, notify_msg}tuple[str, str]GetString()→std::stringTransferResult {req, status}tuple[int, str]reinterpret_cast<uintptr_t>(req)+TransferStatusToStr(status)TransferArgs{},reserved[120]默认为 0GetTransferStatusArgs5.4 容器桥接
std::map<std::string, std::string>dict[str, str]std::map<AscendString, AscendString>dict[str, str].c_str()构造 AscendStringstd::map<AscendString, AsyncConnectStatus>list[tuple[str, str]]GetString()→engine,AsyncConnectStatusToStr()→statusstd::vector<TransferOpDesc>list[tuple[int, int, int]]UnpackTransferOpDescsstd::vector<NotifyDesc>list[tuple[str, str]]GetString()→name/msgstd::vector<TransferResult>list[tuple[int, str]]5.5 输出参数桥接(C++
&引用 → Python tuple)Status RegisterMem(..., MemHandle &out)pair<Status, uintptr_t>tuple[int, int]Status GetAsyncConnectStatus(..., AsyncConnectStatus &out)pair<Status, string>tuple[int, str]Status GetAsyncConnectStatus(map<AscendString, AsyncConnectStatus> &out)pair<Status, vector<pair<string,string>>>tuple[int, list[tuple[str, str]]]Status TransferAsync(..., TransferReq &out)pair<Status, uintptr_t>tuple[int, int]Status GetTransferStatus(TransferReq &, TransferStatus &out)pair<Status, string>tuple[int, str]Status GetTransferStatus(GetTransferStatusArgs &, vector<TransferResult> &out)pair<Status, vector<tuple<uintptr_t,string>>>tuple[int, list[tuple[int, str]]]Status GetNotifies(vector<NotifyDesc> &out)pair<Status, vector<NotifyDescTuple>>tuple[int, list[tuple[str, str]]]static Status GetCapability(FeatureType, int32_t &out)pair<Status, int32_t>tuple[int, int]void Finalize()voidNone6. GIL 安全设计
每个 Python → C++ 调用路径遵循以下时序:
所有方法均使用
py::call_guard<py::gil_scoped_release>(),理由是所有 C++ 操作均不访问 Python 对象(纯 C++ 类型参数和返回值),且可能涉及阻塞操作(建链、传输等),释放 GIL 可让其他 Python 线程并发执行。编译时额外定义
PYBIND11_NO_ASSERT_GIL_HELD_INCREF_DECREF,避免 GIL 释放状态下 pybind11 内部 incref/decref 断言误报。7. 线程安全设计
Wrapper 内部使用
std::shared_mutex保护全局hixl_engine_单例:Initializestd::unique_lock(写锁)Finalizestd::unique_lock(写锁)RegisterMemstd::shared_lock(读锁)DeregisterMemstd::shared_lock(读锁)Connect/Disconnectstd::shared_lock(读锁)ConnectAsync/DisconnectAsyncstd::shared_lock(读锁)GetAsyncConnectStatusstd::shared_lock(读锁)GetAllAsyncConnectStatusstd::shared_lock(读锁)TransferSyncstd::shared_lock(读锁)TransferAsyncstd::shared_lock(读锁)GetTransferStatusstd::shared_lock(读锁)GetTransferStatusBatchstd::shared_lock(读锁)SendNotifystd::shared_lock(读锁)GetNotifiesstd::shared_lock(读锁)GetCapabilityhixl_engine_)Hixl::GetCapability8. 构建与安装集成
8.1 CMake 构建
src/python/hixl_wrapper/CMakeLists.txt8.2 上层引用
src/python/CMakeLists.txt:add_subdirectory(llm_datadist) add_subdirectory(llm_wrapper) add_subdirectory(metadef_wrapper) add_subdirectory(hixl_wrapper) # ← 新增 add_subdirectory(hixl_engine) # ← 新增8.3 独立 Wheel 打包
src/python/hixl_engine/CMakeLists.txtpyproject.toml打包
安装后的文件布局:
用户导入路径:
from hixl_engine import hixl_engine_wrapper hixl_engine_wrapper.initialize("192.168.1.1:5000", {})9. 测试方案(UT)
9.1 单元测试
测试文件:tests/python/test_hixl_engine_api.py
测试桩:tests/depends/python/CMakeLists.txt 中的 hixl_stub 模块
from hixl_engine import hixl_engine_wrapperhixl_engine_wrapper.kSuccess == 0initialize/finalizeinitializeregister_mem/deregister_mem(status, handle),handle 为非零整数connect/disconnectconnect_async/get_async_connect_statusget_all_async_connect_statustransfer_synctransfer_async/get_transfer_status(status, req_id),状态从 "WAITING" 变为 "COMPLETED"get_transfer_status_batch(req_id, status_str)列表send_notify/get_notifies(name, msg)对get_capability(status, 1 or 0)9.2 异常与边界测试
connectregister_mem((addr, len), "invalid"){PARAM_INVALID, handle=0}transfer_sync("IP", "INVALID", ...)get_capability("invalid"){PARAM_INVALID, 0}transfer_sync("IP", "READ", [])transfer_sync设置极短 timeoutderegister_mem(0)共 11 个测试类、50+ 个测试方法,覆盖 RFC 中的全部测试场景:
HixlModuleImportTestHixlInitializeFinalizeTestHixlRegisterMemTestHixlConnectDisconnectTestHixlAsyncConnectTestHixlTransferTestHixlNotifyTestHixlCapabilityTestHixlUninitializedTestHixlInvalidParamTestHixlTransferStatusStrTest10. 相关文档
include/hixl/hixl.h、include/hixl/hixl_types.hsrc/python/hixl_wrapper/hixl_engine_wrapper.h、hixl_engine_wrapper.cc、hixl_wrapper.ccsrc/python/hixl_wrapper/CMakeLists.txt、src/python/hixl_engine/CMakeLists.txtsrc/python/llm_wrapper/(LLM-DataDist Python 绑定)