Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / ModelTestInputReshaping.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <gtest/gtest.h>
18 #include <nnfw_internal.h>
19
20 #include "fixtures.h"
21 #include "NNPackages.h"
22 #include "common.h"
23
24 using TestInputReshapingAddModelLoaded = ValidationTestModelLoaded<NNPackages::INPUT_RESHAPING_ADD>;
25
26 /**
27  * @brief Testing the following model:
28  *       #1 = placeholder (shape = [2, 2], dtype=float)
29  *       #2 = placeholder (shape = [2], dtype=float)
30  *       #3 = add(#1, #2)
31  *
32  * @note Run this test with "cpu" backend and "linear" executor
33  */
34 TEST_F(TestInputReshapingAddModelLoaded, reshaping_2x2_to_4x2)
35 {
36   NNFW_STATUS res = NNFW_STATUS_ERROR;
37
38   NNFW_ENSURE_SUCCESS(nnfw_set_available_backends(_session, "cpu"));
39   NNFW_ENSURE_SUCCESS(nnfw_set_config(_session, "EXECUTOR", "Linear"));
40
41   // input and output values
42   const std::vector<float> input1 = {0, 1, 2, 3, 4, 5, 6, 7}; // of changed shape [4, 2]
43   const std::vector<float> input2 = {-10, -10};
44   const std::vector<float> expected = {-10, -9, -8, -7, -6, -5, -4, -3}; // of shape [4, 2]
45
46   /*
47   testing sequence and what's been done:
48     1. nnfw_set_input_tensorinfo : set input shape to different shape (static inference)
49     2. nnfw_prepare
50     3. nnfw_set_input
51     4. nnfw_run
52   */
53
54   // input reshaping from [2, 2] to [4, 2]
55   nnfw_tensorinfo ti = {NNFW_TYPE_TENSOR_FLOAT32, 2, {4, 2}};
56   res = nnfw_set_input_tensorinfo(_session, 0, &ti);
57
58   res = nnfw_prepare(_session);
59   NNFW_ENSURE_SUCCESS(res);
60
61   nnfw_tensorinfo ti_input = {}; // Static inference result will be stored
62   nnfw_input_tensorinfo(_session, 0, &ti_input);
63   ASSERT_TRUE(tensorInfoEqual(ti, ti_input));
64
65   nnfw_tensorinfo ti_output = {}; // Static inference result will be stored
66   nnfw_output_tensorinfo(_session, 0, &ti_output);
67   ASSERT_TRUE(tensorInfoEqual(ti, ti_output)); // input/output shapes are same with for this model
68
69   res = nnfw_set_input(_session, 0, NNFW_TYPE_TENSOR_FLOAT32, input1.data(),
70                        sizeof(float) * input1.size());
71   NNFW_ENSURE_SUCCESS(res);
72   res = nnfw_set_input(_session, 1, NNFW_TYPE_TENSOR_FLOAT32, input2.data(),
73                        sizeof(float) * input2.size());
74   NNFW_ENSURE_SUCCESS(res);
75
76   uint64_t output_num_elements = tensorInfoNumElements(ti_output);
77   ASSERT_EQ(output_num_elements, expected.size());
78   std::vector<float> actual_output(output_num_elements);
79   res = nnfw_set_output(_session, 0, NNFW_TYPE_TENSOR_FLOAT32, actual_output.data(),
80                         sizeof(float) * actual_output.size());
81   NNFW_ENSURE_SUCCESS(res);
82
83   // Do inference
84   res = nnfw_run(_session);
85   NNFW_ENSURE_SUCCESS(res);
86
87   // compare
88   for (int i = 0; i < expected.size(); ++i)
89     ASSERT_EQ(expected[i], actual_output[i]);
90 }