trnblas
BLAS operations for AWS Trainium via NKI (Neuron Kernel Interface).
Trainium ships no BLAS library. trnblas provides Level 1-3 BLAS operations with NKI kernel acceleration on the Tensor Engine, targeting scientific computing workloads that are GEMM-dominated.
Part of the trnsci scientific computing suite (github.com/trnsci).
Current phase
trnblas follows the trnsci 5-phase roadmap. Active work is tracked in phase-labeled GitHub issues:
- Phase 1 — correctness: complete as of v0.4.0 (GEMM, SYRK, MP2 energy reduction kernels hardware-validated on trn1; end-to-end DF-MP2 validated against PySCF at nanohartree tolerance).
- Phase 2 — precision:
trnblas.gemm_fp64, an fp64-class GEMM on the bf16 Tensor Engine by Ozaki splitting (exact integer slices, exact PSUM groups, VectorE TwoSum). At the default 8 slices it matches host fp64 BLAS accuracy and is bitwise LNC-invariant (see Level 3). Double-double was measured and dropped (it stays at fp32 accuracy). FP32 remains sufficient for DF-MP2 energies through glycine/cc-pVDZ and H₂O/cc-pVTZ (see Precision envelope). Unblocks trnsolver#27 and trntensor#28. - Phase 3 — perf: complete (tile autotuner, fused DF-MP2 pair-energy kernels, NEFF cache reuse). On Trn2 the native path adds measured
autorouting (v0.9.0) and the fusedpair_energy_symkernel, 1.8× faster than aten on the energy step at 96/672/2304. - Phase 4 — multi-chip: complete as of v0.9.0 for one Trn2 chip:
trnblas.parallelSPMD GEMM and DF-MP2 across NeuronCores (2.5–2.9× on 4 cores). - Phase 5 — generation: trn2 FP16-accumulate GEMM path.
Suite-wide tracker: trnsci/trnsci#1.
Why
NVIDIA has cuBLAS with 152 optimized routines. Trainium has torch.matmul. That's fine for ML training but insufficient for scientific computing codes that need TRSM, SYRK, SYMM, and batched GEMM with specific transpose/scaling semantics.
trnblas closes this gap — same BLAS API surface, NKI kernels on Trainium, PyTorch fallback everywhere else. On Trn2/Trn3 (native TorchNeuron), auto routes each GEMM-family call by measured time: device-resident operands run as aten on the neuron device (the NKI GEMM is 2.4–3.2× slower there), host operands run on the host CPU below a size threshold and as upload + aten above it. The fused DF-MP2 pair-energy kernel (pair_energy_sym) still runs at large sizes, where it beats aten.
Install
pip install trnblas
# Trn2/Trn3 with TorchNeuron (native PyTorch, Neuron SDK 2.33+)
pip install "trnblas[torchneuron]" --extra-index-url https://pip.repos.neuron.amazonaws.com
# Trn1 / PyTorch-XLA lineage
pip install "trnblas[neuron]" --extra-index-url https://pip.repos.neuron.amazonaws.com
Kernels dispatch on whichever Neuron PyTorch lineage is installed:
- TorchNeuron (native): Trn2/Trn3, Neuron SDK 2.33+, Python ≥ 3.12,
torch2.14,torch-neuronx2.14.*. Each kernel is a PyTorch custom op (torch.ops.trnblas.*) on theneurondevice; notorch_xla. - PyTorch/XLA: Trn1 (and any host on the
torch-neuronx2.9 lineage). Kernels dispatch throughtorch_xla, unchanged.
trnblas picks the native path when the native torch_neuronx and a neuron
device are present, and the XLA path when torch_xla is installed; without
nki it uses torch.matmul. Set TRNSCI_NKI_DISPATCH=native or xla
(shared by all trnsci libraries) to force a path. With an editable install
(pip install -e), set NKI_ENABLE_TRACE_CACHE=0: the NKI kernel cache only
versions installed wheels and can serve stale kernels. See
installation.
Known platform issue: NKI trace cache and kernel factories
import torch_neuronx turns on the NKI trace cache, and the cache key ignores
constants captured in a kernel's closure: two @nki.jit kernels built by one
factory, differing only in a captured value, share one trace, and the second
silently returns the first's results
(aws-neuron/aws-neuron-sdk#1454).
trnblas's kernels do not rely on closure constants (or on module globals that
can differ between processes): every value that specialises a kernel is a
kernel argument, and tests/test_trace_cache_safety.py enforces this. If you
write your own NKI kernels with a factory, set NKI_ENABLE_TRACE_CACHE=0
before importing torch_neuronx.
Usage
import torch
import trnblas
# Level 3 — Matrix multiply (the hot path)
C = trnblas.gemm(alpha=1.0, A=A, B=B, beta=0.5, C=C_init, transA=True)
# Batched GEMM (DF-MP2 tensor contractions)
C = trnblas.batched_gemm(1.0, A_batch, B_batch)
# Symmetric matrix multiply (Fock builds)
F = trnblas.symm(1.0, density, H_core, side="left")
# Triangular solve (Cholesky-based density fitting)
X = trnblas.trsm(1.0, L, B, uplo="lower")
# Symmetric rank-k update (metric construction)
J = trnblas.syrk(1.0, integrals, trans=True)
# Level 2 — Matrix-vector
y = trnblas.gemv(1.0, A, x, beta=1.0, y=y)
# Level 1 — Vector operations
y = trnblas.axpy(alpha, x, y)
d = trnblas.dot(x, y)
n = trnblas.nrm2(x)
DF-MP2 Example
# Run the density-fitted MP2 example
python examples/df_mp2.py --demo
python examples/df_mp2.py --nbasis 100 --nocc 20
The example demonstrates all core BLAS operations in a realistic quantum chemistry workflow: the metric factor J^-1/2 (trnblas.metric_inv_half, Cholesky + triangular solve; float64 LAPACK on the host for naux ≤ 1024 on the native path), half-transform GEMMs, metric contraction, and energy evaluation.
Real-molecule validation (via PySCF)
pip install trnblas[pyscf]
python examples/df_mp2_pyscf.py # H2O / STO-3G
python examples/df_mp2_pyscf.py --mol ch4 --basis cc-pvdz
Runs SCF + density fitting via PySCF, feeds the integrals through trnblas, and compares to PySCF's own DF-MP2 reference energy. Matches to < 10⁻⁷ Hartree on H2O, CH4, NH3 at cc-pvdz.
Operations
| Level | Operation | Description |
|---|---|---|
| 1 | axpy |
y = αx + y |
| 1 | dot |
x^T y |
| 1 | nrm2 |
‖x‖₂ |
| 1 | scal |
x = αx |
| 1 | asum |
Σ|xᵢ| |
| 1 | iamax |
argmax |xᵢ| |
| 2 | gemv |
y = α op(A) x + βy |
| 2 | symv |
y = α A x + βy (A symmetric) |
| 2 | trmv |
x = op(A) x (A triangular) |
| 2 | ger |
A = α x yᵀ + A |
| 3 | gemm |
C = α op(A) op(B) + βC |
| 3 | batched_gemm |
Batched GEMM |
| 3 | symm |
C = α A B + βC (A symmetric) |
| 3 | syrk |
C = α A Aᵀ + βC |
| 3 | trsm |
Solve op(A) X = αB |
| 3 | trmm |
B = α op(A) B |
Status
- Level 1-3 BLAS with PyTorch backend
- NKI GEMM kernel validation on trn1/trn2
- NKI GEMM with stationary tile reuse
- Batched GEMM NKI kernel
- Native TorchNeuron dispatch (Trn2/Trn3) with measured
autorouting - Multi-core DF-MP2 and GEMM (
trnblas.parallel) - Benchmarks vs cuBLAS
- fp64-class GEMM (
gemm_fp64, Ozaki splitting; double-double measured and dropped)
Known issues in earlier releases
Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-04 releases. Upgrade to 0.11.1 or later. Details are in CHANGELOG.md.
| Bug | Affected | Fixed in |
|---|---|---|
trsm on host operands, and the host route of metric_inv_half for a device-resident J, detached autograd on a Neuron host (TorchNeuron), so gradients silently stopped; trnsolver's eigh_generalized inherited it (#101) |
trsm 0.9.0–0.11.0, metric_inv_half 0.8.0–0.11.0; native route under auto (metric_inv_half also under nki) |
0.11.1 |
syrk ignored uplo and symmetrized C, so a C stored in one triangle lost half its off-diagonal entries and the other triangle of a non-symmetric C leaked in (#74) |
through 0.10.0, every backend | 0.11.0 |
Complex asum / iamax used the modulus |x| instead of the BLAS |Re| + |Im| (asum up to √2 off; iamax could pick another index) (#75) |
through 0.10.0, every backend | 0.11.0 |
Mixed-dtype gemm / batched_gemm / symm operands returned a result in the left operand's dtype where torch raises (#90) |
0.2.0–0.10.0, NKI route | 0.11.0 |
float64, integer and complex operands of nki_mp2_energy, nki_fused_gemm_energy and nki_batched_pair_energy went into the FP32 kernels and came back as garbage |
every release with these kernels through 0.10.0, NKI route | 0.11.0 |
BLAS letter flags (uplo="U", diag="U", trans="N") ran the opposite branch (#73) |
0.2.0–0.9.1, every backend | 0.10.0 |
gemm(transA=True) / transB=True on batched (>= 3-D) operands reversed every dimension (#72) |
0.2.0–0.9.1, every backend | 0.10.0 |
batched_gemm on the NKI route ignored batch broadcasting and returned only A[0] @ B[0] (#84) |
0.3.0–0.9.1, NKI route | 0.10.0 |
metric_inv_half with an integer J on the neuron device returned a truncated (mostly zero) factor (#86) |
0.8.0–0.9.1, native route | 0.10.0 |
float64 gemm / syrk / trsm under set_backend("nki") returned non-finite values (#68) |
0.2.0–0.9.0, set_backend("nki") |
0.9.1 |
Related Projects
| Project | What |
|---|---|
| trnfft | FFT + complex ops for Trainium |
| trnrand | Random number generation (Philox/Sobol) for Trainium |
| trnsolver | Linear solvers and eigendecomposition |
| trnsparse | Sparse formats and SpMM |
| trntensor | einsum and tensor decompositions |
License
Apache 2.0 — Copyright 2026 Scott Friedman
Disclaimer
trnsci is an independent open-source project. It is not sponsored by, endorsed by, or affiliated with Amazon.com, Inc., Amazon Web Services, Inc., or Annapurna Labs Ltd.
"AWS", "Amazon", "Trainium", "Inferentia", "NeuronCore", "Neuron SDK", and related identifiers are trademarks of their respective owners and are used here solely for descriptive and interoperability purposes. Use does not imply endorsement, partnership, or any other relationship.
All work, opinions, analyses, benchmark results, architectural commentary, and editorial judgments in this repository and on trnsci.dev are those of the project's contributors. They do not represent the views, positions, or commitments of Amazon, AWS, or Annapurna Labs.
Feedback directed at the Neuron SDK or Trainium hardware is good-faith ecosystem commentary from independent users. It is not privileged information, is not pre-reviewed by AWS, and should not be read as authoritative about product roadmap, behavior, or quality.
For official AWS guidance, see aws-neuron documentation and the AWS Trainium product page.
Metadata
Release files for trnblas 0.13.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| trnblas-0.13.0.tar.gz | 438.9 kB | With provenance | Details for trnblas-0.13.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| trnblas-0.13.0-py3-none-any.whl | Python 3 | none | any | Details for trnblas-0.13.0-py3-none-any.whl |
Total release size: 541.1 kB
Release files / trnblas-0.13.0.tar.gz
| Download URL | trnblas-0.13.0.tar.gz |
|---|---|
| Size | 438.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
018529e6311de4e83edd2b54895732fb5df68ec3a99b708f0d538566de93e2c9
|
|
BLAKE2b-256 checksum How to use checksums |
fe385368921e1353ebaca3e28feda99f5c21f3d6294d3719b86c7f3adc55fc89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.
Transparency logRelease files / trnblas-0.13.0-py3-none-any.whl
| Download URL | trnblas-0.13.0-py3-none-any.whl |
|---|---|
| Size | 102.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
68ab060d31221f3eff476ad17a966c4f189f835d7173a1a009ad11233264526f
|
|
BLAKE2b-256 checksum How to use checksums |
9dcbbc01395e4476caed671fadb6d3d175b2bf39aaaf4ab154a1ca1335b9047c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.
Transparency logSecurity for trnblas 0.13.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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for trnblas-0.13.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.
Transparency logA 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.