onnx_import.in.cpp 123 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2019 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15
//
// 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.
//*****************************************************************************
16

17
#include <algorithm>
18
#include <cmath>
19
#include <cstdint>
20
#include <fstream>
21 22
#include <iterator>
#include <limits>
23
#include <sstream>
24
#include <stdexcept>
25
#include <vector>
26 27 28 29

#include "gtest/gtest.h"
#include "ngraph/frontend/onnx_import/onnx.hpp"
#include "ngraph/ngraph.hpp"
tsocha's avatar
tsocha committed
30
#include "util/all_close.hpp"
31 32
#include "util/all_close_f.hpp"
#include "util/ndarray.hpp"
33
#include "util/test_control.hpp"
34 35
#include "util/test_tools.hpp"

36 37
using namespace ngraph;

38 39
static std::string s_manifest = "${MANIFEST}";

40 41
using Inputs = std::vector<std::vector<float>>;
using Outputs = std::vector<std::vector<float>>;
42

43
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_output_names_check)
44 45
{
    auto function = onnx_import::import_onnx_model(
46
        file_util::path_join(SERIALIZED_ZOO, "onnx/split_equal_parts_default.prototxt"));
47 48 49 50 51 52 53 54

    std::size_t size = function->get_output_size();
    for (std::size_t i{0}; i < size; ++i)
    {
        std::shared_ptr<Node> node = function->get_output_op(i);
        EXPECT_EQ(node->get_friendly_name(), "output_" + std::to_string(i + 1));
    }
}
55

56
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_add_abc)
57 58 59 60 61 62 63 64 65 66 67
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/add_abc.prototxt"));

    Inputs inputs{{1}, {2}, {3}};
    Outputs expected_outputs{{6}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

68
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_binary_add_abc)
69
{
70 71
    auto function =
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/add_abc.onnx"));
72 73 74 75

    Inputs inputs{{1}, {2}, {3}};
    Outputs expected_outputs{{6}};

76
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
77
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
78 79
}

80
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_add_abc_initializers)
81
{
82
    auto function = onnx_import::import_onnx_model(
83
        file_util::path_join(SERIALIZED_ZOO, "onnx/add_abc_initializers.prototxt"));
84

85 86
    Inputs inputs{{1, 2, 3, 4}};
    Outputs expected_outputs{{3, 6, 9, 12}};
87

88
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
89
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
90 91
}

92
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_addmul_abc)
tsocha's avatar
tsocha committed
93
{
94
    auto function = onnx_import::import_onnx_model(
95
        file_util::path_join(SERIALIZED_ZOO, "onnx/addmul_abc.prototxt"));
tsocha's avatar
tsocha committed
96 97 98

    std::vector<std::vector<float>> inputs;

99
    Shape shape{1, 2, 2};
100 101 102
    inputs.emplace_back(test::NDArray<float, 3>({{{9, 10}}, {{11, 12}}}).get_vector());
    inputs.emplace_back(test::NDArray<float, 3>({{{5, 6}}, {{7, 8}}}).get_vector());
    inputs.emplace_back(test::NDArray<float, 3>({{{1, 2}}, {{3, 4}}}).get_vector());
tsocha's avatar
tsocha committed
103

104
    auto expected_output = test::NDArray<float, 3>({{{46, 62}}, {{80, 100}}}).get_vector();
tsocha's avatar
tsocha committed
105

106
    auto result_vectors = execute(function, inputs, "${BACKEND_NAME}");
tsocha's avatar
tsocha committed
107 108 109
    EXPECT_TRUE(test::all_close_f(expected_output, result_vectors.front()));
}

110
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_argmin_no_keepdims)
111
{
112
    auto function = onnx_import::import_onnx_model(
113
        file_util::path_join(SERIALIZED_ZOO, "onnx/argmin_no_keepdims.prototxt"));
114 115 116 117

    Inputs inputs{test::NDArray<float, 2>{{2, 1}, {3, 10}}.get_vector()};
    std::vector<std::vector<int64_t>> expected_output{{1, 0}};
    std::vector<std::vector<int64_t>> result{
118
        execute<float, int64_t>(function, inputs, "${BACKEND_NAME}")};
119 120 121
    EXPECT_EQ(expected_output, result);
}

122
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_split_equal_parts_default)
123
{
124
    auto function = onnx_import::import_onnx_model(
125
        file_util::path_join(SERIALIZED_ZOO, "onnx/split_equal_parts_default.prototxt"));
126

127 128
    Inputs inputs{{1, 2, 3, 4, 5, 6}};
    Outputs expected_outputs{{1, 2}, {3, 4}, {5, 6}};
129

130
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
131 132
    EXPECT_EQ(outputs.size(), expected_outputs.size());

133
    for (std::size_t i = 0; i < expected_outputs.size(); ++i)
134
    {
135 136
        EXPECT_EQ(outputs[i].size(), expected_outputs[i].size());
        EXPECT_TRUE(test::all_close_f(outputs[i], expected_outputs[i]));
137 138 139
    }
}

140
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_split_equal_parts_2d)
141 142
{
    // Split into 2 equal parts along axis=1
143
    auto function = onnx_import::import_onnx_model(
144
        file_util::path_join(SERIALIZED_ZOO, "onnx/split_equal_parts_2d.prototxt"));
145

146 147
    Inputs inputs{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
    Outputs expected_outputs{{0, 1, 2, 6, 7, 8}, {3, 4, 5, 9, 10, 11}};
148

149
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
150 151
    EXPECT_EQ(outputs.size(), expected_outputs.size());

152
    for (std::size_t i = 0; i < expected_outputs.size(); ++i)
153
    {
154 155
        EXPECT_EQ(outputs[i].size(), expected_outputs[i].size());
        EXPECT_TRUE(test::all_close_f(outputs[i], expected_outputs[i]));
156 157 158
    }
}

159
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_split_variable_parts_2d)
160 161
{
    // Split into variable parts {2, 4} along axis=1
162
    auto function = onnx_import::import_onnx_model(
163
        file_util::path_join(SERIALIZED_ZOO, "onnx/split_variable_parts_2d.prototxt"));
164

165 166
    Inputs inputs{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}};
    Outputs expected_outputs{{0, 1, 6, 7}, {2, 3, 4, 5, 8, 9, 10, 11}};
167

168
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
169 170
    EXPECT_EQ(outputs.size(), expected_outputs.size());

171
    for (std::size_t i = 0; i < expected_outputs.size(); ++i)
172
    {
173 174
        EXPECT_EQ(outputs[i].size(), expected_outputs[i].size());
        EXPECT_TRUE(test::all_close_f(outputs[i], expected_outputs[i]));
175 176
    }
}
177

178 179
namespace
{
180
    std::vector<std::vector<float>> conv2d_execute(const std::shared_ptr<Function>& function)
181 182 183 184
    {
        std::vector<std::vector<float>> args;

        // data (1, 1, 7, 5) input tensor
185 186 187 188 189 190 191
        args.emplace_back(test::NDArray<float, 4>{{{{{0.f, 1.f, 2.f, 3.f, 4.f},
                                                     {5.f, 6.f, 7.f, 8.f, 9.f},
                                                     {10.f, 11.f, 12.f, 13.f, 14.f},
                                                     {15.f, 16.f, 17.f, 18.f, 19.f},
                                                     {20.f, 21.f, 22.f, 23.f, 24.f},
                                                     {25.f, 26.f, 27.f, 28.f, 29.f},
                                                     {30.f, 31.f, 32.f, 33.f, 34.f}}}}}
192 193 194 195
                              .get_vector());

        // filters (1, 1, 3, 3) aka convolution weights
        args.emplace_back(
196
            test::NDArray<float, 4>{{{{{1.f, 1.f, 1.f}, {1.f, 1.f, 1.f}, {1.f, 1.f, 1.f}}}}}
197 198
                .get_vector());

199
        return execute(function, args, "${BACKEND_NAME}");
200
    }
201

202 203
} // namespace

204
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_conv2d_strides_padding)
205 206
{
    // Convolution with strides=2 and padding=1
207
    auto function = onnx_import::import_onnx_model(
208
        file_util::path_join(SERIALIZED_ZOO, "onnx/conv_with_strides_padding.prototxt"));
209 210

    // (1, 1, 4, 3)
211 212 213 214
    auto expected_output = test::NDArray<float, 4>({{{{12.f, 27.f, 24.f},
                                                      {63.f, 108.f, 81.f},
                                                      {123.f, 198.f, 141.f},
                                                      {112.f, 177.f, 124.f}}}})
215 216
                               .get_vector();

217
    auto result = conv2d_execute(function);
218 219 220
    EXPECT_EQ(expected_output, result.front());
}

221
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_conv2d_strides_no_padding)
222 223
{
    // Convolution with strides=2 and padding=1
224
    auto function = onnx_import::import_onnx_model(
225
        file_util::path_join(SERIALIZED_ZOO, "onnx/conv_with_strides_no_padding.prototxt"));
226 227 228

    // (1, 1, 3, 2)
    auto expected_output =
229
        test::NDArray<float, 4>({{{{54.f, 72.f}, {144.f, 162.f}, {234.f, 252.f}}}}).get_vector();
230

231
    auto result = conv2d_execute(function);
232 233 234
    EXPECT_EQ(expected_output, result.front());
}

235
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_conv2d_strides_assymetric_padding)
236 237
{
    // Convolution with strides=2 and padding=1
238 239
    auto function = onnx_import::import_onnx_model(file_util::path_join(
        SERIALIZED_ZOO, "onnx/conv_with_strides_and_asymmetric_padding.prototxt"));
240 241

    // (1, 1, 4, 2)
242 243 244
    auto expected_output =
        test::NDArray<float, 4>({{{{21.f, 33.f}, {99.f, 117.f}, {189.f, 207.f}, {171.f, 183.f}}}})
            .get_vector();
245

246
    auto result = conv2d_execute(function);
247 248 249
    EXPECT_EQ(expected_output, result.front());
}

250
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_average_pool_2d)
251 252
{
    // Pooling with strides=2 and no padding
253
    auto function = onnx_import::import_onnx_model(
254
        file_util::path_join(SERIALIZED_ZOO, "onnx/average_pool_2d.prototxt"));
255 256 257 258 259 260 261 262 263 264 265 266

    // input data shape (1, 1, 4, 4)
    Inputs inputs;
    inputs.push_back(test::NDArray<float, 4>({{{{0.f, 1.f, 2.f, 3.f},
                                                {4.f, 5.f, 6.f, 7.f},
                                                {8.f, 9.f, 10.f, 11.f},
                                                {12.f, 13.f, 14.f, 15.f}}}})
                         .get_vector());

    // (1, 1, 2, 2)
    auto expected_output = test::NDArray<float, 4>({{{{2.5f, 4.5f}, {10.5f, 12.5f}}}}).get_vector();

267
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
268 269 270 271

    EXPECT_EQ(expected_output, outputs.front());
}

272
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_average_pool_2d_pads)
273 274
{
    // Pooling with strides=2 and padding=1
275
    auto function = onnx_import::import_onnx_model(
276
        file_util::path_join(SERIALIZED_ZOO, "onnx/average_pool_2d_pads.prototxt"));
277 278 279 280 281 282 283 284 285 286 287 288 289 290

    // input data shape (1, 1, 4, 4)
    Inputs inputs;
    inputs.push_back(test::NDArray<float, 4>({{{{0.f, 1.f, 2.f, 3.f},
                                                {4.f, 5.f, 6.f, 7.f},
                                                {8.f, 9.f, 10.f, 11.f},
                                                {12.f, 13.f, 14.f, 15.f}}}})
                         .get_vector());

    // (1, 1, 3, 3)
    auto expected_output =
        test::NDArray<float, 4>({{{{0.f, 1.5f, 3.f}, {6.f, 7.5f, 9.f}, {12.f, 13.5f, 15.f}}}})
            .get_vector();

291
    Outputs outputs = execute(function, inputs, "${BACKEND_NAME}");
292 293 294 295

    EXPECT_EQ(expected_output, outputs.front());
}

296
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_max_pool_2d_pads)
297 298
{
    // Pooling with strides=2 and padding=1
299
    auto function = onnx_import::import_onnx_model(
300
        file_util::path_join(SERIALIZED_ZOO, "onnx/max_pool_2d_pads.prototxt"));
301 302 303 304 305 306 307 308 309 310 311 312 313 314

    // input data shape (1, 1, 4, 4)
    Inputs inputs;
    inputs.push_back(test::NDArray<float, 4>({{{{0.f, 1.f, 2.f, 3.f},
                                                {4.f, 5.f, 6.f, 7.f},
                                                {8.f, 9.f, 10.f, 11.f},
                                                {12.f, 13.f, 14.f, 15.f}}}})
                         .get_vector());

    // (1, 1, 3, 3)
    auto expected_output =
        test::NDArray<float, 4>({{{{0.f, 2.f, 3.f}, {8.f, 10.f, 11.f}, {12.f, 14.f, 15.f}}}})
            .get_vector();

315
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
316 317 318 319

    EXPECT_EQ(expected_output, outputs.front());
}

320
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_batchnorm_default)
321 322
{
    // Batch Normalization with default parameters
323
    auto function = onnx_import::import_onnx_model(
324
        file_util::path_join(SERIALIZED_ZOO, "onnx/batchnorm_default.prototxt"));
325

326
    Inputs inputs;
327 328

    // input data shape (1, 2, 1, 3)
329 330
    inputs.push_back(
        test::NDArray<float, 4>({{{{-1.f, 0.f, 1.f}}, {{2.f, 3.f, 4.f}}}}).get_vector());
331 332

    // scale (3)
333
    inputs.emplace_back(std::vector<float>{1.f, 1.5f});
334
    // bias (3)
335
    inputs.emplace_back(std::vector<float>{0.f, 1.f});
336
    // mean (3)
337
    inputs.emplace_back(std::vector<float>{0.f, 3.f});
338
    // var (3)
339
    inputs.emplace_back(std::vector<float>{1.f, 1.5f});
340 341

    // shape (1, 2, 1, 3)
342 343 344
    Outputs expected_outputs{test::NDArray<float, 4>{
        {{{{-0.999995f, 0.f, 0.999995f}}, {{-0.22474074f, 1.f, 2.2247407f}}}}}
                                 .get_vector()};
345

346
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
347
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
348
}
349

