forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmean.cpp
More file actions
380 lines (291 loc) · 10.4 KB
/
mean.cpp
File metadata and controls
380 lines (291 loc) · 10.4 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
/*******************************************************
* Copyright (c) 2014, 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
********************************************************/
#include <arrayfire.h>
#include <gtest/gtest.h>
#include <half.hpp>
#include <testHelpers.hpp>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <algorithm>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
using af::array;
using af::cdouble;
using af::cfloat;
using af::constant;
using af::dim4;
using af::randu;
using half_float::half;
using std::endl;
using std::string;
using std::vector;
template<typename T>
class Mean : public ::testing::Test {
public:
virtual void SetUp() {}
};
// create a list of types to be tested
// This list does not allow to cleanly add the af_half/half_float type : at the
// moment half tested in some special unittests
typedef ::testing::Types<cdouble, cfloat, float, double, int, uint, intl, uintl,
char, uchar, short, ushort, half_float::half>
TestTypes;
// register the type list
TYPED_TEST_CASE(Mean, TestTypes);
template<typename T>
struct f32HelperType {
typedef
typename cond_type<is_same_type<T, double>::value, double, float>::type
type;
};
template<typename T>
struct c32HelperType {
typedef typename cond_type<is_same_type<T, cfloat>::value, cfloat,
typename f32HelperType<T>::type>::type type;
};
template<typename T>
struct elseType {
typedef typename cond_type<is_same_type<T, uintl>::value ||
is_same_type<T, intl>::value,
double, T>::type type;
};
template<typename T>
struct meanOutType {
typedef typename cond_type<
is_same_type<T, float>::value || is_same_type<T, int>::value ||
is_same_type<T, uint>::value || is_same_type<T, uchar>::value ||
is_same_type<T, short>::value || is_same_type<T, ushort>::value ||
is_same_type<T, char>::value,
float, typename elseType<T>::type>::type type;
};
template<typename T>
void meanDimTest(string pFileName, dim_t dim, bool isWeighted = false) {
typedef typename meanOutType<T>::type outType;
SUPPORTED_TYPE_CHECK(T);
SUPPORTED_TYPE_CHECK(outType);
double tol = 1.0e-3;
if ((af_dtype)af::dtype_traits<T>::af_type == f16) tol = 4.e-3;
vector<dim4> numDims;
vector<vector<int> > in;
vector<vector<float> > tests;
readTestsFromFile<int, float>(pFileName, numDims, in, tests);
dim4 goldDims = numDims[0];
goldDims[dim] = 1;
if (!isWeighted) {
dim4 dims = numDims[0];
vector<T> input(in[0].begin(), in[0].end());
array inArray(dims, &(input.front()));
array outArray = mean(inArray, dim);
vector<outType> outData(dims.elements());
outArray.host((void*)outData.data());
vector<outType> currGoldBar(tests[0].begin(), tests[0].end());
dim4 goldDims = dims;
goldDims[dim] = 1;
ASSERT_VEC_ARRAY_NEAR(currGoldBar, goldDims, outArray, tol);
} else {
dim4 dims = numDims[0];
dim4 wdims = numDims[1];
vector<T> input(in[0].begin(), in[0].end());
vector<float> weights(in[1].size());
transform(in[1].begin(), in[1].end(), weights.begin(),
convert_to<float, int>);
array inArray(dims, &(input.front()));
array wtsArray(wdims, &(weights.front()));
array outArray = mean(inArray, wtsArray, dim);
vector<outType> outData(dims.elements());
outArray.host((void*)outData.data());
vector<outType> currGoldBar(tests[0].begin(), tests[0].end());
ASSERT_VEC_ARRAY_NEAR(currGoldBar, goldDims, outArray, tol);
}
}
TYPED_TEST(Mean, Dim0Matrix) {
meanDimTest<TypeParam>(string(TEST_DIR "/mean/mean_dim0_matrix.test"), 0);
}
TYPED_TEST(Mean, Dim1Cube) {
meanDimTest<TypeParam>(string(TEST_DIR "/mean/mean_dim1_cube.test"), 1);
}
TYPED_TEST(Mean, Dim0HyperCube) {
meanDimTest<TypeParam>(string(TEST_DIR "/mean/mean_dim0_hypercube.test"),
0);
}
TYPED_TEST(Mean, Dim2Matrix) {
meanDimTest<TypeParam>(string(TEST_DIR "/mean/mean_dim2_matrix.test"), 2);
}
TYPED_TEST(Mean, Dim2Cube) {
meanDimTest<TypeParam>(string(TEST_DIR "/mean/mean_dim2_cube.test"), 2);
}
TYPED_TEST(Mean, Dim2HyperCube) {
meanDimTest<TypeParam>(string(TEST_DIR "/mean/mean_dim2_hypercube.test"),
2);
}
TYPED_TEST(Mean, Wtd_Dim0Matrix) {
meanDimTest<TypeParam>(string(TEST_DIR "/mean/wtd_mean_dim0_mat.test"), 0,
true);
}
TYPED_TEST(Mean, Wtd_Dim1Matrix) {
meanDimTest<TypeParam>(string(TEST_DIR "/mean/wtd_mean_dim1_mat.test"), 1,
true);
}
template<typename T>
void meanAllTest(T const_value, dim4 dims) {
typedef typename meanOutType<T>::type outType;
SUPPORTED_TYPE_CHECK(T);
SUPPORTED_TYPE_CHECK(outType);
using af::array;
using af::mean;
vector<T> hundred(dims.elements(), const_value);
outType gold = outType(0);
// for(auto i:hundred) gold += i;
for (int i = 0; i < (int)hundred.size(); i++) { gold = gold + hundred[i]; }
gold = gold / dims.elements();
array a(dims, &(hundred.front()));
outType output = mean<outType>(a);
ASSERT_NEAR(::real(output), ::real(gold), 1.0e-3);
ASSERT_NEAR(::imag(output), ::imag(gold), 1.0e-3);
}
template<>
void meanAllTest(half_float::half const_value, dim4 dims) {
SUPPORTED_TYPE_CHECK(half_float::half);
using af::array;
using af::mean;
vector<float> hundred(dims.elements(), const_value);
float gold = float(0);
for (int i = 0; i < (int)hundred.size(); i++) { gold = gold + hundred[i]; }
gold = gold / dims.elements();
array a = array(dims, &(hundred.front())).as(f16);
half output = mean<half>(a);
af_half output2 = mean<af_half>(a);
// make sure output2 and output are binary equals. This is necessary
// because af_half is not a complete type
half output2_copy;
memcpy(static_cast<void*>(&output2_copy), &output2, sizeof(af_half));
ASSERT_EQ(output, output2_copy);
ASSERT_NEAR(output, gold, 1.0e-3);
}
TEST(MeanAll, f64) { meanAllTest<double>(2.1, dim4(10, 10, 1, 1)); }
TEST(MeanAll, f32) { meanAllTest<float>(2.1f, dim4(10, 5, 2, 1)); }
TEST(MeanAll, f16) { meanAllTest<half>((half)0.3f, dim4(10, 5, 2, 1)); }
TEST(MeanAll, s32) { meanAllTest<int>(2, dim4(5, 5, 2, 2)); }
TEST(MeanAll, u32) { meanAllTest<unsigned>(2, dim4(100, 1, 1, 1)); }
TEST(MeanAll, s8) { meanAllTest<char>(2, dim4(5, 5, 2, 2)); }
TEST(MeanAll, u8) { meanAllTest<uchar>(2, dim4(100, 1, 1, 1)); }
TEST(MeanAll, c32) { meanAllTest<cfloat>(cfloat(2.1f), dim4(10, 5, 2, 1)); }
TEST(MeanAll, s16) { meanAllTest<short>(2, dim4(5, 5, 2, 2)); }
TEST(MeanAll, u16) { meanAllTest<ushort>(2, dim4(100, 1, 1, 1)); }
TEST(MeanAll, c64) { meanAllTest<cdouble>(cdouble(2.1), dim4(10, 10, 1, 1)); }
template<typename T>
T random() {
return T(std::rand() % 10);
}
template<>
half random<half>() {
// create values from -0.5 to 0.5 to ensure sum does not deviate
// too far out of half's useful range
float r = static_cast<float>(rand()) / static_cast<float>(RAND_MAX) - 0.5f;
return half(r);
}
template<>
cfloat random<cfloat>() {
return cfloat(float(std::rand() % 10), float(std::rand() % 10));
}
template<>
cdouble random<cdouble>() {
return cdouble(double(std::rand() % 10), double(std::rand() % 10));
}
template<typename T>
class WeightedMean : public ::testing::Test {
public:
virtual void SetUp() {}
};
// register the type list
TYPED_TEST_CASE(WeightedMean, TestTypes);
template<typename T, typename wtsType>
void weightedMeanAllTest(dim4 dims) {
typedef typename meanOutType<T>::type outType;
SUPPORTED_TYPE_CHECK(T);
SUPPORTED_TYPE_CHECK(outType);
SUPPORTED_TYPE_CHECK(wtsType);
using af::array;
using af::mean;
std::srand(std::time(0));
vector<T> data(dims.elements());
vector<wtsType> wts(dims.elements());
std::generate(data.begin(), data.end(), random<T>);
std::generate(wts.begin(), wts.end(), random<wtsType>);
outType wtdSum = outType(0);
wtsType wtsSum = wtsType(0);
for (int i = 0; i < (int)data.size(); i++) {
wtdSum = wtdSum + data[i] * wts[i];
wtsSum = wtsSum + wts[i];
}
outType gold = wtdSum / outType(wtsSum);
array a(dims, &(data.front()));
array w(dims, &(wts.front()));
outType output = mean<outType>(a, w);
ASSERT_NEAR(::real(output), ::real(gold), 1.0e-2);
ASSERT_NEAR(::imag(output), ::imag(gold), 1.0e-2);
}
TYPED_TEST(WeightedMean, Basic) {
weightedMeanAllTest<TypeParam, float>(dim4(32, 30, 33, 17));
}
TEST(WeightedMean, Broadacst) {
float val = 0.5f;
array a = randu(4096, 32);
array w = constant(val, a.dims());
array c = mean(a);
array d = mean(a, w);
vector<float> hc(c.elements());
vector<float> hd(d.elements());
c.host(hc.data());
d.host(hd.data());
for (size_t i = 0; i < hc.size(); i++) {
// C and D are the same because they are normalized by the sum of the
// weights.
ASSERT_NEAR(hc[i], hd[i], 1E-5);
}
}
TEST(Mean, Issue2093) {
const int NELEMS = 512;
array data = randu(1, NELEMS);
array wts = constant(1.0f, 1, NELEMS);
vector<float> hdata(NELEMS);
data.host(hdata.data());
array out = mean(data, wts, 1);
float outVal;
out.host(&outVal);
float expected = 0.0;
for (size_t i = 0; i < NELEMS; ++i) expected += hdata[i];
expected /= NELEMS;
ASSERT_NEAR(outVal, expected, 0.001);
}
TEST(MeanAll, SubArray) {
// Fixes Issue 2636
using af::mean;
using af::span;
using af::sum;
const dim4 inDims(10, 10, 10, 10);
array in = randu(inDims);
array sub = in(0, span, span, span);
size_t nElems = sub.elements();
ASSERT_FLOAT_EQ(mean<float>(sub), sum<float>(sub) / nElems);
}
TEST(MeanHalf, dim0) {
SUPPORTED_TYPE_CHECK(half_float::half);
// Keeping N low to be able to run on 6GB GPUs
int N = 1024;
const dim4 inDims(N, N, 1, 1);
array in = randu(inDims, f16);
array m16 = af::mean(in, 0);
array m32 = af::mean(in.as(f32), 0);
// Some diffs appears at 0.0001 max diff : example: float: 0.507014 vs half:
// 0.506836
ASSERT_ARRAYS_NEAR(m16.as(f32), m32, 0.001f);
}