File size: 11,369 Bytes
712dbf0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# Copyright © 2023 Apple Inc.
import itertools
import unittest
import mlx.core as mx
import mlx_tests
import numpy as np
try:
import torch
has_torch = True
except ImportError as e:
has_torch = False
class TestFFT(mlx_tests.MLXTestCase):
def check_mx_np(self, op_mx, op_np, a_np, atol=1e-5, rtol=1e-6, **kwargs):
out_np = op_np(a_np, **kwargs)
a_mx = mx.array(a_np)
out_mx = op_mx(a_mx, **kwargs)
np.testing.assert_allclose(out_np, out_mx, atol=atol, rtol=rtol)
def test_fft(self):
r = np.random.rand(100).astype(np.float32)
i = np.random.rand(100).astype(np.float32)
a_np = r + 1j * i
self.check_mx_np(mx.fft.fft, np.fft.fft, a_np)
# Check with slicing and padding
r = np.random.rand(100).astype(np.float32)
i = np.random.rand(100).astype(np.float32)
a_np = r + 1j * i
self.check_mx_np(mx.fft.fft, np.fft.fft, a_np, n=80)
self.check_mx_np(mx.fft.fft, np.fft.fft, a_np, n=120)
# Check different axes
r = np.random.rand(100, 100).astype(np.float32)
i = np.random.rand(100, 100).astype(np.float32)
a_np = r + 1j * i
self.check_mx_np(mx.fft.fft, np.fft.fft, a_np, axis=0)
self.check_mx_np(mx.fft.fft, np.fft.fft, a_np, axis=1)
# Check real fft
a_np = np.random.rand(100).astype(np.float32)
self.check_mx_np(mx.fft.rfft, np.fft.rfft, a_np)
self.check_mx_np(mx.fft.rfft, np.fft.rfft, a_np, n=80)
self.check_mx_np(mx.fft.rfft, np.fft.rfft, a_np, n=120)
# Check real inverse
r = np.random.rand(100, 100).astype(np.float32)
i = np.random.rand(100, 100).astype(np.float32)
a_np = r + 1j * i
self.check_mx_np(mx.fft.ifft, np.fft.ifft, a_np)
self.check_mx_np(mx.fft.ifft, np.fft.ifft, a_np, n=80)
self.check_mx_np(mx.fft.ifft, np.fft.ifft, a_np, n=120)
x = np.fft.rfft(np.real(a_np))
self.check_mx_np(mx.fft.irfft, np.fft.irfft, x)
def test_fftn(self):
r = np.random.randn(8, 8, 8).astype(np.float32)
i = np.random.randn(8, 8, 8).astype(np.float32)
a = r + 1j * i
axes = [None, (1, 2), (2, 1), (0, 2)]
shapes = [None, (10, 5), (5, 10)]
ops = [
"fft2",
"ifft2",
"rfft2",
"irfft2",
"fftn",
"ifftn",
"rfftn",
"irfftn",
]
for op, ax, s in itertools.product(ops, axes, shapes):
if ax is None and s is not None:
continue
x = a
if op in ["rfft2", "rfftn"]:
x = r
elif op == "irfft2":
x = np.ascontiguousarray(np.fft.rfft2(r, axes=ax, s=s))
elif op == "irfftn":
x = np.ascontiguousarray(np.fft.rfftn(r, axes=ax, s=s))
mx_op = getattr(mx.fft, op)
np_op = getattr(np.fft, op)
self.check_mx_np(mx_op, np_op, x, axes=ax, s=s)
def _run_ffts(self, shape, atol=1e-4, rtol=1e-4):
np.random.seed(9)
r = np.random.rand(*shape).astype(np.float32)
i = np.random.rand(*shape).astype(np.float32)
a_np = r + 1j * i
self.check_mx_np(mx.fft.fft, np.fft.fft, a_np, atol=atol, rtol=rtol)
self.check_mx_np(mx.fft.ifft, np.fft.ifft, a_np, atol=atol, rtol=rtol)
self.check_mx_np(mx.fft.rfft, np.fft.rfft, r, atol=atol, rtol=rtol)
ia_np = np.fft.rfft(r)
self.check_mx_np(
mx.fft.irfft, np.fft.irfft, ia_np, atol=atol, rtol=rtol, n=shape[-1]
)
self.check_mx_np(mx.fft.irfft, np.fft.irfft, ia_np, atol=atol, rtol=rtol)
def test_fft_shared_mem(self):
nums = np.concatenate(
[
# small radix
np.arange(2, 14),
# powers of 2
[2**k for k in range(4, 13)],
# stockham
[3 * 3 * 3, 3 * 11, 11 * 13 * 2, 7 * 4 * 13 * 11, 13 * 13 * 11],
# rader
[17, 23, 29, 17 * 8 * 3, 23 * 2, 1153, 1982],
# bluestein
[47, 83, 17 * 17],
# large stockham
[3159, 3645, 3969, 4004],
]
)
for batch_size in (1, 3, 32):
for num in nums:
atol = 1e-4 if num < 1025 else 1e-3
self._run_ffts((batch_size, num), atol=atol)
@unittest.skip("Too slow for CI but useful for local testing.")
def test_fft_exhaustive(self):
nums = range(2, 4097)
for batch_size in (1, 3, 32):
for num in nums:
print(num)
atol = 1e-4 if num < 1025 else 1e-3
self._run_ffts((batch_size, num), atol=atol)
def test_fft_big_powers_of_two(self):
# TODO: improve precision on big powers of two on GPU
for k in range(12, 17):
self._run_ffts((3, 2**k), atol=1e-3)
for k in range(17, 20):
self._run_ffts((3, 2**k), atol=1e-2)
def test_fft_large_numbers(self):
numbers = [
1037, # prime > 2048
18247, # medium size prime factors
1259 * 11, # large prime factors
7883, # large prime
3**8, # large stockham decomposable
3109, # bluestein
4006, # large rader
]
for large_num in numbers:
self._run_ffts((1, large_num), atol=1e-3)
def test_fft_contiguity(self):
r = np.random.rand(4, 8).astype(np.float32)
i = np.random.rand(4, 8).astype(np.float32)
a_np = r + 1j * i
a_mx = mx.array(a_np)
# non-contiguous in the FFT dim
out_mx = mx.fft.fft(a_mx[:, ::2])
out_np = np.fft.fft(a_np[:, ::2])
np.testing.assert_allclose(out_np, out_mx, atol=1e-5, rtol=1e-5)
# non-contiguous not in the FFT dim
out_mx = mx.fft.fft(a_mx[::2])
out_np = np.fft.fft(a_np[::2])
np.testing.assert_allclose(out_np, out_mx, atol=1e-5, rtol=1e-5)
out_mx = mx.broadcast_to(mx.reshape(mx.transpose(a_mx), (4, 8, 1)), (4, 8, 16))
out_np = np.broadcast_to(np.reshape(np.transpose(a_np), (4, 8, 1)), (4, 8, 16))
np.testing.assert_allclose(out_np, out_mx, atol=1e-5, rtol=1e-5)
out2_mx = mx.fft.fft(mx.abs(out_mx) + 4)
out2_np = np.fft.fft(np.abs(out_np) + 4)
np.testing.assert_allclose(out2_mx, out2_np, atol=1e-5, rtol=1e-5)
b_np = np.array([[0, 1, 2, 3]])
out_mx = mx.abs(mx.fft.fft(mx.tile(mx.reshape(mx.array(b_np), (1, 4)), (4, 1))))
out_np = np.abs(np.fft.fft(np.tile(np.reshape(np.array(b_np), (1, 4)), (4, 1))))
np.testing.assert_allclose(out_mx, out_np, atol=1e-5, rtol=1e-5)
def test_fft_into_ifft(self):
n_fft = 8193
mx.random.seed(0)
segment = mx.random.normal(shape=[1, n_fft]) + 1j * mx.random.normal(
shape=(1, n_fft)
)
segment = mx.fft.fft(segment, n=n_fft)
r = mx.fft.ifft(segment, n=n_fft)
r_np = np.fft.ifft(segment, n=n_fft)
self.assertTrue(np.allclose(r, r_np, atol=1e-5, rtol=1e-5))
def test_fft_throws(self):
x = mx.array(3.0)
with self.assertRaises(ValueError):
mx.fft.irfftn(x)
def test_fftshift(self):
# Test 1D arrays
r = np.random.rand(100).astype(np.float32)
self.check_mx_np(mx.fft.fftshift, np.fft.fftshift, r)
# Test with specific axis
r = np.random.rand(4, 6).astype(np.float32)
self.check_mx_np(mx.fft.fftshift, np.fft.fftshift, r, axes=[0])
self.check_mx_np(mx.fft.fftshift, np.fft.fftshift, r, axes=[1])
self.check_mx_np(mx.fft.fftshift, np.fft.fftshift, r, axes=[0, 1])
# Test with negative axes
self.check_mx_np(mx.fft.fftshift, np.fft.fftshift, r, axes=[-1])
# Test with odd lengths
r = np.random.rand(5, 7).astype(np.float32)
self.check_mx_np(mx.fft.fftshift, np.fft.fftshift, r)
self.check_mx_np(mx.fft.fftshift, np.fft.fftshift, r, axes=[0])
# Test with complex input
r = np.random.rand(8, 8).astype(np.float32)
i = np.random.rand(8, 8).astype(np.float32)
c = r + 1j * i
self.check_mx_np(mx.fft.fftshift, np.fft.fftshift, c)
def test_ifftshift(self):
# Test 1D arrays
r = np.random.rand(100).astype(np.float32)
self.check_mx_np(mx.fft.ifftshift, np.fft.ifftshift, r)
# Test with specific axis
r = np.random.rand(4, 6).astype(np.float32)
self.check_mx_np(mx.fft.ifftshift, np.fft.ifftshift, r, axes=[0])
self.check_mx_np(mx.fft.ifftshift, np.fft.ifftshift, r, axes=[1])
self.check_mx_np(mx.fft.ifftshift, np.fft.ifftshift, r, axes=[0, 1])
# Test with negative axes
self.check_mx_np(mx.fft.ifftshift, np.fft.ifftshift, r, axes=[-1])
# Test with odd lengths
r = np.random.rand(5, 7).astype(np.float32)
self.check_mx_np(mx.fft.ifftshift, np.fft.ifftshift, r)
self.check_mx_np(mx.fft.ifftshift, np.fft.ifftshift, r, axes=[0])
# Test with complex input
r = np.random.rand(8, 8).astype(np.float32)
i = np.random.rand(8, 8).astype(np.float32)
c = r + 1j * i
self.check_mx_np(mx.fft.ifftshift, np.fft.ifftshift, c)
def test_fftshift_errors(self):
# Test invalid axes
x = mx.array(np.random.rand(4, 4).astype(np.float32))
with self.assertRaises(ValueError):
mx.fft.fftshift(x, axes=[2])
with self.assertRaises(ValueError):
mx.fft.fftshift(x, axes=[-3])
# Test empty array
x = mx.array([])
self.assertTrue(mx.array_equal(mx.fft.fftshift(x), x))
@unittest.skipIf(not has_torch, "requires PyTorch")
def test_fft_grads(self):
real = [True, False]
inverse = [True, False]
axes = [
(-1,),
(-2, -1),
]
shapes = [
(4, 4),
(2, 4),
(2, 7),
(7, 7),
]
mxffts = {
(True, True): mx.fft.irfftn,
(True, False): mx.fft.rfftn,
(False, True): mx.fft.ifftn,
(False, False): mx.fft.fftn,
}
tffts = {
(True, True): torch.fft.irfftn,
(True, False): torch.fft.rfftn,
(False, True): torch.fft.ifftn,
(False, False): torch.fft.fftn,
}
for r, i, ax, sh in itertools.product(real, inverse, axes, shapes):
def f(x):
y = mxffts[r, i](x)
return (mx.abs(y) ** 2).sum()
def g(x):
y = tffts[r, i](x)
return (torch.abs(y) ** 2).sum()
if r and not i:
x = mx.random.normal(sh)
else:
x = mx.random.normal((*sh, 2)).view(mx.complex64).squeeze()
fx = f(x)
gx = g(torch.tensor(x))
self.assertLess((fx - gx).abs().max() / gx.abs().mean(), 1e-4)
dfdx = mx.grad(f)(x)
dgdx = torch.func.grad(g)(torch.tensor(x))
self.assertLess((dfdx - dgdx).abs().max() / dgdx.abs().mean(), 1e-4)
if __name__ == "__main__":
mlx_tests.MLXTestRunner()
|