-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy patharrayfire_test.cpp
More file actions
2227 lines (1963 loc) · 80.3 KB
/
arrayfire_test.cpp
File metadata and controls
2227 lines (1963 loc) · 80.3 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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************
* Copyright (c) 2020, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#define EXTERN_TEMPLATE
#include <testHelpers.hpp>
#include <arrayfire.h>
#include <af/algorithm.h>
#include <af/compatible.h>
#include <af/internal.h>
#include <gtest/gtest.h>
#include <half.hpp>
#include <relative_difference.hpp>
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <limits>
#include <numeric>
#include <sstream>
#include <stdexcept>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using af::af_cdouble;
using af::af_cfloat;
using std::vector;
bool operator==(const af_half &lhs, const af_half &rhs) {
return lhs.data_ == rhs.data_;
}
std::ostream &operator<<(std::ostream &os, const af_half &val) {
float out = *reinterpret_cast<const half_float::half *>(&val);
os << out;
return os;
}
std::ostream &operator<<(std::ostream &os, af::Backend bk) {
switch (bk) {
case AF_BACKEND_CPU: os << "AF_BACKEND_CPU"; break;
case AF_BACKEND_CUDA: os << "AF_BACKEND_CUDA"; break;
case AF_BACKEND_OPENCL: os << "AF_BACKEND_OPENCL"; break;
case AF_BACKEND_ONEAPI: os << "AF_BACKEND_ONEAPI"; break;
case AF_BACKEND_DEFAULT: os << "AF_BACKEND_DEFAULT"; break;
}
return os;
}
std::ostream &operator<<(std::ostream &os, af_err e) {
return os << af_err_to_string(e);
}
std::ostream &operator<<(std::ostream &os, af::dtype type) {
std::string name;
switch (type) {
case f32: name = "f32"; break;
case c32: name = "c32"; break;
case f64: name = "f64"; break;
case c64: name = "c64"; break;
case b8: name = "b8"; break;
case s32: name = "s32"; break;
case u32: name = "u32"; break;
case s8: name = "s8"; break;
case u8: name = "u8"; break;
case s64: name = "s64"; break;
case u64: name = "u64"; break;
case s16: name = "s16"; break;
case u16: name = "u16"; break;
case f16: name = "f16"; break;
default: assert(false && "Invalid type");
}
return os << name;
}
std::string readNextNonEmptyLine(std::ifstream &file) {
std::string result = "";
// Using a for loop to read the next non empty line
for (std::string line; std::getline(file, line);) {
result += line;
if (result != "") break;
}
// If no file has been found, throw an exception
if (result == "") {
throw std::runtime_error("Non empty lines not found in the file");
}
return result;
}
std::string getBackendName(bool lower) {
af::Backend backend = af::getActiveBackend();
switch (backend) {
case AF_BACKEND_CPU:
return lower ? std::string("cpu") : std::string("CPU");
case AF_BACKEND_CUDA:
return lower ? std::string("cuda") : std::string("CUDA");
case AF_BACKEND_OPENCL:
return lower ? std::string("opencl") : std::string("OpenCL");
case AF_BACKEND_ONEAPI:
return lower ? std::string("oneapi") : std::string("oneAPI");
default: return lower ? std::string("unknown") : std::string("Unknown");
}
}
std::string getTestName() {
std::string testname =
::testing::UnitTest::GetInstance()->current_test_info()->name();
return testname;
}
namespace half_float {
std::ostream &operator<<(std::ostream &os, half_float::half val) {
os << (float)val;
return os;
}
} // namespace half_float
// Called by ASSERT_ARRAYS_EQ
::testing::AssertionResult assertArrayEq(std::string aName, std::string bName,
const af::array &a, const af::array &b,
float maxAbsDiff) {
af::dtype aType = a.type();
af::dtype bType = b.type();
if (aType != bType)
return ::testing::AssertionFailure()
<< "TYPE MISMATCH: \n"
<< " Actual: " << bName << "(" << b.type() << ")\n"
<< "Expected: " << aName << "(" << a.type() << ")";
af::dtype arrDtype = aType;
if (a.dims() != b.dims())
return ::testing::AssertionFailure()
<< "SIZE MISMATCH: \n"
<< " Actual: " << bName << "([" << b.dims() << "])\n"
<< "Expected: " << aName << "([" << a.dims() << "])";
switch (arrDtype) {
case f32:
return elemWiseEq<float>(aName, bName, a, b, maxAbsDiff);
break;
case c32:
return elemWiseEq<af::cfloat>(aName, bName, a, b, maxAbsDiff);
break;
case f64:
return elemWiseEq<double>(aName, bName, a, b, maxAbsDiff);
break;
case c64:
return elemWiseEq<af::cdouble>(aName, bName, a, b, maxAbsDiff);
break;
case b8: return elemWiseEq<char>(aName, bName, a, b, maxAbsDiff); break;
case s32: return elemWiseEq<int>(aName, bName, a, b, maxAbsDiff); break;
case u32:
return elemWiseEq<uint>(aName, bName, a, b, maxAbsDiff);
break;
case s8:
return elemWiseEq<schar>(aName, bName, a, b, maxAbsDiff);
break;
case u8:
return elemWiseEq<uchar>(aName, bName, a, b, maxAbsDiff);
break;
case s64:
return elemWiseEq<long long>(aName, bName, a, b, maxAbsDiff);
break;
case u64:
return elemWiseEq<unsigned long long>(aName, bName, a, b,
maxAbsDiff);
break;
case s16:
return elemWiseEq<short>(aName, bName, a, b, maxAbsDiff);
break;
case u16:
return elemWiseEq<unsigned short>(aName, bName, a, b, maxAbsDiff);
break;
case f16:
return elemWiseEq<half_float::half>(aName, bName, a, b, maxAbsDiff);
break;
default:
return ::testing::AssertionFailure()
<< "INVALID TYPE, see enum numbers: " << bName << "("
<< b.type() << ") and " << aName << "(" << a.type() << ")";
}
return ::testing::AssertionSuccess();
}
template<typename T>
::testing::AssertionResult imageEq(std::string aName, std::string bName,
const af::array &a, const af::array &b,
float maxAbsDiff) {
std::vector<T> avec(a.elements());
a.host(avec.data());
std::vector<T> bvec(b.elements());
b.host(bvec.data());
double NRMSD = computeArraysRMSD(a.elements(), avec.data(), bvec.data());
if (NRMSD < maxAbsDiff) {
return ::testing::AssertionSuccess();
} else {
std::string test_name =
::testing::UnitTest::GetInstance()->current_test_info()->name();
std::string valid_path =
std::string(TEST_RESULT_IMAGE_DIR) + test_name + "ValidImage.png";
std::string result_path =
std::string(TEST_RESULT_IMAGE_DIR) + test_name + "ResultImage.png";
std::string diff_path =
std::string(TEST_RESULT_IMAGE_DIR) + test_name + "DiffImage.png";
// af::array img = af::join(1, a, b);
// af::Window win;
// while (!win.close()) { win.image(img); }
af::saveImage(valid_path.c_str(), a.as(f32));
af::saveImage(result_path.c_str(), b.as(f32));
af::saveImage(diff_path.c_str(), abs(a.as(f32) - b.as(f32)));
std::cout << "<DartMeasurementFile type=\"image/png\" "
"name=\"ValidImage\">"
<< valid_path << "</DartMeasurementFile>\n";
std::cout
<< "<DartMeasurementFile type=\"image/png\" name=\"TestImage\">"
<< result_path << "</DartMeasurementFile>\n";
std::cout << "<DartMeasurementFile "
<< "type=\"image/png\" name=\"DifferenceImage2\">"
<< diff_path << "</DartMeasurementFile>\n";
return ::testing::AssertionFailure()
<< "RMSD Error(" << NRMSD << ") exceeds threshold(" << maxAbsDiff
<< "): " << bName << "(" << b.type() << ") and " << aName << "("
<< a.type() << ")";
}
}
// Called by ASSERT_ARRAYS_EQ
::testing::AssertionResult assertImageEq(std::string aName, std::string bName,
const af::array &a, const af::array &b,
float maxAbsDiff) {
af::dtype aType = a.type();
af::dtype bType = b.type();
if (aType != bType)
return ::testing::AssertionFailure()
<< "TYPE MISMATCH: \n"
<< " Actual: " << bName << "(" << b.type() << ")\n"
<< "Expected: " << aName << "(" << a.type() << ")";
af::dtype arrDtype = aType;
if (a.dims() != b.dims())
return ::testing::AssertionFailure()
<< "SIZE MISMATCH: \n"
<< " Actual: " << bName << "([" << b.dims() << "])\n"
<< "Expected: " << aName << "([" << a.dims() << "])";
switch (arrDtype) {
case s8: return imageEq<signed char>(aName, bName, a, b, maxAbsDiff);
case u8: return imageEq<unsigned char>(aName, bName, a, b, maxAbsDiff);
case b8: return imageEq<char>(aName, bName, a, b, maxAbsDiff);
case s32: return imageEq<int>(aName, bName, a, b, maxAbsDiff);
case u32: return imageEq<unsigned int>(aName, bName, a, b, maxAbsDiff);
case f32: return imageEq<float>(aName, bName, a, b, maxAbsDiff);
case f64: return imageEq<double>(aName, bName, a, b, maxAbsDiff);
case s16: return imageEq<short>(aName, bName, a, b, maxAbsDiff);
case u16:
return imageEq<unsigned short>(aName, bName, a, b, maxAbsDiff);
case u64:
return imageEq<unsigned long long>(aName, bName, a, b, maxAbsDiff);
case s64: return imageEq<long long>(aName, bName, a, b, maxAbsDiff);
default: throw(AF_ERR_NOT_SUPPORTED);
}
return ::testing::AssertionSuccess();
}
template<>
float convert(af::half in) {
return static_cast<float>(half_float::half(in.data_));
}
template<>
af_half convert(int in) {
half_float::half h = half_float::half(in);
af_half out;
memcpy(&out, &h, sizeof(af_half));
return out;
}
template<typename inType, typename outType, typename FileElementType>
void readTests(const std::string &FileName, std::vector<af::dim4> &inputDims,
std::vector<std::vector<inType>> &testInputs,
std::vector<std::vector<outType>> &testOutputs) {
using std::vector;
std::ifstream testFile(FileName.c_str());
if (testFile.good()) {
unsigned inputCount;
testFile >> inputCount;
inputDims.resize(inputCount);
for (unsigned i = 0; i < inputCount; i++) { testFile >> inputDims[i]; }
unsigned testCount;
testFile >> testCount;
testOutputs.resize(testCount);
vector<unsigned> testSizes(testCount);
for (unsigned i = 0; i < testCount; i++) { testFile >> testSizes[i]; }
testInputs.resize(inputCount, vector<inType>(0));
for (unsigned k = 0; k < inputCount; k++) {
dim_t nElems = inputDims[k].elements();
testInputs[k].resize(nElems);
FileElementType tmp;
for (unsigned i = 0; i < nElems; i++) {
testFile >> tmp;
testInputs[k][i] = convert<inType, FileElementType>(tmp);
}
}
testOutputs.resize(testCount, vector<outType>(0));
for (unsigned i = 0; i < testCount; i++) {
testOutputs[i].resize(testSizes[i]);
FileElementType tmp;
for (unsigned j = 0; j < testSizes[i]; j++) {
testFile >> tmp;
testOutputs[i][j] = convert<outType, FileElementType>(tmp);
}
}
} else {
FAIL() << "TEST FILE NOT FOUND";
}
}
#define INSTANTIATE(Tin, Tout, Tfile) \
template void readTests<Tin, Tout, Tfile>( \
const std::string &FileName, std::vector<af::dim4> &inputDims, \
std::vector<std::vector<Tin>> &testInputs, \
std::vector<std::vector<Tout>> &testOutputs)
INSTANTIATE(float, float, int);
INSTANTIATE(double, float, int);
INSTANTIATE(int, float, int);
INSTANTIATE(unsigned int, float, int);
INSTANTIATE(char, float, int);
INSTANTIATE(signed char, float, int);
INSTANTIATE(unsigned char, float, int);
INSTANTIATE(short, float, int);
INSTANTIATE(unsigned short, float, int);
INSTANTIATE(long long, float, int);
INSTANTIATE(unsigned long long, float, int);
INSTANTIATE(af_cfloat, af_cfloat, int);
INSTANTIATE(double, double, int);
INSTANTIATE(af_cdouble, af_cdouble, int);
INSTANTIATE(int, int, int);
INSTANTIATE(unsigned int, unsigned int, int);
INSTANTIATE(unsigned int, unsigned int, unsigned int);
INSTANTIATE(long long, long long, int);
INSTANTIATE(unsigned long long, unsigned long long, int);
INSTANTIATE(char, char, int);
INSTANTIATE(signed char, signed char, int);
INSTANTIATE(unsigned char, unsigned char, int);
INSTANTIATE(short, short, int);
INSTANTIATE(unsigned short, unsigned short, int);
INSTANTIATE(half_float::half, half_float::half, int);
INSTANTIATE(af_half, af_half, int);
INSTANTIATE(float, int, int);
INSTANTIATE(unsigned int, int, int);
INSTANTIATE(char, int, int);
INSTANTIATE(signed char, int, int);
INSTANTIATE(unsigned char, int, int);
INSTANTIATE(short, int, int);
INSTANTIATE(unsigned short, int, int);
INSTANTIATE(signed char, unsigned short, int);
INSTANTIATE(signed char, short, int);
INSTANTIATE(signed char, unsigned char, int);
INSTANTIATE(signed char, double, int);
INSTANTIATE(unsigned char, unsigned short, int);
INSTANTIATE(unsigned char, short, int);
INSTANTIATE(unsigned char, signed char, int);
INSTANTIATE(unsigned char, double, int);
INSTANTIATE(long long, unsigned int, unsigned int);
INSTANTIATE(unsigned long long, unsigned int, unsigned int);
INSTANTIATE(int, unsigned int, unsigned int);
INSTANTIATE(short, unsigned int, unsigned int);
INSTANTIATE(unsigned short, unsigned int, unsigned int);
INSTANTIATE(char, unsigned int, unsigned int);
INSTANTIATE(signed char, unsigned int, unsigned int);
INSTANTIATE(unsigned char, unsigned int, unsigned int);
INSTANTIATE(float, unsigned int, unsigned int);
INSTANTIATE(double, unsigned int, unsigned int);
INSTANTIATE(float, unsigned int, int);
INSTANTIATE(double, unsigned int, int);
INSTANTIATE(int, unsigned int, int);
INSTANTIATE(long long, unsigned int, int);
INSTANTIATE(unsigned long long, unsigned int, int);
INSTANTIATE(char, unsigned int, int);
INSTANTIATE(signed char, unsigned int, int);
INSTANTIATE(unsigned char, unsigned int, int);
INSTANTIATE(short, unsigned int, int);
INSTANTIATE(unsigned short, unsigned int, int);
INSTANTIATE(float, char, int);
INSTANTIATE(double, char, int);
INSTANTIATE(signed char, char, int);
INSTANTIATE(unsigned char, char, int);
INSTANTIATE(short, char, int);
INSTANTIATE(unsigned short, char, int);
INSTANTIATE(int, char, int);
INSTANTIATE(unsigned int, char, int);
INSTANTIATE(char, float, float);
INSTANTIATE(int, float, float);
INSTANTIATE(unsigned int, float, float);
INSTANTIATE(short, float, float);
INSTANTIATE(signed char, float, float);
INSTANTIATE(unsigned char, float, float);
INSTANTIATE(unsigned short, float, float);
INSTANTIATE(double, float, float);
INSTANTIATE(af::af_cfloat, float, float);
INSTANTIATE(af::af_cdouble, float, float);
INSTANTIATE(long long, float, float);
INSTANTIATE(long long, double, float);
INSTANTIATE(unsigned long long, double, float);
INSTANTIATE(float, float, float);
INSTANTIATE(af_cfloat, af_cfloat, float);
INSTANTIATE(af_cfloat, af_cfloat, af_cfloat);
INSTANTIATE(af_cdouble, af_cdouble, af_cdouble);
INSTANTIATE(double, double, float);
INSTANTIATE(double, double, double);
INSTANTIATE(af_cdouble, af_cdouble, float);
INSTANTIATE(int, int, float);
INSTANTIATE(unsigned int, unsigned int, float);
INSTANTIATE(long long, long long, float);
INSTANTIATE(unsigned long long, unsigned long long, float);
INSTANTIATE(char, char, float);
INSTANTIATE(signed char, signed char, float);
INSTANTIATE(unsigned char, unsigned char, float);
INSTANTIATE(short, short, float);
INSTANTIATE(unsigned short, unsigned short, float);
INSTANTIATE(half_float::half, half_float::half, float);
INSTANTIATE(half_float::half, half_float::half, double);
INSTANTIATE(af_cdouble, af_cdouble, double);
INSTANTIATE(double, af_cdouble, float);
INSTANTIATE(float, af_cfloat, float);
INSTANTIATE(half_float::half, uint, uint);
INSTANTIATE(float, float, double);
INSTANTIATE(int, float, double);
INSTANTIATE(unsigned int, float, double);
INSTANTIATE(short, float, double);
INSTANTIATE(unsigned short, float, double);
INSTANTIATE(char, float, double);
INSTANTIATE(signed char, float, double);
INSTANTIATE(unsigned char, float, double);
INSTANTIATE(long long, double, double);
INSTANTIATE(unsigned long long, double, double);
INSTANTIATE(af_cfloat, af_cfloat, double);
INSTANTIATE(half_float::half, float, double);
#undef INSTANTIATE
bool noDoubleTests(af::dtype ty) {
bool isTypeDouble = (ty == f64) || (ty == c64);
int dev = af::getDevice();
bool isDoubleSupported = af::isDoubleAvailable(dev);
return ((isTypeDouble && !isDoubleSupported) ? true : false);
}
bool noHalfTests(af::dtype ty) {
bool isTypeHalf = (ty == f16);
int dev = af::getDevice();
bool isHalfSupported = af::isHalfAvailable(dev);
return ((isTypeHalf && !isHalfSupported) ? true : false);
}
af_half abs(af_half in) {
half_float::half in_;
// casting to void* to avoid class-memaccess warnings on windows
memcpy(static_cast<void *>(&in_), &in, sizeof(af_half));
half_float::half out_ = abs(in_);
af_half out;
memcpy(&out, &out_, sizeof(af_half));
return out;
}
af_half operator-(af_half lhs, af_half rhs) {
half_float::half lhs_;
half_float::half rhs_;
// casting to void* to avoid class-memaccess warnings on windows
memcpy(static_cast<void *>(&lhs_), &lhs, sizeof(af_half));
memcpy(static_cast<void *>(&rhs_), &rhs, sizeof(af_half));
half_float::half out = lhs_ - rhs_;
af_half o;
memcpy(&o, &out, sizeof(af_half));
return o;
}
const af::cfloat &operator+(const af::cfloat &val) { return val; }
const af::cdouble &operator+(const af::cdouble &val) { return val; }
const af_half &operator+(const af_half &val) { return val; }
// Calculate a multi-dimensional coordinates' linearized index
dim_t ravelIdx(af::dim4 coords, af::dim4 strides) {
return std::inner_product(coords.get(), coords.get() + 4, strides.get(),
0LL);
}
// Calculate a linearized index's multi-dimensonal coordinates in an
// af::array,
// given its dimension sizes and strides
af::dim4 unravelIdx(dim_t idx, af::dim4 dims, af::dim4 strides) {
af::dim4 coords;
coords[3] = idx / (strides[3]);
coords[2] = idx / (strides[2]) % dims[2];
coords[1] = idx / (strides[1]) % dims[1];
coords[0] = idx % dims[0];
return coords;
}
af::dim4 unravelIdx(dim_t idx, af::array arr) {
af::dim4 dims = arr.dims();
af::dim4 st = af::getStrides(arr);
return unravelIdx(idx, dims, st);
}
af::dim4 calcStrides(const af::dim4 &parentDim) {
af::dim4 out(1, 1, 1, 1);
dim_t *out_dims = out.get();
const dim_t *parent_dims = parentDim.get();
for (dim_t i = 1; i < 4; i++) {
out_dims[i] = out_dims[i - 1] * parent_dims[i - 1];
}
return out;
}
std::string minimalDim4(af::dim4 coords, af::dim4 dims) {
std::ostringstream os;
os << "(" << coords[0];
if (dims[1] > 1 || dims[2] > 1 || dims[3] > 1) { os << ", " << coords[1]; }
if (dims[2] > 1 || dims[3] > 1) { os << ", " << coords[2]; }
if (dims[3] > 1) { os << ", " << coords[3]; }
os << ")";
return os.str();
}
// Generates a random array. testWriteToOutputArray expects that it will
// receive the same af_array that this generates after the af_* function is
// called
void genRegularArray(TestOutputArrayInfo *metadata, const unsigned ndims,
const dim_t *const dims, const af_dtype ty) {
metadata->init(ndims, dims, ty);
}
void genRegularArray(TestOutputArrayInfo *metadata, double val,
const unsigned ndims, const dim_t *const dims,
const af_dtype ty) {
metadata->init(val, ndims, dims, ty);
}
// Generates a large, random array, and extracts a subarray for the af_*
// function to use. testWriteToOutputArray expects that the large array that
// it receives is equal to the same large array with the gold array injected
// on the same subarray location
void genSubArray(TestOutputArrayInfo *metadata, const unsigned ndims,
const dim_t *const dims, const af_dtype ty) {
const dim_t pad_size = 2;
// The large array is padded on both sides of each dimension
// Padding is only applied if the dimension is used, i.e. if dims[i] > 1
dim_t full_arr_dims[4] = {dims[0], dims[1], dims[2], dims[3]};
for (uint i = 0; i < ndims; ++i) {
full_arr_dims[i] = dims[i] + 2 * pad_size;
}
// Calculate index of sub-array. These will be used also by
// testWriteToOutputArray so that the gold sub array will be placed in
// the same location. Currently, this location is the center of the
// large array
af_seq subarr_idxs[4] = {af_span, af_span, af_span, af_span};
for (uint i = 0; i < ndims; ++i) {
af_seq idx = {pad_size, pad_size + dims[i] - 1.0, 1.0};
subarr_idxs[i] = idx;
}
metadata->init(ndims, full_arr_dims, ty, &subarr_idxs[0]);
}
void genSubArray(TestOutputArrayInfo *metadata, double val,
const unsigned ndims, const dim_t *const dims,
const af_dtype ty) {
const dim_t pad_size = 2;
// The large array is padded on both sides of each dimension
// Padding is only applied if the dimension is used, i.e. if dims[i] > 1
dim_t full_arr_dims[4] = {dims[0], dims[1], dims[2], dims[3]};
for (uint i = 0; i < ndims; ++i) {
full_arr_dims[i] = dims[i] + 2 * pad_size;
}
// Calculate index of sub-array. These will be used also by
// testWriteToOutputArray so that the gold sub array will be placed in
// the same location. Currently, this location is the center of the
// large array
af_seq subarr_idxs[4] = {af_span, af_span, af_span, af_span};
for (uint i = 0; i < ndims; ++i) {
af_seq idx = {pad_size, pad_size + dims[i] - 1.0, 1.0};
subarr_idxs[i] = idx;
}
metadata->init(val, ndims, full_arr_dims, ty, &subarr_idxs[0]);
}
// Generates a reordered array. testWriteToOutputArray expects that this
// array will still have the correct output values from the af_* function,
// even though the array was initially reordered.
void genReorderedArray(TestOutputArrayInfo *metadata, const unsigned ndims,
const dim_t *const dims, const af_dtype ty) {
// The rest of this function assumes that dims has 4 elements. Just in
// case dims has < 4 elements, use another dims array that is filled
// with 1s
dim_t all_dims[4] = {1, 1, 1, 1};
for (uint i = 0; i < ndims; ++i) { all_dims[i] = dims[i]; }
// This reorder combination will not move data around, but will simply
// call modDims and modStrides (see src/api/c/reorder.cpp).
// The output will be checked if it is still correct even with the
// modified dims and strides "hack" with no data movement
uint reorder_idxs[4] = {0, 2, 1, 3};
// Shape the output array such that the reordered output array will have
// the correct dimensions that the test asks for (i.e. must match dims
// arg)
dim_t init_dims[4] = {all_dims[0], all_dims[1], all_dims[2], all_dims[3]};
for (uint i = 0; i < 4; ++i) { init_dims[i] = all_dims[reorder_idxs[i]]; }
metadata->init(4, init_dims, ty);
af_array reordered = 0;
ASSERT_SUCCESS(af_reorder(&reordered, metadata->getOutput(),
reorder_idxs[0], reorder_idxs[1], reorder_idxs[2],
reorder_idxs[3]));
metadata->setOutput(reordered);
}
void genReorderedArray(TestOutputArrayInfo *metadata, double val,
const unsigned ndims, const dim_t *const dims,
const af_dtype ty) {
// The rest of this function assumes that dims has 4 elements. Just in
// case dims has < 4 elements, use another dims array that is filled
// with 1s
dim_t all_dims[4] = {1, 1, 1, 1};
for (uint i = 0; i < ndims; ++i) { all_dims[i] = dims[i]; }
// This reorder combination will not move data around, but will simply
// call modDims and modStrides (see src/api/c/reorder.cpp).
// The output will be checked if it is still correct even with the
// modified dims and strides "hack" with no data movement
uint reorder_idxs[4] = {0, 2, 1, 3};
// Shape the output array such that the reordered output array will have
// the correct dimensions that the test asks for (i.e. must match dims
// arg)
dim_t init_dims[4] = {all_dims[0], all_dims[1], all_dims[2], all_dims[3]};
for (uint i = 0; i < 4; ++i) { init_dims[i] = all_dims[reorder_idxs[i]]; }
metadata->init(val, 4, init_dims, ty);
af_array reordered = 0;
ASSERT_SUCCESS(af_reorder(&reordered, metadata->getOutput(),
reorder_idxs[0], reorder_idxs[1], reorder_idxs[2],
reorder_idxs[3]));
metadata->setOutput(reordered);
}
// Partner function of testWriteToOutputArray. This generates the "special"
// array that testWriteToOutputArray will use to check if the af_* function
// correctly uses an existing array as its output
void genTestOutputArray(af_array *out_ptr, const unsigned ndims,
const dim_t *const dims, const af_dtype ty,
TestOutputArrayInfo *metadata) {
switch (metadata->getOutputArrayType()) {
case FULL_ARRAY: genRegularArray(metadata, ndims, dims, ty); break;
case SUB_ARRAY: genSubArray(metadata, ndims, dims, ty); break;
case REORDERED_ARRAY:
genReorderedArray(metadata, ndims, dims, ty);
break;
default: break;
}
*out_ptr = metadata->getOutput();
}
void genTestOutputArray(af_array *out_ptr, double val, const unsigned ndims,
const dim_t *const dims, const af_dtype ty,
TestOutputArrayInfo *metadata) {
switch (metadata->getOutputArrayType()) {
case FULL_ARRAY: genRegularArray(metadata, val, ndims, dims, ty); break;
case SUB_ARRAY: genSubArray(metadata, val, ndims, dims, ty); break;
case REORDERED_ARRAY:
genReorderedArray(metadata, val, ndims, dims, ty);
break;
default: break;
}
*out_ptr = metadata->getOutput();
}
// Partner function of genTestOutputArray. This uses the same "special"
// array that genTestOutputArray generates, and checks whether the
// af_* function wrote to that array correctly
::testing::AssertionResult testWriteToOutputArray(
std::string gold_name, std::string result_name, const af_array gold,
const af_array out, TestOutputArrayInfo *metadata) {
// In the case of NULL_ARRAY, the output array starts out as null.
// After the af_* function is called, it shouldn't be null anymore
if (metadata->getOutputArrayType() == NULL_ARRAY) {
if (out == 0) {
return ::testing::AssertionFailure()
<< "Output af_array " << result_name << " is null";
}
metadata->setOutput(out);
}
// For every other case, must check if the af_array generated by
// genTestOutputArray was used by the af_* function as its output array
else {
if (metadata->getOutput() != out) {
return ::testing::AssertionFailure()
<< "af_array POINTER MISMATCH:\n"
<< " Actual: " << out << "\n"
<< "Expected: " << metadata->getOutput();
}
}
if (metadata->getOutputArrayType() == SUB_ARRAY) {
// There are two full arrays. One will be injected with the gold
// subarray, the other should have already been injected with the
// af_* function's output. Then we compare the two full arrays
af_array gold_full_array = metadata->getFullOutputCopy();
af_assign_seq(&gold_full_array, gold_full_array,
metadata->getSubArrayNumDims(),
metadata->getSubArrayIdxs(), gold);
return assertArrayEq(gold_name, result_name,
metadata->getFullOutputCopy(),
metadata->getFullOutput());
} else {
return assertArrayEq(gold_name, result_name, gold, out);
}
}
// Called by ASSERT_SPECIAL_ARRAYS_EQ
::testing::AssertionResult assertArrayEq(std::string aName, std::string bName,
std::string metadataName,
const af_array a, const af_array b,
TestOutputArrayInfo *metadata) {
UNUSED(metadataName);
return testWriteToOutputArray(aName, bName, a, b, metadata);
}
// To support C API
::testing::AssertionResult assertArrayEq(std::string aName, std::string bName,
const af_array a, const af_array b) {
af_array aa = 0, bb = 0;
af_retain_array(&aa, a);
af_retain_array(&bb, b);
af::array aaa(aa);
af::array bbb(bb);
return assertArrayEq(aName, bName, aaa, bbb, 0.0f);
}
// Called by ASSERT_ARRAYS_NEAR
::testing::AssertionResult assertArrayNear(std::string aName, std::string bName,
std::string maxAbsDiffName,
const af::array &a,
const af::array &b,
float maxAbsDiff) {
UNUSED(maxAbsDiffName);
return assertArrayEq(aName, bName, a, b, maxAbsDiff);
}
// Called by ASSERT_IMAGES_NEAR
::testing::AssertionResult assertImageNear(std::string aName, std::string bName,
std::string maxAbsDiffName,
const af_array &a, const af_array &b,
float maxAbsDiff) {
UNUSED(maxAbsDiffName);
af_array aa = 0, bb = 0;
af_retain_array(&aa, a);
af_retain_array(&bb, b);
af::array aaa(aa);
af::array bbb(bb);
return assertImageEq(aName, bName, aaa, bbb, maxAbsDiff);
}
// Called by ASSERT_IMAGES_NEAR
::testing::AssertionResult assertImageNear(std::string aName, std::string bName,
std::string maxAbsDiffName,
const af::array &a,
const af::array &b,
float maxAbsDiff) {
UNUSED(maxAbsDiffName);
return assertImageEq(aName, bName, a, b, maxAbsDiff);
}
// To support C API
::testing::AssertionResult assertArrayNear(std::string aName, std::string bName,
std::string maxAbsDiffName,
const af_array a, const af_array b,
float maxAbsDiff) {
af_array aa = 0, bb = 0;
af_retain_array(&aa, a);
af_retain_array(&bb, b);
af::array aaa(aa);
af::array bbb(bb);
return assertArrayNear(aName, bName, maxAbsDiffName, aaa, bbb, maxAbsDiff);
}
void cleanSlate() {
const size_t step_bytes = 1024;
size_t alloc_bytes, alloc_buffers;
size_t lock_bytes, lock_buffers;
af::deviceGC();
af::deviceMemInfo(&alloc_bytes, &alloc_buffers, &lock_bytes, &lock_buffers);
ASSERT_EQ(0u, alloc_buffers);
ASSERT_EQ(0u, lock_buffers);
ASSERT_EQ(0u, alloc_bytes);
ASSERT_EQ(0u, lock_bytes);
af::setMemStepSize(step_bytes);
ASSERT_EQ(af::getMemStepSize(), step_bytes);
}
template<typename inType, typename outType>
void readTestsFromFile(const std::string &FileName,
std::vector<af::dim4> &inputDims,
std::vector<std::vector<inType>> &testInputs,
std::vector<std::vector<outType>> &testOutputs) {
using std::vector;
std::ifstream testFile(FileName.c_str());
if (testFile.good()) {
unsigned inputCount;
testFile >> inputCount;
for (unsigned i = 0; i < inputCount; i++) {
af::dim4 temp(1);
testFile >> temp;
inputDims.push_back(temp);
}
unsigned testCount;
testFile >> testCount;
testOutputs.resize(testCount);
vector<unsigned> testSizes(testCount);
for (unsigned i = 0; i < testCount; i++) { testFile >> testSizes[i]; }
testInputs.resize(inputCount, vector<inType>(0));
for (unsigned k = 0; k < inputCount; k++) {
dim_t nElems = inputDims[k].elements();
testInputs[k].resize(nElems);
inType tmp;
for (unsigned i = 0; i < nElems; i++) {
testFile >> tmp;
testInputs[k][i] = tmp;
}
}
testOutputs.resize(testCount, vector<outType>(0));
for (unsigned i = 0; i < testCount; i++) {
testOutputs[i].resize(testSizes[i]);
outType tmp;
for (unsigned j = 0; j < testSizes[i]; j++) {
testFile >> tmp;
testOutputs[i][j] = tmp;
}
}
} else {
FAIL() << "TEST FILE NOT FOUND";
}
}
#define INSTANTIATE(Ti, To) \
template void readTestsFromFile<Ti, To>( \
const std::string &FileName, std::vector<af::dim4> &inputDims, \
std::vector<std::vector<Ti>> &testInputs, \
std::vector<std::vector<To>> &testOutputs)
INSTANTIATE(float, float);
INSTANTIATE(float, af_cfloat);
INSTANTIATE(af_cfloat, af_cfloat);
INSTANTIATE(double, double);
INSTANTIATE(double, af_cdouble);
INSTANTIATE(af_cdouble, af_cdouble);
INSTANTIATE(int, float);
#undef INSTANTIATE
template<typename outType>
void readImageTests(const std::string &pFileName,
std::vector<af::dim4> &pInputDims,
std::vector<std::string> &pTestInputs,
std::vector<std::vector<outType>> &pTestOutputs) {
using std::vector;
std::ifstream testFile(pFileName.c_str());
if (testFile.good()) {
unsigned inputCount;
testFile >> inputCount;
for (unsigned i = 0; i < inputCount; i++) {
af::dim4 temp(1);
testFile >> temp;
pInputDims.push_back(temp);
}
unsigned testCount;
testFile >> testCount;
pTestOutputs.resize(testCount);
vector<unsigned> testSizes(testCount);
for (unsigned i = 0; i < testCount; i++) { testFile >> testSizes[i]; }
pTestInputs.resize(inputCount, "");
for (unsigned k = 0; k < inputCount; k++) {
pTestInputs[k] = readNextNonEmptyLine(testFile);
}
pTestOutputs.resize(testCount, vector<outType>(0));
for (unsigned i = 0; i < testCount; i++) {
pTestOutputs[i].resize(testSizes[i]);
outType tmp;
for (unsigned j = 0; j < testSizes[i]; j++) {
testFile >> tmp;
pTestOutputs[i][j] = tmp;
}
}
} else {
FAIL() << "TEST FILE NOT FOUND";
}
}
#define INSTANTIATE(To) \
template void readImageTests<To>( \
const std::string &pFileName, std::vector<af::dim4> &pInputDims, \
std::vector<std::string> &pTestInputs, \
std::vector<std::vector<To>> &pTestOutputs)
INSTANTIATE(float);
#undef INSTANTIATE
void readImageTests(const std::string &pFileName,
std::vector<af::dim4> &pInputDims,
std::vector<std::string> &pTestInputs,
std::vector<dim_t> &pTestOutSizes,
std::vector<std::string> &pTestOutputs) {
using std::vector;
std::ifstream testFile(pFileName.c_str());
if (testFile.good()) {
unsigned inputCount;
testFile >> inputCount;
for (unsigned i = 0; i < inputCount; i++) {
af::dim4 temp(1);
testFile >> temp;
pInputDims.push_back(temp);
}
unsigned testCount;
testFile >> testCount;
pTestOutputs.resize(testCount);
pTestOutSizes.resize(testCount);
for (unsigned i = 0; i < testCount; i++) {
testFile >> pTestOutSizes[i];
}