2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
18 #include <nnfw_internal.h>
21 #include "NNPackages.h"
24 using TestInputReshapingAddModelLoaded = ValidationTestModelLoaded<NNPackages::INPUT_RESHAPING_ADD>;
27 * @brief Testing the following model:
28 * #1 = placeholder (shape = [2, 2], dtype=float)
29 * #2 = placeholder (shape = [2], dtype=float)
32 * @note Run this test with "cpu" backend and "linear" executor
34 TEST_F(TestInputReshapingAddModelLoaded, reshaping_2x2_to_4x2)
36 NNFW_STATUS res = NNFW_STATUS_ERROR;
38 NNFW_ENSURE_SUCCESS(nnfw_set_available_backends(_session, "cpu"));
39 NNFW_ENSURE_SUCCESS(nnfw_set_config(_session, "EXECUTOR", "Linear"));
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]
47 testing sequence and what's been done:
48 1. nnfw_set_input_tensorinfo : set input shape to different shape (static inference)
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);
58 res = nnfw_prepare(_session);
59 NNFW_ENSURE_SUCCESS(res);
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));
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
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);
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);
84 res = nnfw_run(_session);
85 NNFW_ENSURE_SUCCESS(res);
88 for (int i = 0; i < expected.size(); ++i)
89 ASSERT_EQ(expected[i], actual_output[i]);