Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Relu.cpp
index 74c2c0f..c38f332 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * 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/Relu.h"
+#include "Builders.h"
 #include "kernels/Utils.h"
+#include "SISOKernel.h"
 
-#include "PALRelu.h"
+#include "PALReluCommon.h"
 
 namespace luci_interpreter
 {
 
-namespace kernels
+void configure_kernel_CircleRelu(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
 {
+  kernels::SISOKernel kernel(cur_op, runtime_graph);
 
-Relu::Relu(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}
+  LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input()) ==
+                         Tensor::element_type(kernel.output()));
+  LUCI_INTERPRETER_CHECK(Tensor::num_dims(kernel.input()) == Tensor::num_dims(kernel.output()));
+  LUCI_INTERPRETER_CHECK(Tensor::num_elements(kernel.input()) ==
+                         Tensor::num_elements(kernel.output()));
+}
 
-void Relu::configure()
+void execute_kernel_CircleRelu(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
 {
-  LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
-  if (input()->element_type() == DataType::S16)
-  {
-    LUCI_INTERPRETER_CHECK(input()->zero_point() == 0 && output()->zero_point() == 0);
-  }
+  kernels::SISOKernel kernel(cur_op, runtime_graph);
 
-  if (input()->element_type() == DataType::U8 || input()->element_type() == DataType::S16)
-  {
-    double multiplier = input()->scale() / output()->scale();
-    quantizeMultiplier(multiplier, &_output_multiplier, &_output_shift);
-  }
-  // TODO: enable it only if kernel with dynamic shapes
-  output()->resize(input()->shape());
-}
+  const auto *input_data = runtime_graph->getDataByTensor(kernel.input());
+  assert(input_data);
 
-void Relu::execute() const
-{
-  switch (input()->element_type())
+  auto *output_data = runtime_graph->getDataByTensor(kernel.output());
+
+  bool is_inplace = runtime_graph->is_inplace_op(cur_op);
+
+  switch (Tensor::element_type(kernel.input()))
   {
+#ifndef DIS_FLOAT
     case DataType::FLOAT32:
-      evalFloat();
-      break;
-    case DataType::U8:
-      evalQuantized();
-      break;
-    case DataType::S16:
-      evalQuantizedS16();
+    {
+      const float *input_data_float = kernels::getTensorData<float>(input_data);
+      float *output_data_float = kernels::getTensorData<float>(output_data);
+      if (is_inplace)
+      {
+        output_data_float = const_cast<float *>(input_data_float);
+      }
+
+      assert(output_data_float);
+      const int flat_size =
+        kernels::getTensorRuntimeShape(kernel.input(), runtime_graph).flatSize();
+
+      luci_interpreter_pal::ReLUCommon(flat_size, input_data_float, output_data_float, 0.0f, false);
       break;
+    }
+#endif // DIS_FLOAT
     default:
-      assert(false && "Unsupported type.");
+      assert(false && "Unsupported type");
   }
-}
-
-void Relu::evalFloat() const
-{
-  const auto input_data = getTensorData<float>(input());
-  const auto input_shape = getTensorShape(input());
-  auto output_data = getTensorData<float>(output());
-  auto output_shape = getTensorShape(output());
 
-  luci_interpreter_pal::Relu(input_shape, input_data, output_shape, output_data);
-}
-
-void Relu::evalQuantized() const
-{
-  tflite::ReluParams params;
-  params.input_offset = input()->zero_point();
-  params.output_offset = output()->zero_point();
-  params.output_multiplier = _output_multiplier;
-  params.output_shift = _output_shift;
-
-  params.quantized_activation_min =
-    std::max(static_cast<int32_t>(std::numeric_limits<uint8_t>::min()), params.output_offset);
-  params.quantized_activation_max = static_cast<int32_t>(std::numeric_limits<uint8_t>::max());
-
-  luci_interpreter_pal::ReluX(params, getTensorShape(input()), getTensorData<uint8_t>(input()),
-                              getTensorShape(output()), getTensorData<uint8_t>(output()));
-}
-
-void Relu::evalQuantizedS16() const
-{
-  const auto *input_data = getTensorData<int16_t>(input());
-  auto *output_data = getTensorData<int16_t>(output());
-
-  constexpr int32_t output_min = 0;
-  constexpr int32_t output_max = std::numeric_limits<int16_t>::max();
-
-  const int32_t num_elements = input()->shape().num_elements();
-
-  for (int32_t i = 0; i < num_elements; ++i)
-  {
-    const int32_t input_val = input_data[i];
-    int32_t output_val =
-      tflite::MultiplyByQuantizedMultiplier(input_val, _output_multiplier, _output_shift);
-    output_val = std::max(output_val, output_min);
-    output_val = std::min(output_val, output_max);
-    output_data[i] = static_cast<int16_t>(output_val);
-  }
+  if (is_inplace)
+    runtime_graph->makeInplaceOperation(kernel.input(), kernel.output());
 }
 
-} // namespace kernels
 } // namespace luci_interpreter