PyAsc (Python-Ascend) is a Python programming model for writing compute kernels that run on Huawei Ascend NPUs. Kernels are written in Python, translated through an MLIR pipeline into Ascend C, and then compiled by the Bisheng compiler into an NPU binary. Two APIs are available:
asc2(tile-based, NumPy-like,@asc2.jit) for high-level kernels,asc(1:1 Ascend C mapping,@asc.jit) for low-level control.
-
Install CANN toolkit (provides the Bisheng compiler and NPU runtime).
-
Install PyAsc:
python3 -m pip install pyasc
import asc
import asc2
import torch
@asc2.jit
def vadd_kernel(x_ptr: asc.GlobalAddress, y_ptr: asc.GlobalAddress, out_ptr: asc.GlobalAddress,
size: int, tile_size: asc.ConstExpr[int], tile_per_block: asc.ConstExpr[int]):
x_gm = asc2.global_tensor(x_ptr, [size])
y_gm = asc2.global_tensor(y_ptr, [size])
out_gm = asc2.global_tensor(out_ptr, [size])
base = asc2.block_idx() * tile_size * tile_per_block
for i in range(tile_per_block):
off = base + i * tile_size
x = asc2.copy_in(x_gm, [tile_size], offsets=[off])
y = asc2.copy_in(y_gm, [tile_size], offsets=[off])
asc2.copy_out(x + y, out_gm, offsets=[off])
# Create tensors
x = torch.rand(8192, dtype=torch.float32)
y = torch.rand_like(x)
out = torch.empty_like(x)
# Launch on 16 NPU cores
asc.runtime.config.set_plarform("NPU")
tile_size, cores = 128, 16
vadd_kernel[cores](x, y, out, out.size, tile_size, asc.ceildiv(out.size // tile_size, cores))
# Verify the result
torch.testing.assert_close(out, x + y)Read the full documentation to learn more:
| Section | Description |
|---|---|
| Installation | Environment setup (CANN), build from source |
| Design | Project overview, design decisions, key implementation details |
| Development | Coding style (C++, Python), adding new APIs, development tools |
It also includes autogenerated Python API reference and MLIR dialect listing.
CANN Open Software License Agreement Version 2.0. See LICENSE.