DongyunZou commited on
Commit
ae1f584
·
verified ·
1 Parent(s): 57309ca

Uploaded using `kernel-builder`.

Browse files
Files changed (1) hide show
  1. README.md +86 -36
README.md CHANGED
@@ -22,9 +22,10 @@ The results are those of `torch.fft.rfftn` and
22
  `torch.fft.irfftn(..., norm="forward")`.
23
 
24
  **Against cuFFT's own 3D plans, geometric mean over eight grids from 32^3 to
25
- 256^3: r2c 1.99x and c2r 1.85x in fp32, r2c 2.62x and c2r 2.63x in fp64.
26
  Against `torch.fft`, which runs a 3D transform as two cuFFT plans and a copy,
27
- about 3x.**
 
28
 
29
  ## Requirements
30
 
@@ -55,7 +56,8 @@ spectrum = launch() # ...then transform its current contents
55
  `c2r(X)` takes a `[nz, ny, nf, 2]` half-spectrum of the same dtypes and
56
  returns `[nz, ny, 2 * (nf - 1)]`. Each call returns a new tensor and leaves
57
  its input unchanged. Other dtypes, devices and shapes raise `TypeError` or
58
- `ValueError`.
 
59
  - `prepare_r2c(x)` and `prepare_c2r(X)` plan for that input buffer and return
60
  `launch()`, which transforms the buffer's current contents into one output
61
  tensor that it reuses on every call. Calls of one `launch` must be ordered
@@ -64,18 +66,22 @@ spectrum = launch() # ...then transform its current contents
64
  - Calls on different CUDA streams may run concurrently, including calls of the
65
  same shape.
66
  - The first call for a new shape compiles its kernels (CuTe DSL JIT), typically
67
- in 1 to 30 s, up to about 4 minutes for a very long z axis over a tiny y-x
68
  plane (such as 4096x8x16). Make that first call outside CUDA graph capture;
69
  later calls can be captured.
70
  - `c2r` expects a Hermitian-consistent half-spectrum, such as the output of
71
  `r2c` or Fourier-space data that keeps its symmetry. On other input the result
72
  depends on the order of the 1D passes, as with cuFFT; these kernels then
73
  match `torch.fft.irfftn`.
74
- - Tuned on the eight grids below and checked on dozens of others per kernel,
75
- including odd and prime extents and up to 1024x1024x512. fp64 r2c raises
 
 
 
 
76
  `NotImplementedError` when a y or z axis needs a direct DFT longer than 6336
77
- points (prime factors above 13, or smooth lengths above 1536); fp64 c2r raises
78
- it for nz from 1280 to 4096, ny from 3072 to 4096 or nx / 2 from 3072 to 4096.
79
 
80
  ## Accuracy
81
 
@@ -83,7 +89,10 @@ fp32 kernels compute in fp32 from twiddle tables computed in float64 on the
83
  host: no reduced precision, no tensor cores, no approximate math. Against the
84
  float64 result the relative L2 error is 1.4e-7 to 1.8e-7 on the grids below,
85
  where cuFFT's is 2.0e-7 to 2.8e-7. Every element is within 1e-2, or within 1e-3
86
- of its own magnitude.
 
 
 
87
 
88
  fp64 kernels are IEEE float64 throughout, within 3.5e-16 to 5.9e-16 relative L2
89
  of `torch.fft`'s float64 result. The RTX 5090 runs FP64 at 1/64 of the FP32
@@ -94,7 +103,8 @@ input, subnormal operands and results, overflow, signed zeros, Inf and NaN
94
  included (2.5e10 operand pairs per routine compared with the native
95
  instructions). Until 2026-10-24 it was exact only for finite normal operands and
