Skip to content

NKI Backend

Backend selection mirrors the sister trnblas and trnfft packages.

set_backend(backend)

trnsolver.set_backend("auto")  # NKI if available, else PyTorch (default)
trnsolver.set_backend("pytorch")  # force PyTorch fallback
trnsolver.set_backend("nki")  # require NKI; raises if unavailable

get_backend()

Returns the current backend string.

HAS_NKI

Module-level boolean — True iff the nki package (NKI 0.3.0 Stable, Neuron SDK 2.29+) imported successfully.

Precision and dtypes

Every trnsolver kernel computes in FP32 (matmul_fp32out: BF16 in, FP32 out), and Trainium computes float64 at FP32 precision on device. float64 input follows one rule wherever an FP32 input would run a kernel (eigh on the Householder path and eigh_generalized through it, eigh_randomized, eigh(method="jacobi"), the auto Jacobi region, occupied_subspace):

mode float64 input
backend "auto" computes in torch at float64 accuracy; no kernel runs. Host input runs host torch (LAPACK); input on the neuron device is computed on the host (see below)
backend "nki" cast to FP32 for the kernels, result returned as float64; one UserWarning per op (on its first call) says precision is reduced to fp32
TRNSOLVER_REQUIRE_NKI=1 raises NotImplementedError (either backend; takes precedence over the cast). float64 / complex128 on the neuron device raise at every public op, including the routes with no kernel (see below)
  • float64 on the neuron device. Device aten computes float64 at fp32 precision (measured on Trn2), so under backend "auto" every public op (eigh, eigh_generalized, eigh_randomized, occupied_subspace, the factorizations, solves, inverses and cg / gmres / gmres_ir) copies a float64 or complex128 operand that is on the neuron device to the host, computes there at float64, and returns the result on the original device, still float64 / complex128 (the warm-start SubspaceState.theta and residual stay on the host, as always). One UserWarning per op, on its first such call: "float64 on the neuron device is computed on the host for float64 accuracy (device arithmetic is fp32); pass float32 to stay on device". A matvec or preconditioner callable passed to cg / gmres still receives device tensors. The jacobi_preconditioner, block_jacobi_preconditioner and ssor_preconditioner constructors take the same host path: built from device-resident float64 A, they return a closure that computes on the host and returns z on r's device. Block CG follows the same rule: cg_block is guarded, pivoted_cholesky builds in float64 on the host for every input (L returns to the diagonal's device), and PivotedLowRankPreconditioner(apply="auto" or "projected") applies a float64 L that is on the device as "host64". To keep device aten (fp32 precision, no host round trip), pass float32, or set TRNSOLVER_FP64_ON_DEVICE=1. Backends "nki" and "pytorch" are not affected. Under TRNSOLVER_REQUIRE_NKI=1 (backends "auto" and "nki"), a float64 / complex128 operand on the neuron device raises NotImplementedError at every public op (and in pivoted_cholesky / PivotedLowRankPreconditioner), with no host copy, ahead of TRNSOLVER_FP64_ON_DEVICE; before, the routes with no kernel (solve_spd, inv_spd, pinv, cg, ...) ran device aten at fp32 precision with no error.
  • Result dtype always matches what the pytorch backend returns for the same inputs: float32, float64, bfloat16 and float16 (computed in FP32 and cast back), and mixed-dtype operands (torch.promote_types rules, e.g. eigh_generalized takes A's dtype).
  • Integer and bool input takes torch, which raises as the pytorch backend does (NotImplementedError under TRNSOLVER_REQUIRE_NKI=1).
  • trnsolver has no complex kernels: complex64 / complex128 input takes aten or torch on every backend (complex128 on the neuron device under "auto" is computed on the host, as above).
  • Paths where FP32 input does not run a kernel either (the eigh aten cutover below TRNSOLVER_EIGH_NKI_MIN_N, Jacobi on the XLA lineage) run torch for float64 too.

The same rule holds across the trnsci suite (trnsci/trnsolver#89).

Environment variables

Variable Effect
TRNSOLVER_REQUIRE_NKI=1 Kernel-path failures re-raise instead of silently falling back to PyTorch, and inputs no kernel takes (float64, integer, bool) raise NotImplementedError (see Precision and dtypes). Used by the validation suite to catch silent kernel breakage.
TRNSOLVER_FP64_ON_DEVICE=1 Under backend "auto", float64 / complex128 operands on the neuron device stay there and compute with device aten at fp32 precision, instead of on the host at float64 (see Precision and dtypes). For callers who prefer speed. Read at import.
TRNSOLVER_EIGH_NKI_MIN_N=<n> On the native neuron device (and for host tensors on a TorchNeuron host), eigh uses torch.linalg.eigh for matrices with fewer than n rows and the NKI path otherwise. The NKI path is single-tile (n ≤ 128) and falls back to torch.linalg.eigh above that. Default 512; 0 always uses NKI. Ignored when TRNSOLVER_REQUIRE_NKI=1. (#42)
TRNSOLVER_CG_REDUCTION=pairwise\|host64\|device How cg_block and the projected preconditioner reduce over n when reduction= is not passed (#128). pairwise (default) and host64 give the same bits at LNC=1 and LNC=2; device is the previous device-sum path, which is faster but LNC-dependent. See cg_block.
TRNSOLVER_EIGH_METHOD=auto\|jacobi eigh method when method= is not passed. jacobi selects the batched Jacobi solver (#48). Default auto.
TRNSOLVER_JACOBI_GROUP=<g> Jacobi kernel: tiles per launch (rounded down to a power of two). Default 128 (n ≤ 32: scalar kernel; 33 ≤ n ≤ 128: block kernel), split across the two physical cores of a logical NeuronCore.
TRNSOLVER_JACOBI_CHAINS=<c> Jacobi kernel: independent dependency chains per step. Default 8 for n ≤ 32, 2 for the block kernel.
TRNSOLVER_EIGH_CORES=<k>\|auto Split a Jacobi eigh batch over logical NeuronCores, one host thread per core (#54). Default (unset or 1): one core.
TRNSOLVER_EIGH_POOL=proc With TRNSOLVER_EIGH_CORES, solve a host batch on persistent per-core worker processes instead of threads (#57). Cores from TRNSOLVER_EIGH_POOL_CORES, else NEURON_RT_VISIBLE_CORES. trnsolver.nki.eigh_pool.shutdown() frees them.
TRNSOLVER_JACOBI_SWEEPS_PER_LAUNCH=<k> Scalar Jacobi kernel (padded n ≤ 32): run the first sweeps k per launch. Default 1.
TRNSOLVER_JACOBI_BLOCK=0 Run the scalar Jacobi kernel for 33 ≤ n ≤ 128 instead of the block kernel (#48).
TRNSOLVER_EIGH_JACOBI_AUTO=off\|<rows> Region where method="auto" runs the Jacobi kernel (FP32 batches on neuron). off, or rows n_min-n_max:B_min, e.g. 1-32:512,33-64:256. Default: the measured table in eigen._JACOBI_AUTO_REGION.
TRNSOLVER_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 trnsolver:: 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 (the Jacobi onezero) pass through with the op body's int(). Under torch.compile, and for tensors that require grad, int64 tensors and tensor subclasses, the custom op runs as before; so do ops whose body is more than one kernel call. 0 always uses the custom op.
TRNSCI_NKI_DISPATCH=auto\|native\|xla\|cpu Suite-wide. Forces the native or XLA dispatch path; auto (default) detects. Forcing an unavailable path raises.
TRNSCI_XLA_DEPRECATION=1 The XLA path emits one DeprecationWarning. Off by default.
NKI_ENABLE_TRACE_CACHE=0 TorchNeuron (not trnsolver) setting. Turns off the cross-process NKI kernel cache, which cannot version editable installs.
TRNSOLVER_USE_SIMULATOR=1 Route kernel dispatch through nki.simulate(kernel)(numpy_args) on CPU instead of torch_xla. No hardware needed. See Developing kernels.

Dispatch path

On Neuron hardware, kernels run on one of two torch-neuronx lineages, detected on the first kernel launch:

  • Native (TorchNeuron, Trn2/Trn3): each kernel is a PyTorch custom op on device="neuron", also callable directly and inside torch.compile(backend="neuron"): torch.ops.trnsolver.matvec, rank2_update, matmul, matmul_fp32out, apply_reflectors, jacobi_sweeps, jacobi_block, subspace_filter, subspace_step. The ops are registered only once this path is selected.
  • XLA (Trn1): kernels run on the torch_xla device, as before.

set_backend is unaffected: it still chooses NKI vs PyTorch. See Installation.

Jacobi kernels

trnsolver/nki/jacobi.py holds the batched one-sided (Hestenes) Jacobi kernels behind eigh(method="jacobi") (#48): the scalar kernel (trnsolver::jacobi_sweeps, padded n ≤ 32) and the block kernel (trnsolver::jacobi_block, 33 ≤ n ≤ 128). Up to 128 tiles of 128 / n matrices stay in SBUF for a whole sweep; the Tensor Engine applies the step's partner permutation (and, in the block kernel, the Gram and block-rotation matmuls) and the Vector Engine the rotations. See Eigensolvers.

The earlier rotate_pairs_kernel was removed; see #9 for that post-mortem.