Skip to main content

pto-kernels

PyPI version Python versions License

A collection of high-performance custom kernels for Ascend NPUs, built on top of pto-isa — the Parallel Tile Operation virtual instruction set architecture designed by Ascend CANN.

PTO focuses on tile-level operations, enabling efficient, composable kernel development targeting Huawei's Ascend AI processors, and ships as ready-to-use PyTorch (torch-npu) operators.


Why pto-kernels?

  • Fast — hand-tuned tile-level kernels for Ascend NPUs, benchmarked against torch-npu built-ins.
  • Drop-in — kernels are exposed as plain Python functions that operate on torch_npu tensors.
  • Broad coverage — everything from elementwise ops (abs, swiglu) to linear-algebra primitives (tri_inv, matmul) to gated-linear-attention building blocks (GDN, KDA chunked recurrence, WY representation, KKT).
  • Extensible — built on pto-isa, so new kernels can be written once at the tile level and reused across ops.

Contents

Installation

Prebuilt wheels are published to PyPI for Python 3.10–3.12 on x86_64 and aarch64:

pip install pto-kernels

Requires a working torch-npu + Ascend CANN runtime environment to run kernels on-device; the package itself installs without one.

From source

Building from source is only needed for unreleased kernels or if you plan to contribute.

Prerequisites:

  • A configured torch-npu environment
  • Ascend toolkit installed at /usr/local/Ascend/ascend-toolkit
# One-time setup
make setup_once

# Install directly from GitHub
export CMAKE_GENERATOR="Unix Makefiles"
pip install -v git+https://github.com/huawei-csl/pto-kernels.git

Or build a wheel locally:

source /usr/local/Ascend/ascend-toolkit/set_env.sh
pip3 install -r requirements.txt
make wheel                          # produces pto_kernels-X.Y.Z-*.whl
pip install --force-reinstall pto_kernels-*.whl

Choosing the target SoC

Two build settings select the target hardware:

Setting Values Meaning
SOC_VERSION Ascend910B*, Ascend910_93*, Ascend950*, Ascend310P* Target SoC; mapped to the compiler's NPU arch (dav-2201, dav-3510, dav-2002)
BASE_MODE MEMORY, REGISTER Base addressing mode (-DMEMORY_BASE / -DREGISTER_BASE)

Use MEMORY for A2-generation parts (Ascend910B*) and REGISTER for A5 (Ascend950*).

Building for A5 (Ascend950):

# Wheel build — both settings are read from the environment
export SOC_VERSION=Ascend950
export BASE_MODE=REGISTER
make wheel
pip install --force-reinstall pto_kernels-*.whl

# In-tree CMake build — flags take precedence over the environment
bash scripts/build.sh --soc-version Ascend950 --base-mode REGISTER

A2 is the default when neither is set: BASE_MODE=MEMORY with SOC_VERSION=Ascend910B4 for the wheel build and Ascend910B2 for scripts/build.sh.

To compile a single kernel without building the whole package, make compile_<name> targets A2 and make compile_a5_<name> targets A5 (e.g. make compile_a5_abs).


Quickstart

import torch
import torch_npu  # noqa
from pto_kernels import pto_swiglu

x = torch.randn(4, 2048, device="npu", dtype=torch.float16)
y = pto_swiglu(x)  # fused SwiGLU on Ascend NPU

Available kernels

Category Kernels
Elementwise / activation pto_abs, pto_swiglu
Attention pto_flash_attention
Linear algebra pto_simple_matmul, pto_batch_matrix_square, pto_tri_inv_col_sweep, pto_tri_inv_ns, pto_tri_inv_rank_one, pto_tri_inv_rec_unroll, pto_tri_inv_trick
Scan / gather pto_scan_ul1, pto_csr_gather
GDN (Gated DeltaNet) pto_gdn_chunk_cumsum, pto_gdn_chunk_o, pto_gdn_scaled_dot_kkt, pto_gdn_wy_fast
GDN fused pto_gdn_mega — one launch, two phases: shared Gram/gate preparation and a persistent fused inverse/WY/state/output pipeline. Benchmarks, accuracy
KDA (Kimi Delta Attention) pto_kda_chunk_h, pto_kda_chunk_o, pto_kda_gate_cumsum, pto_kda_kkt, pto_kda_wy

