JfanLiu ccfdd97783 test(utils): add checkpoint API coverage tests on NPU
Co-authored-by: Jinfan Liu<1300083451@qq.com>



# message auto-generated for no-merge-commit merge:
!37241 merge test-utils-checkpoint-weak-npu-master into master

test(utils): add checkpoint API coverage tests on NPU

Created-by: JfanLiu
Commit-by: Jinfan Liu
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->

# 【合入来源】
> <font color="red">**https://gitcode.com/Ascend/pytorch/issues/2026**</font>
> <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font>

[x] issue/工单

# 【修改方案】
一、API功能说明
```
本 PR 补充 torch.utils.checkpoint 弱覆盖 API 的直接验证用例:
- torch.utils.checkpoint.SelectiveCheckpointContext:selective checkpoint policy function 接收的上下文对象,用于区分 forward/recompute 阶段;2.12.0 起还可携带 op_output。
- torch.utils.checkpoint.detach_variable:reentrant checkpoint 内部用于 detach 输入 Tensor 并保持 requires_grad 状态的辅助函数。
```

二、测试用例完备性说明
```
PyTorch 官方社区 test/test_autograd.py 已通过 checkpoint 调用链间接覆盖上述 API,但缺少直接、聚焦的 API 行为断言。本 PR 按 1.3 类补充 torch-npu 自定义测试文件 test/test_utils_checkpoint_api.py。

1. torch.utils.checkpoint.detach_variable 直接覆盖:
   - test_detach_variable_keeps_device_and_requires_grad 直接调用 torch.utils.checkpoint.detach_variable((x, y, marker))。
   - x 和 y 均为 NPU Tensor,覆盖返回 Tensor 保持 NPU device、requires_grad 与原输入一致、grad_fn 被断开并成为 leaf Tensor,非 Tensor 对象透传。
   - test_detach_variable_rejects_non_tuple_input 覆盖非 tuple 输入的 RuntimeError 分支。

2. torch.utils.checkpoint.SelectiveCheckpointContext 直接覆盖:
   - test_selective_checkpoint_context_direct_attributes 直接实例化 torch.utils.checkpoint.SelectiveCheckpointContext,断言对象类型和 is_recompute 属性;对支持 op_output 的版本,额外断言 op_output 指向 NPU Tensor 输出。
   - test_selective_checkpoint_context_passed_to_policy_fn 通过 create_selective_checkpoint_contexts + checkpoint(..., use_reentrant=False, context_fn=...) 捕获 policy_fn 收到的 ctx,断言 forward/recompute 两阶段均收到 SelectiveCheckpointContext,并验证上下文来自 NPU Tensor 的 selective checkpoint 运行路径。

上述新增用例把原来的调用链弱覆盖补充为直接 API 行为覆盖;其中涉及 Tensor 的路径均使用 torch.accelerator.current_accelerator() 获取 NPU 设备并在 NPU Tensor 上执行。
```

三、NPU适配方案
```
1. API 源码无需修改:torch.utils.checkpoint.SelectiveCheckpointContext 与 torch.utils.checkpoint.detach_variable 均复用 PyTorch Python 层实现,不新增 NPU kernel,不改变接口语义。
2. 测试新增路径:新增 test/test_utils_checkpoint_api.py,文件聚焦 checkpoint API 直接行为,可后续扩展同模块 API。
3. 分支提交范围:新增测试文件提交到 2.7.1、2.9.0、2.10.0、2.11.0、2.12.0 以及 master。
```

四、变更文件
- `test/test_utils_checkpoint_api.py`

# 【资料变更】
> 已检查资料支持情况,结论如下:
>
> checkpoint:
> - 公开 API,在文档中记录:torch.utils.checkpoint.CheckpointPolicy、torch.utils.checkpoint.SelectiveCheckpointContext、torch.utils.checkpoint.create_selective_checkpoint_contexts、torch.utils.checkpoint.set_checkpoint_debug_enabled。对应资料 PR:https://gitcode.com/Ascend/pytorch/merge_requests/36231
> - 内部函数,不在文档中记录:torch.utils.checkpoint.detach_variable。

# 【接口变更】
> 不涉及。API 源码无修改,不新增、删除或变更对外接口。

