-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest_fft.py
More file actions
956 lines (795 loc) · 37.5 KB
/
Copy pathtest_fft.py
File metadata and controls
956 lines (795 loc) · 37.5 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
import dpctl
import dpctl.tensor as dpt
import numpy
import pytest
from numpy.testing import assert_raises
import dpnp
from dpnp.dpnp_utils import map_dtype_to_device
from dpnp.exceptions import ExecutionPlacementError
from .helper import (
assert_dtype_allclose,
generate_random_numpy_array,
get_all_dtypes,
get_complex_dtypes,
get_float_dtypes,
has_support_aspect16,
numpy_version,
)
from .third_party.cupy import testing
class TestFft:
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
@pytest.mark.parametrize("n", [None, 5, 20])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
def test_basic(self, dtype, n, norm):
a = generate_random_numpy_array(11, dtype)
ia = dpnp.array(a)
result = dpnp.fft.fft(ia, n=n, norm=norm)
expected = numpy.fft.fft(a, n=n, norm=norm)
flag = True if numpy_version() < "2.0.0" else False
assert_dtype_allclose(result, expected, check_only_type_kind=flag)
# inverse FFT
result = dpnp.fft.ifft(result, n=n, norm=norm)
expected = numpy.fft.ifft(expected, n=n, norm=norm)
assert_dtype_allclose(result, expected, check_only_type_kind=flag)
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 8])
@pytest.mark.parametrize("axis", [-1, 1, 0])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_2d_array(self, dtype, n, axis, norm, order):
a = generate_random_numpy_array((3, 4), dtype=dtype, order=order)
ia = dpnp.array(a)
result = dpnp.fft.fft(ia, n=n, axis=axis, norm=norm)
expected = numpy.fft.fft(a, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected)
# inverse FFT
result = dpnp.fft.ifft(result, n=n, axis=axis, norm=norm)
expected = numpy.fft.ifft(expected, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 8])
@pytest.mark.parametrize("axis", [0, 1, 2])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_3d_array(self, dtype, n, axis, norm, order):
a = generate_random_numpy_array((2, 3, 4), dtype=dtype, order=order)
ia = dpnp.array(a)
result = dpnp.fft.fft(ia, n=n, axis=axis, norm=norm)
expected = numpy.fft.fft(a, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected)
# inverse FFT
result = dpnp.fft.ifft(result, n=n, axis=axis, norm=norm)
expected = numpy.fft.ifft(expected, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("n", [None, 5, 20])
def test_usm_ndarray(self, n):
a = generate_random_numpy_array(11, dtype=numpy.complex64)
a_usm = dpt.asarray(a)
expected = numpy.fft.fft(a, n=n)
out = dpt.empty(expected.shape, dtype=a_usm.dtype)
result = dpnp.fft.fft(a_usm, n=n, out=out)
assert out is result.get_array()
assert_dtype_allclose(result, expected)
# in-place
if n is None:
result = dpnp.fft.fft(a_usm, n=n, out=a_usm)
assert a_usm is result.get_array()
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 20])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
def test_out(self, dtype, n, norm):
a = generate_random_numpy_array(11, dtype=dtype)
ia = dpnp.array(a)
# FFT
expected = numpy.fft.fft(a, n=n, norm=norm)
out = dpnp.empty(expected.shape, dtype=a.dtype)
result = dpnp.fft.fft(ia, n=n, norm=norm, out=out)
assert out is result
assert_dtype_allclose(result, expected)
# inverse FFT
result = dpnp.fft.ifft(result, n=n, norm=norm, out=out)
assert out is result
expected = numpy.fft.ifft(expected, n=n, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("axis", [0, 1])
def test_inplace_out(self, axis):
# Test some weirder in-place combinations
y_np = generate_random_numpy_array((20, 20), dtype=numpy.complex64)
y = dpnp.array(y_np)
# Fully in-place.
y1 = y.copy()
expected1 = numpy.fft.fft(y1.asnumpy(), axis=axis)
result1 = dpnp.fft.fft(y1, axis=axis, out=y1)
assert result1 is y1
assert_dtype_allclose(result1, expected1)
# In-place of part of the array; rest should be unchanged.
y2 = y.copy()
out2 = y2[:10] if axis == 0 else y2[:, :10]
expected2 = numpy.fft.fft(y2.asnumpy(), n=10, axis=axis)
result2 = dpnp.fft.fft(y2, n=10, axis=axis, out=out2)
assert result2 is out2
assert_dtype_allclose(out2, expected2)
assert_dtype_allclose(result2, expected2)
if axis == 0:
assert_dtype_allclose(y2[10:], y_np[10:])
else:
assert_dtype_allclose(y2[:, 10:], y_np[:, 10:])
# In-place of another part of the array.
y3 = y.copy()
y3_sel = y3[5:] if axis == 0 else y3[:, 5:]
out3 = y3[5:15] if axis == 0 else y3[:, 5:15]
expected3 = numpy.fft.fft(y3_sel.asnumpy(), n=10, axis=axis)
result3 = dpnp.fft.fft(y3_sel, n=10, axis=axis, out=out3)
assert result3 is out3
assert_dtype_allclose(result3, expected3)
if axis == 0:
assert_dtype_allclose(y3[:5], y_np[:5])
assert_dtype_allclose(y3[15:], y_np[15:])
else:
assert_dtype_allclose(y3[:, :5], y_np[:, :5])
assert_dtype_allclose(y3[:, 15:], y_np[:, 15:])
# In-place with n > nin; rest should be unchanged.
# for this case, out-of-place FFT is called with a temporary
# buffer for output array in FFT call
y4 = y.copy()
y4_sel = y4[:10] if axis == 0 else y4[:, :10]
out4 = y4[:15] if axis == 0 else y4[:, :15]
expected4 = numpy.fft.fft(y4_sel.asnumpy(), n=15, axis=axis)
result4 = dpnp.fft.fft(y4_sel, n=15, axis=axis, out=out4)
assert result4 is out4
assert_dtype_allclose(result4, expected4)
if axis == 0:
assert_dtype_allclose(y4[15:], y_np[15:])
else:
assert_dtype_allclose(y4[:, 15:], y_np[:, 15:])
# Overwrite in a transpose.
# for this case, out-of-place FFT is called with a temporary
# buffer for output array in FFT call
y5 = y.copy()
out5 = y5.T
result5 = dpnp.fft.fft(y5, axis=axis, out=out5)
assert result5 is out5
assert_dtype_allclose(y5, expected1.T)
assert_dtype_allclose(result5, expected1)
# Reverse strides.
# for this case, out-of-place FFT is called with a temporary
# buffer for output array in FFT call
y6 = y.copy()
out6 = y6[::-1] if axis == 0 else y6[:, ::-1]
result6 = dpnp.fft.fft(y6, axis=axis, out=out6)
assert result6 is out6
assert_dtype_allclose(result6, expected1)
if axis == 0:
assert_dtype_allclose(y6, expected1[::-1])
else:
assert_dtype_allclose(y6, expected1[:, ::-1])
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 8])
@pytest.mark.parametrize("axis", [-1, 0])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_2d_array_out(self, dtype, n, axis, norm, order):
a = generate_random_numpy_array((3, 4), dtype=dtype, order=order)
ia = dpnp.array(a)
expected = numpy.fft.fft(a, n=n, axis=axis, norm=norm)
out = dpnp.empty(expected.shape, dtype=a.dtype)
result = dpnp.fft.fft(ia, n=n, axis=axis, norm=norm, out=out)
assert out is result
assert_dtype_allclose(result, expected)
# inverse FFT
result = dpnp.fft.ifft(result, n=n, axis=axis, norm=norm, out=out)
assert out is result
expected = numpy.fft.ifft(expected, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("stride", [-1, -3, 2, 5])
def test_strided_1d(self, stride):
a = generate_random_numpy_array(20, dtype=numpy.complex64)
ia = dpnp.array(a)
a = a[::stride]
ia = ia[::stride]
result = dpnp.fft.fft(ia)
expected = numpy.fft.fft(a)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("stride_x", [-1, -3, 2, 3])
@pytest.mark.parametrize("stride_y", [-1, -3, 2, 3])
def test_strided_2d(self, stride_x, stride_y):
a = generate_random_numpy_array((12, 10), dtype=numpy.complex64)
ia = dpnp.array(a)
a = a[::stride_x, ::stride_y]
ia = ia[::stride_x, ::stride_y]
result = dpnp.fft.fft(ia)
expected = numpy.fft.fft(a)
assert_dtype_allclose(result, expected)
def test_empty_array(self):
a = numpy.empty((10, 0, 4), dtype=numpy.complex64)
ia = dpnp.array(a)
# returns empty array, a.size=0
result = dpnp.fft.fft(ia, axis=0)
expected = numpy.fft.fft(a, axis=0)
assert_dtype_allclose(result, expected)
# calculates FFT, a.size become non-zero because of n=2
result = dpnp.fft.fft(ia, axis=1, n=2)
expected = numpy.fft.fft(a, axis=1, n=2)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("xp", [numpy, dpnp])
def test_error(self, xp):
# 0-D input
a = xp.array(3)
# dpnp and Intel NumPy raise ValueError
# stock NumPy raises IndexError
assert_raises((ValueError, IndexError), xp.fft.fft, a)
# n is not int
a = xp.ones((4, 3))
if xp == dpnp:
# dpnp and stock NumPy raise TypeError
# Intel NumPy raises SystemError for Python 3.10 and 3.11
assert_raises(TypeError, xp.fft.fft, a, n=5.0)
# Invalid number of FFT point for incorrect n value
assert_raises(ValueError, xp.fft.fft, a, n=-5)
# invalid norm
assert_raises(ValueError, xp.fft.fft, a, norm="square")
# Invalid number of FFT point for empty arrays
a = xp.ones((5, 0, 4))
assert_raises(ValueError, xp.fft.fft, a, axis=1)
def test_validate_out(self):
# Inconsistent sycl_queue
a = dpnp.ones((10,), dtype=dpnp.complex64, sycl_queue=dpctl.SyclQueue())
out = dpnp.empty((10,), sycl_queue=dpctl.SyclQueue())
assert_raises(ExecutionPlacementError, dpnp.fft.fft, a, out=out)
# Invalid shape
a = dpnp.ones((10,), dtype=dpnp.complex64)
out = dpnp.empty((11,), dtype=dpnp.complex64)
assert_raises(ValueError, dpnp.fft.fft, a, out=out)
# Invalid dtype for c2c or r2c FFT
a = dpnp.ones((10,), dtype=dpnp.complex64)
out = dpnp.empty((10,), dtype=dpnp.float32)
assert_raises(TypeError, dpnp.fft.fft, a, out=out)
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
@pytest.mark.parametrize("stride", [2, 3, -1, -3])
def test_strided(self, dtype, stride):
a = generate_random_numpy_array(20, dtype=dtype)
ia = dpnp.array(a)
result = dpnp.fft.fft(ia[::stride])
expected = numpy.fft.fft(a[::stride])
assert_dtype_allclose(result, expected)
class TestFft2:
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
@pytest.mark.parametrize("axes", [(0, 1), (1, 2), (0, 2), (2, 1), (2, 0)])
@pytest.mark.parametrize("norm", [None, "forward", "backward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_basic(self, dtype, axes, norm, order):
a = generate_random_numpy_array((2, 3, 4), dtype, order)
ia = dpnp.array(a)
result = dpnp.fft.fft2(ia, axes=axes, norm=norm)
expected = numpy.fft.fft2(a, axes=axes, norm=norm)
assert_dtype_allclose(result, expected)
# inverse FFT
result = dpnp.fft.ifft2(result, axes=axes, norm=norm)
expected = numpy.fft.ifft2(expected, axes=axes, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("s", [None, (3, 3), (10, 10), (3, 10)])
def test_s(self, s):
a = generate_random_numpy_array((6, 8), dtype=numpy.complex64)
ia = dpnp.array(a)
result = dpnp.fft.fft2(ia, s=s)
expected = numpy.fft.fft2(a, s=s)
assert_dtype_allclose(result, expected)
# inverse FFT
result = dpnp.fft.ifft2(result, s=s)
expected = numpy.fft.ifft2(expected, s=s)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("xp", [numpy, dpnp])
def test_error(self, xp):
# 0-D input
a = xp.ones(())
assert_raises(IndexError, xp.fft.fft2, a)
@pytest.mark.parametrize("func", ["fftfreq", "rfftfreq"])
class TestFftfreq:
@pytest.mark.parametrize("n", [10, 20])
@pytest.mark.parametrize("d", [0.5, 2])
def test_basic(self, func, n, d):
result = getattr(dpnp.fft, func)(n, d)
expected = getattr(numpy.fft, func)(n, d)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("dt", [None] + get_float_dtypes())
def test_dtype(self, func, dt):
n = 15
result = getattr(dpnp.fft, func)(n, dtype=dt)
expected = getattr(numpy.fft, func)(n).astype(dt)
assert_dtype_allclose(result, expected)
def test_error(self, func):
func = getattr(dpnp.fft, func)
# n must be an integer
assert_raises(ValueError, func, 10.0)
# d must be an scalar
assert_raises(ValueError, func, 10, (2,))
# dtype must be None or a real-valued floating-point dtype
# which is passed as a keyword argument only
assert_raises(TypeError, func, 10, 2, None)
assert_raises(ValueError, func, 10, 2, dtype=dpnp.intp)
assert_raises(ValueError, func, 10, 2, dtype=dpnp.complex64)
class TestFftn:
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
@pytest.mark.parametrize(
"axes", [None, (0, 1, 2), (-1, -4, -2), (-2, -4, -1, -3)]
)
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_basic(self, dtype, axes, norm, order):
a = generate_random_numpy_array((2, 3, 4, 5), dtype, order)
ia = dpnp.array(a)
result = dpnp.fft.fftn(ia, axes=axes, norm=norm)
expected = numpy.fft.fftn(a, axes=axes, norm=norm)
assert_dtype_allclose(result, expected)
# inverse FFT
result = dpnp.fft.ifftn(result, axes=axes, norm=norm)
expected = numpy.fft.ifftn(expected, axes=axes, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize(
"axes", [(2, 0, 2, 0), (0, 1, 1), (2, 0, 1, 3, 2, 1)]
)
def test_repeated_axes(self, axes):
a = generate_random_numpy_array((2, 3, 4, 5), dtype=numpy.complex64)
ia = dpnp.array(a)
result = dpnp.fft.fftn(ia, axes=axes)
# Intel NumPy ignores repeated axes (mkl_fft-gh-104), handle it one by one
expected = a
for ii in axes[::-1]:
expected = numpy.fft.fft(expected, axis=ii)
assert_dtype_allclose(result, expected)
# inverse FFT
result = dpnp.fft.ifftn(result, axes=axes)
for ii in axes[::-1]:
expected = numpy.fft.ifft(expected, axis=ii)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("axes", [(2, 3, 3, 2), (0, 0, 3, 3)])
@pytest.mark.parametrize("s", [(5, 4, 3, 3), (7, 8, 10, 9)])
def test_repeated_axes_with_s(self, axes, s):
a = generate_random_numpy_array((2, 3, 4, 5), dtype=numpy.complex64)
ia = dpnp.array(a)
result = dpnp.fft.fftn(ia, s=s, axes=axes)
# Intel NumPy ignores repeated axes (mkl_fft-gh-104), handle it one by one
expected = a
for jj, ii in zip(s[::-1], axes[::-1]):
expected = numpy.fft.fft(expected, n=jj, axis=ii)
assert_dtype_allclose(result, expected)
# inverse FFT
result = dpnp.fft.ifftn(result, s=s, axes=axes)
for jj, ii in zip(s[::-1], axes[::-1]):
expected = numpy.fft.ifft(expected, n=jj, axis=ii)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("axes", [(0, 1, 2, 3), (1, 2, 1, 2), (2, 2, 2, 3)])
@pytest.mark.parametrize("s", [(2, 3, 4, 5), (5, 4, 7, 8), (2, 5, 1, 2)])
def test_out(self, axes, s):
a = generate_random_numpy_array((2, 3, 4, 5), dtype=numpy.complex64)
ia = dpnp.array(a)
# Intel NumPy ignores repeated axes (mkl_fft-gh-104), handle it one by one
expected = a
for jj, ii in zip(s[::-1], axes[::-1]):
expected = numpy.fft.fft(expected, n=jj, axis=ii)
out = dpnp.empty(expected.shape, dtype=a.dtype)
result = dpnp.fft.fftn(ia, out=out, s=s, axes=axes)
assert out is result
assert_dtype_allclose(result, expected)
# inverse FFT
out = dpnp.empty(expected.shape, dtype=a.dtype)
result = dpnp.fft.ifftn(result, out=out, s=s, axes=axes)
assert out is result
for jj, ii in zip(s[::-1], axes[::-1]):
expected = numpy.fft.ifft(expected, n=jj, axis=ii)
assert_dtype_allclose(result, expected)
def test_negative_s(self):
a = generate_random_numpy_array((3, 4, 5), dtype=numpy.complex64)
ia = dpnp.array(a)
# For dpnp and stock NumPy 2.0, if s is -1, the whole input is used
# (no padding or trimming).
result = dpnp.fft.fftn(ia, s=(-1, -1), axes=(0, 2))
expected = numpy.fft.fftn(a, s=(3, 5), axes=(0, 2))
assert_dtype_allclose(result, expected)
def test_empty_array(self):
a = numpy.empty((10, 0, 4), dtype=numpy.complex64)
ia = dpnp.array(a)
result = dpnp.fft.fftn(ia, axes=(0, 2))
expected = numpy.fft.fftn(a, axes=(0, 2))
assert_dtype_allclose(result, expected)
result = dpnp.fft.fftn(ia, axes=(0, 1, 2), s=(5, 2, 4))
expected = numpy.fft.fftn(a, axes=(0, 1, 2), s=(5, 2, 4))
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
def test_0D(self, dtype):
a = dpnp.array(3, dtype=dtype) # 0-D input
# axes is None
# For 0-D array, stock Numpy and dpnp return input array
# while Intel NumPy return a complex zero
result = dpnp.fft.fftn(a)
expected = a.asnumpy()
assert_dtype_allclose(result, expected)
# axes=()
# For 0-D array with axes=(), stock Numpy and dpnp return input array
# Intel NumPy does not support empty axes and raises an Error
result = dpnp.fft.fftn(a, axes=())
expected = a.asnumpy()
assert_dtype_allclose(result, expected)
# axes=(0,)
# For 0-D array with non-empty axes, stock Numpy and dpnp raise
# IndexError, while Intel NumPy raises ZeroDivisionError
assert_raises(IndexError, dpnp.fft.fftn, a, axes=(0,))
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
def test_empty_axes(self, dtype):
a = dpnp.ones((2, 3, 4), dtype=dtype)
# For axes=(), stock Numpy and dpnp return input array
# Intel NumPy does not support empty axes and raises an Error
result = dpnp.fft.fftn(a, axes=())
expected = a.asnumpy()
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("xp", [numpy, dpnp])
def test_error(self, xp):
# s is not int
a = xp.ones((4, 3))
# dpnp and stock NumPy raise TypeError
# Intel NumPy raises ValueError
assert_raises(
(TypeError, ValueError), xp.fft.fftn, a, s=(5.0,), axes=(0,)
)
# s is not a sequence
assert_raises(TypeError, xp.fft.fftn, a, s=5, axes=(0,))
# Invalid number of FFT point, invalid s value
assert_raises(ValueError, xp.fft.fftn, a, s=(-5,), axes=(0,))
# axes should be given if s is not None
# dpnp raises ValueError
# stock NumPy will raise an Error in future versions
# Intel NumPy raises TypeError for a different reason:
# when given, axes and shape arguments have to be of the same length
if xp == dpnp:
assert_raises(ValueError, xp.fft.fftn, a, s=(5,))
# axes and s should have the same length
assert_raises(ValueError, xp.fft.fftn, a, s=(5, 5), axes=(0,))
class TestFftshift:
@pytest.mark.parametrize("func", ["fftshift", "ifftshift"])
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
@pytest.mark.parametrize("axes", [None, 1, (0, 1)])
def test_basic(self, func, dtype, axes):
a = generate_random_numpy_array((3, 4), dtype=dtype)
ia = dpnp.array(a)
expected = getattr(dpnp.fft, func)(ia, axes=axes)
result = getattr(numpy.fft, func)(a, axes=axes)
assert_dtype_allclose(expected, result)
class TestHfft:
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
@pytest.mark.parametrize("n", [None, 5, 18])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
def test_basic(self, dtype, n, norm):
a = generate_random_numpy_array(11, dtype)
ia = dpnp.array(a)
result = dpnp.fft.hfft(ia, n=n, norm=norm)
expected = numpy.fft.hfft(a, n=n, norm=norm)
flag = True if numpy_version() < "2.0.0" else False
assert_dtype_allclose(
result, expected, factor=24, check_only_type_kind=flag
)
@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_none=True, no_complex=True)
)
@pytest.mark.parametrize("n", [None, 5, 20])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
def test_inverse(self, dtype, n, norm):
a = generate_random_numpy_array(11, dtype)
ia = dpnp.array(a)
result = dpnp.fft.ihfft(ia, n=n, norm=norm)
expected = numpy.fft.ihfft(a, n=n, norm=norm)
flag = True if numpy_version() < "2.0.0" else False
assert_dtype_allclose(result, expected, check_only_type_kind=flag)
def test_error(self):
a = dpnp.ones(11)
# incorrect norm
assert_raises(ValueError, dpnp.fft.hfft, a, norm="backwards")
@testing.with_requires("numpy>=2.0.0")
@pytest.mark.parametrize("dtype", get_complex_dtypes())
def test_complex_error(self, dtype):
a = generate_random_numpy_array(11, dtype)
ia = dpnp.array(a)
assert_raises(TypeError, dpnp.fft.ihfft, ia)
assert_raises(TypeError, numpy.fft.ihfft, a)
class TestIrfft:
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
@pytest.mark.parametrize("n", [None, 5, 18])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
def test_basic(self, dtype, n, norm):
a = generate_random_numpy_array(11, dtype=dtype)
ia = dpnp.array(a)
result = dpnp.fft.irfft(ia, n=n, norm=norm)
expected = numpy.fft.irfft(a, n=n, norm=norm)
flag = True if numpy_version() < "2.0.0" else False
assert_dtype_allclose(
result, expected, factor=24, check_only_type_kind=flag
)
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 8])
@pytest.mark.parametrize("axis", [-1, 1, 0])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_2d_array(self, dtype, n, axis, norm, order):
a = generate_random_numpy_array((3, 4), dtype=dtype, order=order)
ia = dpnp.array(a)
result = dpnp.fft.irfft(ia, n=n, axis=axis, norm=norm)
expected = numpy.fft.irfft(a, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 8])
@pytest.mark.parametrize("axis", [0, 1, 2])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_3d_array(self, dtype, n, axis, norm, order):
a = generate_random_numpy_array((4, 5, 6), dtype, order)
ia = dpnp.array(a)
result = dpnp.fft.irfft(ia, n=n, axis=axis, norm=norm)
expected = numpy.fft.irfft(a, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected, factor=16)
@pytest.mark.parametrize("n", [None, 5, 17, 18])
def test_usm_ndarray(self, n):
a = generate_random_numpy_array(11, dtype=numpy.complex64)
a_usm = dpt.asarray(a)
expected = numpy.fft.irfft(a, n=n)
out = dpt.empty(expected.shape, dtype=a_usm.real.dtype)
result = dpnp.fft.irfft(a_usm, n=n, out=out)
assert out is result.get_array()
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 18])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
def test_out(self, dtype, n, norm):
a = generate_random_numpy_array(11, dtype=dtype)
ia = dpnp.array(a)
expected = numpy.fft.irfft(a, n=n, norm=norm)
out = dpnp.empty(expected.shape, dtype=a.real.dtype)
result = dpnp.fft.irfft(ia, n=n, norm=norm, out=out)
assert out is result
assert_dtype_allclose(result, expected, factor=24)
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 8])
@pytest.mark.parametrize("axis", [-1, 0])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_2d_array_out(self, dtype, n, axis, norm, order):
a = generate_random_numpy_array((3, 4), dtype=dtype, order=order)
ia = dpnp.array(a)
expected = numpy.fft.irfft(a, n=n, axis=axis, norm=norm)
out = dpnp.empty(expected.shape, dtype=expected.dtype)
result = dpnp.fft.irfft(ia, n=n, axis=axis, norm=norm, out=out)
assert out is result
assert_dtype_allclose(result, expected)
def test_validate_out(self):
# Invalid dtype for c2r FFT
a = dpnp.ones((10,), dtype=dpnp.complex64)
out = dpnp.empty((18,), dtype=dpnp.complex64)
assert_raises(TypeError, dpnp.fft.irfft, a, out=out)
class TestRfft:
@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_none=True, no_complex=True)
)
@pytest.mark.parametrize("n", [None, 5, 20])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
def test_basic(self, dtype, n, norm):
a = generate_random_numpy_array(11, dtype, low=-1, high=1)
ia = dpnp.array(a)
result = dpnp.fft.rfft(ia, n=n, norm=norm)
expected = numpy.fft.rfft(a, n=n, norm=norm)
factor = 120 if dtype in [dpnp.int8, dpnp.uint8] else 8
assert_dtype_allclose(result, expected, factor=factor)
@pytest.mark.parametrize("dtype", get_float_dtypes())
@pytest.mark.parametrize("n", [None, 5, 8])
@pytest.mark.parametrize("axis", [-1, 1, 0])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_2d_array(self, dtype, n, axis, norm, order):
a = generate_random_numpy_array((3, 4), dtype=dtype, order=order)
ia = dpnp.array(a)
result = dpnp.fft.rfft(ia, n=n, axis=axis, norm=norm)
expected = numpy.fft.rfft(a, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("dtype", get_float_dtypes())
@pytest.mark.parametrize("n", [None, 5, 8])
@pytest.mark.parametrize("axis", [0, 1, 2])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_3d_array(self, dtype, n, axis, norm, order):
a = generate_random_numpy_array((2, 3, 4), dtype=dtype, order=order)
ia = dpnp.array(a)
result = dpnp.fft.rfft(ia, n=n, axis=axis, norm=norm)
expected = numpy.fft.rfft(a, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("n", [None, 5, 20])
def test_usm_ndarray(self, n):
a = generate_random_numpy_array(11)
a_usm = dpt.asarray(a)
expected = numpy.fft.rfft(a, n=n)
out_dt = map_dtype_to_device(dpnp.complex128, a_usm.sycl_device)
out = dpt.empty(expected.shape, dtype=out_dt)
result = dpnp.fft.rfft(a_usm, n=n, out=out)
assert out is result.get_array()
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("dtype", get_float_dtypes())
@pytest.mark.parametrize("n", [None, 5, 20])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
def test_out(self, dtype, n, norm):
a = generate_random_numpy_array(11, dtype=dtype)
ia = dpnp.array(a)
expected = numpy.fft.rfft(a, n=n, norm=norm)
out = dpnp.empty(expected.shape, dtype=expected.dtype)
result = dpnp.fft.rfft(ia, n=n, norm=norm, out=out)
assert out is result
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("dtype", get_float_dtypes())
@pytest.mark.parametrize("n", [None, 5, 8])
@pytest.mark.parametrize("axis", [-1, 0])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_2d_array_out(self, dtype, n, axis, norm, order):
a = generate_random_numpy_array((3, 4), dtype=dtype, order=order)
ia = dpnp.array(a)
expected = numpy.fft.rfft(a, n=n, axis=axis, norm=norm)
out = dpnp.empty(expected.shape, dtype=expected.dtype)
result = dpnp.fft.rfft(ia, n=n, axis=axis, norm=norm, out=out)
assert out is result
assert_dtype_allclose(result, expected)
@pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support")
def test_float16(self):
a = numpy.arange(10, dtype=numpy.float16)
ia = dpnp.array(a)
expected = numpy.fft.rfft(a)
result = dpnp.fft.rfft(ia)
# TODO: change to the commented line when mkl_fft-gh-204 is resolved
flag = True
# flag = True if numpy_version() < "2.0.0" else False
assert_dtype_allclose(result, expected, check_only_type_kind=flag)
@testing.with_requires("numpy>=2.0.0")
@pytest.mark.parametrize("xp", [numpy, dpnp])
def test_error(self, xp):
a = xp.ones((4, 3), dtype=xp.complex64)
# invalid dtype of input array for r2c FFT
assert_raises(TypeError, xp.fft.rfft, a)
def test_validate_out(self):
# Invalid shape for r2c FFT
a = dpnp.ones((10,), dtype=dpnp.float32)
out = dpnp.empty((10,), dtype=dpnp.complex64)
assert_raises(ValueError, dpnp.fft.rfft, a, out=out)
class TestRfft2:
@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_none=True, no_complex=True)
)
@pytest.mark.parametrize("axes", [(0, 1), (1, 2), (0, 2), (2, 1), (2, 0)])
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_basic(self, dtype, axes, norm, order):
a = generate_random_numpy_array((2, 3, 4), dtype, order)
ia = dpnp.array(a)
result = dpnp.fft.rfft2(ia, axes=axes, norm=norm)
expected = numpy.fft.rfft2(a, axes=axes, norm=norm)
assert_dtype_allclose(result, expected)
s = (a.shape[axes[0]], a.shape[axes[1]])
result = dpnp.fft.irfft2(result, s=s, axes=axes, norm=norm)
expected = numpy.fft.irfft2(expected, s=s, axes=axes, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
def test_inverse(self, dtype):
# x is Hermitian symmetric
x = numpy.array([[0, 1, 2], [5, 4, 6], [5, 7, 6]])
a = numpy.array(x, dtype=dtype)
ia = dpnp.array(a)
result = dpnp.fft.irfft2(ia)
expected = numpy.fft.irfft2(a)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("s", [None, (3, 3), (10, 10), (3, 10)])
def test_s(self, s):
a = generate_random_numpy_array((6, 8))
ia = dpnp.array(a)
result = dpnp.fft.rfft2(ia, s=s)
expected = numpy.fft.rfft2(a, s=s)
assert_dtype_allclose(result, expected)
result = dpnp.fft.irfft2(result, s=s)
expected = numpy.fft.irfft2(expected, s=s)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("xp", [numpy, dpnp])
def test_error(self, xp):
a = xp.ones((2, 3))
# empty axes
assert_raises(IndexError, xp.fft.rfft2, a, axes=())
a = xp.ones((2, 3), dtype=xp.complex64)
# Input array must be real
# Stock NumPy 2.0 raises TypeError
# while stock NumPy 1.26 ignores imaginary part
if xp == dpnp:
assert_raises(TypeError, xp.fft.rfft2, a)
class TestRfftn:
@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_none=True, no_complex=True)
)
@pytest.mark.parametrize(
"axes", [(0, 1, 2), (-2, -4, -1, -3), (-1, -4, -2)]
)
@pytest.mark.parametrize("norm", [None, "backward", "forward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_basic(self, dtype, axes, norm, order):
a = generate_random_numpy_array((2, 3, 4, 5), dtype, order)
ia = dpnp.array(a)
result = dpnp.fft.rfftn(ia, axes=axes, norm=norm)
expected = numpy.fft.rfftn(a, axes=axes, norm=norm)
assert_dtype_allclose(result, expected)
# inverse FFT
s = []
for axis in axes:
s.append(a.shape[axis])
result = dpnp.fft.irfftn(result, s=s, axes=axes, norm=norm)
expected = numpy.fft.irfftn(expected, s=s, axes=axes, norm=norm)
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize(
"axes", [(2, 0, 2, 0), (0, 1, 1), (2, 0, 1, 3, 2, 1)]
)
def test_repeated_axes(self, axes):
a = generate_random_numpy_array((2, 3, 4, 5))
ia = dpnp.array(a)
result = dpnp.fft.rfftn(ia, axes=axes)
# Intel NumPy ignores repeated axes (mkl_fft-gh-104), handle it one by one
expected = numpy.fft.rfft(a, axis=axes[-1])
# need to pass shape for c2c FFT since expected and a
# do not have the same shape after calling rfft
shape = []
for axis in axes:
shape.append(a.shape[axis])
for jj, ii in zip(shape[-2::-1], axes[-2::-1]):
expected = numpy.fft.fft(expected, n=jj, axis=ii)
assert_dtype_allclose(result, expected)
# inverse FFT
result = dpnp.fft.irfftn(result, axes=axes)
for ii in axes[:-1]:
expected = numpy.fft.ifft(expected, axis=ii)
expected = numpy.fft.irfft(expected, axis=axes[-1])
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("axes", [(2, 3, 3, 2), (0, 0, 3, 3)])
@pytest.mark.parametrize("s", [(5, 4, 3, 3), (7, 8, 10, 9)])
def test_repeated_axes_with_s(self, axes, s):
a = generate_random_numpy_array((2, 3, 4, 5), dtype=numpy.float32)
ia = dpnp.array(a)
result = dpnp.fft.rfftn(ia, s=s, axes=axes)
# Intel NumPy ignores repeated axes (mkl_fft-gh-104), handle it one by one
expected = numpy.fft.rfft(a, n=s[-1], axis=axes[-1])
for jj, ii in zip(s[-2::-1], axes[-2::-1]):
expected = numpy.fft.fft(expected, n=jj, axis=ii)
assert_dtype_allclose(result, expected)
result = dpnp.fft.irfftn(result, s=s, axes=axes)
for jj, ii in zip(s[:-1], axes[:-1]):
expected = numpy.fft.ifft(expected, n=jj, axis=ii)
expected = numpy.fft.irfft(expected, n=s[-1], axis=axes[-1])
assert_dtype_allclose(result, expected)
@pytest.mark.parametrize("axes", [(0, 1, 2, 3), (1, 2, 1, 2), (2, 2, 2, 3)])
@pytest.mark.parametrize("s", [(2, 3, 4, 5), (5, 6, 7, 9), (2, 5, 1, 2)])
def test_out(self, axes, s):
a = generate_random_numpy_array((2, 3, 4, 5), dtype=numpy.float32)
ia = dpnp.array(a)
# Intel NumPy ignores repeated axes (mkl_fft-gh-104), handle it one by one
expected = numpy.fft.rfft(a, n=s[-1], axis=axes[-1])
for jj, ii in zip(s[-2::-1], axes[-2::-1]):
expected = numpy.fft.fft(expected, n=jj, axis=ii)
out = dpnp.empty(expected.shape, dtype=numpy.complex64)
result = dpnp.fft.rfftn(ia, out=out, s=s, axes=axes)
assert out is result
assert_dtype_allclose(result, expected)
# inverse FFT
for jj, ii in zip(s[:-1], axes[:-1]):
expected = numpy.fft.ifft(expected, n=jj, axis=ii)
expected = numpy.fft.irfft(expected, n=s[-1], axis=axes[-1])
out = dpnp.empty(expected.shape, dtype=numpy.float32)
result = dpnp.fft.irfftn(result, out=out, s=s, axes=axes)
assert out is result
assert_dtype_allclose(result, expected)
def test_1d_array(self):
a = generate_random_numpy_array(20, dtype=numpy.float32)
ia = dpnp.array(a)
result = dpnp.fft.rfftn(ia)
expected = numpy.fft.rfftn(a)
assert_dtype_allclose(result, expected)
result = dpnp.fft.irfftn(ia)
expected = numpy.fft.irfftn(a)
flag = True if numpy_version() < "2.0.0" else False
assert_dtype_allclose(result, expected, check_only_type_kind=flag)