Skip to content

Installation

From PyPI

pip install trnblas

With Neuron hardware support

# 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

[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). These are only needed on Trainium/Inferentia instances. On CPU or GPU, trnblas falls back to torch.matmul automatically. The nki, neuronxcc and torch-neuronx wheels ship on the AWS pip index rather than PyPI, hence --extra-index-url.

Two Neuron PyTorch lineages

torch-neuronx ships in two lineages, and trnblas dispatches its NKI kernels on whichever one is installed:

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 (torch.ops.trnblas.gemm_ragged, syrk, j_batched, ...) registered with torch_neuronx.nki_op; tensors live on device="neuron". Works in eager mode and inside torch.compile(backend="neuron").
PyTorch/XLA Trn1 (also Inf2) torch-neuronx 2.9 lineage, torch_xla Unchanged: kernels run on the torch_xla device.

Selection is automatic, on the first kernel launch:

  1. native: torch_neuronx exports nki_op and a neuron device is available (torch_neuronx.device_count() > 0). This path never imports torch_xla.
  2. xla: torch_xla is installed.
  3. otherwise there is no device path, and the nki backend falls back to torch.matmul with an NkiFallbackWarning (unchanged from earlier releases). Use set_backend("pytorch") or TRNBLAS_USE_SIMULATOR=1.

The trnblas:: custom ops are registered only when the native path is selected, so importing trnblas on CPU or on the XLA lineage has no TorchNeuron side effects. Both lineages publish a package named torch-neuronx, so pick the extra (or Deep Learning AMI Neuron) that matches the instance.

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.

TRNSCI_XLA_DEPRECATION=1 makes the XLA path emit one DeprecationWarning per process. It is off by default; the suite turns it on when TorchNeuron reaches GA (Trn1/Inf2 stay supported on the XLA path in maintenance mode).

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 dispatch.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); trnblas'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.

Simulator-only install (CPU, no hardware)

NKI 0.3.0 ships a CPU simulator (nki.simulate(kernel)(numpy_args)) that runs kernels without a Neuron device. The GH Actions nki-simulator job uses this install line; mirror it locally for fast kernel iteration:

pip install -e ".[dev]"
pip install --extra-index-url https://pip.repos.neuron.amazonaws.com "nki>=0.3.0"

torch-neuronx is not required for the simulator path — dispatch routes NumPy directly through nki.simulate and bypasses torch_xla.

With PySCF (real-molecule DF-MP2 validation)

pip install trnblas[pyscf]

Pulls in PySCF so the end-to-end DF-MP2 example (examples/df_mp2_pyscf.py) and the correctness test (tests/test_df_mp2_pyscf.py, @pytest.mark.pyscf) can run real molecules and compare against PySCF's own mp.dfmp2.DFMP2 reference.

From source

git clone https://github.com/trnsci/trnblas
cd trnblas
pip install -e ".[dev]"
pytest tests/ -v

Runtime environment variables

