Closed
[Usage]: 【API一致性任务】torch.distributed.algorithms.ddp_comm_hooks.powerSGD_hook.PowerSGDState.__getstate__ 纯 Python 接口验证说明 #4045
GokoAdd on  Aug 15Closed at  Sep 16
Goko
Goko
Aug 15 Create

在提交新问题之前,请确保您已经在社区中搜索过相关问题,并使用了社区中提供的资源/工具后,仍未找到满意的解决方式。

环境信息

envi-1.png

使用场景及问题

来源任务:https://gitcode.com/Ascend/pytorch/issues/2721

API 功能

torch.distributed.algorithms.ddp_comm_hooks.powerSGD_hook.PowerSGDState.__getstate__ 用于生成 PowerSGDState 的可保存状态字典。该方法遍历 PowerSGDState.__slots__,保留除 process_group 以外的状态;process_group 关联运行时分布式通信资源,不进入序列化结果。

PyTorch 官方用例情况

PyTorch v2.7.1、v2.11.0、v2.12.0 和 main 均已实现目标方法。对各版本官方 test/ 目录进行精确检索后,未发现直接调用或完整验证 PowerSGDState.__getstate__ 的测试用例。

官方测试中的 test/distributed/test_c10d_common.py 和 test/distributed/test_c10d_nccl.py 会构造或使用 PowerSGDState,但没有直接验证以下序列化契约:

  1. 返回值是否为字典;
  2. 返回键是否严格等于 PowerSGDState.__slots__ - {"process_group"};
  3. process_group 是否从结果中排除;
  4. 状态字典中的设备 Tensor 是否保持设备和数值;
  5. 方法是否拒绝额外参数。

因此,PyTorch 官方社区对该方法缺少直接、完整的用例覆盖。

API 类型与新增用例判断

目标方法的官方实现如下:

def __getstate__(self):
    r"""
    Return a ``Dict[str, Any]`` which will be pickled and saved.
    ``process_group`` is not serializable and excluded from a returned state.
    """
    logger.warning("NOTE: Process group is not serializable and excluded from a saved state.")
    return {slot: getattr(self, slot) for slot in self.__slots__ if slot != "process_group"}

对应位置为:v2.7.1、v2.11.0 的 torch/distributed/algorithms/ddp_comm_hooks/powerSGD_hook.py:275,以及 v2.12.0、main 的同文件约第 276 行。

该方法是纯 Python 状态整理接口,只读取对象槽位并返回 Python 字典;方法自身不调用 HCCL、分布式 collective、NPU 算子或其他设备相关接口。状态中可以保存 NPU Tensor,但本方法仅保持对象引用,不执行 Tensor 计算或设备迁移。

因此本 API 归入1.2①:PyTorch 官方社区无用例或覆盖不完整,且 API 为纯 Python、非硬件接口。

按照要求,本任务不新增测试用例,不提交 test_upstream patch,也不提交测试 PR;通过本 Issue 提供源码分析和运行验证结果。

运行验证

验证环境:

操作系统:Ubuntu 22.04.5 LTS
系统架构:aarch64
昇腾硬件:Ascend 910B2,单卡,Health 为 OK
npu-smi:25.5.2
CANN:9.0.0
Python:3.11.15

安装的对应软件版本及验证结果:

PyTorch torch-npu NPU 可用 结果
2.7.1+cpu 2.7.1.post4 True PASS
2.11.0+cpu 2.11.0rc1 True PASS
2.12.0+cpu 2.12.0.rc1 True PASS

三个版本均确认:

API callable: True
pure Python function: True
source contains collective call: False
return type: dict
process_group excluded: True
state keys match __slots__: True
CPU tensor device preserved: cpu
NPU tensor device preserved: npu:0
NPU tensor value: [1.0, 2.0]
invalid arguments rejected: True
RESULT: PASS

验证同时构造 CPU 状态和 NPU 状态,确认状态中的 Tensor 不会被 __getstate__ 错误迁移,且返回值符合官方实现契约。

运行过程中出现的:

NOTE: Process group is not serializable and excluded from a saved state.

是 PyTorch 官方实现主动输出的预期提示,正好对应 process_group 排除语义,不是运行失败。v2.12.0 环境中的 32-byte padding warning 是当前 CANN/SoC 组合的内存分配提示,也不影响验证结论。

1-271.png

1-2110.png

1-2120.png

资料检查

当前 master 下以下正式资料文件均已列出 PowerSGDState,但没有单独列出其公开方法 PowerSGDState.__getstate__:

docs/zh/api/native_api/pytorch_2-7-1/DDP-Communication-Hooks.md
docs/zh/api/native_api/pytorch_2-11-0/DDP-Communication-Hooks.md
docs/zh/api/native_api/pytorch_2-12-0/DDP-Communication-Hooks.md
docs/zh/api/native_api/pytorch_2-13-0/DDP-Communication-Hooks.md

该资料缺失将通过一个独立、仅面向 master 的资料 PR 补齐。资料 PR 只更新本 API 对应的上述四份资料文件,不包含测试、NPU 适配 patch 或其他 API 内容。PR 创建后将在本 Issue 中补充链接。
资料 PR:https://gitcode.com/Ascend/pytorch/pull/44658

结论

检查项 结论
TorchNPU 环境可调用目标 API ✔
返回状态与 PyTorch 官方契约一致 ✔
状态中的 NPU Tensor 设备和数值保持正确 ✔
方法自身涉及硬件计算或 collective ✘
PyTorch 官方存在完整直接用例 ✘
需要新增测试用例或 NPU 适配 patch ✘
需要修改 TorchNPU API 实现 ✘
需要补充正式资料 ✔

欢迎加入社区,感谢您对社区的贡献 🎉!

likedislike
GokoGoko
Aug 15 associated Kanban: FrameworkPTAdapter 版本issue看板
ascend-robotascend-robotCollaborators
Aug 15 Added label: usage
GokoGoko
Aug 15 Modify description of issue
GokoGoko
Aug 15 Associated pull request
GokoGoko
Aug 15 Modify description of issue
TorchNPU-BotTorchNPU-BotCollaborators
Aug 17 Added label: help-wantedevent: api-consistencybot-triaged
TorchNPU-Bot
TorchNPU-BotCollaborators
Aug 17 Comments:

检测到社区任务相关 issue,自动添加标签:event: api-consistency、help-wanted

likedislike
ascend-robotascend-robotCollaborators
Sep 16 Close issue
ascend-robotascend-robotCollaborators
Sep 16 changed custom state from TODO to DONE
ascend-robotascend-robotCollaborators
Sep 16 Added label: resolved