Files
qq_45774794 865a00b289 2.14版本特性跟随
Co-authored-by: qq_45774794<hetiancheng1@huawei.com>



# message auto-generated for no-merge-commit merge:
!46747 merge community_check into master

2.14版本特性跟随

Created-by: qq_45774794
Commit-by: qq_45774794
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->

# 【合入来源】
> <font color="red">**如有社区issue,请关联issue链接**</font>\
> <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font>

- [x] 需求
- [ ] 问题单
- [ ] issue/工单
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
> 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\
> 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)
社区演进,继承新接口和方法。本 PR 合并两个 2.14 社区特性跟随项:

社区演进,继承新接口和方法。本 PR 合并两个 2.14 社区特性跟随项:

## 一、pytorch/pytorch#188548:PyProcessGroup 补充 all_gather_single / reduce_scatter_single override(零适配,补测试锁定)

PyTorch 2.14(pytorch/pytorch#188548,对应 issue pytorch/pytorch#188542)为PyProcessGroup(pybind11 trampoline)补充 `all_gather_single`、`reduce_scatter_single`两个 C++ override。修复前,纯 Python 自定义 ProcessGroup(继承 torch.distributed.ProcessGroup的,如 Torchcomms)上由 C++内核发起的这些 collective(如 `torch.ops._c10d_functional.all_gather_into_tensor_out`)会穿透 trampoline 落到基类实现、因无 C++后端,导致getbackend()返回为空 报错;修复后正确路由到 Python override。

torch_npu 适配结论为**零适配**:改动仅在上游 C++ 头文件(PyProcessGroup.hpp),torch_npu 构建时使用所装 torch 的头文件,升级 torch 即自动获得;torch_npu 的 HCCL ProcessGroup 为 C++ 原生实现,不走 trampoline;无 Python API 签名变化。

新增 `test/distributed/test_pypg_npu.py`,将"零适配"结论固化为回归测试:

1. `test_all_gather_single_trampoline_npu`(torch>=2.14 版本门控):
   注册纯 Python 假 PG 后,在 NPU tensor 上通过`torch.ops._c10d_functional.all_gather_into_tensor_out`(CompositeExplicitAutograd
   设备无关 op,必然走 `group->all_gather_single` 虚函数)重放与上游测试完全相同的调用链,断言调用路由到 Python override(指纹登记)且结果正确,验证上游修复在
   torch_npu 环境生效。torch<2.14 无此桥,用例按版本跳过。
2. `test_reduce_scatter_single_npu`(全版本):
   Python 同名方法直接调用的 sanity 检查(不经 trampoline),验证假实现本身正确。

## 二、pytorch/pytorch#189353:object collectives 支持 weights_only(适配 + 补测试)

PyTorch 2.14(pytorch/pytorch#189353)为 object collectives(all_gather_object、gather_object、send_object_list、recv_object_list、broadcast_object_list、scatter_object_list)新增`weights_only` 参数(默认 False 向后兼容;True 时序列化走 `torch.save` / 反序列化走`torch.load(weights_only=True)` 安全模式)。

torch_npu 同步适配(当前 master 需同时兼容 torch 2.13 / 2.14,按`torch_npu._compat` 惯例做版本门控,以 `CURRENT_VERSION >= (2, 14)` 判定):

1. `torch_npu/distributed/distributed_c10d.py`:
      `_gather_object`(被 alias 为 `dist.gather_object`,是 torch_npu 唯一自定义实现的object collective)与上游签名对齐并适配:
   - 新增 `weights_only=False` 参数:torch >= 2.14 透传给上游`_object_to_tensor` / `_tensor_to_object` 的第 4 个参数,使 NPU 上`dist.gather_object(...,weights_only=True)` 可用;torch < 2.14 上游这两个函数无第 4 个参数,走原 3 参调用;显式传 `weights_only=True` 时直接`raise RuntimeError`,绝不静默降级到非安全的 pickle 反序列化路径;
   - 补充上游 pytorch#140827(RFC 0042)自 torch 2.6 起已有的 `group_dst` 参数(PTA 重写自 2024-12 引入后长期未同步):`dst` 为全局进程组视角、`group_dst`为 group 内视角,两者互斥(同时给出 raise ValueError);入口处将 `group_dst`规范化为全局 rank(`get_global_rank`),适配 PTA `_gather` 的全局视角约定;`dst` 默认值由 0 改为 None(两者均 None 时等价默认 dst=0,行为不变);
   - 修复子组 rootRank 转换(实测必现 bug):在引入 `group_dst` 参数后存在的隐性问题:PTA `_gather` 的 HCCL use_compatible_impl=true的 分支原将全局 `dst` 原样填入 `opts.rootRank` 直传ProcessGroupHCCL::gather(C++ assertRootRank 按**组内视角**校验 root ∈[0, size_))。HCCL用的是组内编号(从0开始),而此时传入的是全局编号(不一定从0开始)。在以往的默认组下全局等于组内侥幸正确,如果遇到子组不是从0开始的必崩,比如`new_group([2,3])`时,传入全局编号2将会导致HCCL找不到对应编号报错。添加`opts.rootRank = get_group_rank(group, dst)`保证传入HCCL的是组内编号。

其余 5 个 collectives 直接使用上游实现,升级后自动获得该能力,无需改动。

2. `test/distributed/test_c10d_object_collectives.py`:
    - 模块级新增 `_WEIGHTS_ONLY_PARAMS = [True, False] if CURRENT_VERSION >= (2, 14) else [False]`:torch 2.14 生成 True/False 两个变体,torch 2.13 只生成 False 变体;
     - 3 个现有用例(all_gather_object / broadcast_object_list / scatter_object_list)增加 `@parametrize("weights_only", _WEIGHTS_ONLY_PARAMS)` 覆盖,调用处按版本分支:2.14 透传 `weights_only` kwarg,2.13 完全不传(上游 2.13 签名无此参数,即使 False 变体传该 kwarg 也会 TypeError);
     - 新增 `test_gather_object`,覆盖 torch_npu 自定义的 `_gather_object` 实现(调用处无需版本分支:`_gather_object` 为 torch_npu 自身实现,签名已含该参数,2.13 上传 `weights_only=False` 走老路径);
     - 新增 `test_gather_object_weights_only_rejects_unsafe_object`:验证 PTA 自己的`_gather_object` 在 weights_only=True 下同样拒绝不安全对象。gather_object 仅dst rank 反序列化、仅 dst rank 抛异常,故用例采用各 rank 自行捕获、collective 结束后再断言的写法,避免 assertRaises 导致非 dst rank 失败或 barrier 死锁。加 `@unittest.skipIf(CURRENT_VERSION < (2, 14))` 门控;
     - 新增 `test_gather_object_group_dst`(parametrize weights_only):默认组下group_dst=0 与 dst=0 等价;
     - 新增 `test_gather_object_subgroup`:子组 [2,3] 上 `group_dst=0`(组内视角的全局 rank 2),rank2 断言收 [2, 3],回归锁定子组 rootRank 转换修复;用例内显式 `use_compatible_impl(True)` 强制走真实 HCCL gather 路径(不开则走 allgather 替代路径,不涉及 rootRank,测不到该修复);
     - 新增 `test_gather_object_dst_and_group_dst_conflict`:dst 与 group_dst 同时给出,应在通信前于所有 rank 抛 ValueError;
     - 新增 `test_weights_only_rejects_unsafe_object`,验证 weights_only=True 拒绝不安全对象,加 `@unittest.skipIf(CURRENT_VERSION < (2, 14))` 门控;
     - 修复测试骨架 `with_comms` 包装器未透传 `*args, **kwargs` 的问题(支持带参测试方法)。
# 【资料变更】
> 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

不涉及

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

  涉及:`dist.gather_object` 新增可选参数 `weights_only`(默认 False,向后兼容,#189353)和 `group_dst`(默认 None,与 `dst` 互斥,对齐上游 2.6+ 签名);`dst` 默认值由 0改为 None(语义等价)。

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

一、#188548:
社区新增测试用例测试结果:
![image.png](https://raw.gitcode.com/user-images/assets/7404318/7352464c-5272-42df-8562-41e764cb4bd5/image.png 'image.png')
PTA新增测试用例测试结果:
torch2.13+torch_npu环境运行 test_pypg_npu.py,2 个用例全部通过(新增的测试用例2.13未涉及)
![image.png](https://raw.gitcode.com/user-images/assets/7404318/cc200e5c-f1ad-4982-9502-5f47e145e045/image.png 'image.png')
torch 2.14 + torch_npu 环境运行 test_pypg_npu.py,2 个用例全部通过
![image-14.png](https://raw.gitcode.com/user-images/assets/7404318/1fb4ef7a-971f-479b-afd0-36c86684938d/image-14.png 'image-14.png')

二、#189353:
社区修改后的测试用例:
![image.png](https://raw.gitcode.com/user-images/assets/7404318/a6b2bc70-aee8-458e-8bb3-dcd2645fd038/image.png 'image.png')
![image.png](https://raw.gitcode.com/user-images/assets/7404318/bbc9a038-cf7e-4cf9-8319-16c2316cc937/image.png 'image.png')
PTA修改后的测试用例:
torch2.13+torchnpu下运行 test_c10d_object_collectives.py,全量7 个用例通过(每个用例默认weights_only是false,新增的测试用例2.13未涉及)
![image.png](https://raw.gitcode.com/user-images/assets/7404318/dc34b488-cda8-49f7-8626-0421d37adeec/image.png 'image.png')
torch2.14+torchnpu下运行 test_c10d_object_collectives.py,全量14个用例通过:
![image.png](https://raw.gitcode.com/user-images/assets/7404318/082c8b6e-6974-4e3b-84f8-f0b675593239/image.png 'image.png')
# 【CheckList】
> PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [ ] PR持续集成流水线(CI)执行通过,代码检查无异常


See merge request: Ascend/pytorch!46747
2026-09-30 15:36:53 +08:00
..
2026-05-25 14:25:16 +08:00
2026-05-26 20:29:02 +08:00
2026-09-30 15:36:53 +08:00