# 【功能验证】
> 已在 master 对应 torch-npu 环境执行新增用例,结果如下;master 使用 2.12.0 验证环境执行。

## 验证环境

- 操作系统:Ubuntu / Linux aarch64
- 昇腾硬件:910B2
- CANN 软件版本:8.3.RC1
- 验证环境:
  - 2.7.1:torch 2.7.1+cpu / torch-npu 2.7.1.post3
  - 2.9.0:torch 2.9.0+cpu / torch-npu 2.9.0.post1
  - 2.10.0:torch 2.10.0+cpu / torch-npu 2.10.0rc3
  - 2.11.0:torch 2.11.0+cpu / torch-npu 2.11.0rc1
  - 2.12.0 / master 验证:torch 2.12.0+cpu / torch-npu 2.12.0.rc1

## 运行命令与结果

```bash
python test/test_utils_checkpoint_api.py
Ran 4 tests in 1.560s
OK
```

# 【CheckList】

- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [ ] PR持续集成流水线(CI)执行通过,代码检查无异常


See merge request: Ascend/pytorch!37241
2026-06-05 14:33:50 +08:00
2024-07-04 02:58:08 +00:00
2026-02-12 14:35:09 +08:00
2026-05-26 20:07:05 +08:00
2025-07-17 07:09:05 +00:00
2026-06-05 10:38:45 +08:00
2025-07-04 03:53:27 +00:00
2025-09-15 09:27:40 +08:00
2026-05-25 14:25:16 +08:00
2026-05-09 16:45:13 +08:00
2026-05-14 10:52:47 +08:00
2026-05-25 11:54:48 +08:00
2025-09-15 09:27:40 +08:00
2026-05-14 10:52:47 +08:00

Ascend Extension for PyTorch

Overview

This repository develops the Ascend Extension for PyTorch named torch_npu to adapt Ascend NPU to PyTorch so that developers who use the PyTorch can obtain powerful compute capabilities of Ascend AI Processors.

Ascend is a full-stack AI computing infrastructure for industry applications and services based on Huawei Ascend processors and software. For more information about Ascend, see Ascend Community.

Installation

From Binary

Provide users with wheel package to quickly install torch_npu. Before installing torch_npu, complete the installation of CANN according to Ascend Auxiliary Software. To obtain the CANN installation package, refer to the CANN Installation.

  1. Install PyTorch

Install PyTorch through pip.

For Aarch64:

pip3 install torch==2.1.0

For x86:

pip3 install torch==2.1.0+cpu  --index-url https://download.pytorch.org/whl/cpu
  1. Install torch-npu dependencies

Run the following command to install dependencies.

pip3 install pyyaml
pip3 install setuptools

If the installation fails, use the download link or visit the PyTorch official website to download the installation package of the corresponding version.

OS arch Python version link
x86 Python3.8 link
x86 Python3.9 link
x86 Python3.10 link
x86 Python3.11 link
aarch64 Python3.8 link
aarch64 Python3.9 link
aarch64 Python3.10 link
aarch64 Python3.11 link
  1. Install torch-npu
pip3 install torch-npu==2.1.0.post17

From Source

In some special scenarios, users may need to compile torch-npu by themselves.Select a branch in table Ascend Auxiliary Software and a Python version in table PyTorch and Python Version Matching Table first. The docker image is recommended for compiling torch-npu through the following steps(It is recommended to mount the working path only and avoid the system path to reduce security risks.), the generated .whl file path is ./dist/. Note that gcc version has the following constraints if you try to compile without using docker image: we recommend to use gcc 10.2 for ARM and gcc 9.3.1 for X86.

  1. Clone torch-npu

    git clone https://github.com/ascend/pytorch.git -b 2.1.0-7.2.0 --depth 1
    
  2. Build Docker Image

    cd pytorch/ci/docker/{arch} # {arch} for X86 or ARM
    docker build -t manylinux-builder:v1 .
    
  3. Enter Docker Container

    docker run -it -v /{code_path}/pytorch:/home/pytorch manylinux-builder:v1 bash
    # {code_path} is the torch_npu source code path
    
  4. Compile torch-npu

    Take Python 3.8 as an example.

    cd /home/pytorch
    bash ci/build.sh --python=3.8
    

    Use --torch=<version> to build against a specific PyTorch version (supported: 2.10.0, 2.11.0, 2.12.0). The installed PyTorch must match the specified version.

    bash ci/build.sh --python=3.8 --torch=2.10.0
    

