已合并
test(utils): adapt torch.utils.checkpoint.checkpoint, torch.utils.checkpoint.checkpoint_sequential and torch.overrides.is_tensor_like on NPU #39483
test(utils): adapt torch.utils.checkpoint.checkpoint, torch.utils.checkpoint.checkpoint_sequential and torch.overrides.is_tensor_like on NPU #39483
已合并
yulin520创建于 6月27日
yulin520
yulin520
6月27日

【合入来源】

如有社区issue,请关联issue链接 请勿携带内部流程信息(需求链接、问题单、内部issue等)

关联 Issue:https://gitcode.com/Ascend/pytorch/issues/2529
关联 Issue:#2529

关联任务:torch.overrides.is_tensor_like、torch.utils.checkpoint.checkpoint、torch.utils.checkpoint.checkpoint_sequential API 一致性验证。

本 PR 对 v2.9.0 分支补充上述 API 在 NPU 场景下的上游测试适配。

【修改方案】

本 PR 仅涉及 test_upstream 上游测试 patch 适配,不修改 torch_npu 功能实现代码,不改变已有 API 行为。

修改文件:

  • test_upstream/test/test_autograd.py.patch
  • test_upstream/test/dynamo/test_misc.py.patch

具体修改如下:

  1. 在 test_upstream/test/test_autograd.py.patch 中补充 torch.utils.checkpoint.checkpoint_sequential 相关用例的 NPU 适配:

    • 将 torch.utils.checkpoint.checkpoint_sequential 相关测试输入 Tensor 从 CPU 放置到 NPU;
    • 将 torch.utils.checkpoint.checkpoint_sequential 测试中使用的 torch.nn.Linear 模块迁移到 NPU;
    • 验证 torch.utils.checkpoint.checkpoint_sequential 在 NPU Tensor 输入下能够正常前向、反向传播。
  2. 在 test_upstream/test/dynamo/test_misc.py.patch 中补充 torch.overrides.is_tensor_like 相关用例的 NPU 适配:

    • 将 torch.overrides.is_tensor_like 测试输入 Tensor 放置到 NPU;
    • 验证 NPU Tensor 场景下 torch.overrides.is_tensor_like 返回结果正确。
  3. 保留现有 torch.utils.checkpoint.checkpoint 相关 NPU 适配内容:

    • 本次修改未改变 torch.utils.checkpoint.checkpoint API 的功能实现;
    • 仅在本地验证中同时覆盖 torch.utils.checkpoint.checkpoint、torch.utils.checkpoint.checkpoint_sequential 和 torch.overrides.is_tensor_like,确保三类目标 API 在 NPU 场景下均可正常运行。

【资料变更】

不涉及。

本 PR 仅补充上游测试用例在 NPU 场景下的适配,不新增或修改用户可见资料。相关 API 在目标版本资料中已有说明,因此本任务不需要新增 doc PR。

【接口变更】

不涉及。

本 PR 不新增、删除或修改客户可见接口,不涉及跨代码仓接口变更,不改变 torch.utils.checkpoint.checkpoint、torch.utils.checkpoint.checkpoint_sequential 或 torch.overrides.is_tensor_like 的调用方式和返回语义。

【功能验证】

已在 Ascend 910B4 NPU 环境完成 v2.9.0 对应分支的本地运行验证。

验证环境:

  • 操作系统:Ubuntu 22.04.5 LTS / aarch64
  • CANN:cann-9.1.0-beta.1
  • NPU 设备:Ascend910B4
  • 目标分支:v2.9.0
  • 测试分支:test-checkpoint-overrides-npu-v290
  • torch 版本:2.9.0
  • torch_npu 版本:2.9.0.post2
  • NPU 可用性:torch.npu.is_available() == True

验证内容:

  1. torch.utils.checkpoint.checkpoint_sequential

    • NPU Tensor 输入;
    • NPU Linear 模块;
    • 前向计算和反向传播均通过。
  2. torch.overrides.is_tensor_like

    • NPU Tensor 输入;
    • torch.overrides.is_tensor_like 返回结果为 True。
  3. torch.utils.checkpoint.checkpoint

    • NPU Tensor 输入;
    • 前向计算和反向传播均通过。

本地验证结果:

  • torch.utils.checkpoint.checkpoint_sequential NPU validation PASSED
  • torch.overrides.is_tensor_like NPU validation PASSED
  • torch.utils.checkpoint.checkpoint NPU validation PASSED
  • RUNTIME VALIDATION PASSED

测试自验证截图:
2.9.0.png

Patch 生成与检查说明:

本 PR 中的 patch 基于对应版本的 PyTorch 社区源码生成。已有 patch 时,先在对应版本 PyTorch 社区源码中应用原 patch,再根据代码审查意见修改社区源码文件,将 torch.ones(10) 调整为 torch.ones(10).npu(),避免默认生成 CPU Tensor 后与 NPU Tensor x 相加导致设备不一致。修改完成后,通过 git diff 重新生成 patch 文件,未在生成后直接编辑 patch 文件。

已完成以下检查:

  • 已执行 patch 应用检查;
  • 已检查 patch 不包含整文件删除;
  • 已检查 patch 不包含冲突标记;
  • 已检查本 PR 仅包含目标 patch 文件修改;
  • 已确认 torch.ones(10) 已在社区源码中迁移至 NPU,并通过 git diff 重新生成对应 patch;
  • 已在匹配版本的 torch / torch_npu 运行时环境中完成本地 NPU 复验。

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 yulin520 的贡献)
yulin520yulin520
6月27日 创建了 pull request,commit cba0413e
ascend-robotascend-robot成员
6月27日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
6月27日 评论:

CLA Signature Pass

weixin_65332823, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robotascend-robot成员
6月27日 添加了label:needs-issue
此处折叠了121条消息 查看更多
ascend-robotascend-robot成员
7月7日 合入了pull request
ascend-robot
ascend-robot成员
7月7日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
7月7日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
7月7日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
7月7日 评论:
流水线 pytorch_gitcode_PR_multiVersion#12082 [ commitID:7ab19c53 ] 已完成
likedislike