已合并
reduce scatter support tensorlist.size != world_size #44436
limuan创建于 14 天前
reduce scatter support tensorlist.size != world_size #44436
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 limuan 的贡献)ascend-robot
14 天前 评论:
14 天前 评论:
atomgit-bot
14 天前 评论:
14 天前 评论:
变更摘要
本次变更旨在使 reduce_scatter 操作支持 tensor_lists.size() != world_size 的场景,对齐 NCCL 的行为。通过新增专用的 flatten_for_reduce_scatter 函数替代原先复用 flatten_for_scatter_gather 的方式,移除了对 size_(即 world_size)的依赖,仅在函数内部校验输入列表与输出列表长度一致以及对应张量位于同一设备。
主要改动
- 新增
flatten_for_reduce_scatter函数: 在ProcessGroupHCCL.cpp中新增该函数,用于reduce_scatter的输入展平,不再依赖size_参数,允许输入张量列表大小与 world_size 不同;同时校验输入/输出列表长度一致及设备一致性。 reduce_scatter方法调用替换: 将ProcessGroupHCCL::reduce_scatter中原本调用flatten_for_scatter_gather(inputTensors, outputTensors, size_)替换为flatten_for_reduce_scatter(inputTensors, outputTensors),解除对 world_size 的强制约束。c10d::newLikeFlat调用适配: 新函数内部使用c10d::newLikeFlat(tensor_lists, i)对每个设备上的张量列表进行展平,替代原有展平逻辑中对 world_size 的遍历限制。


atomgit-bot
14 天前 评论:
14 天前 评论:
14 天前 添加了label:ascend-cla/yes
此处折叠了76条消息 查看更多
13 天前 删除了label:ci-pipeline-running
13 天前 添加了label:ci-pipeline-passed
ascend-robot
13 天前 评论:
13 天前 评论:
流水线 PR-pipeline_pytorch#60512 [ commitID:6cacec6d ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_LibTorch_x86 | ✅ | >>> | |
| Build_LibTorch_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | 🛑 | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | codecheck_pre-commit | ✅ | >>> |
| check_error | ✅ | >>> | |
| lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_ARM_A3_Part_01 | 🛑 | >>> |
| UT_ARM_A3_Part_02 | 🛑 | >>> | |
| UT_ARM_A2_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Part_02 | ✅ | >>> | |
| UT_ARM_A2_Part_03 | ✅ | >>> | |
| UT_inductor_Part_01 | 🛑 | >>> | |
| UT_inductor_Part_02 | 🛑 | >>> | |
| UT_inductor_Part_03 | 🛑 | >>> | |
| UT_inductor_Part_04 | 🛑 | >>> | |
| UT_DIST_ARM_Part_01 | ✅ | >>> | |
| UT_DIST_ARM_Part_02 | ✅ | >>> | |
| UT_DIST_ARM_Part_03 | ✅ | >>> | |
| UT_DIST_ARM_Part_04 | ✅ | >>> | |
| UT_ARM_A2_Select_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Select_Part_02 | ✅ | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


13 天前 合入了pull request
ascend-robot
13 天前 评论:
13 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13959 [ commitID:6cacec6d ] 已完成


【合入来源】
【修改方案】
本提案修改
torch_npu的ProcessGroupHCCL::reduce_scatter,使其输入支持范围与 PyTorch 社区(NCCL)保持一致。PyTorch 2.2 的
reduce_scatter对输入张量列表有形状/计数约束(列表长度须等于world_size、每张量 numel 须等于输出 numel 等),2.3 起去除了这些约束。pta(torch_npu)当前的reduce_scatter与 PyTorch 2.2 实现一致:输入是 tensor list,包含多个 tensor,数量与卡数一致。这与社区后续版本不一致,需要兼容输入只有一个 tensor 的场景等用例,和社区保持一致。核心改动:在
reduce_scatter的 same_size 分支新增展平函数flatten_for_reduce_scatter,替代原先复用的flatten_for_scatter_gather——移除"输入张量数须等于 world_size""每张量 numel 须等于输出 numel"两条校验,保留列表长度一致与 input/output 同设备校验,并新增非空检查。Python 层torch.distributed.reduce_scatter签名不变。)各输入形态数值示例
统一
world_size=4、output=[4]、fp32、SUM,输入约定同 §2.1(rankr展平后第k元 =r*10+k)。need = 16(输出 numel × world_size);全局位置p的归约值在p < have时为60+4p,p ≥ have时零填充。下文列出每个 rank 的输入与输出。① 单 tensor(
input_list = [[16]],have = 16 = need):② 张量数 < 卡数(
input_list = [[4],[4]],have = 8 < 16,尾部两 rank 零填充):③ 张量数 > 卡数(
input_list = [[4]]*5,have = 20 > 16,第 5 个张量整体忽略):④ 张量数 = 卡数,每张量 > 输出(
input_list = [[5]]*4,have = 20 > 16,展平后尾部 4 元忽略):展平后每 rank 共 20 元,前 16 元参与归约,尾部 4 元(第 4 个张量的后 4 元)忽略。
⑤ 张量数 = 卡数,每张量 < 输出(
input_list = [[3]]*4,have = 12 < 16,尾部 rank 零填充):⑥ 2D 及多维(
input_list = [[5,4]],have = 20 > 16,展平后尾部 4 元忽略,同 ③):【资料变更】
不涉及
【接口变更】
不涉及
【功能验证】
1、基础功能验证


2、dtypeoptensorshape(1D,2D)*input_tensor_list.size(==,>,< world_size)*all_input_numbel(==, >, < )all_output_numbel reduce_scatter算子输出与gpu结果对比
3、性能验证,修改前后pta调用reduce_scatter,用时基本无变化
【CheckList】