Tips

If you would like to compile with new C++ ABI, then first run this command, at this point, the recommended compilation environment is same to community torch package: glibc 2.28, gcc 11.2.1

export _GLIBCXX_USE_CXX11_ABI=1

Meanwhile, we support configuring -fabi-version using the following variables,require consistency with the community torch package

export _ABI_VERSION=16

Getting Started

Prerequisites

Initialize CANN environment variable by running the command as shown below.

# Default path, change it if needed.
source /usr/local/Ascend/ascend-toolkit/set_env.sh

Quick Verification

You can quickly experience Ascend NPU by the following simple examples.

import torch
- import torch_npu # No longer needed in torch_npu 2.5.1 and later versions

x = torch.randn(2, 2).npu()
y = torch.randn(2, 2).npu()
z = x.mm(y)

print(z)

User Manual

Refer to API of Ascend Extension for PyTorch for more detailed information.

PyTorch and Python Version Matching Table

PyTorch Version Python Version
PyTorch1.11.0 Python3.7.x(>=3.7.5),Python3.8.x,Python3.9.x,Python3.10.x
PyTorch2.1.0 Python3.8.x,Python3.9.x,Python3.10.x,Python3.11.x
PyTorch2.2.0 Python3.8.x,Python3.9.x,Python3.10.x
PyTorch2.3.1 Python3.8.x,Python3.9.x,Python3.10.x,Python3.11.x
PyTorch2.4.0 Python3.8.x,Python3.9.x,Python3.10.x,Python3.11.x
PyTorch2.5.1 Python3.9.x,Python3.10.x,Python3.11.x
PyTorch2.6.0 Python3.9.x,Python3.10.x,Python3.11.x
PyTorch2.7.1 Python3.9.x,Python3.10.x,Python3.11.x
PyTorch2.8.0 Python3.9.x,Python3.10.x,Python3.11.x

Ascend Auxiliary Software

PyTorch Extension versions follow the naming convention {PyTorch version}-{Ascend version}, where the former represents the PyTorch version compatible with the PyTorch Extension, and the latter is used to match the CANN version. The detailed matching is as follows:

CANN Version Supported PyTorch Version Supported Extension Version Github Branch
CANN 8.5.0 2.9.0 2.9.0 v2.9.0-7.3.0
2.8.0 2.8.0.post2 v2.8.0-7.3.0
2.7.1 2.7.1.post2 v2.7.1-7.3.0
2.6.0 2.6.0.post5 v2.6.0-7.3.0
CANN 8.3.RC1 2.8.0 2.8.0 v2.8.0-7.2.0
2.7.1 2.7.1 v2.7.1-7.2.0
2.6.0 2.6.0.post3 v2.6.0-7.2.0
2.1.0 2.1.0.post17 v2.1.0-7.2.0
CANN 8.2.RC1 2.6.0 2.6.0 v2.6.0-7.1.0
2.5.1 2.5.1.post1 v2.5.1-7.1.0
2.1.0 2.1.0.post13 v2.1.0-7.1.0
CANN 8.1.RC1 2.5.1 2.5.1 v2.5.1-7.0.0
2.4.0 2.4.0.post4 v2.4.0-7.0.0
2.3.1 2.3.1.post6 v2.3.1-7.0.0
2.1.0 2.1.0.post12 v2.1.0-7.0.0
CANN 8.0.0 2.4.0 2.4.0.post2 v2.4.0-6.0.0
2.3.1 2.3.1.post4 v2.3.1-6.0.0
2.1.0 2.1.0.post10 v2.1.0-6.0.0
CANN 8.0.RC3 2.4.0 2.4.0 v2.4.0-6.0.rc3
2.3.1 2.3.1.post2 v2.3.1-6.0.rc3
2.1.0 2.1.0.post8 v2.1.0-6.0.rc3
CANN 8.0.RC2 2.3.1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5.0 1.5.0.post4 2.0.4.tr5
CANN 5.0.3 1.8.1 1.5.0.post3 2.0.3.tr5
CANN 5.0.2 1.5.0 1.5.0.post2 2.0.2.tr5

