trnsparse
Sparse matrix operations for AWS Trainium via NKI.
CSR/COO formats, SpMV, SpMM, and integral screening for sparse scientific computing on Trainium. Part of the trnsci scientific computing suite (github.com/trnsci).
Current phase
trnsparse follows the trnsci 5-phase roadmap. Active work is tracked in phase-labeled GitHub issues:
- Phase 1 — correctness ✅ v0.2.0: NKI SpMM validated on trn1 via densify-then-GEMM (see
trnsci/trnsci#3). - v0.3.0 ✅
BSRMatrix— Trainium-native 128×128 block-sparse format;bsr_spmmNKI kernel. CSR becomes interop; BSR is the compute path. - v0.3.2 ✅
cg_bsr,power_iteration_bsr— iterative solvers over BSR (Python loop; fused kernel gated on NKI capability). - v0.4.0 ✅
screened_spmm— fused Schwarz-screened SpMM in one NKI dispatch. - v0.4.2 ✅ Block-sparse attention —
BSRMatrix+bsr_spmmas the primitive;examples/block_sparse_attention.py+docs/sparse_attention.md. - v0.4.3 ✅ Architecture-friendly alternatives:
chebyshev_bsr/richardson_bsr(fixed-K solvers, no inner products);block_sparse_attention_tiled(two-pass, no O(seq²) intermediate). - Phase 3 — perf: nnz-bucketing, NKI attention kernel pair — parked on NKI indirect DMA gather / hardware validation.
- Phase 4 — multi-chip: sharded BSR across NeuronCores.
- Phase 5 — generation: trn2 DMA bandwidth exploitation.
(No Phase 2 for trnsparse — precision inherited from trnblas.)
Suite-wide tracker: trnsci/trnsci#1.
Install
pip install trnsparse
# Trn2/Trn3 with TorchNeuron (native PyTorch, Neuron SDK 2.33+)
pip install "trnsparse[torchneuron]" --extra-index-url https://pip.repos.neuron.amazonaws.com
# Trn1 / PyTorch-XLA lineage
pip install "trnsparse[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.trnsparse.*) on theneurondevice, with its backward attached viaregister_autograd; notorch_xla. - PyTorch/XLA — Trn1 (and any host on the
torch-neuronx2.9 lineage). Kernels dispatch throughtorch_xla, unchanged.
Placement on the native path: the neuron device has no sparse CSR
tensors, so keep sparse structure (CSR row_ptrs / col_indices, BSR
block_row_ptrs / block_col_indices) on the host and put dense operands
(and BSR blocks) on device="neuron". Results come back on the dense
operand's device. See installation.
Set TRNSCI_NKI_DISPATCH=native|xla|cpu (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.
Usage
import torch
import trnsparse
# Dense → sparse
A = torch.randn(100, 100)
A[torch.abs(A) < 1.0] = 0.0
csr = trnsparse.from_dense(A)
# SpMV: y = A @ x
y = trnsparse.spmv(csr, x, alpha=2.0)
# SpMM: C = A @ B
C = trnsparse.spmm(csr, B)
# Integral screening
Q = trnsparse.schwarz_bounds(diagonal_integrals)
mask = trnsparse.screen_quartets(Q, threshold=1e-10)
stats = trnsparse.sparsity_stats(Q)
Operations
| Operation | Description |
|---|---|
spmv |
Sparse × dense vector (CSR) |
spmm |
Sparse × dense matrix (CSR, PyTorch fallback) |
bsr_spmm |
Block-sparse × dense (BSR-native, Tensor Engine) |
screened_spmm |
Fused Schwarz-screened matmul (one NKI dispatch) |
spmv_symmetric |
Symmetric SpMV (half storage) |
sparse_add |
C = αA + βB |
sparse_scale |
B = αA |
sparse_transpose |
Aᵀ |
cg_bsr |
Conjugate Gradient on BSR matrix |
cg_bsr_fused / prepare_cg_bsr |
One-launch CG / block-Jacobi PCG, A in SBUF, vector-stationary TensorE matvec, device-side convergence check, multi-RHS, opt-in bf16 A |
chebyshev_bsr |
Fixed-K Chebyshev semi-iteration (no inner products) |
richardson_bsr |
Fixed-K Richardson iteration |
power_iteration_bsr |
Dominant eigenpair via power iteration |
power_iteration_bsr_fused |
Power iteration in one launch (the fused CG kernel's machinery) |
jacobi_preconditioner_bsr |
Diagonal preconditioner for cg_bsr |
bsr_diagonal |
Extract main diagonal from BSR matrix |
block_sparse_attention_tiled |
Two-pass sparse attention, no O(seq²) intermediate |
schwarz_bounds |
Schwarz screening bounds |
screen_quartets |
Shell quartet significance mask |
density_screen |
Density-weighted screening |
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 trnsparse 0.12.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 |
|---|---|---|---|---|
| trnsparse-0.12.0.tar.gz | 233.5 kB | With provenance | Details for trnsparse-0.12.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| trnsparse-0.12.0-py3-none-any.whl | Python 3 | none | any | Details for trnsparse-0.12.0-py3-none-any.whl |
Total release size: 322.0 kB
Release files / trnsparse-0.12.0.tar.gz
| Download URL | trnsparse-0.12.0.tar.gz |
|---|---|
| Size | 233.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8c8e8a00963c990f74b5aa518e938f551cd60b30de84bf4a418b9f45b945a65e
|
|
BLAKE2b-256 checksum How to use checksums |
d65125021513a41db1caa1c7b4475f1f401543b49c65b93fb62e5b7a075908ab
|
| 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 1, 2026.
Transparency logRelease files / trnsparse-0.12.0-py3-none-any.whl
| Download URL | trnsparse-0.12.0-py3-none-any.whl |
|---|---|
| Size | 88.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ed66d18e9e08b678c593636d298a3ab5b0e586f42b5c79b8e192288c6b3770e6
|
|
BLAKE2b-256 checksum How to use checksums |
086243f270181e1e9d1c608cb47d8bf9fe4d3279c12842f5192af4eb93913a2a
|
| 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 1, 2026.
Transparency logSecurity for trnsparse 0.12.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 trnsparse-0.12.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 1, 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.