forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xblockwise_reducer.cpp
314 lines (275 loc) · 11.5 KB
/
test_xblockwise_reducer.cpp
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
#include "test_common.hpp"
#include "xtensor/xblockwise_reducer.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xnorm.hpp"
#include <tuple>
#include <sstream>
#include <string>
#include <typeinfo>
#include <vector>
#define XTENSOR_REDUCER_TESTER(FNAME)\
struct FNAME ## _tester\
{ \
template<class T=void, class ... ARGS>\
static auto op(ARGS &&... args)\
{\
return xt::blockwise:: FNAME <T>(std::forward<ARGS>(args) ...);\
}\
template<class T=void, class ... ARGS>\
static auto should_op(ARGS &&... args)\
{\
return xt:: FNAME <T>(std::forward<ARGS>(args) ...);\
}\
};\
TYPE_TO_STRING(FNAME ## _tester);\
TYPE_TO_STRING(std::tuple<FNAME##_tester, std::tuple<xt::keep_dims_type>>);\
TYPE_TO_STRING(std::tuple<FNAME##_tester, std::tuple<xt::evaluation_strategy::immediate_type>>)
XTENSOR_REDUCER_TESTER(sum);
XTENSOR_REDUCER_TESTER(prod);
XTENSOR_REDUCER_TESTER(amin);
XTENSOR_REDUCER_TESTER(amax);
XTENSOR_REDUCER_TESTER(mean);
XTENSOR_REDUCER_TESTER(variance);
XTENSOR_REDUCER_TESTER(stddev);
#undef XTENSOR_REDUCER_TESTER
#define XTENSOR_NORM_REDUCER_TESTER(FNAME)\
struct FNAME ## _tester\
{ \
template<class T=void, class ... ARGS>\
static auto op(ARGS &&... args)\
{\
return xt::blockwise:: FNAME (std::forward<ARGS>(args) ...);\
}\
template<class T=void, class ... ARGS>\
static auto should_op(ARGS &&... args)\
{\
return xt:: FNAME (std::forward<ARGS>(args) ...);\
}\
};\
TYPE_TO_STRING(FNAME##_tester);\
TYPE_TO_STRING(std::tuple<FNAME##_tester, std::tuple<xt::keep_dims_type>>);\
TYPE_TO_STRING(std::tuple<FNAME##_tester, std::tuple<xt::evaluation_strategy::immediate_type>>)
XTENSOR_NORM_REDUCER_TESTER(norm_l0);
XTENSOR_NORM_REDUCER_TESTER(norm_l1);
XTENSOR_NORM_REDUCER_TESTER(norm_l2);
XTENSOR_NORM_REDUCER_TESTER(norm_sq);
XTENSOR_NORM_REDUCER_TESTER(norm_linf);
XTENSOR_NORM_REDUCER_TESTER(norm_lp_to_p);
XTENSOR_NORM_REDUCER_TESTER(norm_lp);
#undef XTENSOR_NORM_REDUCER_TESTER
namespace xt
{
using test_values_test_types = std::tuple<
std::tuple<sum_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<sum_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<prod_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<prod_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<amin_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<amin_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<amax_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<amax_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<mean_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<mean_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<variance_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<variance_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<stddev_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<stddev_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<norm_l0_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<norm_l0_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<norm_l1_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<norm_l1_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<norm_l2_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<norm_l2_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<norm_sq_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<norm_sq_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<norm_linf_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<norm_linf_tester, std::tuple<xt::evaluation_strategy::immediate_type>>
>;
using test_p_norm_values_test_types = std::tuple<
std::tuple<norm_lp_to_p_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<norm_lp_to_p_tester, std::tuple<xt::evaluation_strategy::immediate_type>>,
std::tuple<norm_lp_tester, std::tuple<xt::keep_dims_type>>,
std::tuple<norm_lp_tester, std::tuple<xt::evaluation_strategy::immediate_type>>
>;
TEST_SUITE("xblockwise_reducer")
{
TEST_CASE_TEMPLATE_DEFINE("test_values", TesterTuple, test_values_id)
{
using tester_type = std::tuple_element_t<0, TesterTuple>;
using options_type = std::tuple_element_t<1, TesterTuple>;
dynamic_shape<std::size_t> shape({21,10,5});
dynamic_shape<std::size_t> chunk_shape({5,4,2});
xarray<int> input_exp(shape);
// just iota is a bit boring since it will
// lead to an uniform variance
std::iota(input_exp.begin(), input_exp.end(), -5);
for(std::size_t i=0; i<input_exp.size(); ++i)
{
if(i % 2)
{
input_exp.flat(i) += 10;
}
}
std::vector<dynamic_shape<std::size_t>> axes_vec = {
dynamic_shape<std::size_t>({0}),
dynamic_shape<std::size_t>({1}),
dynamic_shape<std::size_t>({2}),
dynamic_shape<std::size_t>({0, 1}),
dynamic_shape<std::size_t>({0, 2}),
dynamic_shape<std::size_t>({0, 1, 2})
};
for(const auto & axes : axes_vec)
{
SUBCASE((std::string("axes = ") + stringify(axes)).c_str())
{
auto reducer = tester_type::op(input_exp, chunk_shape, axes, options_type{});
auto should_reducer = tester_type::should_op(input_exp, axes, options_type{});
using should_result_value_type = typename std::decay_t<decltype(should_reducer)>::value_type;
using result_value_type = typename std::decay_t<decltype(reducer)>::value_type;
SUBCASE("result_value_type")
{
CHECK_UNARY(std::is_same<result_value_type, should_result_value_type>::value);
}
SUBCASE("shape")
{
CHECK_EQ(reducer.dimension(), should_reducer.dimension());
CHECK_EQ(reducer.shape(), should_reducer.shape());
}
SUBCASE("assign")
{
auto result = xarray<result_value_type>::from_shape(reducer.shape());
reducer.assign_to(result);
auto should_result = xt::eval(should_reducer);
if(std::is_same<tester_type, variance_tester>::value || std::is_same<tester_type, stddev_tester>::value)
{
CHECK_UNARY(xt::allclose(result, should_result));
}
else
{
CHECK_EQ(result, should_result);
}
}
}
}
}
TEST_CASE_TEMPLATE_APPLY(test_values_id, test_values_test_types);
TEST_CASE_TEMPLATE_DEFINE("test_p_norm_values", TesterTuple, test_p_norm_values_id)
{
using tester_type = std::tuple_element_t<0, TesterTuple>;
using options_type = std::tuple_element_t<1, TesterTuple>;
dynamic_shape<std::size_t> shape({21,10,5});
dynamic_shape<std::size_t> chunk_shape({5,4,2});
xarray<int> input_exp(shape);
// just iota is a bit boring since it will
// lead to an uniform variance
std::iota(input_exp.begin(), input_exp.end(), -5);
for(std::size_t i=0; i<input_exp.size(); ++i)
{
if(i % 2)
{
input_exp.flat(i) += 10;
}
}
std::vector<dynamic_shape<std::size_t>> axes_vec = {
dynamic_shape<std::size_t>({0}),
dynamic_shape<std::size_t>({1}),
dynamic_shape<std::size_t>({2}),
dynamic_shape<std::size_t>({0, 1}),
dynamic_shape<std::size_t>({0, 2}),
dynamic_shape<std::size_t>({0, 1, 2})
};
for(const auto & axes : axes_vec)
{
SUBCASE((std::string("axes = ") + stringify(axes)).c_str())
{
auto reducer = tester_type::op(input_exp, chunk_shape, 2.0, axes, options_type{});
auto should_reducer = tester_type::should_op(input_exp, 2.0, axes, options_type{});
auto should_result = xt::eval(should_reducer);
using should_result_value_type = typename std::decay_t<decltype(should_reducer)>::value_type;
using result_value_type = typename std::decay_t<decltype(reducer)>::value_type;
SUBCASE("result_value_type")
{
CHECK_UNARY(std::is_same<result_value_type, should_result_value_type>::value);
}
SUBCASE("shape")
{
CHECK_EQ(reducer.dimension(), should_reducer.dimension());
CHECK_EQ(reducer.shape(), should_reducer.shape());
}
SUBCASE("assign")
{
auto result = xarray<result_value_type>::from_shape(reducer.shape());
reducer.assign_to(result);
CHECK_UNARY(xt::allclose(result, should_result));
}
}
}
}
TEST_CASE_TEMPLATE_APPLY(test_p_norm_values_id, test_p_norm_values_test_types);
TEST_CASE("test_api")
{
SUBCASE("sum")
{
dynamic_shape<std::size_t> shape({21,10,5});
dynamic_shape<std::size_t> chunk_shape({5,4,2});
xarray<int> input_exp(shape);
SUBCASE("integral_axis")
{
xt::blockwise::sum(input_exp, chunk_shape, 1);
}
SUBCASE("initalizer_list_axis")
{
xt::blockwise::sum(input_exp, chunk_shape, {1});
}
SUBCASE("array")
{
const int axes[2] = {0, 1};
xt::blockwise::sum(input_exp, chunk_shape, axes);
}
SUBCASE("no options")
{
xt::blockwise::sum(input_exp, chunk_shape);
}
SUBCASE("templated")
{
SUBCASE("integral_axis")
{
xt::blockwise::sum<double>(input_exp, chunk_shape, 1);
}
SUBCASE("initalizer_list_axis")
{
xt::blockwise::sum<double>(input_exp, chunk_shape, {1});
}
SUBCASE("array")
{
const int axes[2] = {0, 1};
xt::blockwise::sum<double>(input_exp, chunk_shape, axes);
}
SUBCASE("no options")
{
xt::blockwise::sum<double>(input_exp, chunk_shape);
}
}
}
SUBCASE("l2_norm")
{
dynamic_shape<std::size_t> shape({21,10,5});
dynamic_shape<std::size_t> chunk_shape({5,4,2});
xarray<int> input_exp(shape);
SUBCASE("integral_axis")
{
xt::blockwise::norm_l2(input_exp, chunk_shape, 1);
}
SUBCASE("initalizer_list_axis")
{
xt::blockwise::norm_l2(input_exp, chunk_shape, {1});
}
SUBCASE("array")
{
const int axes[2] = {0, 1};
xt::blockwise::norm_l2(input_exp, chunk_shape, axes);
}
}
}
}
}