Skip to content

v0.16.0

Choose a tag to compare

@scttfrdmn scttfrdmn released this 31 Oct 04:53
· 252 commits to main since this release
v0.16.0
6191736

Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron device) — see trnsci/trnsci#35.

Fixed

  • eigh on the native neuron device uses torch.linalg.eigh below
    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 the neuron device with n below the threshold now
    go to torch.linalg.eigh, and the result stays on the input's device.
    TRNSOLVER_EIGH_NKI_MIN_N=0 turns the cutover off, and
    TRNSOLVER_REQUIRE_NKI=1 bypasses 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. Under torch.compile the cutover runs eagerly
    behind the same single graph break as the NKI path. (#42)

  • The NKI eigh path runs its QR iteration on CPU tensors. diag,
    subdiag and the Givens accumulator are copied to the host before
    _qr_iterate, and the accumulator is moved back to the input's device
    before apply_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_kernel now compiles under NKI 0.7.0 / neuronx-cc 2.28
    (Trn2).
    The kernel loaded all Householder vectors as an SBUF V^T and
    sliced row V_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 19 TestEighNKI hardware tests. Each
    vector is now loaded per step with load_transpose2d(V_refs[:, k:k+1]), so
    it lands at partition 0, the same idiom matmul_kernel_sim uses. 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_nki skips n > 128.
    The NKI eigh path is single-tile, and _householder_tridiag raises
    NotImplementedError for 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_spd and eigh_generalized use
    only Neuron-lowerable ops under torch.compile.
    On Trn2 there were two
    separate failures:

    • torch.linalg.cholesky and torch.linalg.solve decompose to a *_ex
      kernel followed by aten._linalg_check_errors(info). That op only
      raises, and newer PyTorch wraps it in the with_effects higher-order op,
      which TorchNeuron's FxImporter does not implement (BackendCompilerFailed).
    • The Neuron backend has no lowering for linalg_cholesky_ex or
      linalg_solve_ex themselves.

    Inside a compiled region (torch.compiler.is_compiling()), real inputs now
    use traceable implementations built from elementwise ops, where, mm,
    sqrt, argmax, gather and tril/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, like potrf.
    • _solve_lu_traceable: Gaussian elimination with partial pivoting on
      [A | B]. It picks the same pivot as getrf (argmax |a_ik|) and swaps
      rows with a gather permutation.
    • _solve_triangular_traceable: forward and back substitution, used by
      solve_spd in place of torch.linalg.solve_triangular.

    Compiled results match eager to about 1e-6 in fp32 and 1e-13 in fp64 at
    n = 128. Under torch.compile, a non-SPD or singular input yields NaN or
    inf instead of raising. Eager calls and complex inputs are unchanged. (#41)

  • qr traces to StableHLO-lowerable ops under torch.compile.
    aten.linalg_qr has no StableHLO lowering on Neuron. When compiling, real
    inputs now use _householder_qr, a reduced Householder QR made of
    elementwise ops, where masks, mm and triu. 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 match torch.linalg.qr to
    rounding. Supports batch dimensions. Eager calls and complex inputs still
    use torch.linalg.qr. (#41)

  • eigh (NKI path), cg, gmres and gmres_ir are documented as
    eager-only and wrapped in torch.compiler.disable.
    Their convergence and
    deflation logic is data-dependent Python control flow on .item() scalars,
    and they return Python numbers. Under torch.compile this split the graph
    into many pieces (26 graph breaks for eigh at n=128, 4 for cg). They now
    run eagerly behind one graph break, and fullgraph=True raises a clear
    Unsupported error. The algorithms are unchanged. (#41)

Added

  • tests/test_compile.py: CPU checks with torch.compile(backend="aot_eager", fullgraph=True):

    • cholesky, solve (including a case that needs pivoting and a vector
      right-hand side), solve_spd, inv_spd and qr trace 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_lu or linalg_qr.
    • Eager cholesky and solve still raise LinAlgError.
    • The traceable kernels match LAPACK in fp64, including batches, pivoting
      and zero columns.
    • cg falls back to eager. With fullgraph=True it raises Unsupported
      on torch 2.11 and UserError on 2.14.
  • tests/test_nki_compile.py (marker nki_simulator): runs the NKI
    frontend (trace → MLIR → verify()) on CPU for every @nki.jit kernel at
    n ∈ {8, 16, 32, 128} for both trn1 and trn2. nki.simulate does not run
    the MLIR verifier, so this catches hardware-only lowering errors like #40
    without a Neuron device. Needs nki, but not neuronx-cc.