96
  results, and data near the bottom of the normal range came out wrong (see
97
- [Changes](#changes)).
 
98
 
99
  Repeated calls give bitwise identical results.
100
 
@@ -104,29 +114,29 @@ Speedup over cuFFT's 3D plan:
104
 
105
  | grid (nz x ny x nx) | r2c fp32 | c2r fp32 | r2c fp64 | c2r fp64 |
106
  |---|---:|---:|---:|---:|
107
- | 32x32x32 | 3.33x | 2.60x | 3.89x | 2.28x |
108
- | 64x64x64 | 2.13x | 2.00x | 2.48x | 2.44x |
109
- | 80x80x112 | 2.22x | 1.85x | 2.76x | 2.77x |
110
- | 96x96x96 | 2.24x | 2.14x | 2.37x | 2.64x |
111
- | 96x96x144 | 1.92x | 1.77x | 2.12x | 2.53x |
112
- | 128x128x128 | 1.80x | 1.65x | 2.50x | 2.99x |
113
- | 192x192x192 | 1.39x | 1.44x | 2.72x | 2.94x |
114
- | 256x256x256 | 1.42x | 1.59x | 2.45x | 2.56x |
115
- | geomean | 1.99x | 1.85x | 2.62x | 2.63x |
116
- | geomean, 128^3 and up | 1.53x | 1.56x | 2.55x | 2.82x |
117
 
118
  Times in microseconds (torch.fft / cuFFT 3D plan / kernel):
119
 
120
  | grid | r2c fp32 | c2r fp32 | r2c fp64 | c2r fp64 |
121
  |---|---|---|---|---|
122
- | 32x32x32 | 10.4 / 8.6 / 2.6 | 9.3 / 7.0 / 2.7 | 24.8 / 23.5 / 6.0 | 18.0 / 16.6 / 7.3 |
123
- | 64x64x64 | 11.6 / 9.4 / 4.4 | 14.3 / 9.0 / 4.5 | 32.7 / 31.5 / 12.7 | 36.3 / 32.6 / 13.4 |
124
- | 80x80x112 | 21.1 / 15.1 / 6.8 | 20.1 / 13.9 / 7.5 | 89.5 / 78.9 / 28.5 | 72.5 / 76.6 / 27.7 |
125
- | 96x96x96 | 23.4 / 16.5 / 7.4 | 25.6 / 16.7 / 7.8 | 87.4 / 75.6 / 32.0 | 83.1 / 76.2 / 28.8 |
126
- | 96x96x144 | 31.4 / 18.5 / 9.6 | 31.4 / 18.7 / 10.6 | 117.7 / 98.5 / 46.5 | 125.9 / 99.0 / 39.1 |
127
- | 128x128x128 | 42.0 / 24.2 / 13.4 | 56.8 / 23.4 / 14.2 | 182.8 / 164.0 / 65.5 | 202.1 / 173.7 / 58.1 |
128
- | 192x192x192 | 123.2 / 66.9 / 48.1 | 126.4 / 71.2 / 49.3 | 647.9 / 542.5 / 199.5 | 590.8 / 538.5 / 182.9 |
129
- | 256x256x256 | 346.8 / 163.0 / 114.5 | 300.2 / 187.4 / 117.5 | 1625.0 / 1116.0 / 456.0 | 1445.3 / 1130.0 / 441.8 |
130
 
131
  On small grids cuFFT's several kernels are launch-bound, and one or two fused
132
  kernels win most. On large fp32 grids the kernels make fewer passes over
@@ -138,8 +148,25 @@ over CUDA graph replay, 3 warmup and 30 measured iterations, median of 3 trials,
138
  each trial timing cuFFT before and after the kernel. cuFFT is the cuFFT 12.0
139
  that PyTorch 2.12.1+cu130 ships, as one `cufftPlan3d` per grid made before
140
  timing and executed out of place; its C2R overwrites its input and is given a
141
- copy. `torch.fft` is `rfftn` / `irfftn` on the same library. Speeds differ by a
142
- few percent between RTX 5090 cards.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  ## Changes
145
 
@@ -157,14 +184,37 @@ nz, ny in 8..256 and nx in 16..256 (the requests' range), on a B300 with the RTX
157
  to 1e-300 returned NaN everywhere, and data scaled by 1e-300 or 1e-310,
158
  subnormal bands of a spectrum and single NaN or Inf inputs failed on many
159
  grids. They now handle every IEEE case, and fp64 c2r no longer routes 2x - y
160
- through software on five tuned grids. On a B300 this costs up to 4% (r2c) and
161
- 10% (c2r); the RTX 5090 tables above were measured before it.
162
  - fp64 inputs on a CUDA device other than the current one raised
163
  (`ValueError`, `BufferError`); every call now runs under the input's device.
164
 
165
- The changed files are copies of `sm120/fft3d/` at commit `dbb8bd6` of
166
- `humanfia/kda-kernels-internal`, with the file digests in `metadata.json`
167
- updated; the package was not rebuilt with `kernel-builder`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
  ## Source and licensing
170
 
@@ -174,7 +224,7 @@ built from the requests in [NVlabs/kda#7](https://github.com/NVlabs/kda/pull/7),
174
  and accepted by the KDA judge. Published from commit `e539498` of
175
  `humanfia/kda-kernels-internal`, directory `rtx5090/fft3d/` (now
176
  `sm120/fft3d/`), which also holds its tests and the fixes made after the
177
- judge's checks; updated from commit `dbb8bd6` (see [Changes](#changes)).
178
  MIT-licensed.
179
 
180
  ## Available functions
 
22
  `torch.fft.irfftn(..., norm="forward")`.
23
 
24
  **Against cuFFT's own 3D plans, geometric mean over eight grids from 32^3 to
25
+ 256^3: r2c 1.99x and c2r 1.85x in fp32, r2c 2.63x and c2r 2.58x in fp64.
26
  Against `torch.fft`, which runs a 3D transform as two cuFFT plans and a copy,
27
+ about 3x. Any grid runs FFT kernels: on random grids up to 256 per axis, 1.5x to
28
+ 1.9x over cuFFT (geomean), and no sampled grid slower than cuFFT.**
29
 
30
  ## Requirements
31
 
 
56
  `c2r(X)` takes a `[nz, ny, nf, 2]` half-spectrum of the same dtypes and
57
  returns `[nz, ny, 2 * (nf - 1)]`. Each call returns a new tensor and leaves
58
  its input unchanged. Other dtypes, devices and shapes raise `TypeError` or
59
+ `ValueError`. The work runs on the input's device (which need not be the
60
+ current one), on its current CUDA stream.
61
  - `prepare_r2c(x)` and `prepare_c2r(X)` plan for that input buffer and return
62
  `launch()`, which transforms the buffer's current contents into one output
63
  tensor that it reuses on every call. Calls of one `launch` must be ordered
 
66
  - Calls on different CUDA streams may run concurrently, including calls of the
67
  same shape.
68
  - The first call for a new shape compiles its kernels (CuTe DSL JIT), typically
69
+ in 1 to 15 s, up to about 4 minutes for a very long z axis over a tiny y-x
70
  plane (such as 4096x8x16). Make that first call outside CUDA graph capture;
71
  later calls can be captured.
72
  - `c2r` expects a Hermitian-consistent half-spectrum, such as the output of
73
  `r2c` or Fourier-space data that keeps its symmetry. On other input the result
74
  depends on the order of the 1D passes, as with cuFFT; these kernels then
75
  match `torch.fft.irfftn`.
76
+ - Tuned on the eight grids below; any grid with nz, ny >= 8 and nx >= 16 is
77
+ accepted. Within 256 per axis every grid of every kernel runs FFT kernels,
78
+ odd and prime extents included, checked on RTX 5090s by sweeps over every
79
+ plan signature, and on grids up to 1024x1024x512. Beyond 256 per axis, axis
80
+ lengths with two prime factors above 16 (fp32 c2r) or a squared prime factor
81
+ above 13 (fp64, such as 289) still take slow generic kernels. fp64 r2c raises
82
  `NotImplementedError` when a y or z axis needs a direct DFT longer than 6336
83
+ points; fp64 c2r raises it for nz from 1280 to 4096, ny from 3072 to 4096 or
84
+ nx / 2 from 3072 to 4096.
85
 
86
  ## Accuracy
87
 
 
89
  host: no reduced precision, no tensor cores, no approximate math. Against the
90
  float64 result the relative L2 error is 1.4e-7 to 1.8e-7 on the grids below,
91
  where cuFFT's is 2.0e-7 to 2.8e-7. Every element is within 1e-2, or within 1e-3
92
+ of its own magnitude. On other grids within 256 per axis the relative L2 error
93
+ is 1e-7 to 3.4e-7, every element passes that rule on random input, and large
94
+ prime extents stay below cuFFT's error (c2r of a random 251x251x254
95
+ half-spectrum: max 0.006, cuFFT 0.016).
96
 
97
  fp64 kernels are IEEE float64 throughout, within 3.5e-16 to 5.9e-16 relative L2
98
  of `torch.fft`'s float64 result. The RTX 5090 runs FP64 at 1/64 of the FP32
 
103
  included (2.5e10 operand pairs per routine compared with the native
104
  instructions). Until 2026-10-24 it was exact only for finite normal operands and
105
  results, and data near the bottom of the normal range came out wrong (see
106
+ [Changes](#changes)). Data with a large mean keeps cuFFT-level accuracy or
107
+ better in every kernel.
108
 
109
  Repeated calls give bitwise identical results.
110
 
 
114
 
115
  | grid (nz x ny x nx) | r2c fp32 | c2r fp32 | r2c fp64 | c2r fp64 |
116
  |---|---:|---:|---:|---:|
117
+ | 32x32x32 | 3.33x | 2.60x | 3.93x | 2.22x |
118
+ | 64x64x64 | 2.13x | 2.00x | 2.48x | 2.40x |
119
+ | 80x80x112 | 2.22x | 1.85x | 2.63x | 2.67x |
120
+ | 96x96x96 | 2.24x | 2.14x | 2.43x | 2.52x |
121
+ | 96x96x144 | 1.92x | 1.77x | 2.13x | 2.39x |
122
+ | 128x128x128 | 1.80x | 1.65x | 2.51x | 2.91x |
123
+ | 192x192x192 | 1.39x | 1.44x | 2.75x | 2.95x |
124
+ | 256x256x256 | 1.42x | 1.59x | 2.47x | 2.65x |
125
+ | geomean | 1.99x | 1.85x | 2.63x | 2.58x |
126
+ | geomean, 128^3 and up | 1.53x | 1.56x | 2.58x | 2.83x |
127
 
128
  Times in microseconds (torch.fft / cuFFT 3D plan / kernel):
129
 
130
  | grid | r2c fp32 | c2r fp32 | r2c fp64 | c2r fp64 |
131
  |---|---|---|---|---|
132
+ | 32x32x32 | 10.4 / 8.6 / 2.6 | 9.3 / 7.0 / 2.7 | 26.4 / 26.9 / 6.8 | 20.5 / 19.1 / 8.6 |
133
+ | 64x64x64 | 11.6 / 9.4 / 4.4 | 14.3 / 9.0 / 4.5 | 35.3 / 37.4 / 15.1 | 41.5 / 37.5 / 15.6 |
134
+ | 80x80x112 | 21.1 / 15.1 / 6.8 | 20.1 / 13.9 / 7.5 | 98.7 / 92.7 / 35.2 | 83.6 / 89.7 / 33.6 |
135
+ | 96x96x96 | 23.4 / 16.5 / 7.4 | 25.6 / 16.7 / 7.8 | 96.4 / 88.8 / 36.5 | 96.2 / 89.4 / 35.5 |
136
+ | 96x96x144 | 31.4 / 18.5 / 9.6 | 31.4 / 18.7 / 10.6 | 128.1 / 115.8 / 54.5 | 146.0 / 116.1 / 48.6 |
137
+ | 128x128x128 | 42.0 / 24.2 / 13.4 | 56.8 / 23.4 / 14.2 | 201.4 / 193.9 / 77.1 | 233.7 / 205.0 / 70.4 |
138
+ | 192x192x192 | 123.2 / 66.9 / 48.1 | 126.4 / 71.2 / 49.3 | 701.7 / 642.8 / 233.5 | 682.8 / 634.7 / 215.0 |
139
+ | 256x256x256 | 346.8 / 163.0 / 114.5 | 300.2 / 187.4 / 117.5 | 1688.8 / 1323.1 / 536.0 | 1671.9 / 1337.3 / 505.1 |
140
 
141
  On small grids cuFFT's several kernels are launch-bound, and one or two fused
142
  kernels win most. On large fp32 grids the kernels make fewer passes over
 
148
  each trial timing cuFFT before and after the kernel. cuFFT is the cuFFT 12.0
149
  that PyTorch 2.12.1+cu130 ships, as one `cufftPlan3d` per grid made before
150
  timing and executed out of place; its C2R overwrites its input and is given a
151
+ copy. `torch.fft` is `rfftn` / `irfftn` on the same library. The fp64 columns
152
+ were measured again after the IEEE-complete adders (see [Changes](#changes)), on
153
+ an RTX 5090 that runs both cuFFT and the kernels 10-20% slower in absolute time;
154
+ the speedups moved by at most 2%. Speeds differ by a few percent between RTX
155
+ 5090 cards.
156
+
157
+ Beyond the eight grids, against cuFFT's 3D plans on fixed seeded samples of the
158
+ requests' range (nz, ny 8..256, nx 16..256, even nx for c2r; random input),
159
+ geomean with the slowest grid in brackets:
160
+
161
+ | sample | r2c fp32 | c2r fp32 | r2c fp64 | c2r fp64 |
162
+ |---|---:|---:|---:|---:|
163
+ | 60 uniform | 1.81x (1.15x) | 1.63x (1.11x) | 1.93x (1.15x) | 1.67x (1.18x) |
164
+ | 60 with 7-smooth axes | 1.49x (1.06x) | 1.81x (1.28x) | 1.83x (1.29x) | 1.76x (1.17x) |
165
+ | 40 with a prime factor >= 17 | 1.72x (1.22x) | 1.62x (1.15x) | 1.88x (1.20x) | 1.71x (1.16x) |
166
+
167
+ Every sampled grid is faster than cuFFT's 3D plan and than `torch.fft`. Large
168
+ prime extents gain most: 251x251x254 takes 0.25 ms in fp32 r2c (cuFFT 0.59 ms)
169
+ and 2.6 ms in fp64 c2r (cuFFT 4.9 ms).
170
 
171
  ## Changes
172
 
 
184
  to 1e-300 returned NaN everywhere, and data scaled by 1e-300 or 1e-310,
185
  subnormal bands of a spectrum and single NaN or Inf inputs failed on many
186
  grids. They now handle every IEEE case, and fp64 c2r no longer routes 2x - y
187
+ through software on five tuned grids. On an RTX 5090 the eight grids'
188
+ speedups moved by at most 2% (the fp64 columns above).
189
  - fp64 inputs on a CUDA device other than the current one raised
190
  (`ValueError`, `BufferError`); every call now runs under the input's device.
191
 
192
+ Updated again on 2026-10-24 after a sweep on RTX 5090s over every plan signature
193
+ of the four kernels within 256 per axis (about 45,000 grids, random and
194
+ Hermitian c2r input, spectrum-shaped and offset data, prepare, graph and
195
+ two-stream checks):
196
+
197
+ - fp32 c2r missed the 1e-2 / 1e-3 tolerance on about 6,900 grids of 6.5M points
198
+ and more with a large prime extent (such as 251x251x254) on random
199
+ half-spectra, the requests' own input: up to 69 elements per grid off by up to
200
+ 0.03, where cuFFT's fp32 plan has none. Now at most 0.006.
201
+ - fp32 r2c returned garbage on grids of 2^24 points and more with an odd ny
202
+ (such as 256x297x256, beyond 256 per axis).
203
+ - Most grids ran slow fallbacks: fp32 r2c ran every grid whose nx is not a
204
+ multiple of 4 or that has a prime factor above 13 (96.8% of the range) as
205
+ O(n^2) direct DFTs, 17 to 180 times slower than `torch.fft` (96x96x98: 0.97 ms
206
+ against cuFFT's 0.020 ms); fp32 c2r ran 77% of the range on a global-memory
207
+ generic path, and fp64 ran axes with a prime factor above 13 as direct DFTs
208
+ (fp64 c2r: the whole grid, about 90% of the range), 3 to 10 times slower than
209
+ cuFFT. They now run general FFT kernels (two real rows per complex FFT for odd
210
+ x, Rader's or Bluestein's algorithm for large primes), faster than cuFFT on
211
+ every sampled grid (Performance). Sweeps over every new kernel signature found
212
+ no wrong result.
213
+ - fp64 r2c on data with a large mean (randn + 1e3) missed atol 1e-6 on 21 of 100
214
+ sample grids of 1M points and more, where cuFFT passed; now at most 1.07x
215
+ cuFFT's error.
216
+
217
+ The eight grids keep their plans and their outputs are bitwise unchanged.
218
 
219
  ## Source and licensing
220
 
 
224
  and accepted by the KDA judge. Published from commit `e539498` of
225
  `humanfia/kda-kernels-internal`, directory `rtx5090/fft3d/` (now
226
  `sm120/fft3d/`), which also holds its tests and the fixes made after the
227
+ judge's checks; updated from commit `f8b3c93` (see [Changes](#changes)).
228
  MIT-licensed.
229
 
230
  ## Available functions