350
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_relu)
351 352
{
    // Simple ReLU test
353
    auto function =
354
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/relu.prototxt"));
355

356 357
    Inputs inputs{{-1, -2, 0, 1, 2, 3}};
    Outputs expected_outputs{{0, 0, 0, 1, 2, 3}};
358

359
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
360
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
361
}
tsocha's avatar
tsocha committed
362

363
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_sum)
364 365
{
    // Simple Sum test
366
    auto function =
367
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/sum.prototxt"));
368 369 370 371 372 373 374 375

    // input data shape (3, )
    Inputs inputs;
    inputs.emplace_back(std::vector<float>{3.f, 0.f, 2.f});
    inputs.emplace_back(std::vector<float>{1.f, 3.f, 4.f});
    inputs.emplace_back(std::vector<float>{2.f, 6.f, 6.f});

    Outputs expected_outputs{{6.f, 9.f, 12.f}};
376
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
377 378 379
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

380
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_sum_one_input)
381
{
382
    auto function = onnx_import::import_onnx_model(
383
        file_util::path_join(SERIALIZED_ZOO, "onnx/sum_one_input.prototxt"));
384 385 386 387

    // input data shape (3, )
    Inputs inputs{{3.f, 0.f, 2.f}};
    Outputs expected_outputs{{3.f, 0.f, 2.f}};
388
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
389 390 391
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

392
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_min_two_inputs)
393
{
394
    auto function = onnx_import::import_onnx_model(
395
        file_util::path_join(SERIALIZED_ZOO, "onnx/min_two_inputs.prototxt"));
396 397 398 399 400 401 402

    // input data shape (3, )
    Inputs inputs;
    inputs.emplace_back(std::vector<float>{1.f, 2.f, 1.f});
    inputs.emplace_back(std::vector<float>{1.f, 4.f, 4.f});

    Outputs expected_outputs{{1.f, 2.f, 1.f}};
403
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
404 405 406
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

407
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_max)
408
{
409
    auto function =
410
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/max.prototxt"));
411 412 413 414 415 416 417 418

    // input data shape (3, )
    Inputs inputs;
    inputs.emplace_back(std::vector<float>{3.f, 2.f, 1.f});
    inputs.emplace_back(std::vector<float>{1.f, 4.f, 4.f});
    inputs.emplace_back(std::vector<float>{2.f, 5.f, 3.f});

    Outputs expected_outputs{{3.f, 5.f, 4.f}};
419
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
420 421 422
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

423
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_mean)
424
{
425
    auto function =
426
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/mean.prototxt"));
427 428 429 430 431 432 433 434

    // input data shape (3, )
    Inputs inputs;
    inputs.emplace_back(std::vector<float>{3.f, 0.f, 2.f});
    inputs.emplace_back(std::vector<float>{1.f, 3.f, 4.f});
    inputs.emplace_back(std::vector<float>{2.f, 6.f, 6.f});

    Outputs expected_outputs{{2.f, 3.f, 4.f}};
435
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
436 437 438
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

439
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_gemm_abc)
tsocha's avatar
tsocha committed
440
{
441 442
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/gemm_abc.prototxt"));
tsocha's avatar
tsocha committed
443

444
    Inputs inputs;
445
    inputs.emplace_back(test::NDArray<float, 2>(
tsocha's avatar
tsocha committed
446 447 448
                            {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18}})
                            .get_vector());

449 450 451 452 453 454
    inputs.emplace_back(test::NDArray<float, 2>({{19, 20, 21, 22},
                                                 {23, 24, 25, 26},
                                                 {27, 28, 29, 30},
                                                 {31, 32, 33, 34},
                                                 {35, 36, 37, 38},
                                                 {39, 40, 41, 42}})
tsocha's avatar
tsocha committed
455 456 457
                            .get_vector());

    inputs.emplace_back(
458
        test::NDArray<float, 2>({{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}).get_vector());
tsocha's avatar
tsocha committed
459

460
    Outputs expected_outputs{
461
        test::NDArray<float, 2>(
tsocha's avatar
tsocha committed
462
            {{340, 350.5, 361, 371.5}, {862, 890.5, 919, 947.5}, {1384, 1430.5, 1477, 1523.5}})
463
            .get_vector()};
tsocha's avatar
tsocha committed
464

465
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
466
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
tsocha's avatar
tsocha committed
467
}
468

469
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_matmul)
470
{
471 472
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/matmul.prototxt"));
473 474 475 476 477 478 479 480 481 482

    std::vector<std::vector<float>> inputs;

    inputs.emplace_back(
        test::NDArray<float, 2>({{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}).get_vector());

    inputs.emplace_back(
        test::NDArray<float, 2>({{13, 14, 15}, {16, 17, 18}, {19, 20, 21}, {22, 23, 24}})
            .get_vector());

483 484
    Outputs expected_outputs{
        test::NDArray<float, 2>({{190, 200, 210}, {470, 496, 522}, {750, 792, 834}}).get_vector()};
485

486
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
487
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
488
}
489

490
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_softmax)
491
{
492 493
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/softmax.prototxt"));
494 495 496

    Inputs inputs;
    inputs.emplace_back(
497
        test::NDArray<float, 3>(
498 499 500 501 502 503 504 505 506 507 508 509 510 511
            {{{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}}})
            .get_vector());

    auto expected_output =
512
        test::NDArray<float, 3>(
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
            {{{1.50461533e-26f, 4.08996852e-26f, 1.11176871e-25f, 3.02210068e-25f, 8.21492137e-25f},
              {2.23304715e-24f, 6.07005148e-24f, 1.65001106e-23f, 4.48519509e-23f, 1.21920243e-22f},
              {3.31413582e-22f, 9.00875516e-22f, 2.44883355e-21f, 6.65661973e-21f, 1.80945684e-20f},
              {4.91861366e-20f,
               1.33701781e-19f,
               3.63439123e-19f,
               9.87929963e-19f,
               2.68547207e-18f}},

             {{7.29986992e-18f, 1.98431037e-17f, 5.39391483e-17f, 1.46621807e-16f, 3.98559393e-16f},
              {1.08339676e-15f, 2.94497771e-15f, 8.00527940e-15f, 2.17606055e-14f, 5.91514586e-14f},
              {1.60790335e-13f, 4.37073446e-13f, 1.18808881e-12f, 3.22956021e-12f, 8.77885484e-12f},
              {2.38634016e-11f,
               6.48674509e-11f,
               1.76328013e-10f,
               4.79309234e-10f,
               1.30289758e-09f}},

             {{3.54164282e-09f, 9.62718331e-09f, 2.61693974e-08f, 7.11357975e-08f, 1.93367146e-07f},
              {5.25626399e-07f, 1.42880069e-06f, 3.88388295e-06f, 1.05574884e-05f, 2.86982290e-05f},
              {7.80098743e-05f, 2.12052824e-04f, 5.76419338e-04f, 1.56687021e-03f, 4.25919482e-03f},
              {1.15776919e-02f,
               3.14714295e-02f,
               8.55482149e-02f,
               2.32544158e-01f,
               6.32120559e-01f}}})
539 540
            .get_vector();

541
    auto result_vectors = execute(function, inputs, "${BACKEND_NAME}");
542 543
    EXPECT_TRUE(test::all_close_f(expected_output, result_vectors.front()));
}
544

545
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_concat)
546
{
547 548
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/concat.prototxt"));
549 550 551 552 553 554 555 556

    Inputs inputs;

    inputs.emplace_back(test::NDArray<float, 1>({1, 2}).get_vector());
    inputs.emplace_back(test::NDArray<float, 1>({3, 4}).get_vector());

    Outputs expected_outputs{test::NDArray<float, 1>({1, 2, 3, 4}).get_vector()};

557
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
558 559 560
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

561
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_flatten)
562
{
563 564
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/flatten.prototxt"));
565 566 567 568 569 570 571 572

    Inputs inputs;

    inputs.emplace_back(
        test::NDArray<float, 4>({{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}}).get_vector());

    Outputs expected_outputs{test::NDArray<float, 3>({{{1, 2, 3, 4}, {5, 6, 7, 8}}}).get_vector()};

573
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
574 575 576
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

577
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_sub)
578
{
579
    auto function =
580
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/sub.prototxt"));
581 582

    Inputs inputs;
583
    inputs.emplace_back(test::NDArray<float, 3>({{{1, 2, 3}}}).get_vector());
584

585
    inputs.emplace_back(test::NDArray<float, 3>({{{4, 5, 7}}}).get_vector());
586

587
    auto expected_output = test::NDArray<float, 3>({{{-3, -3, -4}}}).get_vector();
588

589
    auto result_vectors = execute(function, inputs, "${BACKEND_NAME}");
590 591 592
    EXPECT_TRUE(test::all_close_f(expected_output, result_vectors.front()));
}

593
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_unsqueeze)
594
{
595 596
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/unsqueeze.prototxt"));
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611

    Inputs inputs;
    inputs.emplace_back(test::NDArray<float, 3>(
                            {{{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}},
                             {{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}},
                             {{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}}})
                            .get_vector());

    Outputs expected_output{
        test::NDArray<float, 4>(
            {{{{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}},
              {{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}},
              {{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}}}})
            .get_vector()};

612
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
613 614 615
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

616
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_squeeze)
617
{
618
    auto function = onnx_import::import_onnx_model(
619
        file_util::path_join(SERIALIZED_ZOO, "onnx/squeeze_duplicate_axes.prototxt"));
620 621 622 623 624 625 626 627 628 629 630

    // {1, 4, 1, 1, 2}
    Inputs inputs{test::NDArray<float, 5>(
                      {{{{{1.0f, 2.0f}}}, {{{3.0f, 4.0f}}}, {{{5.0f, 6.0f}}}, {{{7.0f, 8.0f}}}}})
                      .get_vector()};

    // {4, 2}
    Outputs expected_output{
        test::NDArray<float, 2>({{1.0f, 2.0f}, {3.0f, 4.0f}, {5.0f, 6.0f}, {7.0f, 8.0f}})
            .get_vector()};

631
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
632 633 634
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

635
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_div)
636
{
637
    auto function =
638
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/div.prototxt"));
639 640

    Inputs inputs;
641
    inputs.emplace_back(test::NDArray<float, 3>({{{1, 2, 3}}}).get_vector());
642

643
    inputs.emplace_back(test::NDArray<float, 3>({{{1, 4, 12}}}).get_vector());
644

645
    auto expected_output = test::NDArray<float, 3>({{{1, 0.5, 0.25}}}).get_vector();
646

647
    auto result_vectors = execute(function, inputs, "${BACKEND_NAME}");
648 649
    EXPECT_TRUE(test::all_close_f(expected_output, result_vectors.front()));
}
650

651
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_add_bcast)
652
{
653 654
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/add_bcast.prototxt"));
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671

    Inputs inputs;
    inputs.emplace_back(test::NDArray<float, 3>(
                            {{{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}},
                             {{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}},
                             {{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}}})
                            .get_vector());

    inputs.emplace_back(test::NDArray<float, 1>({1, 2, 3, 4, 5}).get_vector());

    Outputs expected_output{
        test::NDArray<float, 4>(
            {{{{2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}},
              {{2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}},
              {{2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}}}})
            .get_vector()};

672
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
673 674 675
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

676
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reshape_reduced_dims)
677
{
678
    auto function = onnx_import::import_onnx_model(
679
        file_util::path_join(SERIALIZED_ZOO, "onnx/reshape_reduced_dims.prototxt"));
680 681 682 683 684 685 686 687 688 689 690 691

    // input data shape (2, 3, 4)
    Inputs inputs{test::NDArray<float, 3>({{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}},
                                           {{12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23}}})
                      .get_vector()};

    // output data shape (2, 12)
    Outputs expected_outputs{
        test::NDArray<float, 2>({{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
                                 {12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}})
            .get_vector()};

692
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
693 694 695
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

696
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reshape_reordered_dims)
697
{
698
    auto function = onnx_import::import_onnx_model(
699
        file_util::path_join(SERIALIZED_ZOO, "onnx/reshape_reordered_dims.prototxt"));
700 701 702 703 704 705 706 707 708 709 710 711 712

    // input data shape (2, 3, 4)
    Inputs inputs{test::NDArray<float, 3>({{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}},
                                           {{12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23}}})
                      .get_vector()};

    // output data shape (4, 2, 3)
    Outputs expected_outputs{test::NDArray<float, 3>({{{0, 1, 2}, {3, 4, 5}},
                                                      {{6, 7, 8}, {9, 10, 11}},
                                                      {{12, 13, 14}, {15, 16, 17}},
                                                      {{18, 19, 20}, {21, 22, 23}}})
                                 .get_vector()};

713
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
714 715 716
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

717
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reshape_extended_dims)
718
{
719
    auto function = onnx_import::import_onnx_model(
720
        file_util::path_join(SERIALIZED_ZOO, "onnx/reshape_extended_dims.prototxt"));
721 722 723 724 725 726 727 728 729 730 731 732

    // input data shape (2, 3, 4)
    Inputs inputs{test::NDArray<float, 3>({{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}},
                                           {{12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23}}})
                      .get_vector()};

    // output data shape (3, 2, 2, 2)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{0, 1}, {2, 3}}, {{4, 5}, {6, 7}}},
                                                      {{{8, 9}, {10, 11}}, {{12, 13}, {14, 15}}},
                                                      {{{16, 17}, {18, 19}}, {{20, 21}, {22, 23}}}})
                                 .get_vector()};

