已合并
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
已合并
创建于 26 天前
已删除 :test_allreduce_hook_v2.7.1合入到Ascend/pytorchv2.7.1
26 天前

【合入来源】

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

社区任务issue地址:https://gitcode.com/Ascend/pytorch/issues/3129
issue分析报告地址:https://gitcode.com/Ascend/pytorch/issues/3364

【修改方案】

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

  1. 新增 test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py 独立测试文件和 TestAllreduceHook 测试类。测试通过 torch.accelerator.current_accelerator() 获取设备类型,将参与运算的张量迁移到 NPU,并使用 HCCL、动态空闲端口和显式进程组清理完成分布式验证。
  2. 新增 12 个测试方法,验证 torch.distributed.algorithms.ddp_comm_hooks.default_hooks.allreduce_hook 的基础 DDP 梯度一致性、gradient_as_bucket_viewstatic_graph、两者组合、process_group=None、注册辅助接口、自定义子进程组和多 bucket 场景。
  3. 验证 torch.float32torch.float16torch.bfloat16 的平均结果、shape、dtype、NPU设备和 torch._C.Future 返回契约,并覆盖 FP16 溢出边界、单 rank HCCL 正常调用以及缺少参数、空 bucket、非法进程组等异常场景。
  4. 对统一启动双进程测试的 _spawn helper 应用 skipIfUnsupportMultiNPU(WORLD_SIZE)。少于两张 NPU时跳过依赖双进程的 10 个测试,继续执行单 NPU契约和异常参数测试;具备至少两张 NPU时执行全部 12 个测试,避免 rank 1 使用非法设备索引。
  5. PyTorch 社区相关覆盖主要集中于 CUDA/NCCL,并包含 wrapper 和 ZeRO overlap 等组合调用。新增独立测试用于补齐 HCCL/NPU 下的输入、返回契约、边界值、组合配置、异常参数和单设备执行覆盖,为该 API 的 NPU行为提供回归保护。

【资料变更】

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

不涉及。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

结果:

test_allreduce_hook ... ok
test_allreduce_hook_custom_subgroup ... ok
test_allreduce_hook_future_and_dtypes ... ok
test_allreduce_hook_grad_is_view ... ok
test_allreduce_hook_grad_is_view_static_graph ... ok
test_allreduce_hook_invalid_arguments ... ok
test_allreduce_hook_multiple_buckets ... ok
test_allreduce_hook_none_pg ... ok
test_allreduce_hook_overflow_boundary ... ok
test_allreduce_hook_registration_helper ... ok
test_allreduce_hook_single_npu_contract ... ok
test_allreduce_hook_static_graph ... ok

----------------------------------------------------------------------
Ran 12 tests in 162.849s

OK

v2.11.0

执行命令:

python test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py -v

结果:

test_allreduce_hook ... ok
test_allreduce_hook_custom_subgroup ... ok
test_allreduce_hook_future_and_dtypes ... ok
test_allreduce_hook_grad_is_view ... ok
test_allreduce_hook_grad_is_view_static_graph ... ok
test_allreduce_hook_invalid_arguments ... ok
test_allreduce_hook_multiple_buckets ... ok
test_allreduce_hook_none_pg ... ok
test_allreduce_hook_overflow_boundary ... ok
test_allreduce_hook_registration_helper ... ok
test_allreduce_hook_single_npu_contract ... ok
test_allreduce_hook_static_graph ... ok

----------------------------------------------------------------------
Ran 12 tests in 163.894s

OK

v2.12.0

执行命令:

python test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py -v

结果:

test_allreduce_hook ... ok
test_allreduce_hook_custom_subgroup ... ok
test_allreduce_hook_future_and_dtypes ... ok
test_allreduce_hook_grad_is_view ... ok
test_allreduce_hook_grad_is_view_static_graph ... ok
test_allreduce_hook_invalid_arguments ... ok
test_allreduce_hook_multiple_buckets ... ok
test_allreduce_hook_none_pg ... ok
test_allreduce_hook_overflow_boundary ... ok
test_allreduce_hook_registration_helper ... ok
test_allreduce_hook_single_npu_contract ... ok
test_allreduce_hook_static_graph ... ok

----------------------------------------------------------------------
Ran 12 tests in 163.101s

OK

master

执行命令:

python test/distributed/algorithms/ddp_comm_hooks/test_allreduce_hook.py -v

结果:

test_allreduce_hook ... ok
test_allreduce_hook_custom_subgroup ... ok
test_allreduce_hook_future_and_dtypes ... ok
test_allreduce_hook_grad_is_view ... ok
test_allreduce_hook_grad_is_view_static_graph ... ok
test_allreduce_hook_invalid_arguments ... ok
test_allreduce_hook_multiple_buckets ... ok
test_allreduce_hook_none_pg ... ok
test_allreduce_hook_overflow_boundary ... ok
test_allreduce_hook_registration_helper ... ok
test_allreduce_hook_single_npu_contract ... ok
test_allreduce_hook_static_graph ... ok

----------------------------------------------------------------------
Ran 12 tests in 148.368s

OK

【CheckList】

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

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 的贡献)
-此账号已注销
26 天前 创建了 pull request,commit e1cd335c
atomgit-bot
atomgit-bot
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_viewstatic_graph、组合配置、Future 契约、多种浮点 dtype、FP16 溢出边界、自定义子进程组、多 bucket 以及异常入参等场景。

主要改动

  • 新增 TestAllreduceHook 测试类:包含 11 个测试方法,以未注册通信 hook 的 DDP 梯度为基准,验证 allreduce_hook 在各配置下的梯度结果一致性,支持 gradient_as_bucket_viewstatic_graphprocess_group=None 及注册辅助接口 (register_ddp_comm_hook) 的组合测试。
  • 新增 Future 契约与多 dtype 验证:通过自定义 _future_contract_hook 使用 torch._C.Future 检查 hook 返回值类型,并覆盖 torch.float32torch.float16torch.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 或错误类型参数时抛出 TypeErrorAttributeError
likedislike
atomgit-bot
atomgit-bot
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 环境下的测试正确性。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
ascend-robotascend-robot成员
26 天前 添加了label:ascend-cla/yes
此处折叠了93条消息 查看更多
ascend-robot
ascend-robot成员
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 >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan, Jesse]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
Jingwei Huang
Jingwei Huang成员
16 天前 评论:

/lgtm

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