torchao-ascend:基于 PyTorch 与昇腾 NPU 生态的 TorchAO 适配项目

Ascend Extension for PyTorchAO

Branch7Tags0
FilesLast commitLast update
2 months ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
1 month ago
1 month ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago

TorchAO for AscendNPU

An Ascend NPU adaptation plugin for torchao

简体中文 | English

Supported Versions · Build and Installation · Quick Start · Contributing Guide · Community

python pytorch torchao platform repo


Introduction

TorchAO for AscendNPU (torchao_npu) is an Ascend NPU adaptation plugin for the quantization ecosystem torchao. It enables torchao's quantization schemes (FP8 / Blockwise FP8 / MX series) as well as the Ascend-native HiFloat8 (HIF8) data type to run on Ascend NPUs, supporting both quantized training and inference scenarios.

What's New

  • 📢 [2026-09-01] torchao_npu 0.18.0 has completed adaptation development. It supports torchao 0.18.x and PyTorch 2.11+, covers the FP8 / Blockwise FP8 / MXFP8 / HiFloat8 quantization schemes, and provides quantized training and inference capabilities.

Supported Versions

The following versions are currently supported:

  • Python >= 3.10
  • PyTorch >= 2.11.0
  • torchao >= 0.18.0, < 0.19.0
  • CANN 9.1.0

For the compatibility between torch_npu and CANN, refer to the TorchNPU version compatibility table.

Build and Installation

Download the master branch code

Run the following commands to download the master branch code and enter the TorchAONPU plugin root directory:

git clone https://gitcode.com/Ascend/torchao_npu.git
cd torchao_npu

Install environment dependencies

Install PyTorch

Run the following command to install PyTorch 2.11.0:

pip install torch==2.11.0 --index-url https://download.pytorch.org/whl/cpu

Install TorchNPU

Run the following command to install TorchNPU 2.11.0:

pip install torch-npu==2.11.0

Install torchao_npu

Both editable installation from source and building a wheel for installation are supported:

Option 1: Editable installation

Suitable for local development and debugging:

pip install -e .

Option 2: Build a wheel and install

Suitable for production deployment. First build the wheel, then install the generated wheel:

bash ci/build.sh
pip install dist/*.whl

Building requires Python >= 3.10. To specify an interpreter:

bash ci/build.sh --python=3.10

To skip the build isolation environment (use dependencies from the current environment):

bash ci/build.sh --no-isolation

The generated wheel is located in the dist/ directory.

Quick Start

MXFP8 inference example

import torch

from torchao.prototype.mx_formats import MXDynamicActivationMXWeightConfig
from torchao.quantization import quantize_

model = model.to("npu").eval()
quantize_(
    model,
    MXDynamicActivationMXWeightConfig(
        activation_dtype=torch.float8_e4m3fn,
        weight_dtype=torch.float8_e4m3fn,
        block_size=32,
    ),
)

with torch.no_grad():
    output = model(input_tensor.to("npu"))

HiFloat8 training example

import torch
from torchao.quantization import quantize_
from torchao.hifloat8 import HiFloat8LinearConfig

model = model.to("npu").train()
quantize_(model, HiFloat8LinearConfig())

optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4)
loss = model(input_tensor.to("npu")).sum()
loss.backward()
optimizer.step()

For more examples (inference, custom filter_fn quantization scope, etc.), see the HiFloat8 usage guide (in Chinese).

Community

TorchAO for AscendNPU is built on the Ascend and torchao open-source ecosystems, providing quantization adaptation capabilities for the Ascend NPU platform. You are welcome to join the discussion and contribute in the following ways:

  • If you run into any problems or have suggestions, please submit an Issue and we will respond as soon as possible.
  • Pull Requests are welcome. For code standards and the submission process, see the Contributing Guide.
  • For more Ascend developer tools and documentation, visit the Ascend community.

Thank you for your support.

Security Notes

For system security hardening, recommended runtime users, and file permission control of TorchAO for AscendNPU, see the Security Notes.

Disclaimer

This plugin is for debugging and development purposes only. Users must ensure the security of the commands they enter and properly control access to the data generated during use. By using this plugin, you agree to and accept the above statements.

License

The open-source license of this project is subject to the License statement maintained in this repository.

Acknowledgements

Thanks for every PR from the community — contributions to TorchAO for AscendNPU are welcome!

Introduction

Ascend Extension for PyTorchAO

Customize your domain