733
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
734 735 736
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

737
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reshape_single_dim)
738
{
739
    auto function = onnx_import::import_onnx_model(
740
        file_util::path_join(SERIALIZED_ZOO, "onnx/reshape_single_dim.prototxt"));
741 742 743 744 745 746 747 748 749 750 751 752

    // input data shape (2, 3, 4)
    Inputs inputs{test::NDArray<float, 3>({{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}},
                                           {{12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23}}})
                      .get_vector()};

    // output data shape (24, )
    Outputs expected_outputs{
        test::NDArray<float, 1>(
            {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23})
            .get_vector()};

753
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
754 755 756
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

757
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reshape_negative_dim)
758
{
759
    auto function = onnx_import::import_onnx_model(
760
        file_util::path_join(SERIALIZED_ZOO, "onnx/reshape_negative_dim.prototxt"));
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

    // input data shape (2, 3, 4)
    Inputs inputs{test::NDArray<float, 3>({{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}},
                                           {{12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23}}})
                      .get_vector()};

    // output data shape (6, 2, 2)
    Outputs expected_outputs{test::NDArray<float, 3>({{{0, 1}, {2, 3}},
                                                      {{4, 5}, {6, 7}},
                                                      {{8, 9}, {10, 11}},
                                                      {{12, 13}, {14, 15}},
                                                      {{16, 17}, {18, 19}},
                                                      {{20, 21}, {22, 23}}})
                                 .get_vector()};

776
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
777 778 779
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

780
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reshape_negative_with_zero_dim)
781
{
782
    auto function = onnx_import::import_onnx_model(
783
        file_util::path_join(SERIALIZED_ZOO, "onnx/reshape_negative_with_zero_dims.prototxt"));
784 785 786 787 788 789 790 791 792 793 794 795

    // input data shape (2, 3, 4)
    Inputs inputs{test::NDArray<float, 3>({{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}},
                                           {{12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23}}})
                      .get_vector()};

    // output data shape (2, 6, 2)
    Outputs expected_outputs{
        test::NDArray<float, 3>({{{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}, {10, 11}},
                                 {{12, 13}, {14, 15}, {16, 17}, {18, 19}, {20, 21}, {22, 23}}})
            .get_vector()};

796
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
797 798 799
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

800
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reshape_output_shape_as_input)
801
{
802
    auto function = onnx_import::import_onnx_model(
803
        file_util::path_join(SERIALIZED_ZOO, "onnx/reshape_output_shape_as_input.prototxt"));
804 805 806 807 808 809 810 811 812 813 814 815

    // input data shape (2, 3, 4)
    Inputs inputs{test::NDArray<float, 3>({{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}},
                                           {{12, 13, 14, 15}, {16, 17, 18, 19}, {20, 21, 22, 23}}})
                      .get_vector()};

    // output data shape (2, 6, 2)
    Outputs expected_outputs{
        test::NDArray<float, 3>({{{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}, {10, 11}},
                                 {{12, 13}, {14, 15}, {16, 17}, {18, 19}, {20, 21}, {22, 23}}})
            .get_vector()};

816
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
817 818
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
tsocha's avatar
tsocha committed
819

820
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reduce_log_sum)
821
{
822
    auto function = onnx_import::import_onnx_model(
823
        file_util::path_join(SERIALIZED_ZOO, "onnx/reduce_log_sum.prototxt"));
824 825 826 827 828 829 830 831 832

    // input data shape (1, 1, 4, 4)
    Inputs inputs{
        test::NDArray<float, 4>({{{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}}})
            .get_vector()};

    // output data shape (1,)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{2.77258872f}}}}).get_vector()};

833
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
834 835 836
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

837
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reduce_log_sum_exp)
838
{
839
    auto function = onnx_import::import_onnx_model(
840
        file_util::path_join(SERIALIZED_ZOO, "onnx/reduce_log_sum_exp.prototxt"));
841 842 843 844 845 846 847 848 849

    // input data shape (1, 1, 4, 4)
    Inputs inputs{
        test::NDArray<float, 4>({{{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}}})
            .get_vector()};

    // output data shape (1,)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{3.77258872f}}}}).get_vector()};

850
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
851 852 853
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

854
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reduce_l1)
855
{
856 857
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/reduce_l1.prototxt"));
858 859 860 861 862 863 864 865 866

    // input data shape (1, 1, 4, 4)
    Inputs inputs{
        test::NDArray<float, 4>({{{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}}})
            .get_vector()};

    // output data shape (1,)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{16}}}}).get_vector()};

867
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
868 869 870
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

871
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reduce_l2)
872
{
873 874
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/reduce_l2.prototxt"));
875 876 877 878 879 880 881 882 883

    // input data shape (1, 1, 4, 4)
    Inputs inputs{
        test::NDArray<float, 4>({{{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}}})
            .get_vector()};

    // output data shape (1,)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{4}}}}).get_vector()};

884
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
885 886 887
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

888
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reduce_max)
889
{
890
    auto function = onnx_import::import_onnx_model(
891
        file_util::path_join(SERIALIZED_ZOO, "onnx/reduce_max.prototxt"));
892 893 894 895 896 897 898 899 900

    // input data shape (1, 1, 4, 4)
    Inputs inputs{
        test::NDArray<float, 4>({{{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}}})
            .get_vector()};

    // output data shape (1,)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{16}}}}).get_vector()};

901
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
902 903 904
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

905
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reduce_mean)
906
{
907
    auto function = onnx_import::import_onnx_model(
908
        file_util::path_join(SERIALIZED_ZOO, "onnx/reduce_mean.prototxt"));
909 910 911 912 913 914 915 916 917

    // input data shape (1, 1, 4, 4)
    Inputs inputs{
        test::NDArray<float, 4>({{{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}}})
            .get_vector()};

    // output data shape (1,)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{1}}}}).get_vector()};

918
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
919 920 921
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

922
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reduce_min)
923
{
924
    auto function = onnx_import::import_onnx_model(
925
        file_util::path_join(SERIALIZED_ZOO, "onnx/reduce_min.prototxt"));
926 927 928 929 930 931 932 933 934

    // input data shape (1, 1, 4, 4)
    Inputs inputs{
        test::NDArray<float, 4>({{{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}}})
            .get_vector()};

    // output data shape (1,)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{1}}}}).get_vector()};

935
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
936 937 938
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

939
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reduce_prod)
940
{
941
    auto function = onnx_import::import_onnx_model(
942
        file_util::path_join(SERIALIZED_ZOO, "onnx/reduce_prod.prototxt"));
943 944 945 946 947 948 949 950 951

    // input data shape (1, 1, 4, 4)
    Inputs inputs{
        test::NDArray<float, 4>({{{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}}})
            .get_vector()};

    // output data shape (1,)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{1}}}}).get_vector()};

952
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
953 954 955
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

956
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reduce_sum)
957
{
958
    auto function = onnx_import::import_onnx_model(
959
        file_util::path_join(SERIALIZED_ZOO, "onnx/reduce_sum.prototxt"));
960 961 962 963 964 965 966 967 968

    // input data shape (1, 1, 4, 4)
    Inputs inputs{
        test::NDArray<float, 4>({{{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}}})
            .get_vector()};

    // output data shape (1,)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{16}}}}).get_vector()};

969
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
970 971 972
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

973
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_reduce_sum_square)
974
{
975
    auto function = onnx_import::import_onnx_model(
976
        file_util::path_join(SERIALIZED_ZOO, "onnx/reduce_sum_square.prototxt"));
977 978 979 980 981 982 983 984 985

    // input data shape (1, 1, 4, 4)
    Inputs inputs{
        test::NDArray<float, 4>({{{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}}})
            .get_vector()};

    // output data shape (1,)
    Outputs expected_outputs{test::NDArray<float, 4>({{{{16}}}}).get_vector()};

986
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
987 988 989
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

990
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_shape)
tsocha's avatar
tsocha committed
991 992
{
    auto function =
993
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/shape.prototxt"));
tsocha's avatar
tsocha committed
994 995 996 997 998 999 1000 1001 1002 1003 1004

    Inputs inputs;
    inputs.emplace_back(test::NDArray<float, 3>(
                            {{{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}},
                             {{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}},
                             {{1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1}}})
                            .get_vector());

    std::vector<std::vector<int64_t>> expected_output{{3, 4, 5}};

    std::vector<std::vector<int64_t>> outputs =
1005
        execute<float, int64_t>(function, inputs, "${BACKEND_NAME}");
tsocha's avatar
tsocha committed
1006 1007
    EXPECT_TRUE(test::all_close(expected_output.front(), outputs.front()));
}
1008