Variable Effect
TRNBLAS_REQUIRE_NKI=1 Re-raise on NKI kernel errors instead of silently falling back to torch.matmul. Useful in the validation suite to surface kernel breakage. Dtypes the kernels do not compute (float64, for example) raise NotImplementedError instead of running in torch or, for float64 under set_backend("nki"), being cast to float32 (v0.9.1; see Precision and dtypes). Unset (default): kernel exceptions fall back to PyTorch.
TRNBLAS_FP64_ON_DEVICE=1 Under set_backend("auto"), float64 / complex128 operands on the neuron device run as device aten at fp32 precision. Unset (default): they are copied to the host and computed in host torch at float64 accuracy, with one UserWarning per op (see Precision and dtypes). Also lets a forced TRNBLAS_NATIVE_GEMM=aten\|lnc upload host float64.
TRNBLAS_USE_SIMULATOR=1 Route kernel dispatch through nki.simulate(kernel)(numpy_args) on CPU. Bypasses torch_xla + NEFF compile; used for fast correctness iteration. Requires nki>=0.3.0.
TRNSCI_NKI_DISPATCH=auto\|native\|xla Suite-wide. Forces the native or XLA dispatch path; auto (default) detects. Forcing an unavailable path raises.
TRNSCI_XLA_DEPRECATION=1 Suite-wide, off by default. The XLA path emits one DeprecationWarning.
TRNBLAS_BATCHED_PAIR_CHUNK_THRESHOLD=<n> Overrides the batched-pair full-batch vs chunked routing threshold (default 4096 on XLA, 3072 on native). For re-benchmarking.
TRNBLAS_NATIVE_GEMM=auto\|nki\|aten\|host\|lnc Native path only. auto (default) routes gemm / batched_gemm / syrk / symm / trsm by measured time (#62, #63, #79): device-resident operands run as aten on the neuron device, except fp32 gemm with a transposed small-M left operand and a wide right one (the DF-MP2 half-transform), which runs the LNC-split NKI GEMM; host operands run on the host CPU below a size threshold and as upload + aten + download above it; trsm solves with LAPACK on the host. nki forces the NKI kernels, aten forces aten on the device, host keeps host operands on the host, lnc forces the LNC-split GEMM for device-resident 2-D gemm (other ops take aten). An explicit set_backend("nki") always runs the NKI kernels.
TRNBLAS_PAIR_ENERGY=auto\|sym\|aten\|legacy Native path, device-resident inputs only. Picks the nki_batched_pair_energy implementation: the LNC=2 symmetric kernel, aten (chunked GEMM + reduction), or the full-batch / chunked kernels. auto takes sym above 3 GFLOP per row launch.
TRNBLAS_METRIC_SOLVE=auto\|host\|device Native path. Where metric_inv_half factors the metric: auto (default) uses float64 LAPACK on the host for J on the neuron device with naux ≤ 1024; above that, and for host tensors, torch.linalg.cholesky + trsm (v0.8.0).
TRNBLAS_CORES=<n>\|auto Opt-in single-process split of the DF-MP2 pair energy across n visible cores (same as trnblas.set_cores). Unset: one core. See Multi-core.
TRNBLAS_PINNED_OUTPUT=1\|0 Native path only. 1 (default): host results of 1 MiB or more from the device (gemm, batched_gemm, syrk, trsm and the other host-input wrappers) are copied into a fresh tensor from the accelerator's caching host allocator (pin_memory=True). That is about 3.5x faster than .to("cpu"), which page-faults fresh memory. Freed blocks stay cached, rounded up to a power of two, until torch.accelerator.empty_host_cache(). 0 returns plain .to("cpu") results.
TRNBLAS_NKI_EAGER_HANDLE=1\|0 Native path only. 1 (default): eager NKI kernel calls on plain neuron tensors go through a torch_neuronx.wrap_nki handle built once per kernel instead of the trnblas:: custom op, which cuts host time per call from about 0.11 ms to 0.03 ms on trn2. Under torch.compile, and for tensors that require grad, int64 tensors and tensor subclasses, the custom op runs as before. 0 always uses the custom op.
NKI_ENABLE_TRACE_CACHE=0 TorchNeuron (not trnblas) setting. Turns off the cross-process NKI kernel cache, which cannot version editable installs.

Requirements

  • Python ≥ 3.10
  • torch >= 2.1
  • numpy >= 1.24
  • neuronxcc >= 2.24 (optional, for on-hardware NKI kernels — pinned to the 2.24+ nisa.nc_matmul calling convention used across the trnsci suite)
  • torch-neuronx >= 2.9 (optional, pulled in by the [neuron] extra), or torch-neuronx >= 2.14 on Python ≥ 3.12 for native dispatch (the [torchneuron] extra)
  • pyscf >= 2.4 (optional, pulled in by the [pyscf] extra)