Closed
[Usage]: 【API一致性任务】8月社区任务第二期-Ascend for PyTorch API 一致性开发(28)API一致性说明:torch.utils.checkpoint.get_device_states 支持情况及资料补齐说明 #4089
Andy ZhangAdd on  Aug 17Closed at  Oct 13
Andy Zhang
Andy Zhang
Aug 17 Create

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

环境信息

  • Ubuntu 22.04
  • Python 3.11
  • PyTorch 2.10.0+cpu
  • torch-npu 2.10.0
  • CANN 9.0.0
  • Ascend 910B3

关联issue:https://gitcode.com/Ascend/pytorch-ecosystem/issues/149

使用场景及问题

API 功能

torch.utils.checkpoint.get_device_states 用于遍历输入参数中的非 CPU、
非 Meta 张量,收集张量所在设备的设备编号,并保存对应设备当前的 RNG 状态。

PyTorch 2.10.0 源码如下:

def get_device_states(*args) -> Tuple[List[int], List[torch.Tensor]]:
    # This will not error out if "arg" is a CPU tensor or a non-tensor type because
    # the conditionals short-circuit.
    fwd_device_ids = []

    def add_device_ids(arg) -> None:
        nonlocal fwd_device_ids
        if isinstance(arg, torch.Tensor) and arg.device.type not in {"cpu", "meta"}:
            fwd_device_ids.append(arg.get_device())
    tree_map(add_device_ids, args)

    fwd_device_states = []
    device_module = _get_device_module(_infer_device_type(*args))
    for device_id in fwd_device_ids:
        with device_module.device(device_id):
            fwd_device_states.append(device_module.get_rng_state())

    return fwd_device_ids, fwd_device_states

社区用例完整性

PyTorch 社区 checkpoint RNG 用例通过比较启用和不启用 checkpoint 时的梯度,
验证设备 RNG 状态能够正确保存和恢复。

TorchNPU 的 test/test_utils.py::TestCheckpoint::test_checkpoint_rng_cuda
已经完成 NPU 适配:

  • 输入张量运行在 NPU 上;
  • 使用 torch.npu.get_rng_state() 获取 NPU RNG 状态;
  • 使用 NPU Dropout 验证 checkpoint 前后的梯度一致性。

该用例会通过 checkpoint 调用链执行
torch.utils.checkpoint.get_device_states,能够验证其核心设备 RNG 状态采集功能。

现有用例运行结果:

test_checkpoint_rng_cuda (__main__.TestCheckpoint.test_checkpoint_rng_cuda) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.404s

OK

因此现有社区用例已完成 NPU 适配,无需新增测试用例。

NPU 支持验证

验证环境:

  • Ubuntu 22.04
  • Python 3.11
  • PyTorch 2.10.0+cpu
  • torch-npu 2.10.0
  • CANN 9.0.0
  • Ascend 910B3

验证范围:

  • NPU 张量设备编号采集;
  • NPU RNG 状态采集;
  • 嵌套参数中的 NPU 张量;
  • 空参数输入。

验证结果:

torch: 2.10.0+cpu
torch_npu: 2.10.0
accelerator: npu
in __all__: True
NPU tensor device: npu:0
device ids: [0]
state count: 1
nested device ids: [0]
empty result: [] []
RESULT: PASS

运行截图:
image.png
image.png

torch.utils.checkpoint.get_device_states 可以正确调用 TorchNPU 注册的
torch.npu.get_rng_state(),未发现设备硬编码或与 PyTorch 官方行为不一致的问题,
无需修改 TorchNPU 实现。

资料检查

torch.utils.checkpoint.get_device_states 位于
torch.utils.checkpoint.__all__ 中,不属于私有接口。

检查 master 分支的原生 API 支持资料后,以下文件均未收录该 API:

  • pytorch_2-7-1/torch-utils-checkpoint.md
  • pytorch_2-11-0/torch-utils-checkpoint.md
  • pytorch_2-12-0/torch-utils-checkpoint.md
  • pytorch_2-13-0/torch-utils-checkpoint.md

现已在上述四个版本资料中补充
torch.utils.checkpoint.get_device_states 的支持情况。

资料 PR:https://gitcode.com/Ascend/pytorch/pull/44713

结论

  • API 已支持在 NPU 环境运行;
  • 社区已有用例且已完成 NPU 适配;
  • 无需新增测试用例;
  • 无需修改 TorchNPU 实现;
  • 需要补充 API 支持资料,资料修改仅提交至 master。

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

likedislike
Andy ZhangAndy Zhang
Aug 17 associated Kanban: FrameworkPTAdapter 版本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
Aug 17 Added label: usage
Andy ZhangAndy Zhang
Aug 17 Associated pull request
Andy ZhangAndy Zhang
Aug 17 Modify description of issue
Andy ZhangAndy Zhang
Aug 17 Associated pull request
Andy ZhangAndy Zhang
Aug 17 Modify description of issue
群青世界群青世界Collaborators
Oct 13 changed custom state from TODO to DONE
群青世界群青世界Collaborators
Oct 13 Close issue
ascend-robotascend-robotCollaborators
Oct 13 Added label: resolved