1009
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_elu)
1010 1011
{
    auto function =
1012
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/elu.prototxt"));
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051

    Inputs inputs;
    inputs.emplace_back(
        test::NDArray<float, 3>(
            {{{-9, -8, -7, -6, -5}, {-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}},
             {{-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}},
             {{1, 1, 1, 1, 1}, {-1, -1, -1, -1, -1}, {0, 0, 0, 0, 0}, {2, 2, 2, 2, 2}}})
            .get_vector());

    Outputs expected_output{test::NDArray<float, 3>({{{-1.999753180391830f,
                                                       -1.999329074744190f,
                                                       -1.998176236068890f,
                                                       -1.995042495646670f,
                                                       -1.986524106001830f},
                                                      {-1.963368722222530f,
                                                       -1.900425863264270f,
                                                       -1.729329433526770f,
                                                       -1.264241117657120f,
                                                       0},
                                                      {1, 2, 3, 4, 5},
                                                      {6, 7, 8, 9, 10}},
                                                     {{-1.963368722222530f,
                                                       -1.900425863264270f,
                                                       -1.729329433526770f,
                                                       -1.264241117657120f,
                                                       0},
                                                      {1, 2, 3, 4, 5},
                                                      {6, 7, 8, 9, 10},
                                                      {11, 12, 13, 14, 15}},
                                                     {{1, 1, 1, 1, 1},
                                                      {-1.264241117657120f,
                                                       -1.264241117657120f,
                                                       -1.264241117657120f,
                                                       -1.264241117657120f,
                                                       -1.264241117657120f},
                                                      {0, 0, 0, 0, 0},
                                                      {2, 2, 2, 2, 2}}})
                                .get_vector()};

1052
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1053 1054 1055
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1056
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_leaky_relu)
1057
{
1058
    auto function = onnx_import::import_onnx_model(
1059
        file_util::path_join(SERIALIZED_ZOO, "onnx/leaky_relu.prototxt"));
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

    Inputs inputs;
    inputs.emplace_back(
        test::NDArray<float, 3>(
            {{{-9, -8, -7, -6, -5}, {-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}},
             {{-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}},
             {{1, 1, 1, 1, 1}, {-1, -1, -1, -1, -1}, {0, 0, 0, 0, 0}, {2, 2, 2, 2, 2}}})
            .get_vector());

    Outputs expected_output{test::NDArray<float, 3>({{{-0.9f, -0.8f, -0.7f, -0.6f, -0.5f},
                                                      {-0.4f, -0.3f, -0.2f, -0.1f, 0},
                                                      {1, 2, 3, 4, 5},
                                                      {6, 7, 8, 9, 10}},
                                                     {{-0.4f, -0.3f, -0.2f, -0.1f, 0},
                                                      {1, 2, 3, 4, 5},
                                                      {6, 7, 8, 9, 10},
                                                      {11, 12, 13, 14, 15}},
                                                     {{1, 1, 1, 1, 1},
                                                      {-0.1f, -0.1f, -0.1f, -0.1f, -0.1f},
                                                      {0, 0, 0, 0, 0},
                                                      {2, 2, 2, 2, 2}}})
                                .get_vector()};

1083
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1084 1085 1086
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1087
NGRAPH_TEST(onnx_${BACKEND_NAME}, prelu)
1088 1089
{
    auto function =
1090
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/prelu.prototxt"));
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

    Inputs inputs;
    inputs.emplace_back(
        test::NDArray<float, 3>(
            {{{-9, -8, -7, -6, -5}, {-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}},
             {{-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}},
             {{1, 1, 1, 1, 1}, {-1, -1, -1, -1, -1}, {0, 0, 0, 0, 0}, {2, 2, 2, 2, 2}}})
            .get_vector());

    inputs.emplace_back(test::NDArray<float, 3>(
                            {{{1, 0, 1, 0, 1}, {0, 1, 0, 1, 0}, {1, 0, 1, 0, 1}, {0, 1, 0, 1, 0}},
                             {{0, 1, 0, 1, 0}, {1, 0, 1, 0, 1}, {0, 1, 0, 1, 0}, {1, 0, 1, 0, 1}},
                             {{1, 0, 1, 0, 1}, {0, 1, 0, 1, 0}, {1, 0, 1, 0, 1}, {0, 1, 0, 1, 0}}})
                            .get_vector());

    Outputs expected_output{
        test::NDArray<float, 3>(
            {{{-9, 0, -7, 0, -5}, {0, -3, 0, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}},
             {{0, -3, 0, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}},
             {{1, 1, 1, 1, 1}, {0, -1, 0, -1, 0}, {0, 0, 0, 0, 0}, {2, 2, 2, 2, 2}}})
            .get_vector()};

1113
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1114 1115 1116
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1117
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_selu)
1118 1119
{
    auto function =
1120
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/selu.prototxt"));
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

    Inputs inputs;
    inputs.emplace_back(
        test::NDArray<float, 3>(
            {{{-9, -8, -7, -6, -5}, {-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}},
             {{-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}},
             {{1, 1, 1, 1, 1}, {-1, -1, -1, -1, -1}, {0, 0, 0, 0, 0}, {2, 2, 2, 2, 2}}})
            .get_vector());

    Outputs expected_output{
        test::NDArray<float, 3>(
            {{{-5.99925954117548f,
               -5.99798722423258f,
               -5.99452870820667f,
               -5.98512748694000f,
               -5.95957231800549f},
              {-5.89010616666759f, -5.70127758979282f, -5.18798830058032f, -3.79272335297135f, 0},
              {3, 6, 9, 12, 15},
              {18, 21, 24, 27, 30}},
             {{-5.89010616666759f, -5.70127758979282f, -5.18798830058032f, -3.79272335297135f, 0},
              {3, 6, 9, 12, 15},
              {18, 21, 24, 27, 30},
              {33, 36, 39, 42, 45}},
             {{3, 3, 3, 3, 3},
              {-3.79272335297135f,
               -3.79272335297135f,
               -3.79272335297135f,
               -3.79272335297135f,
               -3.79272335297135f},
              {0, 0, 0, 0, 0},
              {6, 6, 6, 6, 6}}})
            .get_vector()};

1154
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1155 1156 1157
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1158
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_sigmoid)
1159
{
1160 1161
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/sigmoid.prototxt"));
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

    Inputs inputs;
    inputs.emplace_back(
        test::NDArray<float, 3>(
            {{{-9, -8, -7, -6, -5}, {-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}},
             {{-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}},
             {{1, 1, 1, 1, 1}, {-1, -1, -1, -1, -1}, {0, 0, 0, 0, 0}, {2, 2, 2, 2, 2}}})
            .get_vector());

    Outputs expected_output{test::NDArray<float, 3>({{{0.00012339457598623f,
                                                       0.00033535013046648f,
                                                       0.00091105119440065f,
                                                       0.00247262315663477f,
                                                       0.00669285092428486f},
                                                      {0.01798620996209160f,
                                                       0.04742587317756680f,
                                                       0.119202922022118f,
                                                       0.268941421369995f,
                                                       0.5f},
                                                      {0.731058578630005f,
                                                       0.880797077977882f,
                                                       0.952574126822433f,
                                                       0.982013790037908f,
                                                       0.993307149075715f},
                                                      {0.997527376843365f,
                                                       0.999088948805599f,
                                                       0.999664649869534f,
                                                       0.999876605424014f,
                                                       0.999954602131298f}},
                                                     {{0.01798620996209160f,
                                                       0.04742587317756680f,
                                                       0.119202922022118f,
                                                       0.268941421369995f,
                                                       0.5f},
                                                      {0.731058578630005f,
                                                       0.880797077977882f,
                                                       0.952574126822433f,
                                                       0.982013790037908f,
                                                       0.993307149075715f},
                                                      {0.997527376843365f,
                                                       0.999088948805599f,
                                                       0.999664649869534f,
                                                       0.999876605424014f,
                                                       0.999954602131298f},
                                                      {0.999983298578152f,
                                                       0.999993855825398f,
                                                       0.999997739675702f,
                                                       0.999999168471972f,
                                                       0.999999694097773f}},
                                                     {{0.731058578630005f,
                                                       0.731058578630005f,
                                                       0.731058578630005f,
                                                       0.731058578630005f,
                                                       0.731058578630005f},
                                                      {0.268941421369995f,
                                                       0.268941421369995f,
                                                       0.268941421369995f,
                                                       0.268941421369995f,
                                                       0.268941421369995f},
                                                      {0.5f, 0.5f, 0.5f, 0.5f, 0.5f},
                                                      {0.880797077977882f,
                                                       0.880797077977882f,
                                                       0.880797077977882f,
                                                       0.880797077977882f,
                                                       0.880797077977882f}}})
                                .get_vector()};

1229
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1230 1231 1232
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1233
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_tanh)
1234 1235
{
    auto function =
1236
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/tanh.prototxt"));
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303

    Inputs inputs;
    inputs.emplace_back(
        test::NDArray<float, 3>(
            {{{-9, -8, -7, -6, -5}, {-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}},
             {{-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}},
             {{1, 1, 1, 1, 1}, {-1, -1, -1, -1, -1}, {0, 0, 0, 0, 0}, {2, 2, 2, 2, 2}}})
            .get_vector());

    Outputs expected_output{test::NDArray<float, 3>({{{-0.999999969540041f,
                                                       -0.999999774929676f,
                                                       -0.999998336943945f,
                                                       -0.999987711650796f,
                                                       -0.999909204262595f},
                                                      {-0.999329299739067f,
                                                       -0.995054753686731f,
                                                       -0.964027580075817f,
                                                       -0.761594155955765f,
                                                       0},
                                                      {0.761594155955765f,
                                                       0.964027580075817f,
                                                       0.995054753686731f,
                                                       0.999329299739067f,
                                                       0.999909204262595f},
                                                      {0.999987711650796f,
                                                       0.999998336943945f,
                                                       0.999999774929676f,
                                                       0.999999969540041f,
                                                       0.999999995877693f}},
                                                     {{-0.999329299739067f,
                                                       -0.995054753686731f,
                                                       -0.964027580075817f,
                                                       -0.761594155955765f,
                                                       0},
                                                      {0.761594155955765f,
                                                       0.964027580075817f,
                                                       0.995054753686731f,
                                                       0.999329299739067f,
                                                       0.999909204262595f},
                                                      {0.999987711650796f,
                                                       0.999998336943945f,
                                                       0.999999774929676f,
                                                       0.999999969540041f,
                                                       0.999999995877693f},
                                                      {0.999999999442106f,
                                                       0.999999999924497f,
                                                       0.999999999989782f,
                                                       0.999999999998617f,
                                                       0.999999999999813f}},
                                                     {{0.761594155955765f,
                                                       0.761594155955765f,
                                                       0.761594155955765f,
                                                       0.761594155955765f,
                                                       0.761594155955765f},
                                                      {-0.761594155955765f,
                                                       -0.761594155955765f,
                                                       -0.761594155955765f,
                                                       -0.761594155955765f,
                                                       -0.761594155955765f},
                                                      {0, 0, 0, 0, 0},
                                                      {0.964027580075817f,
                                                       0.964027580075817f,
                                                       0.964027580075817f,
                                                       0.964027580075817f,
                                                       0.964027580075817f}}})
                                .get_vector()};

1304
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1305 1306 1307
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1308
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_thresholded_relu)
1309
{
1310
    auto function = onnx_import::import_onnx_model(
1311
        file_util::path_join(SERIALIZED_ZOO, "onnx/thresholded_relu.prototxt"));
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327

    Inputs inputs;
    inputs.emplace_back(
        test::NDArray<float, 3>(
            {{{-9, -8, -7, -6, -5}, {-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}},
             {{-4, -3, -2, -1, 0}, {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}},
             {{1, 1, 1, 1, 1}, {-1, -1, -1, -1, -1}, {0, 0, 0, 0, 0}, {2, 2, 2, 2, 2}}})
            .get_vector());

    Outputs expected_output{
        test::NDArray<float, 3>(
            {{{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 3, 4, 5}, {6, 7, 8, 9, 10}},
             {{0, 0, 0, 0, 0}, {0, 0, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}},
             {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}}})
            .get_vector()};

1328
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1329 1330
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
1331

1332
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_unsupported_op)
1333 1334 1335
{
    try
    {
1336
        onnx_import::import_onnx_model(
1337
            file_util::path_join(SERIALIZED_ZOO, "onnx/unsupported_op.prototxt"));
1338 1339 1340 1341 1342
        FAIL() << "Expected ngraph::ngraph_error";
    }
    catch (ngraph::ngraph_error const& err)
    {
        std::string what{err.what()};
1343
        EXPECT_NE(what.find("nGraph does not support"), std::string::npos);
1344 1345 1346 1347 1348 1349 1350 1351
        EXPECT_NE(what.find("FakeOpName"), std::string::npos);
        EXPECT_NE(what.find("AnotherFakeOpName"), std::string::npos);
    }
    catch (...)
    {
        FAIL() << "Expected ngraph::ngraph_error";
    }
}
1352

