Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Pack.test.cpp
index cd5cff9..db820b5 100644 (file)
@@ -1,6 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
- * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd. 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/Pack.h"
 #include "kernels/TestUtils.h"
-#include "luci_interpreter/TestMemoryManager.h"
+#include "luci_interpreter/test_models/pack/PackKernel.h"
+
+#include "loader/ModuleLoader.h"
 
 namespace luci_interpreter
 {
-namespace kernels
-{
 namespace
 {
 
 using namespace testing;
 
-template <typename T>
-void Check(std::vector<std::initializer_list<int32_t>> input_shapes,
-           std::initializer_list<int32_t> output_shape, std::vector<std::vector<T>> input_datas,
-           std::initializer_list<T> output_data, int32_t axis)
+class PackTest : public ::testing::Test
 {
-  std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
-  constexpr DataType element_type = getElementType<T>();
-  std::vector<const Tensor *> inputs(input_datas.size());
-  std::vector<Tensor> tmp_inputs;
-  for (int i = 0; i < input_datas.size(); i++)
-  {
-    if (std::is_same<T, float>::value || std::is_same<T, int32_t>::value ||
-        std::is_same<T, int64_t>::value)
-    {
-      tmp_inputs.push_back(Tensor(element_type, input_shapes[i], {}, ""));
-      memory_manager->allocate_memory(tmp_inputs[i]);
-      tmp_inputs[i].writeData(input_datas[i].data(), input_datas[i].size() * sizeof(T));
-    }
-    else if (std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value)
-    {
-      tmp_inputs.push_back(Tensor(element_type, input_shapes[i], {{1.0f / 255}, {128}}, ""));
-      memory_manager->allocate_memory(tmp_inputs[i]);
-      tmp_inputs[i].writeData(input_datas[i].data(), input_datas[i].size() * sizeof(T));
-    }
-    else
-    {
-      assert((std::is_same<T, int16_t>::value) && "unexpected dtype is tested");
-      tmp_inputs.push_back(Tensor(element_type, input_shapes[i], {{1.0f}, {0}}, ""));
-      memory_manager->allocate_memory(tmp_inputs[i]);
-      tmp_inputs[i].writeData(input_datas[i].data(), input_datas[i].size() * sizeof(T));
-    }
-  }
-  for (int i = 0; i < input_datas.size(); i++)
-  {
-    inputs[i] = &tmp_inputs[i];
-  }
+  // Do nothing
+};
+
+template <typename T> std::vector<T> checkPackKernel(test_kernel::TestDataBase<T> *test_data_base)
+{
+  MemoryManager memory_manager{};
+  RuntimeModule runtime_module{};
+  bool dealloc_input = true;
 
-  Tensor output_tensor = makeOutputTensor(element_type);
-  if (std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value)
+  // 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() == 2);
+
+  // Set input 1 data
   {
-    output_tensor = makeOutputTensor(element_type, 1.0f / 255, 128);
+    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);
   }
-  else if (std::is_same<T, int16_t>::value)
+
+  // Set input 2 data
   {
-    output_tensor = makeOutputTensor(element_type, 1.0f, 0);
+    auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(1));
+    std::copy(test_data_base->get_input_data_by_index(1).begin(),
+              test_data_base->get_input_data_by_index(1).end(), input_tensor_data);
   }
 
-  PackParams params{};
-  params.axis = axis;
-  params.values_count = input_datas.size();
-  Pack kernel(inputs, &output_tensor, params);
+  runtime_module.execute();
 
-  kernel.configure();
-  memory_manager->allocate_memory(output_tensor);
-  kernel.execute();
+  assert(main_runtime_graph->getNumOfOutputTensors() == 1);
 
-  EXPECT_THAT(extractTensorData<T>(output_tensor), ::testing::ElementsAreArray(output_data));
-  EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
+  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;
 }
 
-template <typename T> class PackTest : public ::testing::Test
+TEST_F(PackTest, Float_P)
 {
-};
-
-using DataTypes = ::testing::Types<uint8_t, int8_t, int16_t, int32_t, int64_t, float>;
-TYPED_TEST_SUITE(PackTest, DataTypes);
-
-TYPED_TEST(PackTest, ThreeInputs)
-{
-  Check<TypeParam>(/*input_shapes=*/{{2}, {2}, {2}},
-                   /*output_shape=*/{3, 2},
-                   /*input_datas=*/
-                   {{1, 4}, {2, 5}, {3, 6}},
-                   /*output_data=*/
-                   {1, 4, 2, 5, 3, 6}, /*axis=*/0);
-
-  SUCCEED();
+  test_kernel::TestDataFloatPack test_data_pack_kernel;
+  std::vector<float> output_data_vector = checkPackKernel(&test_data_pack_kernel);
+  EXPECT_THAT(output_data_vector, test_data_pack_kernel.get_output_data_by_index(0));
 }
 
-TYPED_TEST(PackTest, NegAxis)
+TEST_F(PackTest, Int_P)
 {
-  Check<TypeParam>(/*input_shapes=*/{{2}, {2}, {2}},
-                   /*output_shape=*/{2, 3},
-                   /*input_datas=*/
-                   {{1, 4}, {2, 5}, {3, 6}},
-                   /*output_data=*/
-                   {1, 2, 3, 4, 5, 6}, /*axis=*/-1);
-
-  SUCCEED();
+  test_kernel::TestDataIntPack test_data_pack_kernel;
+  std::vector<int32_t> output_data_vector = checkPackKernel(&test_data_pack_kernel);
+  EXPECT_THAT(output_data_vector, test_data_pack_kernel.get_output_data_by_index(0));
 }
 
-TEST(Pack, MismatchingInputValuesCount_NEG)
+TEST_F(PackTest, QuantU8_P)
 {
-  std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
-  std::vector<float> input1_data{1, 4};
-  std::vector<float> input2_data{2, 5};
-  std::vector<float> input3_data{3, 6};
-  Tensor input1_tensor = makeInputTensor<DataType::FLOAT32>({2}, input1_data, memory_manager.get());
-  Tensor input2_tensor = makeInputTensor<DataType::FLOAT32>({2}, input2_data, memory_manager.get());
-  Tensor input3_tensor = makeInputTensor<DataType::FLOAT32>({2}, input3_data, memory_manager.get());
-  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
-  PackParams params{};
-  {
-    params.axis = 0;
-    params.values_count = 2;
-
-    Pack kernel({&input1_tensor, &input2_tensor, &input3_tensor}, &output_tensor, params);
-    EXPECT_ANY_THROW(kernel.configure());
-  }
+  test_kernel::TestDataQuantU8Pack test_data_pack_kernel;
+  std::vector<uint8_t> output_data_vector = checkPackKernel(&test_data_pack_kernel);
+  EXPECT_THAT(output_data_vector, test_data_pack_kernel.get_output_data_by_index(0));
 }
 
-TEST(Pack, InvalidInputAxis_NEG)
-{
-  std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
-  std::vector<float> input1_data{1, 4};
-  std::vector<float> input2_data{2, 5};
-  std::vector<float> input3_data{3, 6};
-  Tensor input1_tensor = makeInputTensor<DataType::FLOAT32>({2}, input1_data, memory_manager.get());
-  Tensor input2_tensor = makeInputTensor<DataType::FLOAT32>({2}, input2_data, memory_manager.get());
-  Tensor input3_tensor = makeInputTensor<DataType::FLOAT32>({2}, input3_data, memory_manager.get());
-  Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
-  PackParams params{};
-  {
-    params.axis = 2;
-    params.values_count = 3;
-
-    Pack kernel({&input1_tensor, &input2_tensor, &input3_tensor}, &output_tensor, params);
-    EXPECT_ANY_THROW(kernel.configure());
-  }
-}
+// TODO: add negative tests?
 
 } // namespace
-} // namespace kernels
 } // namespace luci_interpreter