More end-to-end usage patterns live under examples/ (JIT C++ kernels, AI CPU custom ops) and tests/ (correctness against reference/torch_npu implementations).


Testing

make test

Repository structure

pto-kernels/
├── csrc/                  # C++ kernel source files
├── python/pto_kernels/    # Python bindings and utilities
├── examples/jit_cpp/      # JIT compilation examples
├── examples/aicpu/        # AI CPU custom-op examples
├── tests/                 # Test suite
├── scripts/               # Helper scripts
├── doxygen/               # API documentation config
└── CMakeLists.txt         # CMake build configuration

Contributing

Contributions are welcome! Whether it's a new kernel, a bug fix, or a benchmark, please read CONTRIBUTING.md before opening a pull request.

Release process

See RELEASE.md for how new versions are cut and published.

License

BSD-3-Clause-Clear — see LICENSE for details.

Publications

The following publications are related to the theory and the implementations of the implemented kernels.

  1. Parallel Scan on Ascend AI Accelerators

    Bibtex
    @inproceedings{wroblewski2025parallel,
        title={{Parallel Scan on Ascend AI Accelerators}},
        author={Wr{\'o}blewski, Bart{\l}omiej and Gottardo, Gioele and Zouzias, Anastasios},
        booktitle={2025 IEEE International Parallel and Distributed Processing Symposium Workshops (IPDPSW)},
        pages={1290--1292},
        year={2025},
        organization={IEEE},
        doi={10.1109/IPDPSW66978.2025.00216}
    }
    
  1. A Parallel Scan Algorithm in the Tensor Core Unit Model

    Bibtex
    @inproceedings{zouzias2023parallel,
        author       = {Anastasios Zouzias and William F. McColl},
        title        = {{A Parallel Scan Algorithm in the Tensor Core Unit Model}},
        booktitle    = {International European Conference on Parallel and Distributed Computing (Euro-Par)},
        series       = {Lecture Notes in Computer Science},
        volume       = {14100},
        pages        = {489--502},
        publisher    = {Springer},
        year         = {2023},
        doi          = {10.1007/978-3-031-39698-4_33},
        timestamp    = {Tue, 12 Sep 2023 07:57:22 +0200},
        biburl       = {https://dblp.org/rec/conf/europar/ZouziasM23.bib},
        bibsource    = {dblp computer science bibliography, https://dblp.org}
    }
    
  1. Segmented Operations Using Matrix Multiplications

    Bibtex
    @article{sobczyk2025segmented,
        title={{Segmented Operations using Matrix Multiplications}},
        author={Sobczyk, Aleksandros and Sorrentino, Giuseppe and Zouzias, Anastasios},
        journal={arXiv},
        publisher={arXiv},
        url={https://arxiv.org/abs/2506.23906},
        doi={10.48550/arXiv.2506.23906},
        year={2025}
    }
    
  1. Fast and Stable Triangular Inversion for Delta-Rule Linear Transformers

    Bibtex
    @article{sobczyk2026fast,
        doi = {10.48550/ARXIV.2605.21325},
        url = {https://arxiv.org/abs/2605.21325},
        author = {Sobczyk,  Aleksandros and Gottardo,  Gioele and Matzoros,  Christos K. and De Vita,  Mirko and Skogh,  Filip and Zouzias,  Anastasios and Zhuang,  Jiawei},
        keywords = {Machine Learning (cs.LG),  FOS: Computer and information sciences,  FOS: Computer and information sciences},
        title = {{Fast and Stable Triangular Inversion for Delta-Rule Linear Transformers}},
        publisher = {arXiv},
        journal = {arXiv},
        year = {2026},
    }
    

Metadata

Release files for pto-kernels 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pto-kernels 0.2.0
File Size Uploaded Provenance Actions
pto_kernels-0.2.0.tar.gz 1.6 MB Without provenance Details for pto_kernels-0.2.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for pto-kernels 0.2.0
File Actions
pto_kernels-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ x86-64 Details for pto_kernels-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl
pto_kernels-0.2.0-cp312-cp312-manylinux_2_34_aarch64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ ARM64 Details for pto_kernels-0.2.0-cp312-cp312-manylinux_2_34_aarch64.whl
pto_kernels-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.34+ x86-64 Details for pto_kernels-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl
pto_kernels-0.2.0-cp311-cp311-manylinux_2_34_aarch64.whl CPython 3.11 CPython 3.11 Linux glibc 2.34+ ARM64 Details for pto_kernels-0.2.0-cp311-cp311-manylinux_2_34_aarch64.whl
pto_kernels-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.34+ x86-64 Details for pto_kernels-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl
pto_kernels-0.2.0-cp310-cp310-manylinux_2_34_aarch64.whl CPython 3.10 CPython 3.10 Linux glibc 2.34+ ARM64 Details for pto_kernels-0.2.0-cp310-cp310-manylinux_2_34_aarch64.whl

Total release size: 105.8 MB

Release files / pto_kernels-0.2.0.tar.gz

Download URL pto_kernels-0.2.0.tar.gz
Size 1.6 MB
Tags Source
SHA-256 checksum
How to use checksums
d62283f286bdfbb4c2e32f21d778e82d6c384d407385d2a1d222d5cb7277fc4b
BLAKE2b-256 checksum
How to use checksums
c90fb5f1b7fa8335714e3fd60fa5859784556c5097aeca73021a44c47254a3d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / pto_kernels-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl

Download URL pto_kernels-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl
Size 17.4 MB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
785795ff0d61b0e0bb6c7e1dd82a8004515e86e38885cabef4f8ce207b9f76e1
BLAKE2b-256 checksum
How to use checksums
0a41097f0ae2b2a212b2c9d3c4dbe0068834f1f640bdf6de59eec35cc4ce2bf2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / pto_kernels-0.2.0-cp312-cp312-manylinux_2_34_aarch64.whl

Download URL pto_kernels-0.2.0-cp312-cp312-manylinux_2_34_aarch64.whl
Size 17.3 MB
Tags CPython 3.12 Linux glibc 2.34+ ARM64
SHA-256 checksum
How to use checksums
ddde0950a599f0bb2c14023c149b343bdd88f6bfe7ba6ea5a0bbc2d642132e62
BLAKE2b-256 checksum
How to use checksums
e81716f3649aec4d92c8e7744be86c2f279bcf3374ad41334a8b969b3f321507
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / pto_kernels-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl

Download URL pto_kernels-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl
Size 17.4 MB
Tags CPython 3.11 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
a253dcd9f490010c27966036d6ca5b5942df7f0b4ab7afe8b8c9e795cd9e59b8
BLAKE2b-256 checksum
How to use checksums
52b5e0928bfe91b76a63a9780f4869e4a682e9002a2519727bf5738a072c384c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / pto_kernels-0.2.0-cp311-cp311-manylinux_2_34_aarch64.whl

Download URL pto_kernels-0.2.0-cp311-cp311-manylinux_2_34_aarch64.whl
Size 17.3 MB
Tags CPython 3.11 Linux glibc 2.34+ ARM64
SHA-256 checksum
How to use checksums
366d10b3a3e274244f4067ad174083730d57b320a59062799adff8d677a0b10e
BLAKE2b-256 checksum
How to use checksums
e23449f9aad5555fb59fba1430bf4ebc184a3a21bf770e72e01dc13bd17c3243
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / pto_kernels-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl

Download URL pto_kernels-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl
Size 17.4 MB
Tags CPython 3.10 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
5b18a010da495938b5e37ddad145ec1b9bec10c84f125b02d58bf55e7182899c
BLAKE2b-256 checksum
How to use checksums
3d69ec3bbb5da6e5ab7764755eaba5c4d3e0acd43b93cbf8ecda2debf12aa61a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / pto_kernels-0.2.0-cp310-cp310-manylinux_2_34_aarch64.whl

Download URL pto_kernels-0.2.0-cp310-cp310-manylinux_2_34_aarch64.whl
Size 17.3 MB
Tags CPython 3.10 Linux glibc 2.34+ ARM64
SHA-256 checksum
How to use checksums
78f872e8c278328dbe759b447012c4bb6cae7c96c64877987cb13f6c772f0dda
BLAKE2b-256 checksum
How to use checksums
c6edca91566c405d73d7ae5bd700b5197142a64bf0dd59118af0ce0578d1d4ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Security for pto-kernels 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page