/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
- * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include "kernels/LeakyRelu.h"
#include "kernels/TestUtils.h"
-#include "luci_interpreter/TestMemoryManager.h"
+#include "luci_interpreter/test_models/leaky_relu/FloatLeakyReLUKernel.h"
+#include "luci_interpreter/test_models/leaky_relu/NegLeakyReLUKernel.h"
+
+#include "loader/ModuleLoader.h"
namespace luci_interpreter
{
-namespace kernels
-{
namespace
{
using namespace testing;
-template <typename T>
-void Check(std::initializer_list<int32_t> input_shape, std::initializer_list<int32_t> output_shape,
- std::initializer_list<float> input_data, std::initializer_list<float> output_data,
- float alpha)
+class LeakyReLUTest : public ::testing::Test
{
- std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
- constexpr DataType element_type = getElementType<T>();
- Tensor input_tensor =
- makeInputTensor<element_type>(input_shape, input_data, memory_manager.get());
- Tensor output_tensor = makeOutputTensor(element_type);
-
- LeakyReluParams params{};
- params.alpha = alpha;
-
- LeakyRelu kernel(&input_tensor, &output_tensor, params);
-
- kernel.configure();
- memory_manager->allocate_memory(output_tensor);
- kernel.execute();
-
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
- EXPECT_THAT(extractTensorData<T>(output_tensor), ::testing::ElementsAreArray(output_data));
-}
+ // Do nothing
+};
-template <>
-void Check<uint8_t>(std::initializer_list<int32_t> input_shape,
- std::initializer_list<int32_t> output_shape,
- std::initializer_list<float> input_data,
- std::initializer_list<float> output_data, float alpha)
+template <typename T>
+std::vector<T> checkLeakyReLUKernel(test_kernel::TestDataBase<T> *test_data_base)
{
- std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
- const float quantized_tolerance = getTolerance(-8, 127.f / 16.f, 255);
- std::pair<float, int32_t> quant_param = quantizationParams<uint8_t>(-8, 127.f / 16.f);
- Tensor input_tensor = makeInputTensor<DataType::U8>(
- input_shape, quant_param.first, quant_param.second, input_data, memory_manager.get());
- Tensor output_tensor = makeOutputTensor(DataType::U8, quant_param.first, quant_param.second);
+ MemoryManager memory_manager{};
+ RuntimeModule runtime_module{};
+ bool dealloc_input = true;
- LeakyReluParams params{};
- params.alpha = alpha;
+ // Load model with single op
+ auto *model_data_raw = reinterpret_cast<const char *>(test_data_base->get_model_ptr());
+ ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
- LeakyRelu kernel(&input_tensor, &output_tensor, params);
+ auto *main_runtime_graph = runtime_module.getMainGraph();
+ assert(main_runtime_graph->getNumOfInputTensors() == 1);
- kernel.configure();
- memory_manager->allocate_memory(output_tensor);
- kernel.execute();
+ // Set input data
+ {
+ auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(0));
+ std::copy(test_data_base->get_input_data_by_index(0).begin(),
+ test_data_base->get_input_data_by_index(0).end(), input_tensor_data);
+ }
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
- EXPECT_THAT(dequantizeTensorData(output_tensor),
- FloatArrayNear(output_data, quantized_tolerance));
-}
+ runtime_module.execute();
-template <typename T> class LeakReluTest : public ::testing::Test
-{
-};
+ assert(main_runtime_graph->getNumOfOutputTensors() == 1);
-using DataTypes = ::testing::Types<float, uint8_t>;
-TYPED_TEST_SUITE(LeakReluTest, DataTypes);
+ T *output_data = reinterpret_cast<T *>(main_runtime_graph->getOutputDataByIndex(0));
+ const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(T));
+ std::vector<T> output_data_vector(output_data, output_data + num_elements);
+ return output_data_vector;
+}
-TYPED_TEST(LeakReluTest, Simple)
+TEST_F(LeakyReLUTest, Float_P)
{
- Check<TypeParam>(/*input_shape=*/{2, 3}, /*output_shape=*/{2, 3},
- /*input_data=*/
- {
- 0.0f, 1.0f, 3.0f, // Row 1
- 1.0f, -1.0f, -2.0f, // Row 2
- },
- /*output_data=*/
- {
- 0.0f, 1.0f, 3.0f, // Row 1
- 1.0f, -0.5f, -1.0f, // Row 2
- },
- /*alpha=*/0.5f);
-
- SUCCEED();
+ test_kernel::TestDataFloatLeakyReLU test_data_kernel;
+ std::vector<float> output_data_vector = checkLeakyReLUKernel(&test_data_kernel);
+ EXPECT_THAT(output_data_vector, kernels::testing::FloatArrayNear(
+ test_data_kernel.get_output_data_by_index(0), 0.0001f));
}
-TEST(LeakReluTest, IvalidInputOutputType_NEG)
+TEST_F(LeakyReLUTest, Input_output_type_mismatch_NEG)
{
- std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
- Tensor input_tensor = makeInputTensor<DataType::FLOAT32>({2, 3},
- {
- 0.0f, 1.0f, 3.0f, // Row 1
- 1.0f, -1.0f, -2.0f, // Row 2
- },
- memory_manager.get());
- Tensor output_tensor = makeOutputTensor(DataType::U8);
-
- LeakyReluParams params{};
- params.alpha = 0.5f;
-
- LeakyRelu kernel(&input_tensor, &output_tensor, params);
-
- EXPECT_ANY_THROW(kernel.configure());
+ test_kernel::NegTestDataInputOutputTypeMismatchLeakyReLUKernel test_data_kernel;
+ MemoryManager memory_manager{};
+ RuntimeModule runtime_module{};
+ bool dealloc_input = true;
+ // Load model with single op
+ auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
+ EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
+ "");
}
} // namespace
-} // namespace kernels
} // namespace luci_interpreter