1353
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_custom_op)
1354 1355 1356 1357 1358 1359 1360
{
    onnx_import::register_operator(
        "AddQ", 1, "com.intel.ai", [](const onnx_import::Node& node) -> NodeVector {
            NodeVector ng_inputs{node.get_ng_inputs()};
            return {std::make_shared<ngraph::op::Add>(ng_inputs.at(0), ng_inputs.at(1))};
        });

1361
    auto function = onnx_import::import_onnx_model(
1362
        file_util::path_join(SERIALIZED_ZOO, "onnx/custom_operator.prototxt"));
1363 1364 1365 1366

    Inputs inputs{{1, 2, 3, 4}};
    Outputs expected_outputs{{3, 6, 9, 12}};

1367
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1368 1369 1370
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

1371
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_custom_op_default_domain)
1372 1373 1374 1375 1376 1377 1378
{
    onnx_import::register_operator(
        "AddQ", 1, "com.intel.ai", [](const onnx_import::Node& node) -> NodeVector {
            NodeVector ng_inputs{node.get_ng_inputs()};
            return {std::make_shared<ngraph::op::Add>(ng_inputs.at(0), ng_inputs.at(1))};
        });

1379
    auto function = onnx_import::import_onnx_model(
1380
        file_util::path_join(SERIALIZED_ZOO, "onnx/custom_operator_default_domain.prototxt"));
1381 1382 1383 1384

    Inputs inputs{{1, 2, 3, 4}};
    Outputs expected_outputs{{3, 6, 9, 12}};

1385
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1386 1387
    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
1388

1389
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_conv2d_dilation_assymetric_pads_strides)
1390
{
1391
    auto function = onnx_import::import_onnx_model(
1392
        file_util::path_join(SERIALIZED_ZOO, "onnx/conv2d_dilation_assym_pads_strides.prototxt"));
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424

    //   "",                           // auto_pad
    //   vector<int64_t>{1, 1},        // dilations
    //   1,                            // group
    //   vector<int64_t>{3, 3},        // kernel_shape
    //   vector<int64_t>{1, 1, 1, 2},  // pads
    //   vector<int64_t>{3, 1}         // strides

    Inputs inputs;
    // {2, 1, 1, 1}
    inputs.emplace_back(
        test::NDArray<float, 4>({{{{-0.09103918075561523f}}}, {{{-0.32513630390167236f}}}})
            .get_vector());
    // {2, 1, 3, 3}
    inputs.emplace_back(
        test::NDArray<float, 4>(
            {{{{0.4312484860420227f, -0.12559029459953308f, 0.44889551401138306f},
               {-0.3100617825984955f, 0.13522827625274658f, -0.06791308522224426f},
               {0.22671669721603394f, -0.17391827702522278f, -0.31299442052841187f}}},
             {{{-0.31545522809028625f, 0.06560015678405762f, 0.2656586766242981f},
               {0.41363757848739624f, 0.31231558322906494f, -0.376018226146698f},
               {-0.005708813667297363f, 0.34922850131988525f, 0.45095211267471313f}}}})
            .get_vector());

    // {2, 2, 1, 2}
    Outputs expected_output{
        test::NDArray<float, 4>({{{{-0.012311071157455444f, 0.02822777070105076f}},
                                  {{-0.028432954102754593f, -0.037657227367162704f}}},
                                 {{{-0.04396762326359749f, 0.10081233829259872f}},
                                  {{-0.10154513269662857f, -0.13448859751224518f}}}})
            .get_vector()};

1425
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1426 1427 1428
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1429
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_conv3d_bias)
1430
{
1431
    auto function = onnx_import::import_onnx_model(
1432
        file_util::path_join(SERIALIZED_ZOO, "onnx/conv3d_bias.prototxt"));
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538

    // "",                                 // auto_pad
    // vector<int64_t>{2, 2, 2},           // dilations
    // 1,                                  // group
    // vector<int64_t>{2, 2, 2},           // kernel_shape
    // vector<int64_t>{2, 2, 2, 2, 2, 2},  // pads
    // vector<int64_t>{2, 2, 2}            // strides

    Inputs inputs;
    // X: {2, 1, 4, 4, 4}
    inputs.emplace_back(
        std::vector<float>{0.46796226501464844f,   -0.4613912105560303f,  0.33512794971466064f,
                           -0.4010460674762726f,   0.41722816228866577f,  -0.048133403062820435f,
                           0.20415884256362915f,   0.03189706802368164f,  -0.04779183864593506f,
                           -0.0795503556728363f,   0.4987630844116211f,   0.3506373167037964f,
                           0.48065757751464844f,   0.269855260848999f,    -0.2463444471359253f,
                           0.19044137001037598f,   -0.11830493807792664f, -0.2576887905597687f,
                           -0.33940935134887695f,  -0.257951021194458f,   -0.08279827237129211f,
                           0.3513314127922058f,    -0.29122066497802734f, -0.43358397483825684f,
                           -0.13429927825927734f,  0.44032156467437744f,  0.05308258533477783f,
                           -0.3499870300292969f,   -0.28474611043930054f, -0.44209951162338257f,
                           -0.07418054342269897f,  -0.10919415950775146f, 0.2845439314842224f,
                           0.3498746156692505f,    -0.19313520193099976f, 0.32609254121780396f,
                           0.4880145788192749f,    0.05574071407318115f,  -0.46457427740097046f,
                           -0.02524462342262268f,  -0.18780940771102905f, -0.14720159769058228f,
                           0.207585871219635f,     0.47157740592956543f,  -0.05567386746406555f,
                           -0.49871665239334106f,  0.2274145483970642f,   0.4589425325393677f,
                           -0.4725189805030823f,   -0.4358765780925751f,  0.2841453552246094f,
                           -0.27037882804870605f,  0.34227508306503296f,  0.33575427532196045f,
                           -0.19485199451446533f,  -0.27679920196533203f, -0.4238079786300659f,
                           -0.4385119676589966f,   0.43724071979522705f,  0.3065117597579956f,
                           0.45696544647216797f,   0.05291992425918579f,  -0.023618370294570923f,
                           -0.1860884726047516f,   0.08669537305831909f,  0.32541000843048096f,
                           0.1846179962158203f,    -0.1984834372997284f,  -0.2754465937614441f,
                           0.32004624605178833f,   -0.34846532344818115f, 0.0999596118927002f,
                           -0.11374691128730774f,  0.21225297451019287f,  -0.02315312623977661f,
                           0.1671370267868042f,    0.22319108247756958f,  0.03609824180603027f,
                           -0.1587022840976715f,   0.059984564781188965f, -0.03951650857925415f,
                           -0.4841443598270416f,   0.32919085025787354f,  -0.23115816712379456f,
                           0.39441078901290894f,   -0.3554944396018982f,  -0.17022761702537537f,
                           -0.055081307888031006f, 0.15856128931045532f,  -0.4183449149131775f,
                           -0.2474445104598999f,   0.03603637218475342f,  -0.2836887538433075f,
                           0.4602506160736084f,    0.29092925786972046f,  -0.199321448802948f,
                           0.380856454372406f,     -0.13847029209136963f, -0.238397479057312f,
                           -0.1907123327255249f,   -0.11061936616897583f, -0.08717870712280273f,
                           0.24449139833450317f,   -0.14727482199668884f, 0.1437196135520935f,
                           0.3955056071281433f,    -0.12538021802902222f, 0.11590522527694702f,
                           0.4598066806793213f,    -0.30005723237991333f, -0.46578651666641235f,
                           -0.33955082297325134f,  -0.2671887278556824f,  0.3611910939216614f,
                           -0.11423084139823914f,  -0.08382436633110046f, -0.31819307804107666f,
                           0.14515334367752075f,   0.3157258629798889f,   0.33179205656051636f,
                           -0.2558857202529907f,   0.11888682842254639f,  0.12824326753616333f,
                           -0.33106181025505066f,  0.2549159526824951f,   -0.46760573983192444f,
                           -0.11983257532119751f,  0.1834418773651123f});

    // W: {2, 1, 2, 2, 2}
    inputs.emplace_back(std::vector<float>{0.388077974319458f,
                                           -0.16366064548492432f,
                                           -0.42871910333633423f,
                                           0.4276432394981384f,
                                           0.21517693996429443f,
                                           0.007908165454864502f,
                                           0.33897721767425537f,
                                           0.21843165159225464f,
                                           0.34095364809036255f,
                                           -0.17043980956077576f,
                                           -0.013571739196777344f,
                                           -0.26793742179870605f,
                                           -0.34863436222076416f,
                                           -0.2672275900840759f,
                                           -0.36691007018089294f,
                                           0.37296557426452637f});

    // B: {2}
    inputs.emplace_back(std::vector<float>{0.4310183525085449f, -0.4564093053340912f});

    // {2, 2, 3, 3, 3}
    Outputs expected_output{std::vector<float>{
        0.5332361459732056f,   0.6628494262695312f,   0.544619083404541f,    0.4242798388004303f,
        0.6271085739135742f,   0.6721994876861572f,   0.43064039945602417f,  0.4246789515018463f,
        0.53834068775177f,     0.6932926177978516f,   0.42797625064849854f,  0.2218741625547409f,
        0.29522019624710083f,  0.8329390287399292f,   0.37605351209640503f,  0.43735477328300476f,
        0.2920728623867035f,   0.6692450046539307f,   0.5527016520500183f,   0.22643595933914185f,
        0.5138190984725952f,   0.3041342794895172f,   0.7423423528671265f,   0.26707080006599426f,
        0.4617553651332855f,   0.32416003942489624f,  0.511577844619751f,    -0.28187549114227295f,
        -0.5031181573867798f,  -0.5793710947036743f,  -0.5992864370346069f,  -0.5055556893348694f,
        -0.7562476396560669f,  -0.44363799691200256f, -0.5730307102203369f,  -0.6302952766418457f,
        -0.4756688177585602f,  -0.728988528251648f,   -0.3900943398475647f,  -0.6694478988647461f,
        -0.38822290301322937f, -0.35774707794189453f, -0.39807581901550293f, -0.547709047794342f,
        -0.35872578620910645f, -0.5326492786407471f,  -0.40852290391921997f, -0.4537881314754486f,
        -0.4545857608318329f,  -0.379546195268631f,   -0.5250767469406128f,  -0.42439910769462585f,
        -0.5558245182037354f,  -0.38563215732574463f, 0.44995537400245667f,  0.5007325410842896f,
        0.49359965324401855f,  0.40685802698135376f,  0.407518208026886f,    0.4628955125808716f,
        0.4301188290119171f,   0.40635955333709717f,  0.4260363280773163f,   0.55128413438797f,
        0.5498291254043579f,   0.27105778455734253f,  0.40259143710136414f,  0.5747092962265015f,
        0.4187920391559601f,   0.4507707953453064f,   0.420598566532135f,    0.3950541913509369f,
        0.593889057636261f,    0.16578882932662964f,  0.5332239270210266f,   0.43014785647392273f,
        0.50260329246521f,     0.39225444197654724f,  0.4074971079826355f,   0.5073125958442688f,
        0.3823610544204712f,   -0.4240749180316925f,  -0.41936254501342773f, -0.5241475105285645f,
        -0.5220003724098206f,  -0.502869725227356f,   -0.5122783780097961f,  -0.4260129928588867f,
        -0.4105660617351532f,  -0.4483373165130615f,  -0.33759188652038574f, -0.735706090927124f,
        -0.3714444637298584f,  -0.4888814687728882f,  -0.6191370487213135f,  -0.2640320658683777f,
        -0.47542816400527954f, -0.5078460574150085f,  -0.4205915927886963f,  -0.5584549903869629f,
        -0.39770257472991943f, -0.45317384600639343f, -0.5598302483558655f,  -0.2542789578437805f,
        -0.5359901785850525f,  -0.48090484738349915f, -0.38603779673576355f, -0.4991581439971924f}};

1539
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1540 1541 1542
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1543
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_matmul_vec_ten3d)
1544
{
1545
    auto function = onnx_import::import_onnx_model(
1546
        file_util::path_join(SERIALIZED_ZOO, "onnx/matmul_vec_ten3d.prototxt"));
1547 1548 1549 1550 1551 1552 1553 1554

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{0.f, 1.f});
    inputs.emplace_back(
        test::NDArray<float, 3>{{{0.f}, {1.f}}, {{2.f}, {3.f}}, {{4.f}, {5.f}}}.get_vector());

    Outputs expected_output{test::NDArray<float, 2>{{1.f}, {3.f}, {5.f}}};

1555
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1556 1557
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
1558

1559
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_softplus)
1560
{
1561 1562
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/softplus.prototxt"));
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597

    // -1.0f, 0, 1.0f, 10.f,                    normal input values for activation
    // 100.0f, -100.0f, 1000.0f, -1000.0f,      input values that leads to exp() overflow
    // FLT_MIN, FLT_MIN / 16, -FLT_MIN / 16,    min, denorm, -denorm
    // FLT_MAX, -FLT_MAX,                       max, -max;
    Inputs inputs{std::vector<float>{-1.0f,
                                     0,
                                     1.0f,
                                     10.f,
                                     100.0f,
                                     -100.0f,
                                     1000.0f,
                                     -1000.0f,
                                     FLT_MIN,
                                     FLT_MIN / 16,
                                     -FLT_MIN / 16,
                                     FLT_MAX,
                                     -FLT_MAX}};

    std::vector<float>& input = inputs.back();
    std::vector<float> output;
    auto softplus_impl = [](float x) -> float {
        if (x > 0)
        {
            return x + std::log(std::exp(-x) + 1);
        }
        else
        {
            return std::log(std::exp(x) + 1);
        }
    };

    std::transform(std::begin(input), std::end(input), std::back_inserter(output), softplus_impl);

    Outputs expected_output{output};
1598
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1599
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
1600 1601
}

1602
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_softplus_infinity)
1603
{
1604 1605
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/softplus.prototxt"));
1606

1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
    Inputs inputs{std::vector<float>{std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity(),
                                     std::numeric_limits<float>::infinity()}};
1620

1621
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1622 1623 1624 1625 1626 1627
    for (float v : outputs.front())
    {
        EXPECT_TRUE(std::isinf(v));
    }
}

1628
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_sum_opset8)
1629 1630
{
    auto function = onnx_import::import_onnx_model(
1631
        file_util::path_join(SERIALIZED_ZOO, "onnx/sum_opset8.prototxt"));
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{1.0f, 2.0f, 3.0f});
    inputs.emplace_back(test::NDArray<float, 2>{{10.0f}, {20.0f}, {30.0f}}.get_vector());
    inputs.emplace_back(test::NDArray<float, 3>{{{100.0f}}, {{200.0f}}, {{300.0f}}}.get_vector());

    Outputs expected_output{test::NDArray<float, 3>{
        {{111.0f, 112.0f, 113.0f}, {121.0f, 122.0f, 123.0f}, {131.0f, 132.0f, 133.0f}},

        {{211.0f, 212.0f, 213.0f}, {221.0f, 222.0f, 223.0f}, {231.0f, 232.0f, 233.0f}},

        {{311.0f, 312.0f, 313.0f}, {321.0f, 322.0f, 323.0f}, {331.0f, 332.0f, 333.0f}}}
                                .get_vector()};

1646
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1647 1648
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
1649

1650
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_conv_transpose_w_groups)
1651 1652
{
    auto function = onnx_import::import_onnx_model(
1653
        file_util::path_join(SERIALIZED_ZOO, "onnx/conv_transpose_w_groups.prototxt"));
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{
        0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f, 13.f, 14.f, 15.f});
    inputs.emplace_back(std::vector<float>{0.f,  1.f,  2.f,  3.f,  4.f,  5.f,  6.f,  7.f,
                                           8.f,  9.f,  10.f, 11.f, 12.f, 13.f, 14.f, 15.f,
                                           16.f, 17.f, 18.f, 19.f, 20.f, 21.f, 22.f, 23.f,
                                           24.f, 25.f, 26.f, 27.f, 28.f, 29.f, 30.f, 31.0f});

    Outputs expected_output{
        std::vector<float>{28.f, 34.f, 252.f, 274.f, 732.f, 770.f, 1468.f, 1522.f}};
1665
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
1666 1667
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}
1668

1669
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_argmax_int32)
1670 1671
{
    auto function = onnx_import::import_onnx_model(
1672
        file_util::path_join(SERIALIZED_ZOO, "onnx/argmax_int32.prototxt"));
1673 1674 1675 1676 1677 1678 1679 1680

    std::vector<std::vector<std::int32_t>> inputs{
        std::vector<std::int32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};

    std::vector<std::vector<std::int64_t>> expected_output{
        std::vector<std::int64_t>{1, 1, 1, 1, 1, 1}};

    std::vector<std::vector<std::int64_t>> outputs{
1681
        execute<std::int32_t, std::int64_t>(function, inputs, "${BACKEND_NAME}")};
1682 1683 1684
    EXPECT_TRUE(test::all_close(expected_output.front(), outputs.front()));
}

1685
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_argmin_int32)
1686 1687
{
    auto function = onnx_import::import_onnx_model(
1688
        file_util::path_join(SERIALIZED_ZOO, "onnx/argmin_int32.prototxt"));
1689 1690 1691 1692 1693 1694 1695

    std::vector<std::vector<std::int32_t>> inputs{
        std::vector<std::int32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}};

    std::vector<std::vector<std::int64_t>> expected_output{std::vector<std::int64_t>{0, 0, 0, 0}};

    std::vector<std::vector<std::int64_t>> outputs{
1696
        execute<std::int32_t, std::int64_t>(function, inputs, "${BACKEND_NAME}")};
1697 1698
    EXPECT_TRUE(test::all_close(expected_output.front(), outputs.front()));
}
1699

1700
NGRAPH_TEST(onnx_${BACKEND_NAME}, is_op_supported)
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
{
    // Simple case
    EXPECT_TRUE(onnx_import::is_operator_supported("Sum", 1, "ai.onnx"));
    // With fallback
    EXPECT_TRUE(onnx_import::is_operator_supported("Sum", 100, "ai.onnx"));

    // Different opset versions
    EXPECT_TRUE(onnx_import::is_operator_supported("Add", 1, "ai.onnx"));
    EXPECT_TRUE(onnx_import::is_operator_supported("Add", 7, "ai.onnx"));

    // Default domain name
    EXPECT_TRUE(onnx_import::is_operator_supported("Sum", 1));

    // Unregistered operator
    EXPECT_FALSE(onnx_import::is_operator_supported("DummyOp", 1));
    EXPECT_FALSE(onnx_import::is_operator_supported("DummyOp", 1, "ai.onnx"));
    EXPECT_FALSE(onnx_import::is_operator_supported("DummyOp", 10, "ai.onnx"));

    // Operator with bad domain name
    EXPECT_FALSE(onnx_import::is_operator_supported("Sum", 1, "bad.domain"));

    // Registered custom operator
    onnx_import::register_operator(
Michal Karzynski's avatar
Michal Karzynski committed
1724 1725 1726 1727
        "AddQ", 1, "com.intel.ai", [](const onnx_import::Node& node) -> NodeVector {
            NodeVector ng_inputs{node.get_ng_inputs()};
            return {std::make_shared<ngraph::op::Add>(ng_inputs.at(0), ng_inputs.at(1))};
        });
1728 1729
    EXPECT_TRUE(onnx_import::is_operator_supported("AddQ", 1, "com.intel.ai"));
}
1730

