trnfft
FFT and complex-valued tensor operations for AWS Trainium via NKI.
Trainium has no native complex number support and ships no FFT library. trnfft fills that gap with split real/imaginary representation, complex neural network layers, and NKI kernels optimized for the NeuronCore architecture.
Incorporates neuron-complex-ops. Part of the trnsci scientific computing suite (github.com/trnsci).
Current phase
trnfft follows the trnsci 5-phase roadmap. Active work is tracked in phase-labeled GitHub issues:
- Phase 1 — correctness: complete as of v0.8.0 (butterfly + complex GEMM kernels hardware-validated on trn1.2xlarge, 70/70 benchmark cases passing).
- Phase 2 — precision (active): Kahan / Neumaier compensated summation. Partial delivery in v0.11.0 (precision-modes API + compensated butterfly). On-silicon kahan characterization (#58) and Thread C (Tensor-engine Stockham) are the remaining items.
- Phase 3 — perf: plan reuse, streaming large FFTs, NEFF cache reuse.
- Phase 4 — multi-chip: multi-chip large-N FFT (N > 2²⁰).
- Phase 5 — generation: trn2 larger-SBUF butterfly path.
Suite-wide tracker: trnsci/trnsci#1.
Why
NVIDIA has cuFFT, cuBLAS, and native complex64. Trainium has none of these. Every signal processing, speech enhancement, physics simulation, and spectral method workload on Trainium currently falls back to CPU or requires hand-rolling complex arithmetic. trnfft fixes this.
Install
pip install trnfft
# Trn2/Trn3 with TorchNeuron (native PyTorch, Neuron SDK 2.33+)
pip install "trnfft[torchneuron]" --extra-index-url https://pip.repos.neuron.amazonaws.com
# Trn1 / PyTorch-XLA lineage
pip install "trnfft[neuron]" --extra-index-url https://pip.repos.neuron.amazonaws.com
Kernels dispatch on whichever Neuron PyTorch lineage is installed:
- TorchNeuron (native): Trn2/Trn3, Neuron SDK 2.33+, Python ≥ 3.12,
torch2.14,torch-neuronx2.14.*. Each kernel is a PyTorch custom op (torch.ops.trnfft.*) on theneurondevice; notorch_xla. Gradients flow throughtorch.library.register_autograd. - PyTorch/XLA: Trn1 (and any host on the
torch-neuronx2.9 lineage). Kernels dispatch throughtorch_xla, unchanged.
trnfft picks the native path when the native torch_neuronx and a neuron
device are present, and the XLA path when torch_xla is installed. Set
TRNSCI_NKI_DISPATCH=native, xla or cpu (shared by all trnsci libraries)
to force a path. With an editable install (pip install -e), set
NKI_ENABLE_TRACE_CACHE=0: the NKI kernel cache only versions installed
wheels and can serve stale kernels. See
installation.
Known platform issue: NKI trace cache and kernel factories
import torch_neuronx turns on the NKI trace cache, and the cache key ignores
constants captured in a kernel's closure: two @nki.jit kernels built by one
factory, differing only in a captured value, share one trace, and the second
silently returns the first's results
(aws-neuron/aws-neuron-sdk#1454).
trnfft's kernels do not rely on closure constants (or on module globals that
can differ between processes): every value that specialises a kernel is a
kernel argument, and tests/test_trace_cache_safety.py enforces this. If you
write your own NKI kernels with a factory, set NKI_ENABLE_TRACE_CACHE=0
before importing torch_neuronx.
Usage
import torch
import trnfft
# Drop-in replacement for torch.fft
signal = torch.randn(1024)
X = trnfft.fft(signal)
recovered = trnfft.ifft(X)
# Real-valued FFT
X = trnfft.rfft(signal)
# 2D FFT
image = torch.randn(256, 256)
F = trnfft.fft2(image)
# STFT (torch.stft's arguments; window=None means Hann here, ones in torch.stft)
waveform = torch.randn(16000)
S = trnfft.stft(waveform, n_fft=512, hop_length=256, window=torch.hann_window(512))
# Spherical harmonic transforms (Gauss grid 512 x 1024), batched over fields
from trnfft.sht import SHT
sht = SHT(lmax=511)
alm = sht.forward(torch.randn(128, sht.nlat, sht.nlon)) # ComplexTensor (128, 131328)
fields = sht.inverse(alm)
trnfft.sht runs the latitude (Legendre) stage as one NKI kernel launch. In
that launch the fp32 tables stay stationary in SBUF while every field streams
through the Tensor Engine, and all-zero polar tiles are skipped. The result is
within 1e-6 of fp64 ducc0. See docs/api/sht.md.
Complex Neural Network Layers
from trnfft import ComplexTensor
from trnfft.nn import ComplexLinear, ComplexConv1d, ComplexModReLU
# Build complex-valued models for speech/audio/physics
x = ComplexTensor(real_part, imag_part)
layer = ComplexLinear(256, 128)
y = layer(x)
Architecture
+--------------------------------------------+
| User Code / Model |
+--------------------------------------------+
| trnfft.api (torch.fft API) |
| fft() ifft() rfft() stft() fft2() |
+--------------------------------------------+
| trnfft.fft_core | trnfft.nn |
| Cooley-Tukey | ComplexLinear |
| Bluestein | ComplexConv1d |
| Plan caching | ComplexModReLU |
+------------------------+-------------------+
| trnfft.nki.dispatch |
| "auto" | "pytorch" | "nki" |
+--------------------------------------------+
| PyTorch ops | NKI kernels |
| (any device) | (Trainium only) |
| torch.matmul | nisa.nc_matmul |
| element-wise | Tensor Engine |
| | Vector Engine |
| | SBUF ↔ PSUM pipeline |
+------------------+------------------------+
How It Works
No complex dtype? Trainium's NKI doesn't support complex64/complex128. ComplexTensor stores complex values as paired real tensors and decomposes complex arithmetic into real-valued operations.
FFT → butterflies → matmul. Each Cooley-Tukey butterfly stage performs complex-multiply-and-add across all groups simultaneously. On NKI, the complex multiply maps to the Tensor Engine (systolic array).
Algorithms:
- Power-of-2: Cooley-Tukey radix-2 (iterative, decimation-in-time) on the PyTorch backend. On the NKI path, FP32: a single DFT-GEMM for N < 512, the batch-fused four-step (two GEMMs) up to N = 4096, and the six-step for N ≥ 8192 (the default since v0.25.0;
TRNFFT_SIX_STEP=0restores the Stockham / butterfly path) - Arbitrary sizes: Bluestein's chirp-z transform (pads to power-of-2)
NKI complex GEMM uses stationary tile reuse (2 SBUF loads instead of 8) and PSUM accumulation, overlapping Vector Engine negation with Tensor Engine matmul.
Hardware compatibility
NKI kernels are validated on Trn2 (trn2.3xlarge) against Neuron SDK 2.33 with native TorchNeuron, and on Trn1 against Neuron SDK 2.29+ on the Deep Learning AMI Neuron PyTorch 2.9 (Ubuntu 24.04) AMI (XLA lineage). See docs/installation.md for the full compatibility matrix.
Benchmarks
NKI vs PyTorch on the same Trainium instance — see the benchmarks page for the latest numbers.
Status
v0.26.0 — validated on Trn2 (Neuron SDK 2.33, native TorchNeuron). Correctness releases, upgrade recommended: 0.25.0 fixed GEMM tail tiles that ≤ 0.24.0 dropped silently (small-N DFT-GEMM, bf16 precision modes, nn.ComplexLinear, complex_gemm); 0.26.0 fixed irfft / hfft / irfft2 / irfftn using every bin instead of the first n//2+1 (#105) and float64 FFTs on the NKI path returning inf/NaN (#109). See the CHANGELOG.
API coverage (24 torch.fft functions):
fft, ifft, rfft, irfft, hfft, ihfft, fft2, ifft2, rfft2, irfft2, hfft2, ihfft2, fftn, ifftn, rfftn, irfftn, hfftn, ihfftn, fftshift, ifftshift, fftfreq, rfftfreq, stft, istft. Every transform takes torch.fft's n / s, dim and norm arguments with the same meaning (#123). Beyond torch.fft: scalar spherical harmonic transforms, trnfft.sht.SHT (#148).
Roadmap
- NKI
ComplexConv1d/ComplexModReLUkernels (today both fall back to PyTorch on NKI) - BF16 beyond the N ≤ 256 DFT-GEMM precision modes (
bf16,bf16_refined,ozaki,ozaki_hq); FP16 - Multi-NeuronCore parallelism:
set_multicoresplits host batches across cores, but on Trn2 the host link limits it (#93); large N runs best on one core, on the six-step path that is the default from N=8192 (#94;TRNFFT_SIX_STEP=0turns it off) - SBUF-resident dispatch to reduce small-op overhead
Known issues in earlier releases
Silent wrong-result bugs fixed in the 2026-10-31 to 2026-11-02 releases. Upgrade to the "fixed in" release or later. Details in the CHANGELOG.
| Bug | Affected versions | Fixed in |
|---|---|---|
GEMM-family kernels dropped tail tiles when M, K or N is not a whole tile: unwritten rows or columns, lost contraction terms. Hit the small-N DFT-GEMM, the bf16 / bf16_refined / ozaki / ozaki_hq modes at batches over 128 that are not a multiple of 128, nn.ComplexLinear, and complex_gemm / complex_gemm_bf16 (NKI) (#99, #100, #101) |
≤ 0.24.0 | 0.25.0 |
irfft / hfft / irfft2 / irfftn used every input bin instead of the first n // 2 + 1, wrong for most n (#104) |
≤ 0.25.0 | 0.26.0 |
float64 FFTs on the NKI path, and precision="double" Bluestein at N > 512, returned inf/NaN (#108) |
0.25.0 (earlier not measured) | 0.26.0 |
ComplexLinear / complex_linear with float64 host tensors on the NKI backend returned inf/NaN (#131) |
0.25.0 – 0.26.0 | 0.27.0 |
| Integer inputs computed the FFT in the integer dtype (truncated butterflies, 0/±1 Bluestein chirp) (#113) | 0.1.0 – 0.26.0 | 0.27.0 |
stft with center=True dropped the batch dim of a (1, L) input (#114) |
0.1.0 – 0.26.0 | 0.27.0 |
nn.ComplexModReLU applied its bias along the last dim instead of dim 1 for (B, C, L) input (#115) |
0.1.0 – 0.26.0 | 0.27.0 |
fftn / ifftn with dim=() returned a view of the input, not a copy (#116) |
0.2.0 – 0.26.0 | 0.27.0 |
A negative n / s sliced the input (e.g. n=-1 dropped the last sample) instead of keeping the size or raising (#123) |
≤ 0.26.0 | 0.27.0 |
Related projects in the trnsci suite
All six siblings are on PyPI, along with the umbrella meta-package:
| Project | What | Latest |
|---|---|---|
| trnsci | Umbrella meta-package pulling the whole suite | v0.1.0 |
| trnblas | BLAS Level 1–3 for Trainium | v0.9.1 |
| trnrand | Philox / Sobol / Halton random number generation | v0.12.1 |
| trnsolver | Linear solvers (CG, GMRES) and eigendecomposition | v0.20.0 |
| trnsparse | Sparse matrix operations | v0.12.1 |
| trntensor | Tensor contractions (einsum, TT/Tucker decompositions) | v0.22.0 |
| neuron-complex-ops | Original proof-of-concept, folded into trnfft | archived |
License
Apache 2.0 — Copyright 2026 Scott Friedman
Disclaimer
trnsci is an independent open-source project. It is not sponsored by, endorsed by, or affiliated with Amazon.com, Inc., Amazon Web Services, Inc., or Annapurna Labs Ltd.
"AWS", "Amazon", "Trainium", "Inferentia", "NeuronCore", "Neuron SDK", and related identifiers are trademarks of their respective owners and are used here solely for descriptive and interoperability purposes. Use does not imply endorsement, partnership, or any other relationship.
All work, opinions, analyses, benchmark results, architectural commentary, and editorial judgments in this repository and on trnsci.dev are those of the project's contributors. They do not represent the views, positions, or commitments of Amazon, AWS, or Annapurna Labs.
Feedback directed at the Neuron SDK or Trainium hardware is good-faith ecosystem commentary from independent users. It is not privileged information, is not pre-reviewed by AWS, and should not be read as authoritative about product roadmap, behavior, or quality.
For official AWS guidance, see aws-neuron documentation and the AWS Trainium product page.
Acknowledgments
Built on insights from:
- tcFFT — Tensor Core FFT research
- FFTW — Plan-based FFT architecture
- AWS NKI documentation
Metadata
Release files for trnfft 0.28.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| trnfft-0.28.1.tar.gz | 381.1 kB | With provenance | Details for trnfft-0.28.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| trnfft-0.28.1-py3-none-any.whl | Python 3 | none | any | Details for trnfft-0.28.1-py3-none-any.whl |
Total release size: 500.1 kB
Release files / trnfft-0.28.1.tar.gz
| Download URL | trnfft-0.28.1.tar.gz |
|---|---|
| Size | 381.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4c0d313915528c028dbacd97c7360b4958d6b715dfd5b6146f15cebbc5b5ab7d
|
|
BLAKE2b-256 checksum How to use checksums |
d34a8c24f48b2dce8d0e70f6f95138c77f3970e7acd46733586a784b83e3c26e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.
Transparency logRelease files / trnfft-0.28.1-py3-none-any.whl
| Download URL | trnfft-0.28.1-py3-none-any.whl |
|---|---|
| Size | 119.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9b45af359078974e0c2c2de0bdfe3a86aba30c9e64634bd3a502a7ee9a8019e8
|
|
BLAKE2b-256 checksum How to use checksums |
4610cdb25dc30a5a69bb499194a5d60395aa3e6c1e6f11019a3c1ce706e5c6a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.
Transparency logSecurity for trnfft 0.28.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for trnfft-0.28.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.