Skip to content

v0.25.0 — correctness fix (upgrade recommended)

Choose a tag to compare

@scttfrdmn scttfrdmn released this 01 Nov 00:31
· 116 commits to main since this release
v0.25.0
3082c34

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

Headline: correctness fix — upgrade recommended. Releases up to and including 0.24.0 return silently wrong results when a GEMM dimension is not a whole number of tiles: the small-N DFT-GEMM (N < 512) for batches over 128 rows not a multiple of 128 (e.g. rows 896–999 unwritten at B=1000, N=64), every bf16 precision mode (bf16, bf16_refined, ozaki, ozaki_hq) at such batches, nn.ComplexLinear at awkward shapes, and direct complex_gemm / complex_gemm_bf16 calls with M, K or N tails (#99, #101, #100). Also: the six-step FFT is the default for power-of-2 N ≥ 8192 (9.7–340× faster, 11–17× lower error, #96), host batches move once (#94), pinned-memory downloads (#98), and kernels no longer use load_transpose2d, which rebuilt DMA rings on every new input address (#97, aws-neuron/aws-neuron-sdk#1452).

Fixed

  • Tail tiles in every GEMM-family kernel (#100). _complex_gemm_kernel,
    its load_transpose2d twin, _complex_gemm_kernel_bf16 and
    _complex_linear_kernel ran M // 128, K // 128 and N // 512 tiles
    with no tail. The last partial tile of each dimension was dropped
    silently:
    • rows past the last full 128-row tile were never written
      (complex_gemm_bf16 on 1000 × 64: rows 896–999; on 4097 rows: row
      4096);
    • a K tail lost contraction terms, and an N tail left columns unwritten
      (complex_gemm at 200 × 200 × 600: every row wrong).
      This hit precision="bf16", "bf16_refined", "ozaki" and "ozaki_hq"
      at any batch over 128 that is not a multiple of 128 (released versions up
      to 0.24.0), nn.ComplexLinear with a batch, in_features or
      out_features like that, and complex_gemm / complex_gemm_bf16 called
      directly. The FP32 FFT paths were already padded by their drivers (#99).
      The kernels now trace the tail tile at its own static size after the full
      tiles; shapes that are whole tiles trace exactly as before (same bits).
  • _complex_mul_kernel (complex_mask_apply, the unfused four-step
    twiddle) raised for a (128, F) view with F over 512 and not a multiple of
    512; it now does a tail tile too.
  • _complex_gemm_tn_kernel (six-step pass 1) now refuses shapes that are
    not whole tiles instead of dropping the tail. Its only caller already
    passes whole tiles.

Changed

  • The BF16 GEMM and the complex linear kernel no longer use
    nl.load_transpose2d (#100, as #97 did for the FP32 DFT-GEMM).
    They load
    plainly and transpose on the Tensor engine, so a fresh activation tensor
    no longer makes the runtime rebuild the kernel's IO DMA rings. Outputs are
    bit-identical to the load_transpose2d kernels on Trn2 at every case
    tested.

    • Trn2, fresh device input per call, medians in ms: complex_gemm_bf16
      8192 × 64 goes from 1.20 to 0.38 and 32768 × 64 from 3.04 to 0.94;
      ComplexLinear(256, 256) on 8192 rows from 10.4 to 1.22.
    • With one tensor reused they are slower (the Tensor-engine transposes):
      complex_gemm_bf16 32768 × 64 0.62 → 0.94, ComplexLinear(256, 256)
      0.96 → 1.20. The BF16 FFT has no row packing yet.
    • TRNFFT_PE_TRANSPOSE=0 selects the old kernels
      (_complex_gemm_bf16_t2d_kernel, _complex_linear_t2d_kernel), and the
      XLA path (Trn1) keeps them by default.
  • The small-N DFT-GEMM and the radix-8 Stockham kernel no longer use
    nl.load_transpose2d (#97).
    _complex_gemm_kernel (the default FFT for
    N < 512, and every complex_gemm) and stockham_radix8_w8_kernel load
    their input with a plain nl.load and transpose each tile on the Tensor
    engine (nisa.nc_transpose). Outputs are bit-identical on Trn2 at every
    case measured.

    • On Trn2 (SDK 2.33) the runtime rebuilds a load_transpose2d input's IO
      DMA rings whenever its address changes, so every call with a fresh
      device tensor paid 1–4.4 ms of kbl_exec_pre (32768 × 64: 2.37 MB of
      ring descriptors per plane). Plain loads, and even element-strided DMA
      access patterns, do not (scripts/repro_dma_ring_rebuild.py).
    • For N < 128 the DFT-GEMM packs 128 / N rows into one GEMM row against a
      block-diagonal W, so it does fewer, fuller tiles. With it the
      Tensor-engine transposes cost nothing measurable for N < 128.
    • Device input, two tensors alternating, medians in ms: 32768 × 64 goes
      from 9.75 to 0.85, 8192 × 256 from 4.36 to 0.66 and 1024 × 64 from 0.96
      to 0.33. Bluestein (its sub-FFT inputs are fresh tensors on every call)
      gets faster even with the same input: 8192 × 100 goes from 14.8 to 6.4.
      With the same tensor reused, N = 128 and 256 at 8192 rows or more are
      0.06–0.54 ms slower (the transposes, no packing at K = 128). Host-in,
      host-out calls do not change much: the allocator usually hands back
      the same device address.
    • TRNFFT_PE_TRANSPOSE=0 (read per call) restores the load_transpose2d
      kernels; TRNFFT_GEMM_PACK=0 turns off the row packing alone. The XLA
      path (Trn1) keeps the load_transpose2d kernels by default.
  • Fixed: the DFT-GEMM returned wrong rows for batches over 128 rows that
    are not a multiple of 128 (#97).
    The kernel tiles rows by 128 with no tail
    tile, so for B = 1000, N = 64 rows 896–999 were never written. Such batches
    are now zero-padded to a multiple of 128.

  • The six-step's first pass at N = 2^13 reads x in place (#97). It no
    longer transposes x and runs the load_transpose2d DFT-GEMM. A new kernel,
    _complex_gemm_tn_kernel, loads the (B, N2, N1) view of x with plain
    nl.loads. Outputs are bit-identical.

    • Cause of the 1024 × 2^13 anomaly in #96: on Trn2 (SDK 2.33) the runtime
      rebuilds a transposed load's IO DMA rings whenever the input address
      changes (neuron-explorer system trace: kbl_exec_pre 2–9 ms and
      4.7 MB of ring descriptors per launch, against ~12 µs otherwise). Above
      32768 rows the pass is split into launches on different row slices, so
      every launch paid it. In four 1024 × 2^13 calls, 16 launches rebuilt
      their rings before this change and none did after it.

    • Device input, warm medians, ms (logical core 1, host threads on CPUs 3,9):

      B × 2^13 before after
      1 1.69 1.51
      16 1.69 1.46
      256 3.48 3.41
      512 13.3 5.83
      1024 18.5 10.6
      2048 77.6 20.1
      4096 156 40.4
    • TRNFFT_SIX_STEP_TN=0 restores the old pass. _fft_via_gemm (the
      default for N < 512) still uses load_transpose2d, so it still pays
      the rebuild once whenever its input is at a new address (#97).

  • Large host results come from the caching host allocator, as in
    trnblas#64, trnrand#70 and trntensor#57. On the native TorchNeuron path,
    a device-to-host copy into fresh pageable memory runs at about 4 GB/s. A
    copy into torch.empty(..., pin_memory=True) runs at 13–14 GB/s. An FFT
    result has two planes (real and imaginary), so it makes two such copies.

    • Every host-output exit uses it: _launch, the fused four-step, the
      six-step, the Stockham and butterfly drivers, the batch split across
      cores (each shard now writes into its rows of one output) and the
      host-staged multi-core six-step.

    • Planes of 1 MiB and up are pinned, and only when
      torch.accelerator.current_accelerator().type == "neuron". A CUDA build
      with no GPU raises on pin_memory=True.

    • TRNFFT_PINNED_OUTPUT=0 turns it off. Each call still returns a fresh
      tensor. Cached blocks are rounded up to a power of two and stay cached
      until torch.accelerator.empty_host_cache().

    • Host input and host output, warm medians in ms. Outputs are bitwise equal
      to the pageable ones in every case, forward and inverse
      (scripts/bench_pinned_output.py):

      B × N pageable pinned device-resident
      16384 × 4096 190 88.0 10.6
      16384 × 1024 48.2 23.4 3.54
      4096 × 4096 48.7 22.3 2.39
      1024 × 4096 13.3 6.05 0.82
      1024 × 2^13 33.3 21.0 10.7
      256 × 2^16 59.9 35.0 15.0
      16 × 2^20 54.4 28.4 8.50
      4 × 2^22 53.6 27.9 7.96
      1 × 2^22 14.4 8.31 3.01
      1 × 2^24 54.2 29.1 9.30
      1 × 2^25 142 94.2 54.2
      1 × 2^26 308 207 129

      Results of 8 MiB per plane and less are unchanged within noise
      (1 × 2^13 to 2^20, 16 × 2^13, 256 × 2^13, 64 × 2^14 and 16 × 2^16). The
      inverse gains the same: 16384 × 4096 goes from 181 to 87.7 ms.

  • The six-step FFT is the default for power-of-2 FP32 N >= 8192 (#94).
    "fast" and "kahan" take it in place of the row-chunked Stockham (fast) and
    compensated butterfly (kahan) paths. That includes ifft, rfft/irfft,
    fft2/fftn along a long axis, and Bluestein sizes that pad to 8192 or
    more, since they all go through fft_core.

    • TRNFFT_SIX_STEP=0 restores the old path, and =1 forces the six-step
      for every N above 4096. TRNFFT_SIX_STEP_MIN moves the threshold.

    • The six-step won every case measured from 2^13 up, on warm and first
      calls, from device and host input. The smallest gain is 5× at 1 × 2^13
      from the host; from device input it is 9.7×. Error is 11–17× lower.

    • Trn2, one logical core, SDK 2.33. Times are warm medians in ms, and first
      calls in s, each in a fresh process with an empty compile cache. "rel" is
      relative L2 error against FP64 torch.fft
      (scripts/bench_six_step_crossover.py):

      B × N old, device old, host old, first call old rel six-step, device six-step, host six-step, first call six-step rel
      1 × 2^13 12.9 11.7 9.5 1.7e-6 1.33 2.30 6.3 1.5e-7
      1 × 2^14 12.9 13.2 9.5 2.1e-6 1.66 2.07 4.4 1.4e-7
      1 × 2^15 14.7 14.6 8.9 2.4e-6 1.60 2.10 6.1 1.5e-7
      1 × 2^16 16.6 16.2 12.6 2.1e-6 1.93 2.48 4.6 1.6e-7
      1 × 2^18 47.9 50.7 11.1 2.9e-6 1.67 2.10 4.6 1.8e-7
      1 × 2^20 121 112 82 2.9e-6 2.02 3.25 5.2 2.1e-7
      1 × 2^22 698 663 272 2.9e-6 3.11 15.6 6.6 2.2e-7
      1 × 2^24 1625 1977 163 3.7e-6 9.34 55.9 14.0 2.3e-7
      1 × 2^25 5913 5770 610 3.3e-6 82.1 190 47.6 2.3e-7
      1 × 2^26 9577 9843 627 3.7e-6 130 311 50.6 2.2e-7
      16 × 2^13 24.4 29.9 13.8 1.7e-6 1.71 1.89 6.6 1.5e-7
      256 × 2^13 264 244 135 1.7e-6 3.34 6.57 10.5 1.5e-7
      1024 × 2^13 844 874 541 1.7e-6 41.7 66.9 18.4 1.5e-7
      64 × 2^14 87.7 83.4 81.6 2.1e-6 2.47 4.58 5.8 1.4e-7
      16 × 2^16 114 108 81.4 2.1e-6 2.08 3.55 5.7 1.6e-7
      256 × 2^16 2304 2264 577 2.1e-6 15.3 69.0 26.6 1.6e-7
      16 × 2^20 1887 1817 588 2.9e-6 8.50 57.2 17.6 2.1e-7
      4 × 2^22 2800 2788 592 2.9e-6 8.28 55.2 12.9 2.2e-7
    • The inverse matches: 1 × 2^14 / 2^20 / 2^24 take 11.4 / 104 / 1788 ms on
      the old path and 1.85 / 2.04 / 9.28 ms on the six-step, at the same errors.

    • "kahan" on the six-step: 1.67 ms at 2^16, 1.51 ms at 2^20 and 1.84 ms at
      16 × 2^16, with errors of 1.6e-7 to 2.1e-7. The old path (the compensated
      butterfly) had not finished its first call at 2^16 after 19 min, so it was
      not run at the larger sizes.

    • 1024 × 2^13 was 12× slower than 256 × 2^13 for 4× the data. #97 has
      the cause and the fix (below).

    • The first calls here are longer than the 8–20 s reported in #93 (48–51 s
      at 2^25 and 2^26). That run reused compiled kernels across cases; these
      runs did not.

  • The fused four-step path moves a host batch to the device once (#94).
    _fft_via_four_step_fused launched on host slices of 4096 rows, so
    _launch copied each slice in and each result out, and the results were
    concatenated on the host. It now moves the batch in once, runs the launches
    on device slices, and returns the result once, as the six-step does.
    Outputs are bit-identical. Host in and out, warm ms:

    B × N before after caller moves the batch (.to / .cpu()) device-resident
    16384 × 4096 348 / 353 185 / 185 186–230 11.1
    16384 × 1024 90 / 93 56 / 53 49–56 3.7
    4096 × 4096 57 49 49–50 2.7
    1024 × 4096 (one launch) 13.5 13.5 13.8–14.0 0.8–1.1
    • The per-slice copies were not the cost on their own. Four 64 MB
      host-to-device copies took 43 ms against 40 ms for one 256 MB copy, and
      device-to-host took 173 against 167 ms. Concatenating the slices on the
      host took 136 ms on 2 CPUs. The copy back after each launch probably also
      kept the launches from overlapping; that part is inferred, not profiled.

Added

  • Six-step FFT for large N (TRNFFT_SIX_STEP, #54, #93; default since #94).
    Power-of-2 FP32 transforms above N=4096 can run as N = N1·N2: N1 row FFTs
    of length N2, a twiddle multiply, then N2 row FFTs of length N1, with three transposes, all
    on the device. Each row pass is one batched transform on the batch-fused
    four-step kernel (#82). The default path runs 8–13 row-chunked Stockham stages
    (#83).
    • The split is balanced up to 2^24. Above that, N1 = 4096 and the N2 pass
      recurses.

    • Passes from length 128 up use the fused kernel. TRNFFT_SIX_STEP_FUSED_MIN
      sets this length; the normal dispatch starts the fused kernel at 512.

    • Passes below that length use the single DFT-GEMM, split into launches of at
      most 2^15 rows (TRNFFT_SIX_STEP_GEMM_MAX_ROWS).

    • A host input moves to the device once.

    • Trn2, one logical core, warm medians in ms. Device-resident input unless
      noted. The CPU column is torch.fft on 2 pinned threads:

      B × N default six-step six-step, host in/out CPU
      1 × 2^20 115 1.72 3.42 2.55
      1 × 2^22 616 3.06 8.68 22.9
      1 × 2^24 2006 9.15 57.1 100
      1 × 2^25 – 98.6 213 208
      1 × 2^26 – 130 355 447
      16 × 2^20 1869 8.52 56.4 64.4
      256 × 2^16 2105 15.2 64.3 46.9
    • Relative L2 error against FP64 is 1.6e-7 to 2.3e-7 with the six-step,
      against 2.1e-6 to 3.7e-6 on the default path. CPU measures 1.5e-7 to 2.5e-7.

    • First calls took 8–20 s from 2^20 to 2^26. The default path's first call
      took 203 s at 2^24 and 247 s at 2^26 (#83).

    • The default dispatch was unchanged when the variable was unset. It is
      now the default from N=8192 (#94, above).

  • scripts/bench_multicore.py: the measurements above and below. It covers the
    default and six-step paths on one core, torch.fft on CPU, per-core resident
    shards, the host batch split, process per core, and the six-step split across
    cores.

Changed

  • set_multicore(True) now runs host-batch shards on separate NeuronCores
    (#54, #93).
    On the native TorchNeuron path, shard i of a host batch goes
    to neuron:i and gets its own host thread.
    • The first call for a shard layout runs the shards in turn, because NKI
      tracing is not thread-safe.
    • Native launches are serialised behind a lock. A launch only enqueues work,
      so the cores still run concurrently.
    • A batch already on a NeuronCore is no longer split. Reaching the other
      cores through the host cost 15–20× the single-core transform.
    • Before this change, the shards ran in turn on one device.
  • Trn2 measurements (4 logical cores, 2 host CPUs). The host link sets the
    time, not the cores:
    • Host-to-device copies run at about 12 GB/s whether 1, 2 or 4 cores copy
      at once.
    • Device-to-host runs at 3.4 GB/s for 64 MB tensors and about 10 GB/s as
      four 16 MB copies.
    • The threaded host split is at most 1.5× faster than one core. It is
      slower than torch.fft on the same 2 CPUs in every case measured.
    • One process per core does better: 213 → 120 ms at B=16384, N=4096,
      against 163 ms on CPU. It is not part of the library.
    • Shards the caller places on neuron:0..3, with trnfft.fft called on
      each from one thread, scale on compute alone: 10.9 → 6.3 → 4.9 ms at
      B=16384, N=4096 on 1 / 2 / 4 cores. This already worked before this
      change.
  • A six-step single transform with both passes split across cores
    (multicore._six_step_multicore, host-staged) is slower than one core:
    169 ms on 4 cores against 9.15 ms on one, at N=2^24. It is kept for the
    benchmark and is not wired into dispatch.