torchair:基于 PyTorch 和 TorchNPU 的昇腾 NPU 图推理扩展项目

TorchAir 支持用户基于PyTorch框架和torch_npu插件在昇腾NPU上使用图模式进行推理。

Branch10Tags0

Introduction

English | 简体中文

This project develops an extension library named TorchAir (Torch Ascend Intermediate Representation), which enables you to run inference in graph mode on Ascend NPUs based on the PyTorch framework and the TorchNPU plugin. TorchAir inherits from the Dynamo mode of the PyTorch framework, converts PyTorch FX graphs into GE computing graphs, and provides the capability to compile and execute GE computing graphs on Ascend NPUs.

  • To learn how to use TorchAir, visit the TorchAir graph mode user guide first for more information about TorchAir.
  • If you plan to contribute code to TorchAir, refer to this README.

Installation and Uninstallation

Using existing operators: Prepare the driver, firmware, CANN, PyTorch, and TorchNPU according to the TorchNPU installation guide. After the installation, verify the NPU environment by following the TorchNPU quick start.

Building from source: For more installation, compilation, and uninstallation instructions, refer to Building from Source.

Post-Installation Verification

Run the following Python script to check whether TorchAir has been installed successfully (set the CANN environment variables first in an NPU environment).

import torch
import torchair

config = torchair.CompilerConfig()
npu_backend = torchair.get_npu_backend(compiler_config=config)

class Model(torch.nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, x, y):
        return torch.add(x, y)

model = Model()
model = torch.compile(model, backend=npu_backend, dynamic=False)
x = torch.randn(2, 2)
y = torch.randn(2, 2)
model(x, y)

If no NPU environment is available, specify the Ascend SDK path during configuration and run source tools/env.sh to set LD_LIBRARY_PATH (including the stub .so files and SDK libraries), so that you can perform minimal debugging on the local CPU.

UT Verification

After the installation, run UT tests in the source root directory to further verify that the TorchAir functions properly.

  1. Run ST tests.

    System tests are Python scripts in the tests/st/ directory, such as torchair_st.py and torchair_aclgraph_st.py. Take torchair_eager_st.py as an example. To run only this test case, run the following command:

    bash build.sh -s torchair_eager_st.py
    

    The preceding single-test-case method is recommended for verification. A full bash build.sh -u or bash build.sh -s run takes a long time, depends on a complete UT/ST test environment, and is susceptible to the runtime environment. Therefore, it is not a recommended verification entry point.

Features

The commonly used TorchAir features are as follows. For the complete feature introduction, visit docs for detailed tutorials.

Feature Description
Logging Logging
Graph dump Graph dump
Dynamo export Exports graphs in AIR format
Data dump Dumps precision data
Graph fusion Allows you to disable or enable some fusion operators as needed
Experimental performance improvement Experimental features. Different features apply to specific scenarios. See the references for details.
Converter extension Allows you to extend the converters that are missing for your model
Supported aten API list List of supported aten APIs

Version Compatibility

TorchAir must be used with specific versions of PyTorch, TorchNPU, CANN, and Python. For recommended version combinations and version compatibility details (including the TorchNPU installation package version and the PyTorch and Python version compatibility table), refer to Version Compatibility.

Supported Models

  • Ascend 950PR&950DT products
  • Atlas A3 products
  • Atlas A2 products

Contributing

If you plan to contribute to TorchAir, refer to CONTRIBUTING.

Contact Us

Security Statement

For the TorchAir security statement, refer to the SECURITY_README file.

License

The TorchAir plugin uses the BSD license. For details, see the LICENSE file.


Disclaimer

To TorchAir Users

  1. The models provided by TorchAir are for your non-commercial purposes only.
  2. For each model, the TorchAir platform only suggests datasets that you can use for training. Huawei does not provide any datasets. If you use these datasets for training, strictly comply with the license of the corresponding dataset. Huawei shall not be liable for any infringement disputes arising from your use of the datasets.
  3. If you find any issues (including but not limited to functional and compliance issues) while using TorchAir models, submit an issue on GitCode. We will review and resolve the issues promptly.

To Dataset Owners

If you do not want your dataset to be mentioned in the models in TorchAir, or you want to update the description of your dataset in the TorchAir models, submit an issue on GitCode. We will delete or update the description of your dataset as requested in your issue. Thank you sincerely for your understanding of and contribution to TorchAir.

Introduction

TorchAir 支持用户基于PyTorch框架和torch_npu插件在昇腾NPU上使用图模式进行推理。

Customize your domain