Repository navigation
v0.28.0 — spherical harmonic transforms, half-precision NKI input, fp64 policy
Validated on Trn2 (trn2.3xlarge, Neuron SDK 2.33.0, native TorchNeuron) — see trnsci/trnsci#35.
Headline: spherical harmonic transforms, trnfft.sht (#148, trnsci/trnsci#111); float16/bfloat16 input on the NKI path fixed (#133); float64 ComplexConv1d on device fixed (#134); cached eager NKI handles; and the trnsci float64 policy with a dtype guard at the kernel boundary. Deprecations: a repeated dim in the n-D transforms (#127) and the stft/istft window=None default (#106).
Lands in the next release (0.28.0). Needs device validation on Trn2.
Changed
- float64 under
autonow computes in torch at float64 accuracy
(previously cast to fp32 on the NKI path). This is the trnsci suite
float64 policy (#136). Trainium computes float64 at FP32 precision, so no
NKI kernel takes float64. Affected: float64 (and complex128) FFTs on the
NKI path in 0.26.0–0.27.0 (cast since #108), and float64complex_linear/
ComplexLinearin 0.27.0 (cast since #131). In 0.27.0 and earlier,
complex_gemmandcomplex_mask_apply(andcomplex_gemm_bf16) passed
float64 to the kernel, which returned inf/NaN. Now, for every float64
operand on the NKI path (fftand every other transform,stft/istft,
complex_gemm,complex_linear/ComplexLinear,complex_mask_apply,
complex_gemm_bf16):set_backend("auto")(the default): the call runs as
set_backend("pytorch")runs it, in torch on the tensors' device, with no
kernel. The result is float64-accurate (the pytorch backend's result).
complex_gemm_bf16runs its fallback, which is FP32 by design. Operands
already on the neuron device compute on the host (next item).set_backend("nki"): the kernel runs on float32 copies, and each op
emits aUserWarningthe first time it does this ("precision is reduced
to float32"). The result is cast back to the pytorch backend's dtype.TRNFFT_REQUIRE_NKI=1:NotImplementedErroron either backend,
ahead of the cast or the torch route.precision="double"keeps its host FP64 route on both backends (no
warning, no raise).- float64 already on the neuron device, under
auto: computed on the
host. Device aten on Trainium computes float64 at FP32 precision
(measured on Trn2), so "torch at float64 accuracy" means host torch.
Every transform,stft/istft,complex_gemm,complex_linear,
complex_mask_applyandComplexLinearcopy their neuron-resident
operands to the host, compute in float64 and return the result on the
original device in float64 / complex128 (differentiable copies), with
oneUserWarningper op ("float64 on the neuron device is computed on
the host for float64 accuracy (device arithmetic is fp32); pass float32
to stay on device"). One guard does this for every op
(dispatch._fp64_host_guard). Host-resident float64 is unchanged. - Mixed float64 operands (for example float32 activations with float64
weights) compute attorch.promote_typesof the operands: in torch at
float64 underauto, and in the float32 kernel with a float64 result
undernki.
- Result dtype matches the pytorch backend on every NKI path. The
low-precision modes (bf16,bf16_refined,ozaki,ozaki_hq) with a
bfloat16 or float16 input now return the input dtype, not the BF16 GEMM's
float32 PSUM (#137 item 2). float32 and float64 input are unchanged.
Outside those modes, half input to the transforms runs on float32 copies
and is rounded to the input dtype (#133, under Fixed).
Fixed
-
float64
ComplexConv1don a Neuron device failed to compile (#134).
In every release through 0.27.0,ComplexConv1d(...).double()with
neuron-resident operands sent a float64aten::convolutionto the Neuron
compiler, which has no fp64 datapath (Compilation error ... aten::convolution). It now follows the float64 policy above: under
autoit computes on the host in float64 (oneUserWarning) and returns
to the device; undernki, or withTRNFFT_FP64_ON_DEVICE=1, it convolves
in float32 on the device and returns float64;TRNFFT_REQUIRE_NKI=1
raisesNotImplementedError. float32 and half modules are unchanged. -
One dtype guard at the NKI kernel boundary: float64, integer, bool and
complex operands no longer reach a kernel that does not take them
(trnsci/trnsci#35; the class of #108, #131, #113).dispatch.KERNEL_DTYPES
lists what each kernel takes: float32, bfloat16 and float16, or float32 only
forfour_step_fftandcomplex_gemm_tn._launch(native and XLA),
_simulate_kernel, the directnki.simulatecalls and everynki_opbody
(so a directtorch.ops.trnfft.<op>call too) raiseTypeErrorfor any
other operand dtype. The public wrappers convert first:- float64 follows the policy above.
complex_gemm,complex_mask_apply
andcomplex_gemm_bf16used to return inf/NaN for float64 with no error. - Integer, bool, complex and mixed half/float32 operands run the PyTorch
backend, so the result (or the error) is the oneset_backend("pytorch")
gives. - In the FFT driver, the float64 handling now runs before the bench
toggles, and a directfft_corecall with integer or bool parts raises
TypeError. The public transforms already promote integer and bool input
(#113). complex_mask_applysizes that are not a multiple of 128 keep their
float64 torch route.
- float64 follows the policy above.
-
float16 / bfloat16 input on the NKI path failed to lower (#133). In
0.13.0–0.27.0 on NKI 0.7 (observed on 0.26.0 and 0.27.0), half-precision
input tofft,ifft,rfft,ihfft,stftand the other transforms
raisedRuntimeError: [NkiDispatch] Python lower_func failed ... AttributeError: module 'nki.language' has no attribute 'cast'under
set_backend("nki")and"auto"; direct halfcomplex_gemmcalls did
too. Two changes:- The transforms now run half input on float32 copies and round the result
once to the input dtype, the dtype torch.fft returns. Measured on Trn2
with the kernel cast fixed, the native half path was 0.3 ms faster at
N <= 1024 but up to 3x less accurate (fp16 N=12: 4.9e-4 vs 2.2e-4;
bf16 N=129: 6.6e-3 vs 1.9e-3), and from N=2048 the fp32 route was faster
(four-step 1.7 ms vs 0.96 ms) and more accurate. At N=16384, 65536 and
Bluestein N=3001 the half stage kernels failed MLIR verification.
The low-precision modes (bf16,bf16_refined,ozaki,ozaki_hq)
keep their own handling inside their GEMM ranges (#137). - The four kernels that rounded their FP32 PSUM to a half output
(complex_gemm,complex_gemm_t2d,complex_gemm_tn, the radix-8
Stockham stages) now convert in the PSUM -> SBUFtensor_copyinstead of
callingnl.cast. float32 bits are unchanged.
- The transforms now run half input on float32 copies and round the result
-
fftn-family error on a 0-size transformed dim (#121). In 0.1.0–0.26.0,
fftn,ifftn,rfftnandirfftn(and sofft2-style calls with an
explicitdim) raisedRuntimeError("cannot reshape tensor of 0 elements into shape [-1, 0] ...")when a 0-size dim was transformed, e.g. the
default all-dimsfftn(torch.randn(0, 4, 4)).torch.fftraises there
too, but withInvalid number of data points (0) specified; 0.27.0
already raises torch's error (#123). A 0-size dim that is not
transformed returns an empty result of torch's shape. Both cases are now
pinned by conformance tests againstnumpy.fft, and the conformance
suite's(0, 4, 4)all-dims xfail, which expected an empty result where
torch raises, is removed.
Added
- Spherical harmonic transforms,
trnfft.sht(#148, trnsci/trnsci#111
milestone 1).SHT(lmax, grid="gauss"|"equiangular")gives forward
(analysis) and inverse (synthesis) scalar transforms of(..., nlat, nlon)
real fields. Coefficients are orthonormal with the Condon-Shortley phase, in
ducc0's triangular m-major order. The longitude stage usestrnfft.rfft/
trnfft.irfft. The latitude stage is a per-m GEMM against
parity-split Legendre tables, which are computed on the host in fp64
(log-space start, so high m does not underflow) and stored as fp32.- NKI kernels.
trnfft/nki/sht.pyruns every per-m problem in one
launch (three at L = 1023). Each table tile stays stationary in SBUF
while all 2F field columns stream into fp32 PSUM. - Polar tiles. All-zero 128 x 128 table tiles are skipped through a
static mask, passed in the kernel'splanargument (aws-neuron-sdk#1454). - Kernel rules. Plain
nl.loadwithnisa.nc_transpose, never
load_transpose2d. Both LNC=2 cores split the columns byprogram_id.
Tail tiles are handled in every dimension. - Accuracy. fp32 is within 1e-6 (spectral l2) of fp64 ducc0: measured
2.6e-7 at L = 511 and 3.6e-7 at L = 1023. - Tests. Validated against ducc0, SHTns and a direct
scipy.special.sph_harm_yquadrature. The kernels are tested under
nki.simulateat 1 and 2 programs and pass the MLIR verifier at lnc=1
and lnc=2. - Benchmark.
scripts/bench_sht.py, with--devicerows. See
docs/api/sht.md. - dtypes. The kernels take float32 only (
KERNEL_DTYPESentries
sht_legendre_forward/sht_legendre_inverse, checked on every launch
route and in the op bodies). A float64 plan (SHT(dtype=torch.float64))
follows the float64 policy: torch at float64 underset_backend("auto")
(also withlegendre="nki"), the kernel on float32 copies with a
one-time warning under"nki",NotImplementedErrorunder
TRNFFT_REQUIRE_NKI=1; float64 tables on the neuron device run the
torch latitude stage on the host under"auto". The SHT ops are not on
the eager-handle fast path (they take the staticplanstring) and keep
thetrnfft::op.
- NKI kernels.
TRNFFT_REQUIRE_NKI=1(read per call): an operand dtype a kernel does
not take raisesNotImplementedErrorinstead of being cast to float32 or
computed in torch, asTRNBLAS_REQUIRE_NKIdoes (trnblas#69).
precision="double"keeps its host FP64 route, which is part of the mode
and not a fallback.TRNFFT_FP64_ON_DEVICE=1(read per call): underauto, keep
float64 / complex128 operands that are already on the neuron device on
device aten (FP32 precision, no host copy, no warning) instead of the host
route.- Docs: "Precision and dtypes" in
docs/api/nki.mdstates the policy.
Deprecated
- A repeated
dimin the n-D transforms (#127).fftn,ifftn,
rfftn,irfftn,hfftn,ihfftnand the*2variants with an
explicitdimaccept a repeated dim and transform along it once per
occurrence;torch.fftraisesRuntimeError("FFT dims must be unique").
They now emit aDeprecationWarning, and will raise in the next minor
release, astorch.fftdoes. The result is unchanged in this release.
Pass each dim once; to transform a dim twice, call the transform twice. - The
window=Nonedefault ofstftandistft(#106). It is a Hann
window ofwin_length, whiletorch.stft/torch.istft(whose signature
stftdocuments matching) use a rectangular window of ones. Both now emit
aDeprecationWarning. The default changes to torch's rectangular window
in the next minor release (#145), andstftandistftchange together
so a default round trip stays consistent. The result is unchanged in this
release. To keep the Hann window, pass
window=torch.hann_window(win_length)explicitly. The example and
quickstart now do.