Skip to content

v0.24.0

Choose a tag to compare

@scttfrdmn scttfrdmn released this 31 Oct 04:51
· 133 commits to main since this release
v0.24.0
8742a1c

Native TorchNeuron dispatch (Trn2/Trn3) alongside the PyTorch/XLA path (Trn1, maintenance); validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0) — see trnsci/trnsci#35.

Headline performance: batch-fused four-step FFT kernel, one launch per batch; N=4096 B=1024 goes from 10.1 to 0.93 ms on Trn2 (#91).

Added

  • Native PyTorch dispatch on TorchNeuron, alongside XLA (#80). On
    TorchNeuron (Neuron SDK 2.33+, torch-neuronx 2.14, PyTorch 2.14,
    Trn2/Trn3) every NKI kernel is registered as a PyTorch custom op with
    torch_neuronx.nki_op and dispatched on device="neuron", with no
    torch_xla import. Ops: trnfft::complex_gemm, complex_gemm_bf16,
    complex_linear, complex_mul, butterfly_stage, butterfly_stage_kahan,
    stockham_radix4_stage, stockham_radix8_w8, plus the composite
    trnfft::fft. Complex values stay real/imag pairs. Kernel bodies are
    unchanged. Earlier Trn2 results ran through a test-only torch_xla
    stand-in; this is the real native path. Trn2 native suite: 62 passed,
    1 xfailed (composite trnfft::fft under torch.compile); Trn1 XLA shows
    no regression against main.
    • The dispatch path is detected on the first kernel launch: native when
      torch_neuronx exports nki_op and a neuron device is available, XLA
      when torch_xla is installed, otherwise none. The XLA path (Trn1) is
      unchanged, as are the PyTorch fallback and TRNFFT_USE_SIMULATOR.
    • One launch helper, dispatch._launch, carries every kernel launch: the
      host wrappers, complex_gemm_bf16, the autograd forwards, the butterfly
      driver, and the row-chunked Stockham stages (_launch_stockham_stage,
      #83), which keep their operands on the device between stages. The
      single-GEMM DFT and the fused four-step (#82) go through it via
      complex_gemm / complex_mask_apply.
    • Autograd on the native path uses torch.library.register_autograd on
      complex_gemm, complex_linear, complex_mul and trnfft::fft (the
      fft_autograd adjoint, IFFT·n / FFT/n). The formulas are shared with the
      torch.autograd.Function wrappers, which the XLA path keeps.
    • Custom ops are registered lazily (trnfft/nki/_native.py is imported only
      when the native path is selected), so CPU-only installs and the XLA
      lineage see no import-time side effects.
    • TRNSCI_NKI_DISPATCH=auto|native|xla|cpu forces the dispatch path (a
      suite-wide variable shared by all trnsci libraries). Forcing an
      unavailable path raises instead of falling back.
    • TRNSCI_XLA_DEPRECATION=1 makes the XLA path emit one
      DeprecationWarning. Off by default (#35).
    • New [torchneuron] extra (torch>=2.14, torch-neuronx>=2.14,
      nki>=0.6); [neuron] stays the PyTorch/XLA lineage.
    • Tests: tests/test_native_dispatch.py (CPU: path selection, launch
      routing, lazy registration, schemas, fake shapes, the deprecation hook,
      and register_autograd gradients against torch references);
      tests/test_nki_native.py (@pytest.mark.neuron, native only: each op
      against nki.simulate, end-to-end FFT, gradients, torch.compile).
      conftest.py sets NKI_ENABLE_TRACE_CACHE=0 by default.
    • Tests: new slow marker; slow tests are skipped unless --run-slow or
      -m slow is given. TestKahanCharacterization at N=8193 (about 19 min on
      Trn2) is marked slow so the default -m neuron run stays short.

Changed

  • Batched FFT: one fused four-step kernel launch per batch (#82 follow-up).
    New four_step_fft_kernel (trnfft/nki/four_step.py, native op
    trnfft::four_step_fft) runs the whole four-step transform (GEMM, twiddle,
    GEMM) for a group of rows in SBUF/PSUM. It reads the batch once and writes it
    once, with no transposes. The unfused driver needed 2 GEMM launches, 1
    twiddle launch and 6 transpose copies, each over the whole batch. Default
    for FP32 power-of-2 512 ≤ N ≤ 4096. N ≤ 256 keeps the single DFT-GEMM.
    • Where the time went: profiling on Trn2 at N=4096 put 78% of B=1024
      (10.9 of 13.9 ms) inside the two _complex_gemm_kernel launches, which
      re-transpose A through DMA on each of 512 M-tiles with a 64-wide moving
      operand. At B=16 the cost was the ~0.3 ms dispatch floor times ~9 device
      ops. Host Python was about 1.6 ms per call, overlapped with the device.
    • Kernel layout: each row is read as an (N1, N2) matrix whose partition
      axis is already step 1's contraction axis. P = 128/N2 rows are packed per
      stationary operand, and step 2 uses a block-diagonal W2, so all 128 PE
      columns are used. X_re and X_im are stacked on the contraction axis
      against [[W1r, W1i], [-W1i, W1r]], so complex step 1 is one K=128
      matmul. Trn2 at N=4096, B=1024: one row per stationary 3.13 ms, packed
      rows 1.31 ms, two-matmul step 1 0.88 ms, one K=128 matmul 0.79 ms
      (kernel only). Splitting the twiddle across GpSimd/Scalar made it slower
      and was dropped.
    • Trn2 (SDK 2.33 / NKI 0.7.0), device-resident warm median ms, before →
      after (2-thread CPU torch.fft.fft in brackets): N=4096 B=1024 10.11 →
      0.93 [10.97]; B=128 3.27 → 0.44 [0.38]; N=2048 B=1024 2.67 → 0.62
      [1.33]; N=1024 B=1024 0.83 → 0.54 [0.58]. Rel err vs FP64 is unchanged
      at 2048/4096 (1.5e-7/1.6e-7) and lower at 512/1024 (1.22e-7/1.41e-7,
      was 1.36e-7/1.54e-7). Results are not bit-identical to the unfused
      driver (step 1 accumulates K=2·N1 in one pass): max abs difference is
      6e-5 on outputs of order 10².
    • It also removes an unfused-driver limit: at N=4096 the fused-twiddle
      _complex_mul_kernel asserted for any B > 16 that is not a multiple of
      16 (e.g. B=38). The fused kernel takes any B.
    • Knobs: TRNFFT_FOUR_STEP_BATCH_FUSED=0 restores the unfused dispatch.
      TRNFFT_FOUR_STEP_FUSED_MIN (default 512) sets the lower N bound.
      TRNFFT_FOUR_STEP_FUSED_MAX_ROWS (default 4096) splits larger batches
      into several launches, because first-call compile time grows with B
      (3 s at B=1024, 17 s at B=8192).
    • scripts/bench_batched_fft.py reproduces the table.