已合并
test: Since the PyTorch community's test cases do not provide full coverage, we adopt the method of adding new test cases to supplement and cover all test case scenarios. #42824
创建于 26 天前
test: Since the PyTorch community's test cases do not provide full coverage, we adopt the method of adding new test cases to supplement and cover all test case scenarios. #42824
已合并
从已删除 :test_allreduce_hook_v2.7.1合入到Ascend/pytorchv2.7.1
Pull Request已成功合入, 合并人@ascend-robot
(感谢 的贡献)ascend-robot
26 天前 评论:
26 天前 评论:
atomgit-bot
26 天前 评论:
26 天前 评论:
变更摘要
此 PR 新增了独立测试文件 test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py,用于在 NPU 设备(HCCL 后端)上补充验证 torch.distributed.algorithms.ddp_comm_hooks.default_hooks.allreduce_hook 的完整功能覆盖。测试通过 torch.accelerator.current_accelerator() 动态获取设备类型,以双进程分布式方式运行,覆盖基础梯度一致性、gradient_as_bucket_view、static_graph、组合配置、Future 契约、多种浮点 dtype、FP16 溢出边界、自定义子进程组、多 bucket 以及异常入参等场景。
主要改动
- 新增
TestAllreduceHook测试类:包含 11 个测试方法,以未注册通信 hook 的 DDP 梯度为基准,验证allreduce_hook在各配置下的梯度结果一致性,支持gradient_as_bucket_view、static_graph、process_group=None及注册辅助接口 (register_ddp_comm_hook) 的组合测试。 - 新增 Future 契约与多 dtype 验证:通过自定义
_future_contract_hook使用torch._C.Future检查 hook 返回值类型,并覆盖torch.float32、torch.float16、torch.bfloat16三种 HCCL 支持的浮点 dtype 的梯度正确性与 Future 结果形状/设备一致性。 - 新增 FP16 溢出边界测试:
_run_overflow_boundary使用 60000.0 的 FP16 输入张量验证allreduce_hook在大值场景下不会产生无穷大或 NaN,确保数值稳定性。 - 新增自定义子进程组与多 bucket 测试:
_run_custom_subgroup为每个 rank 创建独立的dist.new_group子组并验证单卡梯度保持不变;_run_multiple_buckets通过低bucket_cap_mb强制多 bucket 分发,用_counting_hook断言 hook 调用次数大于 1 且梯度结果与基准一致。 - 新增辅助类与异常入参测试:定义
_AllreduceModel、_MultiBucketModel、_Bucket等辅助模型/桶类;test_allreduce_hook_invalid_arguments验证allreduce_hook在无参、传入None或错误类型参数时抛出TypeError或AttributeError。


atomgit-bot
26 天前 评论:
26 天前 评论:
代码审查
审查总结
经过对整个 diff 的逐行审查,我完成了以下检查:
- 文件:
test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py(新增 304 行)- 所有 11 个测试方法的业务逻辑、参数传递、梯度计算、结果断言均正确
- Future 契约验证逻辑(
_future_contract_hook/validate)正确 - FP16 溢出边界验证逻辑正确(先除后聚合的保护语义)
- 自定义子进程组、多 bucket、异常入参测试均正确
- 类方法与
mp.spawn的绑定使用正确 - 模型创建与设备迁移顺序正确
- 发现 1 个 P3 问题:缺少 NPU 设备可用性守卫(
@skipIfUnsupportMultiNPU或等价机制)
| 优先级 | 数量 |
|---|---|
| P0 | 0 |
| P1 | 0 |
| P2 | 0 |
| P3 | 1 |
总体风险判断:低风险。该测试文件逻辑正确、覆盖全面,唯一的 P3 问题是缺少 NPU 环境守卫,可能在非 NPU CI 环境中导致硬失败而非优雅跳过,但不影响 NPU 环境下的测试正确性。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


26 天前 添加了label:ascend-cla/yes
此处折叠了93条消息 查看更多
ascend-robot
17 天前 评论:
17 天前 评论:
流水线 PR-pipeline_pytorch#54519 [ commitID:ea1a0bca ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 : 停止流水线


Jingwei Huang
16 天前 评论:
16 天前 评论:
/lgtm


16 天前 添加了label:lgtm
16 天前 合入了pull request
ascend-robot
16 天前 评论:
16 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13642 [ commitID:ea1a0bca ] 已完成


【合入来源】
社区任务issue地址:https://gitcode.com/Ascend/pytorch/issues/3129
issue分析报告地址:https://gitcode.com/Ascend/pytorch/issues/3364
【修改方案】
test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py独立测试文件和TestAllreduceHook测试类。测试通过torch.accelerator.current_accelerator()获取设备类型,将参与运算的张量迁移到 NPU,并使用 HCCL、动态空闲端口和显式进程组清理完成分布式验证。torch.distributed.algorithms.ddp_comm_hooks.default_hooks.allreduce_hook的基础 DDP 梯度一致性、gradient_as_bucket_view、static_graph、两者组合、process_group=None、注册辅助接口、自定义子进程组和多 bucket 场景。torch.float32、torch.float16、torch.bfloat16的平均结果、shape、dtype、NPU设备和torch._C.Future返回契约,并覆盖 FP16 溢出边界、单 rank HCCL 正常调用以及缺少参数、空 bucket、非法进程组等异常场景。_spawnhelper 应用skipIfUnsupportMultiNPU(WORLD_SIZE)。少于两张 NPU时跳过依赖双进程的 10 个测试,继续执行单 NPU契约和异常参数测试;具备至少两张 NPU时执行全部 12 个测试,避免 rank 1 使用非法设备索引。【资料变更】
不涉及。API 本身已经适配,
master/docs/zh/api/native_api目录文档已经记录,无需资料变更。【接口变更】
不涉及。本次仅新增测试用例,不调整
torch.distributed.algorithms.ddp_comm_hooks.default_hooks.allreduce_hook的实现或对外接口。【功能验证】
v2.7.1
执行命令:
python test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py -v结果:
v2.11.0
执行命令:
python test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py -v结果:
v2.12.0
执行命令:
python test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py -v结果:
master
执行命令:
python test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py -v结果:
【CheckList】