Closed
[Usage]: 【API一致性任务】torch.distributed.algorithms.ddp_comm_hooks.powerSGD_hook.PowerSGDState.__getstate__ 纯 Python 接口验证说明 #4045
GokoAdd on Aug 15Closed at Sep 16
Aug 15 associated Kanban: FrameworkPTAdapter 版本issue看板
Aug 15 Added label: usage
Aug 15 Modify description of issue
Aug 15 Associated pull request
Aug 15 Modify description of issue
Aug 17 Added label: help-wantedevent: api-consistencybot-triaged
TorchNPU-Bot
Aug 17 Comments:
Aug 17 Comments:
检测到社区任务相关 issue,自动添加标签:event: api-consistency、help-wanted


Sep 16 Close issue
Sep 16 changed custom state from TODO to DONE
Sep 16 Added label: resolved
在提交新问题之前,请确保您已经在社区中搜索过相关问题,并使用了社区中提供的资源/工具后,仍未找到满意的解决方式。
环境信息
使用场景及问题
来源任务: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,但没有直接验证以下序列化契约:PowerSGDState.__slots__ - {"process_group"};process_group是否从结果中排除;因此,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_upstreampatch,也不提交测试 PR;通过本 Issue 提供源码分析和运行验证结果。运行验证
验证环境:
安装的对应软件版本及验证结果:
三个版本均确认:
验证同时构造 CPU 状态和 NPU 状态,确认状态中的 Tensor 不会被
__getstate__错误迁移,且返回值符合官方实现契约。运行过程中出现的:
是 PyTorch 官方实现主动输出的预期提示,正好对应
process_group排除语义,不是运行失败。v2.12.0 环境中的 32-byte padding warning 是当前 CANN/SoC 组合的内存分配提示,也不影响验证结论。资料检查
当前 master 下以下正式资料文件均已列出
PowerSGDState,但没有单独列出其公开方法PowerSGDState.__getstate__:该资料缺失将通过一个独立、仅面向 master 的资料 PR 补齐。资料 PR 只更新本 API 对应的上述四份资料文件,不包含测试、NPU 适配 patch 或其他 API 内容。PR 创建后将在本 Issue 中补充链接。
资料 PR:https://gitcode.com/Ascend/pytorch/pull/44658
结论
欢迎加入社区,感谢您对社区的贡献 🎉!