1731
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_depth_to_space)
1732 1733
{
    auto function = onnx_import::import_onnx_model(
1734
        file_util::path_join(SERIALIZED_ZOO, "onnx/depth_to_space.prototxt"));
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{
        0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f, 13.f, 14.f, 15.f});

    Outputs expected_output{std::vector<float>{
        0.f, 4.f, 1.f, 5.f, 8.f, 12.f, 9.f, 13.f, 2.f, 6.f, 3.f, 7.f, 10.f, 14.f, 11.f, 15.f}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1747
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_depth_to_space_chw)
1748 1749
{
    auto function = onnx_import::import_onnx_model(
1750
        file_util::path_join(SERIALIZED_ZOO, "onnx/depth_to_space_chw.prototxt"));
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{
        0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f, 13.f, 14.f, 15.f});

    Outputs expected_output{std::vector<float>{
        0.f, 4.f, 1.f, 5.f, 8.f, 12.f, 9.f, 13.f, 2.f, 6.f, 3.f, 7.f, 10.f, 14.f, 11.f, 15.f}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1763
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_depth_to_space_bad_blocksize)
1764 1765 1766 1767
{
    // This model fails to import since the depth channel length must be a multiple of the
    // `blocksize` attribute value.
    EXPECT_THROW(onnx_import::import_onnx_model(file_util::path_join(
1768
                     SERIALIZED_ZOO, "onnx/depth_to_space_bad_blocksize.prototxt")),
1769 1770 1771
                 std::runtime_error);
}

1772
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_depth_to_space_no_blocksize)
1773 1774
{
    // This model fails to import since it lacks of required parameter `blocksize`.
1775 1776
    EXPECT_THROW(onnx_import::import_onnx_model(file_util::path_join(
                     SERIALIZED_ZOO, "onnx/depth_to_space_no_blocksize.prototxt")),
1777 1778 1779
                 std::runtime_error);
}

1780
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_space_to_depth)
1781 1782
{
    auto function = onnx_import::import_onnx_model(
1783
        file_util::path_join(SERIALIZED_ZOO, "onnx/space_to_depth.prototxt"));
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{0.f,  1.f,  2.f,  3.f,  4.f,  5.f,  6.f,  7.f,
                                           8.f,  9.f,  10.f, 11.f, 12.f, 13.f, 14.f, 15.f,
                                           16.f, 17.f, 18.f, 19.f, 20.f, 21.f, 22.f, 23.f,
                                           24.f, 25.f, 26.f, 27.f, 28.f, 29.f, 30.f, 31.f});

    Outputs expected_output{std::vector<float>{
        0.f, 2.f, 8.f,  10.f, 16.f, 18.f, 24.f, 26.f, 1.f, 3.f, 9.f,  11.f, 17.f, 19.f, 25.f, 27.f,
        4.f, 6.f, 12.f, 14.f, 20.f, 22.f, 28.f, 30.f, 5.f, 7.f, 13.f, 15.f, 21.f, 23.f, 29.f, 31.f,
    }};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1800
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_space_to_depth_chw)
1801 1802
{
    auto function = onnx_import::import_onnx_model(
1803
        file_util::path_join(SERIALIZED_ZOO, "onnx/space_to_depth_chw.prototxt"));
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{0.f,  1.f,  2.f,  3.f,  4.f,  5.f,  6.f,  7.f,
                                           8.f,  9.f,  10.f, 11.f, 12.f, 13.f, 14.f, 15.f,
                                           16.f, 17.f, 18.f, 19.f, 20.f, 21.f, 22.f, 23.f,
                                           24.f, 25.f, 26.f, 27.f, 28.f, 29.f, 30.f, 31.f});

    Outputs expected_output{std::vector<float>{
        0.f, 2.f, 8.f,  10.f, 16.f, 18.f, 24.f, 26.f, 1.f, 3.f, 9.f,  11.f, 17.f, 19.f, 25.f, 27.f,
        4.f, 6.f, 12.f, 14.f, 20.f, 22.f, 28.f, 30.f, 5.f, 7.f, 13.f, 15.f, 21.f, 23.f, 29.f, 31.f,
    }};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1820
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_space_to_depth_bad_blocksize)
1821 1822 1823 1824
{
    // This model fails to import since the depth channel length must be a multiple of the
    // `blocksize` attribute value.
    EXPECT_THROW(onnx_import::import_onnx_model(file_util::path_join(
1825
                     SERIALIZED_ZOO, "onnx/space_to_depth_bad_blocksize.prototxt")),
1826 1827 1828
                 std::runtime_error);
}

1829
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_space_to_depth_no_blocksize)
1830 1831
{
    // This model fails to import since it lacks of required `blocksize` attribute.
1832 1833
    EXPECT_THROW(onnx_import::import_onnx_model(file_util::path_join(
                     SERIALIZED_ZOO, "onnx/space_to_depth_no_blocksize.prototxt")),
1834 1835
                 std::runtime_error);
}
tsocha's avatar
tsocha committed
1836

1837
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_missing_op_domain)
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
{
    onnx_import::register_operator(
        "CustomAdd", 1, "custom.op", [](const onnx_import::Node& node) -> NodeVector {
            NodeVector ng_inputs{node.get_ng_inputs()};
            return {std::make_shared<ngraph::op::Add>(ng_inputs.at(0), ng_inputs.at(1))};
        });

    EXPECT_TRUE(onnx_import::is_operator_supported("CustomAdd", 1, "custom.op"));

    auto function = onnx_import::import_onnx_model(
1848
        file_util::path_join(SERIALIZED_ZOO, "onnx/missing_op_domain.prototxt"));
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{0.f, 1.f, 2.f, 3.f});
    inputs.emplace_back(std::vector<float>{0.f, 1.f, 2.f, 3.f});

    Outputs expected_output{std::vector<float>{0.f, 2.f, 4.f, 6.f}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

1860
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_top_k)
tsocha's avatar
tsocha committed
1861 1862
{
    auto function =
1863
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/top_k.prototxt"));
tsocha's avatar
tsocha committed
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});

    std::vector<float> expected_values_output{3, 2, 1, 7, 6, 5, 11, 10, 9};
    std::vector<std::int64_t> expected_indices_output{3, 2, 1, 3, 2, 1, 3, 2, 1};

    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors =
        prepare_and_run(function, inputs, "${BACKEND_NAME}");

    std::vector<float> values_output = read_vector<float>(result_tensors.at(0));
    std::vector<std::int64_t> indices_output = read_vector<std::int64_t>(result_tensors.at(1));

    EXPECT_TRUE(test::all_close_f(expected_values_output, values_output));
    EXPECT_TRUE(test::all_close(expected_indices_output, indices_output));
1879
}
1880

1881
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_lstm_fwd_with_clip)
1882 1883
{
    auto function = onnx_import::import_onnx_model(
1884
        file_util::path_join(SERIALIZED_ZOO, "onnx/lstm_fwd_with_clip.prototxt"));
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965

    Inputs inputs{};
    // X
    inputs.emplace_back(std::vector<float>{-0.455351, -0.276391, -0.185934, -0.269585});

    // W
    inputs.emplace_back(std::vector<float>{-0.494659f,
                                           0.0453352f,
                                           -0.487793f,
                                           0.417264f,
                                           -0.0175329f,
                                           0.489074f,
                                           -0.446013f,
                                           0.414029f,
                                           -0.0091708f,
                                           -0.255364f,
                                           -0.106952f,
                                           -0.266717f,
                                           -0.0888852f,
                                           -0.428709f,
                                           -0.283349f,
                                           0.208792f});

    // R
    inputs.emplace_back(std::vector<float>{0.146626f,
                                           -0.0620289f,
                                           -0.0815302f,
                                           0.100482f,
                                           -0.219535f,
                                           -0.306635f,
                                           -0.28515f,
                                           -0.314112f,
                                           -0.228172f,
                                           0.405972f,
                                           0.31576f,
                                           0.281487f,
                                           -0.394864f,
                                           0.42111f,
                                           -0.386624f,
                                           -0.390225f});

    // B
    inputs.emplace_back(std::vector<float>{0.381619f,
                                           0.0323954f,
                                           -0.14449f,
                                           0.420804f,
                                           -0.258721f,
                                           0.45056f,
                                           -0.250755f,
                                           0.0967895f,
                                           0.0f,
                                           0.0f,
                                           0.0f,
                                           0.0f,
                                           0.0f,
                                           0.0f,
                                           0.0f,
                                           0.0f});
    // P
    inputs.emplace_back(std::vector<float>{0.2345f, 0.5235f, 0.4378f, 0.3475f, 0.8927f, 0.3456f});

    Outputs expected_output{};
    // Y_data
    expected_output.emplace_back(
        std::vector<float>{-0.02280854f, 0.02744377f, -0.03516197f, 0.03875681f});
    // Y_h_data
    expected_output.emplace_back(std::vector<float>{-0.03516197f, 0.03875681f});
    // Y_c_data
    expected_output.emplace_back(std::vector<float>{-0.07415761f, 0.07395997f});

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(outputs.size() == expected_output.size());
    for (std::size_t i{0}; i < expected_output.size(); ++i)
    {
        // We have to enlarge tolerance bits to 3 - it's only one bit more than default value.
        // The discrepancies may occur at most on 7th decimal position.
        EXPECT_TRUE(test::all_close_f(expected_output.at(i), outputs.at(i), 3));
    }
}

