Installation¶
From PyPI¶
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
[torchneuron] pins torch>=2.14, torch-neuronx>=2.14, and nki>=0.6
(torch-neuronx 2.14 requires Python ≥ 3.12). [neuron] is the XLA lineage
(nki>=0.3.0, neuronxcc>=2.24, torch-neuronx>=2.9). nki and
torch-neuronx are not on public PyPI; they ship with the Deep Learning
AMI Neuron or install from https://pip.repos.neuron.amazonaws.com.
Two Neuron PyTorch lineages¶
| Lineage | Instances | Stack | Kernel dispatch |
|---|---|---|---|
| TorchNeuron (native) | Trn2, Trn3 | Neuron SDK 2.33+, Python ≥ 3.12, torch 2.14, torch-neuronx 2.14.* |
Each kernel is a PyTorch custom op registered with torch_neuronx.nki_op (torch.ops.trnsparse.spmm_dense, bsr_spmm, screened_spmm, attn_stats, attn_out, attn_bwd_dq, attn_bwd_dkdv, and the static-structure bsr_spmm_static, attn_fwd_static, attn_bwd_static that bsr_spmm and attention use by default), plus the composite trnsparse::bsr_attn / bsr_attn_bwd; tensors live on device="neuron". Backwards are attached with register_autograd. |
| PyTorch/XLA | Trn1 (also Inf2) | torch-neuronx 2.9 lineage, torch_xla |
Unchanged: kernels run on the torch_xla device; autograd via torch.autograd.Function wrappers. |
Selection is automatic, on the first kernel launch:
- native —
torch_neuronxexportsnki_opand aneurondevice is available (torch_neuronx.device_count() > 0). This path never importstorch_xla. - xla —
torch_xlais installed. - otherwise there is no device path; kernel launches fall back to
PyTorch (or raise under
TRNSPARSE_REQUIRE_NKI=1), as in earlier releases.TRNSPARSE_USE_SIMULATOR=1still routes throughnki.simulate.
The trnsparse:: custom ops are registered only when the native path is
selected, so importing trnsparse on CPU or on the XLA lineage has no
TorchNeuron side effects.
To force a path, set TRNSCI_NKI_DISPATCH before the first launch. The
variable is shared by every trnsci library:
| Value | Effect |
|---|---|
auto (default) |
The order above. |
native |
Use TorchNeuron; raise RuntimeError if no neuron device is available. |
xla |
Use torch_xla even if TorchNeuron is present; raise RuntimeError if torch_xla is missing. |
cpu |
No device path (PyTorch fallback / simulator). |
TRNSCI_XLA_DEPRECATION=1 makes the XLA path emit a one-time
DeprecationWarning. It is off by default.
TRNSPARSE_NKI_EAGER_HANDLE=0 turns off the eager kernel handle. By default
(1), eager NKI kernel calls on plain neuron tensors go through a
torch_neuronx.wrap_nki handle built once per kernel instead of the
trnsparse:: custom op, which cuts host time per call from about 0.1 ms to
0.02-0.03 ms on trn2 (trnblas#92). Static arguments (keyed-structure ids,
fused-solver settings, structure lists, the attention scale) get the same
int() / tuple() / float() conversions as in the op body. Under
torch.compile, and for tensors that require grad, int64 tensors and tensor
subclasses, the custom op runs as before. bsr_attn / bsr_attn_bwd (whole
drivers) keep the op; their kernel launches take the handle.
Placement: sparse structure on the host¶
The TorchNeuron neuron device has no sparse CSR tensors
(aws-neuron-sdk#1442):
torch.sparse_csr_tensor cannot be built on it or densified there. On the
native path trnsparse therefore never puts sparse structure on the device:
| Object | Where it lives | What moves to neuron |
|---|---|---|
CSRMatrix (values, col_indices, row_ptrs) |
host | the densified (padded) A tile, built on the host without a torch sparse tensor |
BSRMatrix.block_row_ptrs, .block_col_indices |
host | nothing; only the small gather index grids derived from them |
BSRMatrix.blocks |
host or neuron |
dense blocks, gathered into kernel tiles on the device |
attention mask (BSRMatrix) structure |
host | nothing; Q, K, V are gathered on the device |
dense operands (B, x, Q/K/V, screened A) |
host or neuron |
themselves |
Results come back on the dense operand's device (B's for spmm /
bsr_spmm / screened_spmm, x's for spmv, Q's for attention).
Index tensors found on a device are copied to the host on each call, which
works but costs a device sync; build them on the host.
A = trnsparse.from_dense(D) # host CSR
B = torch.randn(n, 64, device="neuron") # device operand
C = trnsparse.spmm(A, B) # C on neuron; differentiable in A.values and B
C = torch.compile(lambda B: trnsparse.spmm(A, B), backend="neuron")(B)
spmv / spmm against a device operand densify A on the host each call.
For repeated products with the same matrix (iterative solvers), use
BSRMatrix with blocks on the device.
Editable installs and the NKI kernel cache¶
TorchNeuron enables the NKI trace cache by default
(NKI_ENABLE_TRACE_CACHE=1), which persists compiled kernels across
processes. The cache only versions source that belongs to an installed
wheel (a site-packages tree with .dist-info); an editable install is
not tracked and can get a stale kernel after you edit kernels.py. When
developing with pip install -e, turn the cache off before starting
Python:
export NKI_ENABLE_TRACE_CACHE=0
The test suite keeps the cache on, as users run it, but gives each session
a fresh, empty cache directory (NKI_TRACE_CACHE_URL), so edits never hit
a stale entry.
The cache also ignores constants captured in a kernel's closure
(aws-neuron/aws-neuron-sdk#1454);
trnsparse's kernels take every specialising value as an argument
(tests/test_trace_cache_safety.py). Set NKI_ENABLE_TRACE_CACHE=0 if
you write NKI kernels that a factory specialises through a closure.
See the TorchNeuron environment variables reference.
From source¶
git clone git@github.com:trnsci/trnsparse.git
cd trnsparse
pip install -e ".[dev]"
pytest tests/ -v
Hardware compatibility¶
NKI kernels target Neuron SDK 2.24+ on the Deep Learning AMI Neuron PyTorch 2.9 (Ubuntu 24.04) AMI (XLA lineage), and Neuron SDK 2.33+ with TorchNeuron on Trn2/Trn3 (native lineage). Without Neuron hardware, trnsparse falls through to PyTorch ops on CPU/GPU — all APIs remain functional.