Co-authored-by: yuaiming<yuaiming@h-partners.com> # message auto-generated for no-merge-commit merge: !47691 merge master into master fix: 优化 nightly 流水线:单卡编译、统一 torch 安装、常规测试并发提速 Created-by: y30037164 Commit-by: yuaiming Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 https://gitcode.com/Ascend/pytorch/issues/5012 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 > 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\ > 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容) # 【资料变更】 > 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及” # 【接口变更】 > 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及” # 【功能验证】 > 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\ > 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图 # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [ ] 代码注释完备,正确记录错误日志 - [ ] 代码实现进行了返回值、空指针等校验 - [ ] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [ ] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!47691
TorchNPU
PyTorch Adapter Plugin for Ascend NPU
English | 简体中文
Supported Versions · Installation · Quick Start · Compatibility · Support · Contributing · Docs · Community
Overview
As the core component of the Ascend for PyTorch community, TorchNPU is a deep learning adapter plugin built by Ascend for PyTorch, enabling the PyTorch framework to directly invoke Ascend NPU and providing developers with the powerful compute capabilities of Ascend AI processors.
Ascend is a full-stack AI computing infrastructure for industry applications and services based on Huawei Ascend AI processors and software. For more information about Ascend, see Ascend Community.
Key Features
TorchNPU fully inherits and reuses extensive mature capabilities from upstream PyTorch, and on this foundation provides deep adaptation and optimization for Ascend NPU. The diagram below illustrates the main functional modules of TorchNPU:
Key Modules:
- Basic Compute: Extensive support for PyTorch native APIs and custom APIs, covering mainstream AI scenarios with a consistent experience, enabling rapid model and algorithm implementation.
- Distributed: Supports accelerating distributed training via FSDP2. Core compute APIs support DTensor, along with collective communication primitives such as AllGather, AllReduce, and AllToAll, as well as point-to-point primitives like Send and Recv.
- Graph Mode: Significantly accelerates model training and inference through "dynamic graph capture + static graph optimization + efficient code generation," and supports offloading execution via NPUGraph. Supported in v2.6.0 and above.
- Debug & Tuning: Supports profiling analysis of compute, communication, and memory usage, with real-time communication anomaly monitoring via WatchDog.
- TorchNPU Core: Supports virtual memory management to reduce memory fragmentation, cross-stream memory reuse optimization in distributed scenarios, and integrates operators and device resources into PyTorch via PrivateUse1.
Latest News
- 📢 [2026-06-30] Advance notice on standardized naming conventions for Ascend for PyTorch community components. 🔗 Learn more
- 📢 [2026-04-30] TorchNPU 26.0.0 released, adding support for PyTorch 2.10.0, Python 3.13, P2P communication group dispatch, DTensor strategy extensions, and more. 🔗 Learn more
Supported PyTorch Versions
This branch adapts to multiple PyTorch versions from a single source tree. The PyTorch versions it currently supports are declared in version.txt, which is the single source of truth and is updated as new versions are adapted.
For the compatibility matrix of released packages (TorchNPU / PyTorch / CANN / Python), please refer to Compatibility.
Installation
From Binary
Take installing TorchNPU 2.12.0 as an example. Run the following commands for installation. For other versions, please refer to the community download page: TorchNPU Download.
Install CANN
Install CANN 9.1.0 and the NPU driver and firmware matching your hardware. Use a compatible Python version from the compatibility matrix. For detailed steps, please refer to the CANN Installation Guide.
Install PyTorch
Run the following command to install PyTorch 2.12.0:
pip install torch==2.12.0 --index-url https://download.pytorch.org/whl/cpu
Install TorchNPU
Run the following command to install TorchNPU 2.12.0:
pip install torch-npu==2.12.0
From Source
For detailed steps on compiling TorchNPU, please refer to the Source Installation Guide.
Quick Start
Initialize Environment
# Default path, modify according to your actual installation location
source /usr/local/Ascend/cann/set_env.sh
Run Example
import torch
import torch_npu
x = torch.randn(2, 2).npu()
y = torch.randn(2, 2).npu()
z = x.mm(y)
print(z)
Starting from TorchNPU 2.5.1,
import torch_npuis no longer mandatory (auto-registration occurs), but explicit import is still recommended to ensure device initialization.
If the following output appears, the execution is successful:
tensor([[-0.0515, 0.3664],
[-0.1258, -0.5425]], device='npu:0')
For complete model migration and training tutorials, please refer to the Quick Start Guide.
Community
The Ascend for PyTorch community is coordinated by the Technical Committee (TC), with Special Interest Groups (SIGs) responsible for development, maintenance, and community collaboration in specific technical areas. The table below introduces the TC and current SIGs. Click the corresponding links for details.
| TC / SIG Name | Description | Link |
|---|---|---|
| TC | Oversees technical direction and major technical decisions, SIG governance, and release reviews for Ascend for PyTorch. | 🔗 Learn more |
| Core SIG | Focuses on the development of the PyTorch core adaptation layer on the Ascend NPU platform, responsible for the design, implementation, and maintenance of the TorchNPU extension library and its operator plugin OpPlugin. |
🔗 Learn more |
| Distributed SIG | Dedicated to building efficient, easy-to-use, and scalable parallel training capabilities based on the PyTorch distributed training framework (torch.distributed) on the Ascend NPU hardware foundation, delivering extreme performance for LLM, multimodal, and reinforcement learning scenarios. | 🔗 Learn more |
| Graph Mode SIG | Focuses on core technologies such as Dynamo, Inductor, and NPUGraph, aiming to bridge the gap between "ease of use" and "high performance" through automated graph capture and compilation optimization. | 🔗 Learn more |
| Usability SIG | Dedicated to improving the usability experience of Ascend for PyTorch, including documentation, tutorials, examples, and more. | 🔗 Learn more |
Each SIG holds regular meetings, mailing lists, and contribution guides. Click the corresponding SIG links to view detailed contact information, goals, and participation guidelines. Everyone is welcome to contribute to the community. If you have any questions or suggestions, please submit GitHub Issues. We will reply as soon as possible. Thank you for your support.
Security Note
For system security hardening, recommended user configurations, and file permission controls for TorchNPU, please refer to the Security Note.
Disclaimer
This plugin is for debugging and development purposes only. Users are responsible for ensuring the security of input commands and properly controlling access to data generated during use. By using this plugin, you agree to and accept the above statement.
License
For the license of TorchNPU, please refer to the LICENSE file. For the license of TorchNPU documentation, please refer to the LICENSE file.
Acknowledgments
Thanks for every PR from the community! We welcome developers to contribute code to the TorchNPU plugin!