已合并
libtorch_npu.so support hccl api #31394
libtorch_npu.so support hccl api #31394
已合并
kuhn7创建于 3月4日
kuhn7成员
3月4日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

【资料变更】

请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 kuhn7 的贡献)
Kkuhn7成员
3月4日 创建了 pull request,commit 79714482
ascend-robotascend-robot成员
3月4日 添加了label:stat/needs-squash
ascend-robot
ascend-robot成员
3月4日 评论:

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 /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
kuhn7成员
3月4日 评论:

compile

likedislike
ascend-robotascend-robot成员
3月4日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
3月4日 评论:

CLA Signature Pass

kuhn7, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robotascend-robot成员
3月4日 添加了label:ci-pipeline-running
AtlasAccount
AtlasAccount成员3月4日进行代码检视1
torch_npu/csrc/distributed/TraceUtils.h
@@ -590,4 +617,4 @@
590617 auto arg_sizes = new_list();
591618 for (auto i : c10::irange(dim)) {
592619 (void)i;
593620 arg_sizes.push_back(*it++);
AtlasAccount
AtlasAccount3月4日评论:
条件编译范围不匹配: 在 `getCollectiveTrace` 函数中,第565-580行的堆栈跟踪处理逻辑被 `#ifndef BUILD_LIBTORCH` 保护。然而,第591-597行使用 `includeStacktraces` 参数在 `#ifndef BUILD_LIBTORCH` 块内部访问 `stracebacks` 和 `tb` 变量。如果 `BUILD_LIBTORCH` 被定义,`stracebacks` 和 `tb` 变量将不存在,但第591行的 `if (includeStacktraces)` 检查仍然会执行,导致编译错误,因为 `stracebacks` 未定义。此外,第598行的 `#endif` 位置可能过早,因为第591-597行的代码也依赖于 `BUILD_LIBTORCH` 未定义的条件。
问题类型: 条件编译范围不匹配
文件路径: torch_npu/csrc/distributed/TraceUtils.h
行号: 590
问题代码:
#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);
            }
#endif
修改建议:
1. 将第591-597行的整个 `if (includeStacktraces)` 块也包含在 `#ifndef BUILD_LIBTORCH` 条件编译内,确保当 `BUILD_LIBTORCH` 定义时,这段代码不会被编译。
2. 或者,在函数开头,如果 `BUILD_LIBTORCH` 被定义且 `includeStacktraces` 为 true,可以提前返回或记录警告,因为堆栈跟踪功能不可用。
3. 调整 `#endif` 的位置,使其覆盖从第565行到第597行的所有依赖 `torch::CapturedTraceback` 的代码。
---
此评论由代码审查工具自动生成
likedislike
AtlasAccount
AtlasAccount成员
3月4日 评论:

问题/功能描述

本次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环境下的原有功能。

likedislike
ascend-robotascend-robot成员
3月4日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
3月4日 添加了label:ci-pipeline-failed
ascend-robot
ascend-robot成员
3月4日 评论:
流水线 PR-pipeline_pytorch#9269 运行失败
阶段 任务名 状态 详情
编译构建 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 >>>
likedislike
Kkuhn7成员
3月5日 修改了pull request 的描述
kuhn7成员
3月5日 评论:

compile

likedislike
ascend-robotascend-robot成员
3月5日 删除了label:ci-pipeline-failed
ascend-robotascend-robot成员
3月5日 添加了label:ci-pipeline-running
ascend-robotascend-robot成员
3月5日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
3月5日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
3月5日 评论:
流水线 PR-pipeline_pytorch#9371 已完成
阶段 任务名 状态 详情
编译构建 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 >>>
likedislike
huangyunlong成员3月5日进行代码检视2
build_libtorch_npu.py
@@ -230,3 +233,4 @@
230233 ]
231234 glob_header_files = []
232235 for regex_pattern in header_files:
huangyunlong3月5日评论:

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

likedislike
kuhn7成员
3月5日 评论:

当前先提供功能,这部分补用例的时候调整

huangyunlong成员3月5日进行代码检视2
torch_npu/csrc/distributed/ProcessGroupHCCL.cpp
@@ -59,29 +65,30 @@
65+#endif
5966 
6067namespace c10d_npu {
6168namespace {
@@ -62,5 +69,5 @@
6269static constexpr uint32_t kOpWaitTimeoutOffset = 30U; // second
6370static uint32_t kOpWaitTimeout = 1868U; // second
6471static int32_t defaultExecTimeout = 1836;
6572constexpr const char* P2P_DEVICE_KEY = "_p2p";
6673 
@@ -1377,5 +1384,5 @@
13771384 TORCH_NPU_HCCL_LOGI("process group destroyed, group id is %s.", options_->group_id.c_str());
13781385}
13791386 
1387+#ifndef BUILD_LIBTORCH
13801388std::future<bool> ProcessGroupHCCL::launchAsyncPythonTracebackDump()
13811389{
13821390 std::promise<bool> resultPromise;
@@ -1485,6 +1493,7 @@ void ProcessGroupHCCL::dumpTraceAndResetStatus()
14851493 heartbeat_++;
14861494 shouldDump_.store(false);
14871495}
1496+#endif
14881497 
14891498void ProcessGroupHCCL::terminateProcess(std::string errMsg)
14901499{
@@ -1501,6 +1510,7 @@ int computeDeltaMS(
15011510 .count();
15021511}
15031512 
1513+#ifndef BUILD_LIBTORCH
huangyunlong3月5日评论:

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

likedislike
kuhn7成员
3月5日 评论:

好,下来改

huangyunlong成员3月5日进行代码检视2
torch_npu/csrc/distributed/ProcessGroupHCCL.cpp
@@ -6344,3 +6428,4 @@
63446428 if (c10_npu::is_core_control_enabled) {
63456429 c10_npu::UseStreamResInCurrentThread(stream.stream(false));
63466430 }
huangyunlong3月5日评论:

这里建议用安全拷贝函数

likedislike
kuhn7成员
3月5日 评论:

我认为在当前文件里定义一个安全拷贝函数不太合理

huangyunlong成员3月5日进行代码检视2
torch_npu/csrc/distributed/ProcessGroupHCCL.cpp
@@ -6344,3 +6428,4 @@
63446428 if (c10_npu::is_core_control_enabled) {
63456429 c10_npu::UseStreamResInCurrentThread(stream.stream(false));
63466430 }
huangyunlong3月5日评论:

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

likedislike
kuhn7成员
3月5日 评论:

当前先提供功能,这部分补用例的时候调整

huangyunlong成员3月5日进行代码检视2
torch_npu/csrc/distributed/TraceUtils.h
@@ -573,3 +599,4 @@
573599 
574600 dict.insert(record_id_key, int64_t(e.id_));
575601 dict.insert(pg_id_key, int64_t(e.pg_id_));
huangyunlong3月5日评论:

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

likedislike
kuhn7成员
3月5日 评论:

当前先提供功能,这部分补用例的时候调整

huangyunlong成员
3月5日 评论:

/lgtm

likedislike
shaoyf成员
3月5日 评论:

/approve

likedislike
ascend-robotascend-robot成员
3月5日 添加了label:approvedlgtm
ascend-robot
ascend-robot成员
3月5日 评论:

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.

likedislike
ascend-robotascend-robot成员
3月5日 合入了pull request