|
5 | 5 | #include "gtest/gtest.h" |
6 | 6 | #include "test/providers/provider_test_utils.h" |
7 | 7 | #include "test/util/include/default_providers.h" |
| 8 | +#include "test/common/tensor_op_test_utils.h" |
8 | 9 |
|
9 | 10 | namespace onnxruntime { |
10 | 11 | namespace test { |
11 | 12 |
|
12 | 13 | // Some of the tests can't run on TensorrtExecutionProvider because of unsupported data types. |
13 | 14 | // Those tests will fallback to other EPs |
14 | 15 |
|
15 | | -TEST(GatherOpTest, Gather_axis0) { |
| 16 | +template <typename T> |
| 17 | +class GatherOpTest : public ::testing::Test { |
| 18 | +}; |
| 19 | + |
| 20 | +using GatherOpTestTypes = ::testing::Types<float, MLFloat16>; |
| 21 | +TYPED_TEST_SUITE(GatherOpTest, GatherOpTestTypes); |
| 22 | + |
| 23 | +TYPED_TEST(GatherOpTest, Gather_axis0) { |
16 | 24 | // To test for NNAPI EP, we need the indices to be initializers |
17 | 25 | auto run_test = [](bool indices_is_initializer) { |
18 | 26 | OpTester test("Gather"); |
19 | 27 | test.AddAttribute<int64_t>("axis", 0LL); |
20 | | - test.AddInput<float>("data", {2, 3, 4}, |
21 | | - {0.0f, 0.1f, 0.2f, 0.3f, |
22 | | - 1.0f, 1.1f, 1.2f, 1.3f, |
23 | | - 2.0f, 2.1f, 2.2f, 2.3f, |
24 | | - 10.0f, 10.1f, 10.2f, 10.3f, |
25 | | - 11.0f, 11.1f, 11.2f, 11.3f, |
26 | | - 12.0f, 12.1f, 12.2f, 12.3f}); |
| 28 | + test.AddInput<TypeParam>("data", {2, 3, 4}, |
| 29 | + GetTypedArray<TypeParam>({0.0f, 0.1f, 0.2f, 0.3f, |
| 30 | + 1.0f, 1.1f, 1.2f, 1.3f, |
| 31 | + 2.0f, 2.1f, 2.2f, 2.3f, |
| 32 | + 10.0f, 10.1f, 10.2f, 10.3f, |
| 33 | + 11.0f, 11.1f, 11.2f, 11.3f, |
| 34 | + 12.0f, 12.1f, 12.2f, 12.3f})); |
27 | 35 | test.AddInput<int64_t>("indices", {1}, {1LL}, indices_is_initializer); |
28 | | - test.AddOutput<float>("output", {1, 3, 4}, |
29 | | - {10.0f, 10.1f, 10.2f, 10.3f, |
30 | | - 11.0f, 11.1f, 11.2f, 11.3f, |
31 | | - 12.0f, 12.1f, 12.2f, 12.3f}); |
| 36 | + test.AddOutput<TypeParam>("output", {1, 3, 4}, |
| 37 | + GetTypedArray<TypeParam>({10.0f, 10.1f, 10.2f, 10.3f, |
| 38 | + 11.0f, 11.1f, 11.2f, 11.3f, |
| 39 | + 12.0f, 12.1f, 12.2f, 12.3f})); |
32 | 40 | test.Run(); |
33 | 41 | }; |
34 | 42 |
|
35 | 43 | run_test(false); |
36 | 44 | run_test(true); |
37 | 45 | } |
38 | 46 |
|
39 | | -TEST(GatherOpTest, Gather_negative_axis) { |
| 47 | +TYPED_TEST(GatherOpTest, Gather_negative_axis) { |
40 | 48 | // To test for NNAPI EP, we need the indices to be initializers |
41 | 49 | auto run_test = [](bool indices_is_initializer) { |
42 | 50 | OpTester test("Gather"); |
43 | 51 | test.AddAttribute<int64_t>("axis", -3LL); |
44 | | - test.AddInput<float>("data", {2, 3, 4}, |
45 | | - {0.0f, 0.1f, 0.2f, 0.3f, |
46 | | - 1.0f, 1.1f, 1.2f, 1.3f, |
47 | | - 2.0f, 2.1f, 2.2f, 2.3f, |
48 | | - 10.0f, 10.1f, 10.2f, 10.3f, |
49 | | - 11.0f, 11.1f, 11.2f, 11.3f, |
50 | | - 12.0f, 12.1f, 12.2f, 12.3f}); |
| 52 | + test.AddInput<TypeParam>("data", {2, 3, 4}, |
| 53 | + GetTypedArray<TypeParam>({0.0f, 0.1f, 0.2f, 0.3f, |
| 54 | + 1.0f, 1.1f, 1.2f, 1.3f, |
| 55 | + 2.0f, 2.1f, 2.2f, 2.3f, |
| 56 | + 10.0f, 10.1f, 10.2f, 10.3f, |
| 57 | + 11.0f, 11.1f, 11.2f, 11.3f, |
| 58 | + 12.0f, 12.1f, 12.2f, 12.3f})); |
51 | 59 | test.AddInput<int64_t>("indices", {1}, {1LL}, indices_is_initializer); |
52 | | - test.AddOutput<float>("output", {1, 3, 4}, |
53 | | - {10.0f, 10.1f, 10.2f, 10.3f, |
54 | | - 11.0f, 11.1f, 11.2f, 11.3f, |
55 | | - 12.0f, 12.1f, 12.2f, 12.3f}); |
| 60 | + test.AddOutput<TypeParam>("output", {1, 3, 4}, |
| 61 | + GetTypedArray<TypeParam>({10.0f, 10.1f, 10.2f, 10.3f, |
| 62 | + 11.0f, 11.1f, 11.2f, 11.3f, |
| 63 | + 12.0f, 12.1f, 12.2f, 12.3f})); |
56 | 64 | test.Run(); |
57 | 65 | }; |
58 | 66 |
|
@@ -206,65 +214,63 @@ TEST(GatherOpTest, Gather_axis0_indices2d) { |
206 | 214 | run_test(true); |
207 | 215 | } |
208 | 216 |
|
209 | | -TEST(GatherOpTest, Gather_axis1_indices2d) { |
| 217 | +TYPED_TEST(GatherOpTest, Gather_axis1_indices2d) { |
210 | 218 | // To test for NNAPI EP, we need the indices to be initializers |
211 | 219 | auto run_test = [](bool indices_is_initializer) { |
212 | 220 | OpTester test("Gather"); |
213 | 221 | test.AddAttribute<int64_t>("axis", 1LL); |
214 | | - test.AddInput<float>("data", {3, 3}, |
215 | | - {0.0f, 0.1f, 0.2f, |
216 | | - 1.0f, 1.1f, 1.2f, |
217 | | - 2.0f, 2.1f, 2.2f}); |
| 222 | + test.AddInput<TypeParam>("data", {3, 3}, |
| 223 | + GetTypedArray<TypeParam>({0.0f, 0.1f, 0.2f, |
| 224 | + 1.0f, 1.1f, 1.2f, |
| 225 | + 2.0f, 2.1f, 2.2f})); |
218 | 226 | test.AddInput<int64_t>("indices", {2LL, 2LL}, |
219 | 227 | {1LL, 0LL, |
220 | 228 | 2LL, 1LL}, |
221 | 229 | indices_is_initializer); |
222 | | - test.AddOutput<float>("output", {3, 2, 2}, |
223 | | - {0.1f, 0.0f, 0.2f, 0.1f, |
224 | | - 1.1f, 1.0f, 1.2f, 1.1f, |
225 | | - 2.1f, 2.0f, 2.2f, 2.1f}); |
| 230 | + test.AddOutput<TypeParam>("output", {3, 2, 2}, |
| 231 | + GetTypedArray<TypeParam>({0.1f, 0.0f, 0.2f, 0.1f, |
| 232 | + 1.1f, 1.0f, 1.2f, 1.1f, |
| 233 | + 2.1f, 2.0f, 2.2f, 2.1f})); |
226 | 234 | test.Run(); |
227 | 235 | }; |
228 | 236 |
|
229 | 237 | run_test(false); |
230 | 238 | run_test(true); |
231 | 239 | } |
232 | 240 |
|
233 | | -TEST(GatherOpTest, Gather_axis0_indicesInt32) { |
| 241 | +TYPED_TEST(GatherOpTest, Gather_axis0_indicesInt32) { |
234 | 242 | // NNAPI EP only supports float input data for now, |
235 | 243 | // the following two test cases cover int32_t indices with float input other than int64_t type for Nnapi |
236 | 244 | OpTester test("Gather"); |
237 | 245 | test.AddAttribute<int64_t>("axis", 0LL); |
238 | | - test.AddInput<float>("data", {2, 3, 4}, |
239 | | - {0.0f, 0.1f, 0.2f, 0.3f, |
240 | | - 1.0f, 1.1f, 1.2f, 1.3f, |
241 | | - 2.0f, 2.1f, 2.2f, 2.3f, |
242 | | - 10.0f, 10.1f, 10.2f, 10.3f, |
243 | | - 11.0f, 11.1f, 11.2f, 11.3f, |
244 | | - 12.0f, 12.1f, 12.2f, 12.3f}); |
| 246 | + test.AddInput<TypeParam>("data", {2, 3, 4}, |
| 247 | + GetTypedArray<TypeParam>({0.0f, 0.1f, 0.2f, 0.3f, |
| 248 | + 1.0f, 1.1f, 1.2f, 1.3f, |
| 249 | + 2.0f, 2.1f, 2.2f, 2.3f, |
| 250 | + 10.0f, 10.1f, 10.2f, 10.3f, |
| 251 | + 11.0f, 11.1f, 11.2f, 11.3f, |
| 252 | + 12.0f, 12.1f, 12.2f, 12.3f})); |
245 | 253 | test.AddInput<int32_t>("indices", {1}, {1}); |
246 | | - test.AddOutput<float>("output", {1, 3, 4}, |
247 | | - {10.0f, 10.1f, 10.2f, 10.3f, |
248 | | - 11.0f, 11.1f, 11.2f, 11.3f, |
249 | | - 12.0f, 12.1f, 12.2f, 12.3f}); |
250 | | - |
| 254 | + test.AddOutput<TypeParam>("output", {1, 3, 4}, |
| 255 | + GetTypedArray<TypeParam>({10.0f, 10.1f, 10.2f, 10.3f, |
| 256 | + 11.0f, 11.1f, 11.2f, 11.3f, |
| 257 | + 12.0f, 12.1f, 12.2f, 12.3f})); |
251 | 258 | test.Run(); |
252 | 259 | } |
253 | 260 |
|
254 | | -TEST(GatherOpTest, Gather_axis0_indices2dInt32) { |
| 261 | +TYPED_TEST(GatherOpTest, Gather_axis0_indices2dInt32) { |
255 | 262 | OpTester test("Gather"); |
256 | 263 | test.AddAttribute<int64_t>("axis", 0LL); |
257 | | - test.AddInput<float>("data", {3, 3}, |
258 | | - {0.0f, 0.1f, 0.2f, |
259 | | - 1.0f, 1.1f, 1.2f, |
260 | | - 2.0f, 2.1f, 2.2f}); |
| 264 | + test.AddInput<TypeParam>("data", {3, 3}, |
| 265 | + GetTypedArray<TypeParam>({0.0f, 0.1f, 0.2f, |
| 266 | + 1.0f, 1.1f, 1.2f, |
| 267 | + 2.0f, 2.1f, 2.2f})); |
261 | 268 | test.AddInput<int32_t>("indices", {2, 2}, |
262 | 269 | {1, 0, |
263 | 270 | 2, 1}); |
264 | | - test.AddOutput<float>("output", {2, 2, 3}, |
265 | | - {1.0f, 1.1f, 1.2f, 0.0f, 0.1f, 0.2f, |
266 | | - 2.0f, 2.1f, 2.2f, 1.0f, 1.1f, 1.2f}); |
267 | | - |
| 271 | + test.AddOutput<TypeParam>("output", {2, 2, 3}, |
| 272 | + GetTypedArray<TypeParam>({1.0f, 1.1f, 1.2f, 0.0f, 0.1f, 0.2f, |
| 273 | + 2.0f, 2.1f, 2.2f, 1.0f, 1.1f, 1.2f})); |
268 | 274 | test.Run(); |
269 | 275 | } |
270 | 276 |
|
|
0 commit comments