Pull Request已成功合入, 合并人@ascend-robot
(感谢 kuhn7 的贡献)Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| repo-Ascend/pytorch | ✅ shaoyf, huangyunlong2022 (2/2) | ✅ shaoyf (1/1) |
| torch_npu/csrc/distributed | ✅ shaoyf, huangyunlong2022 (2/2) | ✅ shaoyf (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


torch_npu/csrc/distributed/TraceUtils.h#ifndef BUILD_LIBTORCH
if (includeStacktraces) {
auto &tb = stracebacks.tracebacks.at(i);
auto frames = new_list();
for (int64_t frame : tb) {
frames.push_back(all_frames.at(frame));
}
dict.insert(frames_key, frames);
}
#endif1. 将第591-597行的整个 `if (includeStacktraces)` 块也包含在 `#ifndef BUILD_LIBTORCH` 条件编译内,确保当 `BUILD_LIBTORCH` 定义时,这段代码不会被编译。 2. 或者,在函数开头,如果 `BUILD_LIBTORCH` 被定义且 `includeStacktraces` 为 true,可以提前返回或记录警告,因为堆栈跟踪功能不可用。 3. 调整 `#endif` 的位置,使其覆盖从第565行到第597行的所有依赖 `torch::CapturedTraceback` 的代码。
此评论由代码审查工具自动生成


问题/功能描述
本次PR旨在解决在构建纯C++库(BUILD_LIBTORCH)时,PyTorch分布式模块(distributed)及其HCCL通信库相关代码的编译兼容性问题。核心问题包括:1)分布式模块的源代码未被正确包含到libtorch的编译系统中;2)HCCL通信库中依赖Python环境或PyTorch完整框架的功能(如Python绑定、性能分析、调试监控、堆栈跟踪)在纯C++环境下导致编译失败。通过本次修改,确保了libtorch库在剥离Python依赖后,其核心分布式功能仍能正常编译和运行。
修改方案描述
修改方案围绕构建系统配置和源代码条件编译展开。首先,调整了CMake构建脚本,将分布式模块的源代码全局纳入编译,并在libtorch构建模式下正确集成。其次,在HCCL通信库的关键源代码文件(如ProcessGroupHCCL.cpp和跟踪工具代码)中,广泛使用#ifndef BUILD_LIBTORCH宏进行条件编译隔离,将有Python依赖的代码段(如pybind11头文件、性能分析工具、调试监控函数声明、Python堆栈跟踪功能)排除在libtorch构建之外。同时,对必须保留的字符串操作等安全关键代码,在libtorch分支中替换为纯C++标准库实现。这些修改确保了核心通信逻辑的通用性,并保持了在完整PyTorch环境下的原有功能。


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | 🕚 | >>> |
| Build_LibTorch | 🕚 | >>> | |
| Build_ARM | 🕚 | >>> | |
| Build_ARM_inductor | 🕚 | >>> | |
| Build_X86_torchair | 🕚 | >>> | |
| Build_ARM_torchair | 🕚 | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | CodeCheck | ❌ | >>> |
| check_error | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_X86_Part_01 | 🕚 | >>> |
| UT_X86_Part_02 | 🕚 | >>> | |
| UT_ARM_A2_Part_01 | 🕚 | >>> | |
| UT_ARM_A2_Part_02 | 🕚 | >>> | |
| UT_ARM_A3_Part_01 | 🕚 | >>> | |
| UT_ARM_A3_Part_02 | 🕚 | >>> | |
| UT_DIST_X86_Part_01 | 🕚 | >>> | |
| UT_DIST_X86_Part_02 | 🕚 | >>> | |
| UT_DIST_X86_Part_03 | 🕚 | >>> | |
| UT_DIST_X86_Part_04 | 🕚 | >>> | |
| UT_inductor_Part_01 | 🕚 | >>> | |
| UT_inductor_Part_02 | 🕚 | >>> | |
| UT_inductor_Part_03 | 🕚 | >>> | |
| UT_inductor_Part_04 | 🕚 | >>> | |
| 流水线 | PR-pipeline_pytorch | ❌ | >>> |


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_LibTorch | ✅ | >>> | |
| Build_ARM | ✅ | >>> | |
| Build_ARM_inductor | 🛑 | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | CodeCheck | ✅ | >>> |
| check_error | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_X86_Part_01 | ✅ | >>> |
| UT_X86_Part_02 | ✅ | >>> | |
| UT_ARM_A2_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Part_02 | ✅ | >>> | |
| UT_ARM_A3_Part_01 | ✅ | >>> | |
| UT_ARM_A3_Part_02 | ✅ | >>> | |
| UT_DIST_X86_Part_01 | ✅ | >>> | |
| UT_DIST_X86_Part_02 | ✅ | >>> | |
| UT_DIST_X86_Part_03 | ✅ | >>> | |
| UT_DIST_X86_Part_04 | ✅ | >>> | |
| UT_inductor_Part_01 | 🛑 | >>> | |
| UT_inductor_Part_02 | 🛑 | >>> | |
| UT_inductor_Part_03 | 🛑 | >>> | |
| UT_inductor_Part_04 | 🛑 | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |


如果rpc/symm_mem暂不提供,对应的头文件也不需要


这个和上面的头文件在一起的话,可以考虑用一个宏


这里建议用安全拷贝函数


如果整个函数隔离了,这个感觉有点冗余了


这里尽量不要改变guard的范围


/lgtm


Review Guide
This pull-request passes review.
Committers who wrote a comment of /approve are: shaoyf.
Reviewers who wrote a comment of /lgtm are: shaoyf, huangyunlong2022.


【合入来源】
【修改方案】
【资料变更】
【接口变更】
【功能验证】
【CheckList】