Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Gather.test.cpp
index dacf366..c1fa5ef 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * 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/Gather.h"
 #include "kernels/TestUtils.h"
-#include "luci_interpreter/TestMemoryManager.h"
+#include "luci_interpreter/test_models/gather/FloatGatherKernel.h"
+#include "luci_interpreter/test_models/gather/IntGatherKernel.h"
+#include "luci_interpreter/test_models/gather/NegGatherKernel.h"
+
+#include "loader/ModuleLoader.h"
 
 namespace luci_interpreter
 {
-namespace kernels
-{
 namespace
 {
 
@@ -30,109 +30,93 @@ using namespace testing;
 
 class GatherTest : public ::testing::Test
 {
-protected:
-  void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
-
-  std::unique_ptr<IMemoryManager> _memory_manager;
+  // Do nothing
 };
 
-TEST_F(GatherTest, Simple)
+template <typename T> std::vector<T> checkGatherKernel(test_kernel::TestDataBase<T> *test_data_base)
 {
-  std::vector<float> params_data{1.f, 2.f, 3.f, 4.f, 5.f, 6.f};
-  std::vector<int32_t> indices_data{1, 0, 1, 5};
-  std::vector<float> ref_output_data{2.f, 1.f, 2.f, 6.f};
-
-  Tensor params_tensor =
-    makeInputTensor<DataType::FLOAT32>({1, 6}, params_data, _memory_manager.get());
-  Tensor indices_tensor = makeInputTensor<DataType::S32>({4}, indices_data, _memory_manager.get());
-  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
-  GatherParams gparams;
-
-  gparams.axis = 1;
-  gparams.batch_dims = 0;
-
-  Gather kernel(&params_tensor, &indices_tensor, &output_tensor, gparams);
-  kernel.configure();
-  _memory_manager->allocate_memory(output_tensor);
-  kernel.execute();
-
-  EXPECT_THAT(extractTensorData<float>(output_tensor),
-              ::testing::ElementsAreArray(ref_output_data));
-  EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 4}));
+  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_base->get_model_ptr());
+  ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
+
+  auto *main_runtime_graph = runtime_module.getMainGraph();
+  assert(main_runtime_graph->getNumOfInputTensors() == 1);
+
+  // 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);
+  }
+
+  runtime_module.execute();
+
+  assert(main_runtime_graph->getNumOfOutputTensors() == 1);
+
+  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(GatherTest, Simple_Batch)
+TEST_F(GatherTest, Gather_Float_P)
 {
-  Shape params_shape = {3, 5};
-  Shape indices_shape = {3, 2};
-  std::vector<float> params_data{0., 0., 1., 0., 2., 3., 0., 0., 0., 4., 0., 5., 0., 6., 0.};
-  std::vector<int32_t> indices_data{2, 4, 0, 4, 1, 3};
-  std::vector<float> ref_output_data{1., 2., 3., 4., 5., 6.};
-
-  Tensor params_tensor =
-    makeInputTensor<DataType::FLOAT32>(params_shape, params_data, _memory_manager.get());
-  Tensor indices_tensor =
-    makeInputTensor<DataType::S32>(indices_shape, indices_data, _memory_manager.get());
-  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
-  GatherParams gparams;
-
-  gparams.axis = 1;
-  gparams.batch_dims = 1;
-
-  Gather kernel(&params_tensor, &indices_tensor, &output_tensor, gparams);
-  kernel.configure();
-  _memory_manager->allocate_memory(output_tensor);
-  kernel.execute();
-
-  EXPECT_THAT(extractTensorData<float>(output_tensor),
-              ::testing::ElementsAreArray(ref_output_data));
-  EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({3, 2}));
+  test_kernel::TestDataFloatGather test_data_float_gather;
+  std::vector<float> output_data_vector = checkGatherKernel(&test_data_float_gather);
+  EXPECT_THAT(output_data_vector, kernels::testing::FloatArrayNear(
+                                    test_data_float_gather.get_output_data_by_index(0), 0.0001f));
 }
 
-TEST_F(GatherTest, Simple_NEG)
+TEST_F(GatherTest, Gather_Int_P)
 {
-  Tensor params_tensor = makeInputTensor<DataType::S32>({1}, {1}, _memory_manager.get());
-  Tensor indices_tensor = makeInputTensor<DataType::S32>({1}, {0}, _memory_manager.get());
-  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
-  GatherParams gparams;
-
-  Gather kernel(&params_tensor, &indices_tensor, &output_tensor, gparams);
-  EXPECT_ANY_THROW(kernel.configure());
+  test_kernel::TestDataIntGather test_data_int_gather;
+  std::vector<int32_t> output_data_vector = checkGatherKernel(&test_data_int_gather);
+  EXPECT_THAT(output_data_vector, test_data_int_gather.get_output_data_by_index(0));
 }
 
-TEST_F(GatherTest, Axis_NEG)
+TEST_F(GatherTest, Input_output_type_mismatch_NEG)
 {
-  Tensor params_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.f}, _memory_manager.get());
-  Tensor indices_tensor = makeInputTensor<DataType::S32>({1}, {0}, _memory_manager.get());
-  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
-  GatherParams gparams;
-
-  gparams.axis = 100;
-  gparams.batch_dims = 0;
-
-  Gather kernel(&params_tensor, &indices_tensor, &output_tensor, gparams);
-  EXPECT_ANY_THROW(kernel.configure());
+  test_kernel::NegTestDataInputOutputTypeMismatchGatherKernel 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(GatherTest, Batch_NEG)
+TEST_F(GatherTest, Wrong_position_type_NEG)
 {
-  std::vector<float> params_data{1.f, 2.f, 3.f, 4.f, 5.f, 6.f};
-  std::vector<int32_t> indices_data{1, 0, 1, 5};
-  std::vector<float> ref_output_data{2.f, 1.f, 2.f, 6.f};
-
-  Tensor params_tensor =
-    makeInputTensor<DataType::FLOAT32>({1, 6}, params_data, _memory_manager.get());
-  Tensor indices_tensor = makeInputTensor<DataType::S32>({4}, indices_data, _memory_manager.get());
-  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
-  GatherParams gparams;
-
-  gparams.axis = 0;
-  gparams.batch_dims = 1;
+  test_kernel::NegTestDataWrongPositionTypeGatherKernel 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),
+               "");
+}
 
-  Gather kernel(&params_tensor, &indices_tensor, &output_tensor, gparams);
-  EXPECT_ANY_THROW(kernel.configure());
+TEST_F(GatherTest, Wrong_axis_NEG)
+{
+  test_kernel::NegTestDataWrongAxisGatherKernel 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),
+               "");
 }
+// TODO: add S8 test
 
 } // namespace
-} // namespace kernels
 } // namespace luci_interpreter