2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "kernels/TestUtils.h"
19 #include "luci_interpreter/test_models/sub/FloatSubKernel.h"
20 #include "luci_interpreter/test_models/sub/IntSubKernel.h"
21 #include "luci_interpreter/test_models/sub/NegSubKernel.h"
23 #include "loader/ModuleLoader.h"
25 namespace luci_interpreter
30 using namespace testing;
32 class SubTest : public ::testing::Test
37 template <typename T> std::vector<T> checkSubKernel(test_kernel::TestDataBase<T> *test_data_base)
39 MemoryManager memory_manager{};
40 RuntimeModule runtime_module{};
41 bool dealloc_input = true;
43 // Load model with single op
44 auto *model_data_raw = reinterpret_cast<const char *>(test_data_base->get_model_ptr());
45 ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
47 auto *main_runtime_graph = runtime_module.getMainGraph();
48 assert(main_runtime_graph->getNumOfInputTensors() == 2);
50 // set left input data
52 auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(0));
53 std::copy(test_data_base->get_input_data_by_index(0).begin(),
54 test_data_base->get_input_data_by_index(0).end(), input_tensor_data);
57 // set right input data
59 auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(1));
60 std::copy(test_data_base->get_input_data_by_index(1).begin(),
61 test_data_base->get_input_data_by_index(1).end(), input_tensor_data);
64 runtime_module.execute();
66 assert(main_runtime_graph->getNumOfOutputTensors() == 1);
68 T *output_data = reinterpret_cast<T *>(main_runtime_graph->getOutputDataByIndex(0));
69 const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(T));
70 std::vector<T> output_data_vector(output_data, output_data + num_elements);
71 return output_data_vector;
74 TEST_F(SubTest, Float_P)
78 const bool is_with_broadcast = false;
79 test_kernel::TestDataFloatSub test_data_float_kernel(is_with_broadcast);
80 std::vector<float> output_data_vector = checkSubKernel(&test_data_float_kernel);
81 EXPECT_THAT(output_data_vector, kernels::testing::FloatArrayNear(
82 test_data_float_kernel.get_output_data_by_index(0), 0.0001f));
86 const bool is_with_broadcast = true;
87 test_kernel::TestDataFloatSub test_data_float_kernel(is_with_broadcast);
88 std::vector<float> output_data_vector = checkSubKernel(&test_data_float_kernel);
89 EXPECT_THAT(output_data_vector, kernels::testing::FloatArrayNear(
90 test_data_float_kernel.get_output_data_by_index(0), 0.0001f));
94 TEST_F(SubTest, INT_P)
98 const bool is_with_broadcast = false;
99 test_kernel::TestDataIntSub test_data_kernel(is_with_broadcast);
100 const auto output_data_vector = checkSubKernel<int32_t>(&test_data_kernel);
101 EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
105 const bool is_with_broadcast = true;
106 test_kernel::TestDataIntSub test_data_kernel(is_with_broadcast);
107 const auto output_data_vector = checkSubKernel<int32_t>(&test_data_kernel);
108 EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
112 TEST_F(SubTest, Inputs_type_mismatch_NEG)
114 test_kernel::NegTestDataInputsTypeMismatchSubKernel test_data_kernel;
115 MemoryManager memory_manager{};
116 RuntimeModule runtime_module{};
117 bool dealloc_input = true;
118 // Load model with single op
119 auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
120 EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
124 TEST_F(SubTest, Input_output_type_mismatch_NEG)
126 test_kernel::NegTestDataInputOutputTypeMismatchSubKernel test_data_kernel;
127 MemoryManager memory_manager{};
128 RuntimeModule runtime_module{};
129 bool dealloc_input = true;
130 // Load model with single op
131 auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
132 EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
136 TEST_F(SubTest, No_quant_params_NEG)
138 test_kernel::NegTestDataNoQuantParamsSubKernel test_data_kernel;
139 MemoryManager memory_manager{};
140 RuntimeModule runtime_module{};
141 bool dealloc_input = true;
142 // Load model with single op
143 auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
144 EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
148 // TODO: add tests for U8 and S16
149 // TODO: add tests for inplace optimizations for all types
152 } // namespace luci_interpreter