Files
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-08-13 10:00:19 +08:00
2026-05-25 14:25:16 +08:00
2025-07-15 01:28:34 +00:00
2026-05-21 11:37:41 +08:00
2024-12-23 13:12:41 +00:00