|
| 1 | +// Copyright © 2024 Apple Inc. |
| 2 | + |
| 3 | +// Metal FFT using Stockham's algorithm |
| 4 | +// |
| 5 | +// References: |
| 6 | +// - VkFFT (https://github.com/DTolm/VkFFT) |
| 7 | +// - Eric Bainville's excellent page (http://www.bealto.com/gpu-fft.html) |
| 8 | + |
| 9 | +#include <metal_math> |
| 10 | +#include <metal_common> |
| 11 | + |
| 12 | + |
| 13 | +#include "mlx/backend/metal/kernels/defines.h" |
| 14 | +#include "mlx/backend/metal/kernels/utils.h" |
| 15 | + |
| 16 | +using namespace metal; |
| 17 | + |
| 18 | +float2 complex_mul(float2 a, float2 b) { |
| 19 | + float2 c; |
| 20 | + c.x = a.x * b.x - a.y * b.y; |
| 21 | + c.y = a.x * b.y + a.y * b.x; |
| 22 | + return c; |
| 23 | +} |
| 24 | + |
| 25 | +float2 get_twiddle(int k, int p) { |
| 26 | + float theta = -1.0f * k * M_PI_F / (2*p); |
| 27 | + |
| 28 | + float2 twiddle; |
| 29 | + twiddle.x = metal::fast::cos(theta); |
| 30 | + twiddle.y = metal::fast::sin(theta); |
| 31 | + return twiddle; |
| 32 | +} |
| 33 | + |
| 34 | +// single threaded radix2 implemetation |
| 35 | +void radix2(int i, int p, int m, threadgroup float2* read_buf, threadgroup float2* write_buf) { |
| 36 | + float2 x_0 = read_buf[i]; |
| 37 | + float2 x_1 = read_buf[i + m]; |
| 38 | + |
| 39 | + // The index within this sub-DFT |
| 40 | + int k = i & (p - 1); |
| 41 | + |
| 42 | + float2 twiddle = get_twiddle(k, p); |
| 43 | + |
| 44 | + float2 z = complex_mul(x_1, twiddle); |
| 45 | + |
| 46 | + float2 y_0 = x_0 + z; |
| 47 | + float2 y_1 = x_0 - z; |
| 48 | + |
| 49 | + int j = (i << 1) - k; |
| 50 | + |
| 51 | + write_buf[j] = y_0; |
| 52 | + write_buf[j + p] = y_1; |
| 53 | +} |
| 54 | + |
| 55 | +// single threaded radix4 implemetation |
| 56 | +void radix4(int i, int p, int m, threadgroup float2* read_buf, threadgroup float2* write_buf) { |
| 57 | + float2 x_0 = read_buf[i]; |
| 58 | + float2 x_1 = read_buf[i + m]; |
| 59 | + float2 x_2 = read_buf[i + 2*m]; |
| 60 | + float2 x_3 = read_buf[i + 3*m]; |
| 61 | + |
| 62 | + // The index within this sub-DFT |
| 63 | + int k = i & (p - 1); |
| 64 | + |
| 65 | + float2 twiddle = get_twiddle(k, p); |
| 66 | + // e^a * e^b = e^(a + b) |
| 67 | + float2 twiddle_2 = complex_mul(twiddle, twiddle); |
| 68 | + float2 twiddle_3 = complex_mul(twiddle, twiddle_2); |
| 69 | + |
| 70 | + x_1 = complex_mul(x_1, twiddle); |
| 71 | + x_2 = complex_mul(x_2, twiddle_2); |
| 72 | + x_3 = complex_mul(x_3, twiddle_3); |
| 73 | + |
| 74 | + float2 minus_i; |
| 75 | + minus_i.x = 0; |
| 76 | + minus_i.y = -1; |
| 77 | + |
| 78 | + // Hard coded twiddle factors for DFT4 |
| 79 | + float2 z_0 = x_0 + x_2; |
| 80 | + float2 z_1 = x_0 - x_2; |
| 81 | + float2 z_2 = x_1 + x_3; |
| 82 | + float2 z_3 = complex_mul(x_1 - x_3, minus_i); |
| 83 | + |
| 84 | + float2 y_0 = z_0 + z_2; |
| 85 | + float2 y_1 = z_1 + z_3; |
| 86 | + float2 y_2 = z_0 - z_2; |
| 87 | + float2 y_3 = z_1 - z_3; |
| 88 | + |
| 89 | + int j = ((i - k) << 2) + k; |
| 90 | + |
| 91 | + write_buf[j] = y_0; |
| 92 | + write_buf[j + p] = y_1; |
| 93 | + write_buf[j + 2*p] = y_2; |
| 94 | + write_buf[j + 3*p] = y_3; |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +// Each FFT is computed entirely in shared GPU memory. |
| 99 | +// |
| 100 | +// N is decomposed into radix-2 and radix-4 DFTs: |
| 101 | +// e.g. 128 = 2 * 4 * 4 * 4 |
| 102 | +// |
| 103 | +// At each step we use n / 4 threads, each performing |
| 104 | +// a single-threaded radix-4 or radix-2 DFT. |
| 105 | +// |
| 106 | +// We provide the number of radix-2 and radix-4 |
| 107 | +// steps at compile time for a ~20% performance boost. |
| 108 | +template <size_t n, size_t radix_2_steps, size_t radix_4_steps> |
| 109 | +[[kernel]] void fft( |
| 110 | + const device float2 *in [[buffer(0)]], |
| 111 | + device float2 * out [[buffer(1)]], |
| 112 | + uint3 thread_position_in_grid [[thread_position_in_grid]], |
| 113 | + uint3 threads_per_grid [[threads_per_grid]]) { |
| 114 | + |
| 115 | + // Index of the DFT in batch |
| 116 | + int batch_idx = thread_position_in_grid.x * n; |
| 117 | + // The index in the DFT we're working on |
| 118 | + int i = thread_position_in_grid.y; |
| 119 | + // The number of the threads we're using for each DFT |
| 120 | + int m = threads_per_grid.y; |
| 121 | + |
| 122 | + // Allocate 2 shared memory buffers for Stockham. |
| 123 | + // We alternate reading from one and writing to the other at each radix step. |
| 124 | + threadgroup float2 shared_in[n]; |
| 125 | + threadgroup float2 shared_out[n]; |
| 126 | + |
| 127 | + // Pointers to facilitate Stockham buffer swapping |
| 128 | + threadgroup float2* read_buf = shared_in; |
| 129 | + threadgroup float2* write_buf = shared_out; |
| 130 | + threadgroup float2* tmp; |
| 131 | + |
| 132 | + // Copy input into shared memory |
| 133 | + shared_in[i] = in[batch_idx + i]; |
| 134 | + shared_in[i + m] = in[batch_idx + i + m]; |
| 135 | + shared_in[i + 2*m] = in[batch_idx + i + 2*m]; |
| 136 | + shared_in[i + 3*m] = in[batch_idx + i + 3*m]; |
| 137 | + |
| 138 | + threadgroup_barrier(mem_flags::mem_threadgroup); |
| 139 | + |
| 140 | + int p = 1; |
| 141 | + |
| 142 | + for (size_t r = 0; r < radix_2_steps; r++) { |
| 143 | + radix2(i, p, m*2, read_buf, write_buf); |
| 144 | + radix2(i + m, p, m*2, read_buf, write_buf); |
| 145 | + p *= 2; |
| 146 | + |
| 147 | + threadgroup_barrier(mem_flags::mem_threadgroup); |
| 148 | + |
| 149 | + // Stockham switch of buffers |
| 150 | + tmp = write_buf; |
| 151 | + write_buf = read_buf; |
| 152 | + read_buf = tmp; |
| 153 | + } |
| 154 | + |
| 155 | + for (size_t r = 0; r < radix_4_steps; r++) { |
| 156 | + radix4(i, p, m, read_buf, write_buf); |
| 157 | + p *= 4; |
| 158 | + |
| 159 | + threadgroup_barrier(mem_flags::mem_threadgroup); |
| 160 | + |
| 161 | + // Stockham switch of buffers |
| 162 | + tmp = write_buf; |
| 163 | + write_buf = read_buf; |
| 164 | + read_buf = tmp; |
| 165 | + } |
| 166 | + |
| 167 | + // Copy shared memory to output |
| 168 | + out[batch_idx + i] = read_buf[i]; |
| 169 | + out[batch_idx + i + m] = read_buf[i + m]; |
| 170 | + out[batch_idx + i + 2*m] = read_buf[i + 2*m]; |
| 171 | + out[batch_idx + i + 3*m] = read_buf[i + 3*m]; |
| 172 | +} |
| 173 | + |
| 174 | +#define instantiate_fft(name, n, radix_2_steps, radix_4_steps) \ |
| 175 | + template [[host_name("fft_" #name)]] \ |
| 176 | + [[kernel]] void fft<n, radix_2_steps, radix_4_steps>( \ |
| 177 | + const device float2* in [[buffer(0)]], \ |
| 178 | + device float2* out [[buffer(1)]], \ |
| 179 | + uint3 thread_position_in_grid [[thread_position_in_grid]], \ |
| 180 | + uint3 threads_per_grid [[threads_per_grid]]); |
| 181 | + |
| 182 | + |
| 183 | +// Explicitly define kernels for each power of 2. |
| 184 | +instantiate_fft(4, /* n= */ 4, /* radix_2_steps= */ 0, /* radix_4_steps= */ 1) |
| 185 | +instantiate_fft(8, 8, 1, 1) |
| 186 | +instantiate_fft(16, 16, 0, 2) |
| 187 | +instantiate_fft(32, 32, 1, 2) |
| 188 | +instantiate_fft(64, 64, 0, 3) |
| 189 | +instantiate_fft(128, 128, 1, 3) |
| 190 | +instantiate_fft(256, 256, 0, 4) |
| 191 | +instantiate_fft(512, 512, 1, 4) |
| 192 | +instantiate_fft(1024, 1024, 0, 5) |
| 193 | +// 2048 is the max that will fit into 32KB of threadgroup memory. |
| 194 | +// TODO: implement 4 step FFT for larger n. |
| 195 | +instantiate_fft(2048, 2048, 1, 5) |
0 commit comments