forked from secretflow/spu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape_ops_test.cc
More file actions
440 lines (354 loc) · 14 KB
/
shape_ops_test.cc
File metadata and controls
440 lines (354 loc) · 14 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
// Copyright 2021 Ant Group Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "libspu/kernel/hal/shape_ops.h"
#include "gtest/gtest.h"
#include "xtensor/xbroadcast.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xshape.hpp"
#include "libspu/kernel/test_util.h"
namespace spu::kernel::hal {
using secret_v = std::integral_constant<Visibility, VIS_SECRET>;
using public_v = std::integral_constant<Visibility, VIS_PUBLIC>;
using ShapeOpsUnaryTestTypes = ::testing::Types<
// s
std::tuple<float, secret_v, float>, // (sfxp)
std::tuple<int32_t, secret_v, int64_t>, // (sint)
// p
std::tuple<float, public_v, float>, // (pfxp)
std::tuple<int32_t, public_v, int64_t> // (pint)
>;
template <typename S>
class ShapeOpsUnaryTest : public ::testing::Test {};
TYPED_TEST_SUITE(ShapeOpsUnaryTest, ShapeOpsUnaryTestTypes);
TYPED_TEST(ShapeOpsUnaryTest, Transpose) {
using IN_DT = typename std::tuple_element<0, TypeParam>::type;
using IN_VT = typename std::tuple_element<1, TypeParam>::type;
using RES_DT = typename std::tuple_element<2, TypeParam>::type;
// GIVEN
xt::xarray<IN_DT> x = test::xt_random<IN_DT>({2, 3, 4});
// WHAT
auto transpose_wrapper = [](SPUContext* ctx, const Value& x) {
return transpose(ctx, x);
};
auto z = test::evalUnaryOp<RES_DT>(IN_VT(), transpose_wrapper, x);
// THEN
EXPECT_TRUE(xt::allclose(xt::transpose(x), z, 0.01, 0.001)) << x << std::endl
<< z;
}
TYPED_TEST(ShapeOpsUnaryTest, TransposeWithPermutation) {
using IN_DT = typename std::tuple_element<0, TypeParam>::type;
using IN_VT = typename std::tuple_element<1, TypeParam>::type;
using RES_DT = typename std::tuple_element<2, TypeParam>::type;
// GIVEN
xt::xarray<IN_DT> x = test::xt_random<IN_DT>({2, 2, 4});
auto transpose_wrapper = [](SPUContext* ctx, const Value& x) {
return transpose(ctx, x, {1, 2, 0});
};
// WHAT
auto z = test::evalUnaryOp<RES_DT>(IN_VT(), transpose_wrapper, x);
// THEN
EXPECT_TRUE(xt::allclose(xt::transpose(x, {1, 2, 0}), z, 0.01, 0.001))
<< x << std::endl
<< z;
}
TYPED_TEST(ShapeOpsUnaryTest, BroadcastTo) {
using IN_DT = typename std::tuple_element<0, TypeParam>::type;
using IN_VT = typename std::tuple_element<1, TypeParam>::type;
using RES_DT = typename std::tuple_element<2, TypeParam>::type;
// GIVEN
xt::xarray<IN_DT> x = test::xt_random<IN_DT>({5, 1, 6});
auto broadcast_to_wrapper = [](SPUContext* ctx, const Value& in) {
return broadcast_to(ctx, in, {5, 4, 6});
};
// WHAT
auto z = test::evalUnaryOp<RES_DT>(IN_VT(), broadcast_to_wrapper, x);
// THEN
EXPECT_TRUE(xt::allclose(xt::broadcast(x, std::vector<int>{5, 4, 6}), z, 0.01,
0.001));
}
TYPED_TEST(ShapeOpsUnaryTest, BroadcastScalar) {
using IN_DT = typename std::tuple_element<0, TypeParam>::type;
using IN_VT = typename std::tuple_element<1, TypeParam>::type;
using RES_DT = typename std::tuple_element<2, TypeParam>::type;
// GIVEN
xt::xarray<IN_DT> x = test::xt_random<IN_DT>({});
auto broadcast_to_wrapper = [](SPUContext* ctx, const Value& in) {
return broadcast_to(ctx, in, {1, 1});
};
// WHAT
auto z = test::evalUnaryOp<RES_DT>(IN_VT(), broadcast_to_wrapper, x);
// THEN
EXPECT_EQ(z.shape(), std::vector<size_t>(2, 1));
}
TYPED_TEST(ShapeOpsUnaryTest, BroadcastInDims) {
using IN_DT = typename std::tuple_element<0, TypeParam>::type;
using IN_VT = typename std::tuple_element<1, TypeParam>::type;
using RES_DT = typename std::tuple_element<2, TypeParam>::type;
// GIVEN
xt::xarray<IN_DT> x = {1, 2, 3, 4};
auto broadcast_to_wrapper = [](SPUContext* ctx, const Value& in) {
return broadcast_to(ctx, in, {4, 2}, {0});
};
// WHAT
auto z = test::evalUnaryOp<RES_DT>(IN_VT(), broadcast_to_wrapper, x);
// THEN
EXPECT_TRUE(xt::allclose(
xt::broadcast(xt::reshape_view(x, {4, 1}), std::vector<int>{4, 2}), z,
0.01, 0.001));
}
TEST(SliceTest, Slice) {
// GIVEN
xt::xarray<int32_t> x = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}};
using P_VT = public_v::type;
auto slice_wrapper = [](SPUContext* ctx, const Value& in) {
return slice(ctx, in, {2, 1}, {4, 3}, {});
};
auto z = test::evalUnaryOp<int64_t>(P_VT(), slice_wrapper, x);
EXPECT_EQ(std::vector<int64_t>(z.shape().begin(), z.shape().end()),
std::vector<int64_t>({2, 2}));
EXPECT_EQ(xt::view(x, xt::range(2, 4), xt::range(1, 3)), z);
}
TEST(SliceTest, UpdateSlice) {
// GIVEN
xt::xarray<int32_t> x = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
xt::xarray<int32_t> y = {{0, 1}, {3, 4}};
using P_VT = public_v::type;
auto update_slice_wrapper = [](SPUContext* ctx, const Value& in,
const Value& update) {
return update_slice(ctx, in, update, {1, 1});
};
auto z =
test::evalBinaryOp<int64_t>(P_VT(), P_VT(), update_slice_wrapper, x, y);
EXPECT_EQ(xt::view(z, xt::range(1, 3), xt::range(1, 3)), y);
}
TEST(SliceTest, SliceStride) {
// GIVEN
xt::xarray<int32_t> x = {0, 1, 2, 3};
using P_VT = public_v::type;
auto slice_wrapper = [](SPUContext* ctx, const Value& in) {
return slice(ctx, in, {0}, {4}, {3});
};
auto z = test::evalUnaryOp<int64_t>(P_VT(), slice_wrapper, x);
EXPECT_EQ(std::vector<int64_t>(z.shape().begin(), z.shape().end()),
std::vector<int64_t>({2}));
xt::xarray<int64_t> expected = {0, 3};
EXPECT_EQ(z, expected);
}
TEST(ReshapeTest, Reshape) {
// GIVEN
xt::xarray<int32_t> x = {1, 2, 3, 4};
using P_VT = public_v::type;
auto reshape_wrapper = [](SPUContext* ctx, const Value& in) {
return reshape(ctx, in, {2, 2});
};
auto z = test::evalUnaryOp<int64_t>(P_VT(), reshape_wrapper, x);
EXPECT_EQ(std::vector<int64_t>(z.shape().begin(), z.shape().end()),
std::vector<int64_t>({2, 2}));
}
TEST(ShapeOpsUnaryTest, Reverse) {
// GIVEN
xt::xarray<int32_t> x = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}},
};
using P_VT = public_v::type;
auto reverse_wrapper = [](SPUContext* ctx, const Value& in) {
return reverse(ctx, in, {0, 1, 2});
};
// WHAT
auto z = test::evalUnaryOp<int32_t>(P_VT(), reverse_wrapper, x);
xt::xarray<int32_t> expected = x;
for (const auto& dim : {0, 1, 2}) {
expected = xt::flip(expected, dim);
}
// THEN
EXPECT_TRUE(xt::allclose(z, expected, 0.01, 0.001)) << z << std::endl
<< expected;
}
TEST(ShapeOpsUnaryTest, BroadcastAfterReshape) {
// GIVEN
xt::xarray<int32_t> x = {{1, 2}, {3, 4}};
using P_VT = public_v::type;
auto pad_wrapper = [](SPUContext* ctx, const Value& in) {
auto x1 = reshape(ctx, in, {1, 2, 2});
return broadcast_to(ctx, x1, {2, 2, 2});
};
// WHAT
auto z = test::evalUnaryOp<int32_t>(P_VT(), pad_wrapper, x);
// THEN
xt::xarray<int32_t> expected = {{{1, 2}, {3, 4}}, {{1, 2}, {3, 4}}};
EXPECT_EQ(z, expected) << z << std::endl << expected;
}
TEST(ShapeOpsUnaryTest, Pad) {
// GIVEN
xt::xarray<int32_t> x = {{{
{1, 2}, // row 0
{3, 4}, // row 1
{5, 6}, // row 2
}}};
using P_VT = public_v::type;
auto pad_wrapper = [](SPUContext* ctx, const Value& in) {
return pad(ctx, in, test::makeValue(ctx, 35, P_VT()), {1, 0, 0, 0},
{0, 2, 0, 0}, {2, 1, 0, 0});
};
// WHAT
auto z = test::evalUnaryOp<int32_t>(P_VT(), pad_wrapper, x);
// THEN
auto expected = xt::xarray<int32_t>({{{{35, 35}, {35, 35}, {35, 35}},
{{35, 35}, {35, 35}, {35, 35}},
{{35, 35}, {35, 35}, {35, 35}}},
{{{1, 2}, {3, 4}, {5, 6}},
{{35, 35}, {35, 35}, {35, 35}},
{{35, 35}, {35, 35}, {35, 35}}}});
EXPECT_TRUE(xt::allclose(z, expected, 0.01, 0.001)) << z << std::endl
<< expected;
}
TEST(ShapeOpsUnaryTest, InteriorPadding) {
// GIVEN
xt::xarray<int32_t> x = {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}};
using P_VT = public_v::type;
auto pad_wrapper = [](SPUContext* ctx, const Value& in) {
return pad(ctx, in, test::makeValue(ctx, 0, P_VT()), {0, 0}, {0, 0},
{1, 1});
};
// WHAT
auto z = test::evalUnaryOp<int32_t>(P_VT(), pad_wrapper, x);
// THEN
xt::xarray<int32_t> expected = {{1, 0, 2, 0, 3, 0, 4, 0, 5}, //
{0, 0, 0, 0, 0, 0, 0, 0, 0}, //
{6, 0, 7, 0, 8, 0, 9, 0, 10}, //
{0, 0, 0, 0, 0, 0, 0, 0, 0}, //
{11, 0, 12, 0, 13, 0, 14, 0, 15},
{0, 0, 0, 0, 0, 0, 0, 0, 0}, //
{16, 0, 17, 0, 18, 0, 19, 0, 20}};
EXPECT_EQ(z, expected) << z << std::endl << expected;
}
TEST(ShapeOpsUnaryTest, NegativeEdgePad) {
// GIVEN
xt::xarray<int32_t> x = {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}};
using P_VT = public_v::type;
auto pad_wrapper = [](SPUContext* ctx, const Value& in) {
return pad(ctx, in, test::makeValue(ctx, 0, P_VT()), {-1, -1}, {-1, -1},
{0, 0});
};
// WHAT
auto z = test::evalUnaryOp<int32_t>(P_VT(), pad_wrapper, x);
// THEN
xt::xarray<int32_t> expected = {{7, 8, 9}, //
{12, 13, 14}};
EXPECT_EQ(z, expected) << z << std::endl << expected;
}
TEST(ShapeOpsUnaryTest, NegativeEdgePadWithInteriorPad) {
// GIVEN
xt::xarray<int32_t> x = {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}};
using P_VT = public_v::type;
auto pad_wrapper = [](SPUContext* ctx, const Value& in) {
return pad(ctx, in, test::makeValue(ctx, 0, P_VT()), {-1, -1}, {-1, -1},
{1, 1});
};
// WHAT
auto z = test::evalUnaryOp<int32_t>(P_VT(), pad_wrapper, x);
// THEN
xt::xarray<int32_t> expected = {{0, 0, 0, 0, 0, 0, 0}, //
{0, 7, 0, 8, 0, 9, 0}, //
{0, 0, 0, 0, 0, 0, 0}, //
{0, 12, 0, 13, 0, 14, 0}, //
{0, 0, 0, 0, 0, 0, 0}};
EXPECT_EQ(z, expected) << z << std::endl << expected;
}
TEST(ShapeOpsUnaryTest, HighNegativeEdgePadWithInteriorPad) {
// GIVEN
xt::xarray<int32_t> x = {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}};
using P_VT = public_v::type;
auto pad_wrapper = [](SPUContext* ctx, const Value& in) {
return pad(ctx, in, test::makeValue(ctx, 0, P_VT()), {-3, -3}, {-1, -1},
{1, 1});
};
// WHAT
auto z = test::evalUnaryOp<int32_t>(P_VT(), pad_wrapper, x);
// THEN
xt::xarray<int32_t> expected = {{0, 0, 0, 0, 0}, //
{0, 13, 0, 14, 0},
{0, 0, 0, 0, 0}};
EXPECT_EQ(z, expected) << z << std::endl << expected;
}
using SecretV = std::integral_constant<Visibility, VIS_SECRET>;
using PublicV = std::integral_constant<Visibility, VIS_PUBLIC>;
using ConcatTestTypes = ::testing::Types< //
std::tuple<float, SecretV, SecretV>, // concat(s, s)
std::tuple<float, PublicV, PublicV>, // concat(p, p)
std::tuple<float, SecretV, PublicV>, // concat(s, p)
std::tuple<float, PublicV, SecretV>, // concat(p, s)
//
std::tuple<int16_t, SecretV, SecretV>, // concat(s, s)
std::tuple<int16_t, PublicV, PublicV>, // concat(p, p)
std::tuple<int16_t, SecretV, PublicV>, // concat(s, p)
std::tuple<int16_t, PublicV, SecretV> // concat(p, s)
>;
template <typename S>
class ConcatTest : public ::testing::Test {};
TYPED_TEST_SUITE(ConcatTest, ConcatTestTypes);
TYPED_TEST(ConcatTest, Concatenate) {
using DT = typename std::tuple_element<0, TypeParam>::type;
using LhsVt = typename std::tuple_element<1, TypeParam>::type;
using RhsVt = typename std::tuple_element<2, TypeParam>::type;
// GIVEN
xt::xarray<DT> x = test::xt_random<DT>({3, 3});
xt::xarray<DT> y = test::xt_random<DT>({3, 3});
auto concat_wrapper = [](SPUContext* ctx, const Value& lhs,
const Value& rhs) {
return concatenate(ctx, {lhs, rhs}, 0);
};
// WHAT
auto z = test::evalBinaryOp<DT>(LhsVt(), RhsVt(), concat_wrapper, x, y);
// THEN
EXPECT_TRUE(
xt::allclose(xt::concatenate(xt::xtuple(x, y), 0), z, 0.01, 0.001))
<< x << std::endl
<< y << std::endl
<< z;
}
TYPED_TEST(ConcatTest, VConcatenate) {
using DT = typename std::tuple_element<0, TypeParam>::type;
using LhsVt = typename std::tuple_element<1, TypeParam>::type;
using RhsVt = typename std::tuple_element<2, TypeParam>::type;
// GIVEN
xt::xarray<DT> x = test::xt_random<DT>({3, 3});
xt::xarray<DT> y = test::xt_random<DT>({3, 3});
auto concat_wrapper = [](SPUContext* ctx, const Value& lhs,
const Value& rhs) {
return concatenate(ctx, {lhs, rhs}, 1);
};
// WHAT
auto z = test::evalBinaryOp<DT>(LhsVt(), RhsVt(), concat_wrapper, x, y);
// THEN
EXPECT_TRUE(
xt::allclose(xt::concatenate(xt::xtuple(x, y), 1), z, 0.01, 0.001))
<< x << std::endl
<< y << std::endl
<< z;
}
} // namespace spu::kernel::hal