Skip to main content

trnfft

CI PyPI Python License Docs

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:

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

# With Neuron hardware support
pip install trnfft[neuron]

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 (matches torch.stft signature)
waveform = torch.randn(16000)
S = trnfft.stft(waveform, n_fft=512, hop_length=256)

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)
  • 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 against Neuron SDK 2.24+ on the Deep Learning AMI Neuron PyTorch 2.9 (Ubuntu 24.04) AMI (20260410 or later). 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.10.0 — NKI kernels validated on trn1.2xlarge. For STFT and batched FFT, set_backend("nki") beats vanilla torch.fft.fft. See benchmarks for the full picture.

API coverage (13 common torch.fft functions): fft, ifft, rfft, irfft, fft2, rfft2, irfft2, fftn, ifftn, rfftn, irfftn, stft, istft.

Not implemented: hfft, ihfft — Hermitian-symmetric input variants. These assume the input tensor is already conjugate-symmetric (X[k] = conj(X[N-k])), which in practice means you've post-processed an rfft output or are reconstructing from a known real signal's spectrum. Both workflows are easier expressed with rfft / irfft plus a manual unpack/pack step. If you have a use-case producing Hermitian-symmetric tensors directly, open an issue with the concrete workload and we'll add them.

Roadmap

  • NKI ComplexConv1d / ComplexModReLU kernels (today both fall back to PyTorch on NKI)
  • BF16 / FP16 support across NKI kernels
  • Multi-NeuronCore parallelism (scaffold in trnfft/nki/multicore.py)
  • SBUF-resident dispatch to reduce small-op overhead

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.4.0
trnrand Philox / Sobol / Halton random number generation v0.1.0
trnsolver Linear solvers (CG, GMRES) and eigendecomposition v0.3.0
trnsparse Sparse matrix operations v0.1.1
trntensor Tensor contractions (einsum, TT/Tucker decompositions) v0.1.1
neuron-complex-ops Original proof-of-concept, folded into trnfft archived

License

Apache 2.0 — Copyright 2026 Scott Friedman

Acknowledgments

Built on insights from:

Metadata

Release files for trnfft 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trnfft 0.12.0
File Size Uploaded
trnfft-0.12.0.tar.gz 96.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for trnfft 0.12.0
File Interpreter ABI Platform
trnfft-0.12.0-py3-none-any.whl Python 3 none any Details

Total release size: 130.9 kB

Release files / trnfft-0.12.0.tar.gz

Download URL trnfft-0.12.0.tar.gz
Size 96.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3ac3cdac4fe907f41ed4ba2c16ef52aaa77e07a52fab274c75c2d855714c912b
BLAKE2b-256 checksum
How to use checksums
8dba27710822819b748169034b1a47a2bbe1716157f5c3b2f3e403ca8240c403
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Apr 13, 2026.

Transparency log

Release files / trnfft-0.12.0-py3-none-any.whl

Download URL trnfft-0.12.0-py3-none-any.whl
Size 34.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c2698496cfcdb754ae09aa100f8331118e08a762381820050d0bfba31d4c2312
BLAKE2b-256 checksum
How to use checksums
19f6362d4655f9a6428a4ac28e5d28acd7b1dc3a80ee186284577d81501c14ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Apr 13, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.12.0 This release

2 release files

0.11.0

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page