Repository navigation
v0.16.0
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron device) — see trnsci/trnsci#35.
Fixed
-
eighon the nativeneurondevice usestorch.linalg.eighbelow
TRNSOLVER_EIGH_NKI_MIN_N(default 512). On Trn2 the NKI path took
0.4 s at n=16 and 21 s at n=128, against 0.4-1.1 ms for aten on the same
device, and grew about 4x per doubling of n. It is launch-bound, not
compute-bound. Tridiagonalization makes n-2 host-driven steps, each with
two kernel launches, about six.item()reads and an XLA sync. The
implicit-shift QR iteration then ran element by element on the input's
device: O(n^2) Givens rotations, each with about 20 single-element reads,
writes and column updates, so at ~0.2 ms per eager launch the QR stage
dominated. Tensors on theneurondevice with n below the threshold now
go totorch.linalg.eigh, and the result stays on the input's device.
TRNSOLVER_EIGH_NKI_MIN_N=0turns the cutover off, and
TRNSOLVER_REQUIRE_NKI=1bypasses it, so the hardware suite still runs the
kernels. Other devices, including CPU inputs with the NKI backend, are not
affected. The NKI path is single-tile (n <= 128), so with the default every
size it supports uses aten. Undertorch.compilethe cutover runs eagerly
behind the same single graph break as the NKI path. (#42) -
The NKI
eighpath runs its QR iteration on CPU tensors.diag,
subdiagand the Givens accumulator are copied to the host before
_qr_iterate, and the accumulator is moved back to the input's device
beforeapply_reflectors_kernel. CPU inputs already took this path, so
their results are unchanged. Device-resident inputs no longer pay one
launch or sync per scalar. (#42) -
apply_reflectors_kernelnow compiles under NKI 0.7.0 / neuronx-cc 2.28
(Trn2). The kernel loaded all Householder vectors as an SBUFV^Tand
sliced rowV_T[k:k+1, :]per step. That is a 1-partition access at
partition k, and the newer verifier only allows those to start at partition
0, 32, 64 or 96 ('nisa.tensor_copy' op Expected 'src' partition start in {0, 32, 64, 96}). This failed all 19TestEighNKIhardware tests. Each
vector is now loaded per step withload_transpose2d(V_refs[:, k:k+1]), so
it lands at partition 0, the same idiommatmul_kernel_simuses. The
algorithm is unchanged and simulator output is bit-identical to the old
kernel for n ∈ {4, 8, 16, 33, 64, 128}. (#40) -
benchmarks/bench_solver.py::TestEigh::test_eigh_nkiskips n > 128.
The NKI eigh path is single-tile, and_householder_tridiagraises
NotImplementedErrorfor n > 128, so the 256 and 512 cases failed on
hardware rather than measuring anything. The PyTorch and torch baselines
still run at every size. -
cholesky,solve,solve_spd,inv_spdandeigh_generalizeduse
only Neuron-lowerable ops undertorch.compile. On Trn2 there were two
separate failures:torch.linalg.choleskyandtorch.linalg.solvedecompose to a*_ex
kernel followed byaten._linalg_check_errors(info). That op only
raises, and newer PyTorch wraps it in thewith_effectshigher-order op,
which TorchNeuron's FxImporter does not implement (BackendCompilerFailed).- The Neuron backend has no lowering for
linalg_cholesky_exor
linalg_solve_exthemselves.
Inside a compiled region (
torch.compiler.is_compiling()), real inputs now
use traceable implementations built from elementwise ops,where,mm,
sqrt,argmax,gatherandtril/triu. Each is a static loop that
unrolls at trace time, with no.item():_cholesky_traceable: right-looking column Cholesky that reads only the
lower triangle, likepotrf._solve_lu_traceable: Gaussian elimination with partial pivoting on
[A | B]. It picks the same pivot asgetrf(argmax |a_ik|) and swaps
rows with agatherpermutation._solve_triangular_traceable: forward and back substitution, used by
solve_spdin place oftorch.linalg.solve_triangular.
Compiled results match eager to about 1e-6 in fp32 and 1e-13 in fp64 at
n = 128. Undertorch.compile, a non-SPD or singular input yields NaN or
inf instead of raising. Eager calls and complex inputs are unchanged. (#41) -
qrtraces to StableHLO-lowerable ops undertorch.compile.
aten.linalg_qrhas no StableHLO lowering on Neuron. When compiling, real
inputs now use_householder_qr, a reduced Householder QR made of
elementwise ops,wheremasks,mmandtriu. It has no.item(), no
data-dependent control flow and no in-place slice assignment, and its
static column loop unrolls at trace time. It uses LAPACK's sign convention
(R[k, k] = -sign(A[k, k]) * ||A[k:, k]||, and the reflector is skipped
when the sub-column is already zero), so Q and R matchtorch.linalg.qrto
rounding. Supports batch dimensions. Eager calls and complex inputs still
usetorch.linalg.qr. (#41) -
eigh(NKI path),cg,gmresandgmres_irare documented as
eager-only and wrapped intorch.compiler.disable. Their convergence and
deflation logic is data-dependent Python control flow on.item()scalars,
and they return Python numbers. Undertorch.compilethis split the graph
into many pieces (26 graph breaks foreighat n=128, 4 forcg). They now
run eagerly behind one graph break, andfullgraph=Trueraises a clear
Unsupportederror. The algorithms are unchanged. (#41)
Added
-
tests/test_compile.py: CPU checks withtorch.compile(backend="aot_eager", fullgraph=True):cholesky,solve(including a case that needs pivoting and a vector
right-hand side),solve_spd,inv_spdandqrtrace with zero graph
breaks and match eager in fp32 and fp64.- The traced aten graph contains none of
_linalg_check_errors,
linalg_cholesky_ex,linalg_solve_ex,linalg_solve_triangular,
linalg_luorlinalg_qr. - Eager
choleskyandsolvestill raiseLinAlgError. - The traceable kernels match LAPACK in fp64, including batches, pivoting
and zero columns. cgfalls back to eager. Withfullgraph=Trueit raisesUnsupported
on torch 2.11 andUserErroron 2.14.
-
tests/test_nki_compile.py(markernki_simulator): runs the NKI
frontend (trace → MLIR →verify()) on CPU for every@nki.jitkernel at
n ∈ {8, 16, 32, 128} for both trn1 and trn2.nki.simulatedoes not run
the MLIR verifier, so this catches hardware-only lowering errors like #40
without a Neuron device. Needsnki, but notneuronx-cc.