Hardware support

The Ascend training device includes the following models, all of which can be used as training environments for PyTorch models

Product series Product model
Atlas Training series products Atlas 800(model: 9000)
Atlas 800(model:9010)
Atlas 900 PoD(model:9000)
Atlas 300T(model:9000)
Atlas 300T Pro(model:9000)
Atlas A2 Training series products Atlas 800T A2
Atlas 900 A2 PoD
Atlas 200T A2 Box16
Atlas 300T A2

The Ascend inference device includes the following models, all of which can be used as inference environments for large models

Product series Product model
Atlas 800I A2 Inference product Atlas 800I A2

Pipeline Status

Due to the asynchronous development mechanism of upstream and downstream, incompatible modifications in upstream may cause some functions of torch_npu to be unavailable (only upstream and downstream development branches are involved, excluding stable branches). Therefore, we built a set of daily tasks that make it easy to detect relevant issues in time and fix them within 48 hours (under normal circumstances), providing users with the latest features and stable quality.

OS CANN Version(Docker Image) Upstream Branch Downstream Branch Period Status
openEuler 22.03 SP2 CANN 7.1 main master UTC 1200 daily Ascend NPU

Suggestions and Communication

Everyone is welcome to contribute to the community. If you have any questions or suggestions, you can submit Github Issues. We will reply to you as soon as possible. Thank you very much.

Branch Maintenance Policies

The version branches of AscendPyTorch have the following maintenance phases:

Status Duration Description
Planning 1-3 months Plan features.
Development 6-12 months Develop new features and fix issues, regularly release new versions. Different strategies are adopted for different versions of PyTorch, with a regular branch development cycle of 6 months and a long-term support branch development cycle of 12 months.
Maintained 1 year/3.5 years Regular Release branch for 1 year, Long Term Support branch maintenance for 3.5 years. Fix major issues, do not incorporate new features, and release patch versions based on the impact of fixed bugs.
End Of Life (EOL) N/A Do not accept any modification to a branch.

PyTorch Maintenance Policies

PyTorch Maintenance Policies Status Launch Date Subsequent Status EOL Date
2.9.0 Regular Release Development 2026/01/15 Expected to enter maintenance status from July 15, 2026 -
2.8.0 Regular Release Development 2025/10/15 Expected to enter maintenance status from March 15, 2026 -
2.7.1 Long Term Support Development 2025/10/15 Expected to enter maintenance status from October 15, 2026
2.6.0 Regular Release Development 2025/07/25 Expected to enter maintenance status from January 15, 2026 -
2.5.1 Regular Release Maintained 2024/11/08 Expected to enter maintenance free status from August 8, 2026
2.4.0 Regular Release Maintained 2024/10/15 Expected to enter maintenance free status from June 15, 2026
2.3.1 Regular Release Maintained 2024/06/06 Expected to enter maintenance free status from June 7, 2026
2.2.0 Regular Release EOL 2024/04/01 2025/10/14
2.1.0 Long Term Support Maintained 2023/10/15 Expected to enter maintenance free status from December 30, 2026
2.0.1 Regular Release EOL 2023/7/19 2024/3/14
1.11.0 Long Term Support EOL 2023/4/19 2025/10/25
1.8.1 Long Term Support EOL 2022/4/10 2023/4/10
1.5.0 Long Term Support EOL 2021/7/29 2022/7/29

Reference Documents

For more detailed information on installation guides, model migration, training/inference tutorials, and API lists, please refer to the Ascend Extension for PyTorch on the HiAI Community.

Document Name Document Link
Installation Guide link
Network Model Migration and Training link
Operator Adaptation link
API List (PyTorch and Custom Interfaces) link

License

Ascend Extension for PyTorch has a BSD-style license, as found in the LICENSE file.

S
Description
PyTorch Adapter Plugin for Ascend NPU — TorchNPU 完整代码仓库(含 op-plugin 子模块)
Readme BSD-3-Clause
124 MiB
Languages
Python 80.3%
C++ 18.9%
C 0.3%
CMake 0.2%
Shell 0.2%