Imported Upstream version 1.21.0
[platform/core/ml/nnfw.git] / tests / nnfw_api / src / ModelTestInputReshaping.test.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 "common.h"
22 #include "CircleGen.h"
23
24 /**
25  * @brief Testing the following model:
26  *       #1 = placeholder (shape = [2, 2], dtype=float)
27  *       #2 = placeholder (shape = [2], dtype=float)
28  *       #3 = add(#1, #2)
29  */
30 auto build_model_add_input_reshaping()
31 {
32   // Model is not important
33   CircleGen cgen;
34   auto f32 = circle::TensorType::TensorType_FLOAT32;
35   int in1 = cgen.addTensor({{2, 2}, f32}); // consider this [None, None]
36   int in2 = cgen.addTensor({{2}, f32});
37   int out = cgen.addTensor({{}, f32}); // scalar, meaning output shape is unspecified
38   cgen.addOperatorAdd({{in1, in2}, {out}}, circle::ActivationFunctionType_NONE);
39   cgen.setInputsAndOutputs({in1, in2}, {out});
40   auto cbuf = cgen.finish();
41   return cbuf;
42 }
43
44 TEST(TestDynamicTensor, input_reshaping)
45 {
46   nnfw_session *session = nullptr;
47   NNFW_ENSURE_SUCCESS(nnfw_create_session(&session));
48   const auto model_buf = build_model_add_input_reshaping();
49   NNFW_ENSURE_SUCCESS(nnfw_load_circle_from_buffer(session, model_buf.buffer(), model_buf.size()));
50
51   NNFW_ENSURE_SUCCESS(nnfw_set_available_backends(session, "cpu"));
52
53   // input and output values
54   const std::vector<float> input1 = {0, 1, 2, 3, 4, 5, 6, 7}; // of changed shape [4, 2]
55   const std::vector<float> input2 = {-10, -10};
56   const std::vector<float> expected = {-10, -9, -8, -7, -6, -5, -4, -3}; // of shape [4, 2]
57
58   /*
59   testing sequence and what's been done:
60     1. nnfw_set_input_tensorinfo : set input shape to different shape (static inference)
61     2. nnfw_prepare
62     3. nnfw_set_input
63     4. nnfw_run
64   */
65
66   // input reshaping from [2, 2] to [4, 2]
67   nnfw_tensorinfo ti = {NNFW_TYPE_TENSOR_FLOAT32, 2, {4, 2}};
68   NNFW_ENSURE_SUCCESS(nnfw_set_input_tensorinfo(session, 0, &ti));
69
70   NNFW_ENSURE_SUCCESS(nnfw_prepare(session));
71
72   nnfw_tensorinfo ti_input = {}; // Static inference result will be stored
73   NNFW_ENSURE_SUCCESS(nnfw_input_tensorinfo(session, 0, &ti_input));
74   ASSERT_TRUE(tensorInfoEqual(ti, ti_input));
75
76   nnfw_tensorinfo ti_output = {}; // Static inference result will be stored
77   NNFW_ENSURE_SUCCESS(nnfw_output_tensorinfo(session, 0, &ti_output));
78   ASSERT_TRUE(tensorInfoEqual(ti, ti_output)); // input/output shapes are same with for this model
79
80   NNFW_ENSURE_SUCCESS(nnfw_set_input(session, 0, NNFW_TYPE_TENSOR_FLOAT32, input1.data(),
81                                      sizeof(float) * input1.size()));
82   NNFW_ENSURE_SUCCESS(nnfw_set_input(session, 1, NNFW_TYPE_TENSOR_FLOAT32, input2.data(),
83                                      sizeof(float) * input2.size()));
84
85   uint64_t output_num_elements = tensorInfoNumElements(ti_output);
86   ASSERT_EQ(output_num_elements, expected.size());
87   std::vector<float> actual_output(output_num_elements);
88   NNFW_ENSURE_SUCCESS(nnfw_set_output(session, 0, NNFW_TYPE_TENSOR_FLOAT32, actual_output.data(),
89                                       sizeof(float) * actual_output.size()));
90
91   // Do inference
92   NNFW_ENSURE_SUCCESS(nnfw_run(session));
93
94   // compare
95   for (int i = 0; i < expected.size(); ++i)
96     ASSERT_EQ(expected[i], actual_output[i]);
97 }