-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
qlinear_concat_test.cc
242 lines (204 loc) · 8.66 KB
/
qlinear_concat_test.cc
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "gtest/gtest.h"
#include "test/common/tensor_op_test_utils.h"
#include "test/providers/provider_test_utils.h"
#include "core/providers/common.h"
namespace onnxruntime {
namespace test {
enum QLinearConcatFailCause {
NoFail,
FailByWrongTensorType,
FailByWrongZeroPointType,
FailByWrongScaleType
};
template <typename xint8>
void RunQLinearConcat(
const std::vector<std::vector<int64_t>> x_shapes,
const std::vector<std::vector<xint8>> x_vecs,
int64_t possible_neg_concat_axis,
const std::vector<float> x_scales,
const std::vector<xint8> x_zero_points,
const std::vector<int64_t> y_shape,
const std::vector<xint8> y_vec,
float y_scale,
xint8 y_zero_point,
const std::vector<bool> is_const_inputs,
QLinearConcatFailCause fail_by = NoFail) {
size_t input_count = x_shapes.size();
OpTester test("QLinearConcat", 1, onnxruntime::kMSDomain);
test.AddAttribute<int64_t>("axis", possible_neg_concat_axis);
test.AddInput<float>("y_scale", {}, {y_scale});
test.AddInput<xint8>("y_zero_point", {}, {y_zero_point});
for (size_t input_index = 0; input_index < input_count; input_index++) {
std::stringstream ss;
ss << "X" << input_index;
if (fail_by == FailByWrongTensorType && input_index == 0) {
if (std::is_signed<xint8>().value) {
const uint8_t* rdata = (const uint8_t*)(x_vecs[input_index].data());
std::vector<uint8_t> ov(rdata, rdata + x_vecs[input_index].size());
test.AddInput<uint8_t>(ss.str().c_str(), x_shapes[input_index], ov);
} else {
const int8_t* rdata = (const int8_t*)(x_vecs[input_index].data());
std::vector<int8_t> ov(rdata, rdata + x_vecs[input_index].size());
test.AddInput<int8_t>(ss.str().c_str(), x_shapes[input_index], ov);
}
} else {
test.AddInput<xint8>(ss.str().c_str(), x_shapes[input_index], x_vecs[input_index]);
}
ss.str("");
ss << "x_scale" << input_index;
if (fail_by == FailByWrongScaleType && input_index == 0) {
test.AddInput<int64_t>(ss.str().c_str(), {}, {1LL}, is_const_inputs[input_index]);
} else {
test.AddInput<float>(ss.str().c_str(), {}, {x_scales[input_index]}, is_const_inputs[input_index]);
}
ss.str("");
ss << "x_zero_point" << input_index;
if (fail_by == FailByWrongZeroPointType && input_index == 0) {
if (std::is_signed<xint8>().value) {
test.AddInput<uint8_t>(ss.str().c_str(), {}, {128}, is_const_inputs[input_index]);
} else {
test.AddInput<int8_t>(ss.str().c_str(), {}, {0}, is_const_inputs[input_index]);
}
} else {
test.AddInput<xint8>(ss.str().c_str(), {}, {x_zero_points[input_index]}, is_const_inputs[input_index]);
}
}
test.AddOutput<xint8>("Y", y_shape, y_vec);
test.Run((fail_by == NoFail) ? OpTester::ExpectResult::kExpectSuccess : OpTester::ExpectResult::kExpectFailure);
}
void QLinearConcat3InputsU8(std::vector<bool> is_const_inputs, QLinearConcatFailCause fail_by = NoFail) {
std::vector<int64_t> y_shape = {2, 6, 3};
std::vector<std::vector<int64_t>> x_shapes = {
{2, 1, 3},
{2, 2, 3},
{2, 3, 3}};
std::vector<std::vector<uint8_t>> x_vecs = {
{128, 126, 131, 123, 255, 0},
{128, 127, 129, 125, 131, 124, 132, 122, 135, 120, 255, 0},
{128, 127, 129, 126, 130, 125, 131, 124, 132, 123, 133, 122, 134, 121, 135, 120, 255, 0}};
std::vector<uint8_t> x_zero_points = {128, 128, 128};
std::vector<float> x_scales = {1.0, 0.5, 0.25};
float y_scale = 0.25;
uint8_t y_zero_point = 128;
std::vector<uint8_t> y_vec = {
128, 120, 140,
128, 126, 130, 122, 134, 120,
128, 127, 129, 126, 130, 125, 131, 124, 132,
108, 255, 0,
136, 116, 142, 112, 255, 0,
123, 133, 122, 134, 121, 135, 120, 255, 0};
RunQLinearConcat<uint8_t>(x_shapes, x_vecs, 1, x_scales, x_zero_points, y_shape, y_vec, y_scale, y_zero_point,
is_const_inputs, fail_by);
RunQLinearConcat<uint8_t>(x_shapes, x_vecs, -2, x_scales, x_zero_points, y_shape, y_vec, y_scale, y_zero_point,
is_const_inputs, fail_by);
}
TEST(QLinearConcatU8, Input3_DynamicDynamicDynamic) {
QLinearConcat3InputsU8({false, false, false});
}
TEST(QLinearConcatU8, Input3_ConstConstConst) {
QLinearConcat3InputsU8({true, true, true});
}
TEST(QLinearConcatU8, Input3_MixedConstDynamic) {
QLinearConcat3InputsU8({false, true, false});
QLinearConcat3InputsU8({true, false, true});
QLinearConcat3InputsU8({false, false, true});
QLinearConcat3InputsU8({true, false, false});
}
TEST(QLinearConcatU8, ExpectFail_WrongScaleType_0) {
QLinearConcat3InputsU8({false, false, false}, FailByWrongScaleType);
}
TEST(QLinearConcatU8, ExpectFail_WrongScaleType_1) {
QLinearConcat3InputsU8({true, true, true}, FailByWrongScaleType);
}
TEST(QLinearConcatU8, ExpectFail_WrongTensorType_0) {
QLinearConcat3InputsU8({false, false, false}, FailByWrongTensorType);
}
TEST(QLinearConcatU8, ExpectFail_WrongTensorType_1) {
QLinearConcat3InputsU8({true, true, true}, FailByWrongTensorType);
}
TEST(QLinearConcatU8, ExpectFail_WrongZeroPointType_0) {
QLinearConcat3InputsU8({false, false, false}, FailByWrongZeroPointType);
}
TEST(QLinearConcatU8, ExpectFail_WrongZeroPointType_1) {
QLinearConcat3InputsU8({true, true, true}, FailByWrongZeroPointType);
}
void QLinearConcat3InputsS8(std::vector<bool> is_const_inputs, QLinearConcatFailCause fail_by = NoFail) {
std::vector<int64_t> y_shape = {2, 6, 3};
std::vector<std::vector<int64_t>> x_shapes = {
{2, 1, 3},
{2, 2, 3},
{2, 3, 3}};
std::vector<std::vector<int8_t>> x_vecs = {
{0, -2, 3, -5, 127, -128},
{0, -1, 2, -3, 3, -4, 4, -6, 7, -8, 127, -128},
{0, -1, 2, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, -8, 127, -128}};
std::vector<int8_t> x_zero_points = {0, 0, 0};
std::vector<float> x_scales = {1.0, 0.5, 0.25};
float y_scale = 0.25;
int8_t y_zero_point = 0;
std::vector<int8_t> y_vec = {
0, -8, 12,
0, -2, 4, -6, 6, -8,
0, -1, 2, -2, 2, -3, 3, -4, 4,
-20, 127, -128,
8, -12, 14, -16, 127, -128,
-5, 5, -6, 6, -7, 7, -8, 127, -128};
RunQLinearConcat<int8_t>(x_shapes, x_vecs, 1, x_scales, x_zero_points, y_shape, y_vec, y_scale, y_zero_point,
is_const_inputs, fail_by);
RunQLinearConcat<int8_t>(x_shapes, x_vecs, -2, x_scales, x_zero_points, y_shape, y_vec, y_scale, y_zero_point,
is_const_inputs, fail_by);
}
TEST(QLinearConcatS8, Input3_DynamicDynamicDynamic) {
QLinearConcat3InputsS8({false, false, false});
}
TEST(QLinearConcatS8, Input3_ConstConstConst) {
QLinearConcat3InputsS8({true, true, true});
}
TEST(QLinearConcatS8, ExpectFail_MixedConstDynamic) {
QLinearConcat3InputsS8({false, true, false});
QLinearConcat3InputsS8({true, false, true});
QLinearConcat3InputsS8({false, false, true});
QLinearConcat3InputsS8({true, false, false});
}
TEST(QLinearConcatS8, ExpectFail_WrongScaleType_0) {
QLinearConcat3InputsS8({false, false, false}, FailByWrongScaleType);
}
TEST(QLinearConcatS8, ExpectFail_WrongScaleType_1) {
QLinearConcat3InputsS8({true, true, true}, FailByWrongScaleType);
}
TEST(QLinearConcatS8, ExpectFail_WrongTensorType_0) {
QLinearConcat3InputsS8({false, false, false}, FailByWrongTensorType);
}
TEST(QLinearConcatS8, ExpectFail_WrongTensorType_1) {
QLinearConcat3InputsS8({true, true, true}, FailByWrongTensorType);
}
TEST(QLinearConcatS8, ExpectFail_WrongZeroPointType_0) {
QLinearConcat3InputsS8({false, false, false}, FailByWrongZeroPointType);
}
TEST(QLinearConcatS8, ExpectFail_WrongZeroPointType_1) {
QLinearConcat3InputsS8({true, true, true}, FailByWrongZeroPointType);
}
void QLinearConcatOneInputS8(std::vector<bool> is_const_inputs, QLinearConcatFailCause fail_by = NoFail) {
std::vector<int64_t> y_shape = {2, 1, 3};
std::vector<std::vector<int64_t>> x_shapes = {{2, 1, 3}};
std::vector<std::vector<int8_t>> x_vecs = {{0, -2, 3, -5, 127, -128}};
std::vector<int8_t> x_zero_points = {0};
std::vector<float> x_scales = {1.0};
float y_scale = 0.25;
int8_t y_zero_point = 0;
std::vector<int8_t> y_vec = {0, -8, 12, -20, 127, -128};
RunQLinearConcat<int8_t>(x_shapes, x_vecs, 1, x_scales, x_zero_points, y_shape, y_vec, y_scale, y_zero_point,
is_const_inputs, fail_by);
RunQLinearConcat<int8_t>(x_shapes, x_vecs, -2, x_scales, x_zero_points, y_shape, y_vec, y_scale, y_zero_point,
is_const_inputs, fail_by);
}
TEST(QLinearConcatS8, InputOne_Dynamic) {
QLinearConcatOneInputS8({false});
}
TEST(QLinearConcatS8, InputOne_Const) {
QLinearConcatOneInputS8({true});
}
} // namespace test
} // namespace onnxruntime