qq_44188726
b4d0da5477
Add test for fp16_compress_hook
Co-authored-by: qq_44188726<16634253534@163.com>
# message auto-generated for no-merge-commit merge:
!42829 merge fp16_compress_hook-master into master
Add test for fp16_compress_hook
Created-by: qq_44188726
Commit-by: qq_44188726
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->
# 【合入来源】
关联任务issue: https://gitcode.com/Ascend/pytorch/issues/3132
- [ ] 需求
- [ ] 问题单
- [x] issue/工单
- [ ] 重构优化
- [ ] 资料更新
# 【修改方案】
torch.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_hook
PyTorch 官方社区在 test/distributed/test_c10d_nccl.py 中有相关测试,但依赖 NCCL 后端和多卡环境;该 API 在 NPU 上无直接独立测试,因此自行编写用例提交到 test 目录。
本用例在 NPU 上验证 fp16_compress_hook 的行为:
- fp16 压缩(转 fp16 并按 world size 均分)→ dist.all_reduce → 解压回原始 dtype;
- process_group=None 时回退 dist.group.WORLD;
- world_size > 1 时除以 world size,并断言 all_reduce 的 group 与 async_op=True 调用参数;
- tuple 与 GradBucket 两种 bucket 输入形态;
- dist.all_reduce 异常传播。
新增 test/distributed/test_fp16_compress_hook.py。
# 【资料变更】
不涉及修改。该 API 官方文档已存在:docs/zh/api/native_api/pytorch_2-{7-1,11-0,12-0,13-0}/DDP-Communication-Hooks.md
中均有 torch.distributed.algorithms.ddp_comm_hooks.default_hooks.fp16_compress_hook 条目,
支持状态为 Atlas A2/A3 是、Ascend 950DT 否,且无「限制与说明」条目、无 dtype 表述问题,故无需修改文档。
# 【接口变更】
不涉及
# 【功能介绍】
fp16_compress_hook(process_group, bucket):DDP 通信 hook,将梯度张量转成 float16 并除以进程组大小后,通过 dist.all_reduce 做聚合(async_op=True),再把结果解压回原始 dtype。process_group 为 None 时使用 dist.group.WORLD;bucket 支持 GradBucket 或 tensor 元组两种输入形态。
# 【功能验证】
test/distributed/test_fp16_compress_hook.py (fp16_compress_hook)
- test_fp16_compress_hook_is_callable — fp16_compress_hook 可调用
- test_fp16_compress_hook_dtype_conversion — fp16 压缩→allreduce→解压,dtype 行为正确(GradBucket 输入)
- test_fp16_compress_hook_process_group_none — process_group=None 回退 dist.group.WORLD,async_op=True
- test_fp16_compress_hook_allreduce_world_size — world_size>1 除以 world size,断言 group/async_op 参数与结果
- test_fp16_compress_hook_tuple_bucket — tuple bucket 输入形态
- test_fp16_compress_hook_allreduce_exception — dist.all_reduce 异常向外传播
运行命令:
python -m pytest /workspace/user_data/pytorch-master/test/distributed/test_fp16_compress_hook.py -xvs
```
=========================================== test session starts ===========================================
platform linux -- Python 3.12.13, pytest-8.3.2, pluggy-1.6.0 -- /workspace/envs/torch-npu-test/bin/python
cachedir: .pytest_cache
rootdir: /workspace/user_data/pytorch-master
configfile: pyproject.toml
plugins: xdist-3.6.1
collected 6 items
../workspace/user_data/pytorch-master/test/distributed/test_fp16_compress_hook.py::TestFp16CompressHook::test_fp16_compress_hook_allreduce_exception PASSED
../workspace/user_data/pytorch-master/test/distributed/test_fp16_compress_hook.py::TestFp16CompressHook::test_fp16_compress_hook_allreduce_world_size PASSED
../workspace/user_data/pytorch-master/test/distributed/test_fp16_compress_hook.py::TestFp16CompressHook::test_fp16_compress_hook_dtype_conversion PASSED
../workspace/user_data/pytorch-master/test/distributed/test_fp16_compress_hook.py::TestFp16CompressHook::test_fp16_compress_hook_is_callable PASSED
../workspace/user_data/pytorch-master/test/distributed/test_fp16_compress_hook.py::TestFp16CompressHook::test_fp16_compress_hook_process_group_none PASSED
../workspace/user_data/pytorch-master/test/distributed/test_fp16_compress_hook.py::TestFp16CompressHook::test_fp16_compress_hook_tuple_bucket PASSED
============================================ 6 passed in 8.00s ============================================
```
# 【CheckList】
> PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]
- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [x] PR持续集成流水线(CI)执行通过,代码检查无异常
See merge request: Ascend/pytorch!42829
2026-08-31 22:55:18 +08:00
..
2026-05-28 16:52:51 +08:00
2026-08-17 22:04:32 +08:00
2024-06-04 07:07:08 +00:00
2026-06-04 21:05:44 +08:00
2026-08-06 14:15:41 +08:00
2026-08-19 12:05:06 +08:00
2026-08-13 10:00:19 +08:00
2026-05-25 14:25:16 +08:00
2025-07-15 01:28:34 +00:00
2026-08-21 10:38:22 +08:00
2026-08-28 10:55:42 +08:00
2026-05-21 11:37:41 +08:00
2026-08-18 15:38:40 +08:00
2026-05-21 16:41:18 +08:00
2026-05-18 15:08:46 +08:00
2026-06-23 21:03:20 +08:00
2026-03-30 16:04:56 +08:00
2026-07-15 09:38:27 +08:00
2026-06-22 17:27:40 +08:00
2026-07-15 09:38:27 +08:00
2026-08-20 09:04:30 +08:00
2024-08-09 02:53:55 +00:00
2024-08-20 12:40:31 +00:00
2026-06-23 21:03:20 +08:00
2026-08-10 11:41:04 +08:00
2023-11-04 01:33:25 +00:00
2025-12-05 17:11:36 +08:00
2026-08-31 22:15:04 +08:00
2025-12-05 17:11:36 +08:00
2024-11-05 16:07:28 +00:00
2026-08-13 11:33:02 +08:00
2024-08-19 02:09:48 +00:00
2026-01-22 21:34:59 +08:00
2023-11-30 01:09:40 +00:00
2026-08-11 14:34:04 +08:00
2026-08-05 15:12:18 +08:00
2026-05-21 16:41:18 +08:00
2026-08-28 20:36:26 +08:00
2026-05-21 16:41:18 +08:00
2026-08-17 22:04:32 +08:00
2026-06-22 16:47:27 +08:00
2026-08-31 22:55:18 +08:00
2026-05-21 14:23:28 +08:00
2024-12-23 13:12:41 +00:00
2026-05-13 20:17:55 +08:00
2026-06-22 16:47:27 +08:00
2026-08-11 15:51:28 +08:00
2026-05-21 16:41:18 +08:00
2026-08-06 20:07:13 +08:00
2024-06-04 07:07:08 +00:00
2026-05-28 16:52:51 +08:00
2025-05-28 01:20:26 +00:00
2026-06-05 14:24:47 +08:00
2026-05-21 16:41:18 +08:00
2026-05-21 16:41:18 +08:00
2025-01-21 09:47:12 +00:00
2025-01-21 09:47:12 +00:00
2026-08-21 10:38:22 +08:00
2024-09-10 07:21:59 +00:00
2026-08-20 09:04:30 +08:00
2026-08-20 09:04:30 +08:00
2026-08-20 09:04:30 +08:00
2026-08-20 09:04:30 +08:00
2026-01-07 09:36:59 +08:00
2025-11-11 15:52:40 +08:00
2025-11-11 15:52:40 +08:00
2025-04-02 01:46:04 +00:00
2026-05-21 16:41:18 +08:00
2025-03-11 11:24:02 +00:00
2026-08-18 15:38:40 +08:00
2026-05-21 16:41:18 +08:00
2026-08-17 22:00:48 +08:00
2026-05-21 16:41:18 +08:00
2026-05-21 16:41:18 +08:00
2025-11-27 15:56:17 +08:00