1966
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_missing_input)
tsocha's avatar
tsocha committed
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
{
    onnx_import::register_operator(
        "TestMissingInOut", 1, "com.intel.ai", [](const onnx_import::Node& node) -> NodeVector {
            NodeVector ng_inputs{node.get_ng_inputs()};
            std::shared_ptr<ngraph::Node> A = ng_inputs.at(0);
            std::shared_ptr<ngraph::Node> B = ng_inputs.at(1);
            std::shared_ptr<ngraph::Node> C = ng_inputs.at(2);

            A = A * C;
            if (!B->is_null())
            {
                B = B / C;
            }

            C = C + C;
            return {A, B, C};
        });

    onnx_import::register_operator(
        "TestMissingIn", 1, "com.intel.ai", [](const onnx_import::Node& node) -> NodeVector {
            NodeVector ng_inputs{node.get_ng_inputs()};
            std::shared_ptr<ngraph::Node> result = std::make_shared<ngraph::op::Constant>(
                element::f32, ngraph::Shape{2, 2}, std::vector<float>{1, 1, 1, 1});

            for (const auto& ng_input : ng_inputs)
            {
                if (!ng_input->is_null())
                {
                    result = ng_input * result;
                }
            }

            return {result};
        });

    auto function = onnx_import::import_onnx_model(
2003
        file_util::path_join(SERIALIZED_ZOO, "onnx/missing_input.prototxt"));
tsocha's avatar
tsocha committed
2004 2005 2006 2007 2008 2009 2010 2011 2012

    Inputs inputs{{1, 2, 3, 4}, {5, 6, 7, 8}};
    Outputs expected_outputs{{50, 144, 294, 512}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

2013
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_sinh)
2014 2015
{
    auto function =
2016
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/sinh.prototxt"));
2017 2018 2019 2020 2021 2022 2023 2024 2025

    Inputs inputs{std::vector<float>{-1.0f, 0.0f, 1.0f}};
    Outputs expected_outputs{std::vector<float>{-1.1752012f, 0.f, 1.1752012f}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

2026
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_cosh)
2027 2028
{
    auto function =
2029
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/cosh.prototxt"));
2030 2031 2032 2033 2034 2035 2036 2037

    Inputs inputs{std::vector<float>{-1.0f, 0.0f, 1.0f}};
    Outputs expected_outputs{std::vector<float>{1.54308069f, 1.f, 1.54308069f}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
2038

2039
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_initializer_wo_input)
2040 2041 2042
{
    // This test checks a model which has an initializer, but no input with the same name
    auto function = onnx_import::import_onnx_model(
2043
        file_util::path_join(SERIALIZED_ZOO, "onnx/initializer_wo_input.prototxt"));
2044 2045 2046 2047 2048 2049 2050 2051 2052

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{0, 1, 2, 3, 4, 5});

    std::vector<float> expected_output{0, 2, 6, 12, 20, 30};

    Outputs output{execute(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close_f(expected_output, output.front()));
}
2053

2054
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_sign)
2055 2056
{
    auto function =
2057
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/sign.prototxt"));
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070

    Inputs inputs{std::vector<float>{-std::numeric_limits<float>::infinity(),
                                     -3.141592f,
                                     0.0f,
                                     2.71828f,
                                     std::numeric_limits<float>::infinity()}};

    Outputs expected_outputs{std::vector<float>{-1.0f, -1.0f, 0.0f, 1.0f, 1.0f}};

    Outputs outputs{execute<float>(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}
2071

2072
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_global_lp_pool_p0)
2073 2074
{
    auto function = onnx_import::import_onnx_model(
2075
        file_util::path_join(SERIALIZED_ZOO, "onnx/global_lp_pool_p0.prototxt"));
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086

    std::vector<std::vector<std::int64_t>> inputs{std::vector<std::int64_t>{
        1, 0, -4, 0, 2, 1, -6, 1, 0, 0, 0, 0, -7, 1, -1, 0, -1, 8, 0, 10, 9, 0, 0, 5}};

    std::vector<std::vector<std::int64_t>> expected_outputs{std::vector<std::int64_t>{6, 8}};

    std::vector<std::vector<std::int64_t>> outputs{execute(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(test::all_close(expected_outputs.front(), outputs.front()));
}

2087
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_global_lp_pool_p1)
2088 2089
{
    auto function = onnx_import::import_onnx_model(
2090
        file_util::path_join(SERIALIZED_ZOO, "onnx/global_lp_pool_p1.prototxt"));
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101

    Inputs inputs{std::vector<float>(2 * 3 * 4)};
    std::iota(std::begin(inputs.front()), std::end(inputs.front()), 0.f);

    Outputs expected_outputs{std::vector<float>{66.f, 210.f}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

2102
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_global_lp_pool_p2)
2103 2104
{
    auto function = onnx_import::import_onnx_model(
2105
        file_util::path_join(SERIALIZED_ZOO, "onnx/global_lp_pool_p2.prototxt"));
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115

    Inputs inputs{std::vector<float>(2 * 3 * 4)};
    std::iota(std::begin(inputs.front()), std::end(inputs.front()), 0.f);

    Outputs expected_outputs{std::vector<float>{22.494444f, 61.789967f}};
    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

2116
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_global_lp_pool_p3)
2117 2118
{
    auto function = onnx_import::import_onnx_model(
2119
        file_util::path_join(SERIALIZED_ZOO, "onnx/global_lp_pool_p3.prototxt"));
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130

    Inputs inputs{std::vector<float>(2 * 3 * 4)};
    std::iota(std::begin(inputs.front()), std::end(inputs.front()), 0.f);

    Outputs expected_outputs{std::vector<float>{16.331620904278438f, 41.56697946707537f}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

2131
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_one_hot_with_axis)
2132 2133
{
    auto function = onnx_import::import_onnx_model(
2134
        file_util::path_join(SERIALIZED_ZOO, "onnx/one_hot_axis.prototxt"));
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145

    Inputs inputs{{1.0, 9.0, 2.0, 4.0}, {1.0, 3.0}};
    Outputs expected_outputs{{1.0, 1.0, 3.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
                              1.0, 1.0, 1.0, 1.0, 1.0, 3.0, 1.0, 1.0, 1.0, 1.0, 3.0, 1.0, 1.0, 1.0,
                              1.0, 3.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(test::all_close_f(expected_outputs.front(), outputs.front()));
}

2146
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_one_hot_without_axis)
2147 2148
{
    auto function = onnx_import::import_onnx_model(
2149
        file_util::path_join(SERIALIZED_ZOO, "onnx/one_hot_no_axis.prototxt"));
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160

    std::vector<std::vector<std::int64_t>> inputs{{0, 7, 8}, {2, 5}};
    std::vector<std::vector<std::int64_t>> expected_outputs{{5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                                                             2, 2, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2,
                                                             2, 2, 2, 2, 2, 2, 2, 2, 5, 2, 2, 2}};

    std::vector<std::vector<std::int64_t>> outputs{execute(function, inputs, "${BACKEND_NAME}")};

    EXPECT_TRUE(test::all_close(expected_outputs.front(), outputs.front()));
}

2161
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_where)
2162 2163
{
    auto function =
2164
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/where.prototxt"));
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174

    // conditions tensor - 3x3x3
    auto condition = std::vector<int>{
        {0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0}};

    // 1x3 tensor of "1"
    auto x1 = std::vector<int>{1, 1, 1};
    // 3x1 tensor of "2"
    auto x2 = std::vector<int>{2, 2, 2};

2175
    std::vector<std::vector<int>> inputs;
2176 2177 2178 2179 2180
    inputs.push_back(std::move(condition));
    inputs.push_back(std::move(x1));
    inputs.push_back(std::move(x2));

    // y = 3x3x3
2181
    std::vector<std::vector<int>> expected_outputs{
2182 2183
        {2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2}};

2184
    std::vector<std::vector<int>> outputs{execute(function, inputs, "${BACKEND_NAME}")};
2185 2186 2187

    EXPECT_EQ(expected_outputs.front(), outputs.front());
}
2188

2189
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_override_op)
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
{
    onnx_import::register_operator(
        "FalseAdd", 1, "", [](const onnx_import::Node& node) -> NodeVector {
            NodeVector ng_inputs{node.get_ng_inputs()};
            return {std::make_shared<ngraph::op::Add>(ng_inputs.at(0), ng_inputs.at(1))};
        });

    onnx_import::register_operator(
        "FalseAdd", 1, "", [](const onnx_import::Node& node) -> NodeVector {
            NodeVector ng_inputs{node.get_ng_inputs()};
            return {std::make_shared<ngraph::op::Subtract>(ng_inputs.at(0), ng_inputs.at(1))};
        });

    auto function = onnx_import::import_onnx_model(
2204
        file_util::path_join(SERIALIZED_ZOO, "onnx/override_op.prototxt"));
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{0.f, 1.f, 2.f, 3.f});
    inputs.emplace_back(std::vector<float>{3.f, 2.f, 1.f, 0.f});

    Outputs expected_output{std::vector<float>{-3.f, -1.f, 1.f, 3.f}};

    Outputs outputs{execute(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

2216
NGRAPH_TEST(onnx_${BACKEND_NAME}, import_non_existing_file)
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
{
    try
    {
        onnx_import::import_onnx_model(file_util::path_join(SERIALIZED_ZOO, "onnx/i.dont.exist"));
    }
    catch (const std::runtime_error& exc)
    {
        // asserts that an exception was thrown and that the error message contains the file name
        std::string msg{exc.what()};
        EXPECT_TRUE(msg.find("i.dont.exist") != std::string::npos);
    }
}
2229

2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_lstm_fwd_mixed_seq)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/lstm_fwd_mixed_seq.prototxt"));

    int hidden_size{3};
    int parameters_cout{5};

    // X
    std::vector<float> in_x{1.f, 2.f, 10.f, 11.f};
    // W
    std::vector<float> in_w{0.1f, 0.2f, 0.3f, 0.4f, 1.f, 2.f, 3.f, 4.f, 10.f, 11.f, 12.f, 13.f};
    // R
    std::vector<float> in_r(4 * hidden_size * hidden_size, 0.1f);
    // B
    std::vector<float> in_b(8 * hidden_size, 0.0f);

    std::vector<int> in_seq_lengths{1, 2};

    std::vector<float> out_y_data{0.28828835f,
                                  0.36581863f,
                                  0.45679406f,
                                  0.34526032f,
                                  0.47220859f,
                                  0.55850911f,
                                  0.f,
                                  0.f,
                                  0.f,
                                  0.85882828f,
                                  0.90703777f,
                                  0.92382453f};

    std::vector<float> out_y_h_data{
        0.28828835f, 0.36581863f, 0.45679406f, 0.85882828f, 0.90703777f, 0.92382453f};

    std::vector<float> out_y_c_data{
        0.52497941f, 0.54983425f, 0.5744428f, 1.3249796f, 1.51063104f, 1.61451544f};

    Outputs expected_output;

    expected_output.emplace_back(out_y_data);
    expected_output.emplace_back(out_y_h_data);
    expected_output.emplace_back(out_y_c_data);

    auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");
    auto parameters = function->get_parameters();

    EXPECT_TRUE(parameters.size() == parameters_cout);

    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> arg_tensors;

    auto add_tensor = [&arg_tensors, &backend](const std::vector<float>& v,
                                               const std::shared_ptr<ngraph::op::Parameter>& p) {
        auto t = backend->create_tensor(p->get_element_type(), p->get_shape());
        copy_data(t, v);
        arg_tensors.push_back(t);
    };

    add_tensor(in_x, parameters.at(0));
    add_tensor(in_w, parameters.at(1));
    add_tensor(in_r, parameters.at(2));
    add_tensor(in_b, parameters.at(3));

    auto t_in_seq_lengths =
        backend->create_tensor(parameters.at(4)->get_element_type(), parameters.at(4)->get_shape());
    copy_data(t_in_seq_lengths, in_seq_lengths);
    arg_tensors.push_back(t_in_seq_lengths);

    auto results = function->get_results();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors(results.size());

    for (std::size_t i{0}; i < results.size(); ++i)
    {
        result_tensors.at(i) =
            backend->create_tensor(results.at(i)->get_element_type(), results.at(i)->get_shape());
    }

    auto handle = backend->compile(function);
    handle->call_with_validate(result_tensors, arg_tensors);

    Outputs outputs;
    for (auto rt : result_tensors)
    {
        outputs.push_back(read_vector<float>(rt));
    }

    EXPECT_TRUE(outputs.size() == expected_output.size());
    for (std::size_t i{0}; i < expected_output.size(); ++i)
    {
        // We have to enlarge tolerance bits to 3 - it's only one bit more than default value.
        // The discrepancies may occur at most on 7th decimal position.
        EXPECT_TRUE(test::all_close_f(expected_output.at(i), outputs.at(i), 3));
    }
}

2325 2326 2327
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_quantize_linear)
{
    auto function = onnx_import::import_onnx_model(
2328
        file_util::path_join(SERIALIZED_ZOO, "onnx/quantize_linear.prototxt"));
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{32.25f, 48.34f, 50.f, 83.f});
    inputs.emplace_back(std::vector<float>{0.5f});

    std::vector<std::vector<std::uint8_t>> expected_output{
        std::vector<std::uint8_t>{64, 97, 100, 166}};

    std::vector<std::vector<std::uint8_t>> outputs{
        execute<float, std::uint8_t>(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close(expected_output.front(), outputs.front()));
}

2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_quantize_linear_zero_point)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/quantize_linear_zero_point.prototxt"));

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{0.f, 2.f, 3.f, 1000.f, -254.f, -1000.f}); // x
    inputs.emplace_back(std::vector<float>{2.0f});                                   // y_scale

    std::vector<std::vector<std::uint8_t>> int_inputs;
    int_inputs.emplace_back(std::vector<std::uint8_t>{128}); // y_zero_point

    std::vector<std::vector<std::uint8_t>> expected_output{
        std::vector<std::uint8_t>{128, 129, 130, 255, 1, 0}};

    std::vector<std::vector<std::uint8_t>> outputs{execute<float, std::uint8_t, std::uint8_t>(
        function, inputs, int_inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close(expected_output.front(), outputs.front()));
}

NGRAPH_TEST(onnx_${BACKEND_NAME}, quantize_linear_axis_zero)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/quantize_linear_axis_zero.prototxt"));

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{
        0.f, 2.f, 3.f, 1000.f, 0.f, 2.f, 3.f, 1000.f, 0.f, 2.f, 3.f, 1000.f}); // x
    inputs.emplace_back(std::vector<float>{1.f, 2.f, 4.f});                    // y_scale

    std::vector<std::vector<std::uint8_t>> int_inputs;
    int_inputs.emplace_back(std::vector<std::uint8_t>{0, 0, 0}); // y_zero_point

    std::vector<std::vector<std::uint8_t>> expected_output{
        //  std::vector<std::uint8_t>{0, 2, 3, 255, 0, 1, 2, 255, 0, 1, 1, 250}}; <- bad expected output given HALF_TO_EVEN round mode
        std::vector<std::uint8_t>{0, 2, 3, 255, 0, 1, 2, 255, 0, 0, 1, 250}};

    std::vector<std::vector<std::uint8_t>> outputs{execute<float, std::uint8_t, std::uint8_t>(
        function, inputs, int_inputs, "${BACKEND_NAME}")};
    EXPECT_EQ(expected_output.front(), outputs.front());
}

NGRAPH_TEST(onnx_${BACKEND_NAME}, model_quantize_linear_axis_negative)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/quantize_linear_axis_negative.prototxt"));

    Inputs inputs;
    inputs.emplace_back(std::vector<float>{
        0.f, 2.f, 3.f, 1000.f, 0.f, 2.f, 3.f, 1000.f, 0.f, 2.f, 3.f, 1000.f}); // x
    inputs.emplace_back(std::vector<float>{1.f, 2.f, 4.f});                    // y_scale

    std::vector<std::vector<std::uint8_t>> int_inputs;
    int_inputs.emplace_back(std::vector<std::uint8_t>{0, 0, 0}); // y_zero_point

    std::vector<std::vector<std::uint8_t>> expected_output{
        //  std::vector<std::uint8_t>{0, 2, 3, 255, 0, 1, 2, 255, 0, 1, 1, 250}}; <- bad expected output given HALF_TO_EVEN round mode
        std::vector<std::uint8_t>{0, 2, 3, 255, 0, 1, 2, 255, 0, 0, 1, 250}};

    std::vector<std::vector<std::uint8_t>> outputs{execute<float, std::uint8_t, std::uint8_t>(
        function, inputs, int_inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close(expected_output.front(), outputs.front()));
}

