/*
* Copyright (c) 2022 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/Fill.h"
#include "kernels/TestUtils.h"
-#include "luci_interpreter/TestMemoryManager.h"
+#include "luci_interpreter/test_models/fill/FillKernel.h"
+#include "luci_interpreter/test_models/fill/NegFillKernel.h"
+
+#include "loader/ModuleLoader.h"
namespace luci_interpreter
{
-namespace kernels
-{
namespace
{
class FillTest : public ::testing::Test
{
-protected:
- void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
-
- std::unique_ptr<IMemoryManager> _memory_manager;
+ // Do nothing
};
-template <typename T, DataType DT> void runFillIntKernel(IMemoryManager *memory_manager)
+template <typename T> std::vector<T> checkFillKernel(test_kernel::TestDataBase<T> *test_data_base)
{
- Shape dims_shape{2};
-
- std::vector<int32_t> dims_data = {2, 3};
- std::vector<T> value_data = {5};
-
- Tensor dims = makeInputTensor<loco::DataType::S32>(dims_shape, dims_data, memory_manager);
- Tensor value = makeInputTensor<DT>(/*scalar*/ {}, value_data, memory_manager);
-
- Tensor output_tensor = makeOutputTensor(DT);
-
- Fill kernel(&dims, &value, &output_tensor);
-
- kernel.configure();
- memory_manager->allocate_memory(output_tensor);
- kernel.execute();
-
- std::vector<T> ref_output_data{5, 5, 5, 5, 5, 5};
- EXPECT_THAT(extractTensorData<T>(output_tensor), ref_output_data);
-
- std::vector<int32_t> ref_output_shape{2, 3};
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
-}
-
-template <DataType DT> void runFillQuantIntKernel(IMemoryManager *memory_manager)
-{
- Shape dims_shape{2};
-
- std::vector<int32_t> dims_data = {2, 3};
- std::vector<float> value_data = {5};
-
- int32_t zero_point = 0;
-
- if (DT == loco::DataType::S8)
- zero_point = 1;
-
- Tensor dims = makeInputTensor<loco::DataType::S32>(dims_shape, dims_data, memory_manager);
- Tensor value = makeInputTensor<DT>(/*scalar*/ {}, /*scale*/ 0.25, /*zero_point*/ zero_point,
- value_data, memory_manager);
+ MemoryManager memory_manager{};
+ RuntimeModule runtime_module{};
+ bool dealloc_input = true;
- Tensor output_tensor = makeOutputTensor(DT, /*scale*/ 0.25, /*zero_point*/ zero_point);
+ // 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);
- Fill kernel(&dims, &value, &output_tensor);
+ 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);
+ }
- std::vector<float> ref_output_data{5, 5, 5, 5, 5, 5};
- EXPECT_THAT(dequantizeTensorData(output_tensor), FloatArrayNear(ref_output_data));
+ runtime_module.execute();
- std::vector<int32_t> ref_output_shape{2, 3};
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
-}
+ assert(main_runtime_graph->getNumOfOutputTensors() == 1);
-TEST_F(FillTest, FillInt)
-{
- // Run for int32_t input
- runFillIntKernel<int32_t, loco::DataType::S32>(_memory_manager.get());
- // Run for int64_t input
- runFillIntKernel<int64_t, loco::DataType::S64>(_memory_manager.get());
- // Run for int8_t input
- runFillQuantIntKernel<loco::DataType::S8>(_memory_manager.get());
- // Run for int16_t input
- runFillQuantIntKernel<loco::DataType::S16>(_memory_manager.get());
-
- SUCCEED();
+ 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;
}
-TEST_F(FillTest, FillFloat)
+TEST_F(FillTest, MainTest_P)
{
- Shape dims_shape{3};
-
- std::vector<int64_t> dims_data = {2, 2, 2};
- std::vector<float> value_data = {5};
-
- Tensor dims = makeInputTensor<loco::DataType::S64>(dims_shape, dims_data, _memory_manager.get());
- Tensor value =
- makeInputTensor<loco::DataType::FLOAT32>(/*scalar*/ {}, value_data, _memory_manager.get());
-
- Tensor output_tensor = makeOutputTensor(loco::DataType::FLOAT32);
-
- Fill kernel(&dims, &value, &output_tensor);
-
- kernel.configure();
- _memory_manager->allocate_memory(output_tensor);
- kernel.execute();
-
- std::vector<float> ref_output_data{5, 5, 5, 5, 5, 5, 5, 5};
-
- std::vector<int32_t> ref_output_shape{2, 2, 2};
- EXPECT_THAT(extractTensorData<float>(output_tensor), ref_output_data);
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
+ test_kernel::TestDataFillKernel<float> test_data_kernel;
+ std::vector<float> output_data_vector = checkFillKernel(&test_data_kernel);
+ EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
}
-TEST_F(FillTest, Invalid_Input_Shape_NEG)
+TEST_F(FillTest, Input_output_type_mismatch_NEG)
{
- Shape dims_shape{1, 3};
-
- std::vector<int32_t> dims_data = {2, 2, 2};
- std::vector<float> value_data = {5};
-
- Tensor dims = makeInputTensor<loco::DataType::S32>(dims_shape, dims_data, _memory_manager.get());
- Tensor value =
- makeInputTensor<loco::DataType::FLOAT32>(/*scalar*/ {}, value_data, _memory_manager.get());
-
- Tensor output_tensor = makeOutputTensor(loco::DataType::FLOAT32);
-
- Fill kernel(&dims, &value, &output_tensor);
- EXPECT_ANY_THROW(kernel.configure());
+ test_kernel::NegTestDataInputTypeMismatchFillKernel 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),
+ "");
}
-TEST_F(FillTest, Invalid_Value_Shape_NEG)
+TEST_F(FillTest, Wrong_input_shape_NEG)
{
- Shape dims_shape{3};
-
- std::vector<int32_t> dims_data = {2, 2, 2};
- std::vector<float> value_data = {5};
-
- Tensor dims = makeInputTensor<loco::DataType::S32>(dims_shape, dims_data, _memory_manager.get());
- Tensor value = makeInputTensor<loco::DataType::FLOAT32>({1}, value_data, _memory_manager.get());
-
- Tensor output_tensor = makeOutputTensor(loco::DataType::FLOAT32);
-
- Fill kernel(&dims, &value, &output_tensor);
- EXPECT_ANY_THROW(kernel.configure());
+ test_kernel::NegTestDataWrongInputShapeFillKernel 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