2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/dequant_lin.prototxt"));

    std::vector<std::vector<std::uint8_t>> inputs;
    inputs.emplace_back(std::vector<std::uint8_t>{19, 210, 21, 10});

    Outputs expected_output{std::vector<float>{76.f, 840.f, 84.f, 40.f}};

    Outputs outputs{execute<std::uint8_t, float>(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_scalar_zero_scale_uint8)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_0.prototxt"));

    auto x = std::vector<uint8_t>{0, 3, 128, 255};
    auto scale = std::vector<float>{2.0f};
    auto zero_point = std::vector<uint8_t>{128};

    auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");

    auto params = function->get_parameters();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
    input_tensors.push_back(
        backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));

    copy_data(input_tensors[0], x);
    copy_data(input_tensors[1], scale);
    copy_data(input_tensors[2], zero_point);

    auto results = function->get_results();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
    result_tensors.push_back(
        backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));

    auto handle = backend->compile(function);
    handle->call_with_validate(result_tensors, input_tensors);

    std::vector<std::vector<float>> outputs;
    outputs.push_back(read_vector<float>(result_tensors[0]));

    auto expected_output = std::vector<std::vector<float>>{{-256.0f, -250.0f, 0.0f, 254.0f}};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_scalar_zero_scale_int8)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_1.prototxt"));

    auto x = std::vector<int8_t>{-30, -3, 100, 127};
    auto scale = std::vector<float>{2.0f};
    auto zero_point = std::vector<int8_t>{-10};

    auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");

    auto params = function->get_parameters();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
    input_tensors.push_back(
        backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));

    copy_data(input_tensors[0], x);
    copy_data(input_tensors[1], scale);
    copy_data(input_tensors[2], zero_point);

    auto results = function->get_results();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
    result_tensors.push_back(
        backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));

    auto handle = backend->compile(function);
    handle->call_with_validate(result_tensors, input_tensors);

    std::vector<std::vector<float>> outputs;
    outputs.push_back(read_vector<float>(result_tensors[0]));

    auto expected_output = std::vector<std::vector<float>>{{-40.0f, 14.0f, 220.0f, 274.0f}};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_1d_zero_scale_uint8)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_2.prototxt"));

    auto x = std::vector<uint8_t>{0, 1, 2, 3, 0, 1, 2, 3, 0, 10, 20, 30};
    auto scale = std::vector<float>{1.0f, 2.0f, 4.0f};
    auto zero_point = std::vector<uint8_t>{0, 0, 0};

    auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");

    auto params = function->get_parameters();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
    input_tensors.push_back(
        backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));

    copy_data(input_tensors[0], x);
    copy_data(input_tensors[1], scale);
    copy_data(input_tensors[2], zero_point);

    auto results = function->get_results();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
    result_tensors.push_back(
        backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));

    auto handle = backend->compile(function);
    handle->call_with_validate(result_tensors, input_tensors);

    std::vector<std::vector<float>> outputs;
    outputs.push_back(read_vector<float>(result_tensors[0]));

    auto expected_output = std::vector<std::vector<float>>{
        {0.0f, 1.0f, 2.0f, 3.0f, 0.0f, 2.0f, 4.0f, 6.0f, 0.0f, 40.0f, 80.0f, 120.0f}};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_1d_zero_scale_int8)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_3.prototxt"));

    auto x = std::vector<int8_t>{0, 1, 2, 3, 0, 2, 4, 6, 0, 10, 20, 30};
    auto scale = std::vector<float>{1.0f, 2.0f, 4.0f, 8.0f};
    auto zero_point = std::vector<int8_t>{0, -10, -20, -30};

    auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");

    auto params = function->get_parameters();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
    input_tensors.push_back(
        backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));

    copy_data(input_tensors[0], x);
    copy_data(input_tensors[1], scale);
    copy_data(input_tensors[2], zero_point);

    auto results = function->get_results();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
    result_tensors.push_back(
        backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));

    auto handle = backend->compile(function);
    handle->call_with_validate(result_tensors, input_tensors);

    std::vector<std::vector<float>> outputs;
    outputs.push_back(read_vector<float>(result_tensors[0]));

    auto expected_output = std::vector<std::vector<float>>{
        {0.0f, 22.0f, 88.0f, 264.0f, 0.0f, 24.0f, 96.0f, 288.0f, 0.0f, 40.0f, 160.0f, 480.0f}};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_1d_zero_scale_int8_4d)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_4.prototxt"));

    auto x = std::vector<int8_t>{7, 9, 10, 10, 5,  8, 9, 1, 8, 6, 7, 9, 10, 0, 7, 10,
                                 8, 2, 6,  0,  5,  9, 8, 1, 2, 7, 5, 3, 2,  4, 1, 3,
                                 8, 7, 4,  8,  10, 1, 5, 5, 7, 7, 0, 2, 4,  4, 0, 5};

    auto scale = std::vector<float>{1.0f, 10.0f, 7.0f};
    auto zero_point = std::vector<int8_t>{10, 2, 1};

    auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");

    auto params = function->get_parameters();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
    input_tensors.push_back(
        backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));

    copy_data(input_tensors[0], x);
    copy_data(input_tensors[1], scale);
    copy_data(input_tensors[2], zero_point);

    auto results = function->get_results();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
    result_tensors.push_back(
        backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));

    auto handle = backend->compile(function);
    handle->call_with_validate(result_tensors, input_tensors);

    std::vector<std::vector<float>> outputs;
    outputs.push_back(read_vector<float>(result_tensors[0]));

    auto expected_output = std::vector<std::vector<float>>{
        {-3.0f, -1.0f,  0.0f,  0.0f,  -5.0f, -2.0f, -1.0f, -9.0f, 60.0f, 40.0f, 50.0f, 70.0f,
         80.0f, -20.0f, 50.0f, 80.0f, 49.0f, 7.0f,  35.0f, -7.0f, 28.0f, 56.0f, 49.0f, 0.0f,
         -8.0f, -3.0f,  -5.0f, -7.0f, -8.0f, -6.0f, -9.0f, -7.0f, 60.0f, 50.0f, 20.0f, 60.0f,
         80.0f, -10.0f, 30.0f, 30.0f, 42.0f, 42.0f, -7.0f, 7.0f,  21.0f, 21.0f, -7.0f, 28.0f}};

    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

NGRAPH_TEST(onnx_${BACKEND_NAME}, model_dequantize_linear_1d_zero_scale_uint8_negative_axis)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/dequantize_linear_5.prototxt"));

    auto x = std::vector<uint8_t>{0, 1, 2, 3, 0, 1, 2, 3, 0, 10, 20, 30};
    auto scale = std::vector<float>{1.0f, 2.0f, 4.0f};
    auto zero_point = std::vector<uint8_t>{0, 0, 0};

    auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");

    auto params = function->get_parameters();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
    input_tensors.push_back(
        backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));

    copy_data(input_tensors[0], x);
    copy_data(input_tensors[1], scale);
    copy_data(input_tensors[2], zero_point);

    auto results = function->get_results();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
    result_tensors.push_back(
        backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));

    auto handle = backend->compile(function);
    handle->call_with_validate(result_tensors, input_tensors);

    std::vector<std::vector<float>> outputs;
    outputs.push_back(read_vector<float>(result_tensors[0]));

    auto expected_output = std::vector<std::vector<float>>{
        {0.0f, 1.0f, 2.0f, 3.0f, 0.0f, 2.0f, 4.0f, 6.0f, 0.0f, 40.0f, 80.0f, 120.0f}};
    EXPECT_TRUE(test::all_close_f(expected_output.front(), outputs.front()));
}

2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
NGRAPH_TEST(onnx_${BACKEND_NAME}, model_quant_conv_linear)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/quant_conv_lin.prototxt"));

    std::vector<std::vector<std::uint8_t>> inputs;
    inputs.emplace_back(std::vector<std::uint8_t>{
        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});

    std::vector<std::vector<std::int8_t>> expected_output{std::vector<std::int8_t>{
        2,  3,  3,  3,  4,  4,  4,  5,  2,  4,  6,  7,  8,  8,  9,  9,  10, 3,  8,  11, 12,
        13, 13, 14, 14, 15, 5,  11, 16, 17, 18, 18, 19, 19, 20, 7,  14, 22, 22, 23, 23, 24,
        24, 25, 8,  18, 27, 27, 28, 28, 29, 29, 30, 10, 21, 32, 32, 33, 33, 34, 34, 35, 12,
        24, 37, 37, 38, 38, 39, 40, 40, 13, 17, 26, 27, 27, 27, 28, 28, 28, 9}};

    std::vector<std::vector<std::int8_t>> outputs{
        execute<std::uint8_t, std::int8_t>(function, inputs, "${BACKEND_NAME}")};
    EXPECT_TRUE(test::all_close(expected_output.front(), outputs.front()));
}
2687 2688 2689 2690 2691 2692 2693

NGRAPH_TEST(onnx_${BACKEND_NAME}, model_quant_conv_linear_2d)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/qlinear_conv_2d.prototxt"));

    auto x =
2694 2695 2696
        read_binary_file<uint8_t>(file_util::path_join(TEST_FILES, "onnx/qlinearconv2d/x.bin"));
    auto x_scale =
        read_binary_file<float>(file_util::path_join(TEST_FILES, "onnx/qlinearconv2d/x_scale.bin"));
2697
    auto x_zero_point = read_binary_file<uint8_t>(
2698
        file_util::path_join(TEST_FILES, "onnx/qlinearconv2d/x_zero_point.bin"));
2699 2700

    auto w =
2701 2702 2703
        read_binary_file<uint8_t>(file_util::path_join(TEST_FILES, "onnx/qlinearconv2d/w.bin"));
    auto w_scale =
        read_binary_file<float>(file_util::path_join(TEST_FILES, "onnx/qlinearconv2d/w_scale.bin"));
2704
    auto w_zero_point = read_binary_file<uint8_t>(
2705
        file_util::path_join(TEST_FILES, "onnx/qlinearconv2d/w_zero_point.bin"));
2706

2707 2708
    auto y_scale =
        read_binary_file<float>(file_util::path_join(TEST_FILES, "onnx/qlinearconv2d/y_scale.bin"));
2709
    auto y_zero_point = read_binary_file<uint8_t>(
2710
        file_util::path_join(TEST_FILES, "onnx/qlinearconv2d/y_zero_point.bin"));
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753

    auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");

    auto params = function->get_parameters();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
    input_tensors.push_back(
        backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(3)->get_element_type(), params.at(3)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(4)->get_element_type(), params.at(4)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(5)->get_element_type(), params.at(5)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(6)->get_element_type(), params.at(6)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(7)->get_element_type(), params.at(7)->get_shape()));

    copy_data(input_tensors[0], x);
    copy_data(input_tensors[1], x_scale);
    copy_data(input_tensors[2], x_zero_point);
    copy_data(input_tensors[3], w);
    copy_data(input_tensors[4], w_scale);
    copy_data(input_tensors[5], w_zero_point);
    copy_data(input_tensors[6], y_scale);
    copy_data(input_tensors[7], y_zero_point);

    auto results = function->get_results();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
    result_tensors.push_back(
        backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));

    auto handle = backend->compile(function);
    handle->call_with_validate(result_tensors, input_tensors);

    std::vector<std::vector<uint8_t>> outputs;
    outputs.push_back(read_vector<uint8_t>(result_tensors[0]));

    std::vector<std::vector<uint8_t>> expected_output;
2754 2755
    expected_output.push_back(
        read_binary_file<uint8_t>(file_util::path_join(TEST_FILES, "onnx/qlinearconv2d/y.bin")));
2756 2757 2758 2759 2760 2761 2762 2763 2764 2765

    EXPECT_EQ(expected_output.front(), outputs.front());
}

NGRAPH_TEST(onnx_${BACKEND_NAME}, model_quant_conv_linear_3d)
{
    auto function = onnx_import::import_onnx_model(
        file_util::path_join(SERIALIZED_ZOO, "onnx/qlinear_conv_3d.prototxt"));

    auto x =
2766 2767 2768
        read_binary_file<uint8_t>(file_util::path_join(TEST_FILES, "onnx/qlinearconv3d/x.bin"));
    auto x_scale =
        read_binary_file<float>(file_util::path_join(TEST_FILES, "onnx/qlinearconv3d/x_scale.bin"));
2769
    auto x_zero_point = read_binary_file<uint8_t>(
2770
        file_util::path_join(TEST_FILES, "onnx/qlinearconv3d/x_zero_point.bin"));
2771 2772

    auto w =
2773 2774 2775
        read_binary_file<uint8_t>(file_util::path_join(TEST_FILES, "onnx/qlinearconv3d/w.bin"));
    auto w_scale =
        read_binary_file<float>(file_util::path_join(TEST_FILES, "onnx/qlinearconv3d/w_scale.bin"));
2776
    auto w_zero_point = read_binary_file<uint8_t>(
2777
        file_util::path_join(TEST_FILES, "onnx/qlinearconv3d/w_zero_point.bin"));
2778

2779 2780
    auto y_scale =
        read_binary_file<float>(file_util::path_join(TEST_FILES, "onnx/qlinearconv3d/y_scale.bin"));
2781
    auto y_zero_point = read_binary_file<uint8_t>(
2782
        file_util::path_join(TEST_FILES, "onnx/qlinearconv3d/y_zero_point.bin"));
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825

    auto backend = ngraph::runtime::Backend::create("${BACKEND_NAME}");

    auto params = function->get_parameters();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> input_tensors;
    input_tensors.push_back(
        backend->create_tensor(params.at(0)->get_element_type(), params.at(0)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(1)->get_element_type(), params.at(1)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(2)->get_element_type(), params.at(2)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(3)->get_element_type(), params.at(3)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(4)->get_element_type(), params.at(4)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(5)->get_element_type(), params.at(5)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(6)->get_element_type(), params.at(6)->get_shape()));
    input_tensors.push_back(
        backend->create_tensor(params.at(7)->get_element_type(), params.at(7)->get_shape()));

    copy_data(input_tensors[0], x);
    copy_data(input_tensors[1], x_scale);
    copy_data(input_tensors[2], x_zero_point);
    copy_data(input_tensors[3], w);
    copy_data(input_tensors[4], w_scale);
    copy_data(input_tensors[5], w_zero_point);
    copy_data(input_tensors[6], y_scale);
    copy_data(input_tensors[7], y_zero_point);

    auto results = function->get_results();
    std::vector<std::shared_ptr<ngraph::runtime::Tensor>> result_tensors;
    result_tensors.push_back(
        backend->create_tensor(results.at(0)->get_element_type(), results.at(0)->get_shape()));

    auto handle = backend->compile(function);
    handle->call_with_validate(result_tensors, input_tensors);

    std::vector<std::vector<uint8_t>> outputs;
    outputs.push_back(read_vector<uint8_t>(result_tensors[0]));

    std::vector<std::vector<uint8_t>> expected_output;
2826 2827
    expected_output.push_back(
        read_binary_file<uint8_t>(file_util::path_join(TEST_FILES, "onnx/qlinearconv3d/y.bin")));
2828 2829 2830

    EXPECT_EQ(expected_output.front(), outputs.front());
}