Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / LeakyRelu.cpp
index ab7072f..7f032b5 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/LeakyRelu.h"
-
+#include "Builders.h"
 #include "kernels/Utils.h"
+#include "SISOKernel.h"
 
-#include <tensorflow/lite/kernels/internal/reference/leaky_relu.h>
-
-#include "PALLeakyRelu.h"
+#include "PALReluCommon.h"
 
 namespace luci_interpreter
 {
 
-namespace kernels
+void configure_kernel_CircleLeakyRelu(const circle::Operator *cur_op,
+                                      BaseRuntimeGraph *runtime_graph)
 {
+  kernels::SISOKernel kernel(cur_op, runtime_graph);
 
-LeakyRelu::LeakyRelu(const Tensor *input, Tensor *output, const LeakyReluParams &params)
-  : KernelWithParams<LeakyReluParams>({input}, {output}, params)
-{
+  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 LeakyRelu::configure()
+void execute_kernel_CircleLeakyRelu(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
 {
-  LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
-  if (input()->element_type() == DataType::U8)
-  {
-    double alpha_multiplier = input()->scale() * params().alpha / output()->scale();
-    quantizeMultiplier(alpha_multiplier, &_output_multiplier_alpha, &_output_shift_alpha);
-    double identity_multiplier = input()->scale() / output()->scale();
-    quantizeMultiplier(identity_multiplier, &_output_multiplier_identity, &_output_shift_identity);
-  }
-  // TODO: enable it only if kernel with dynamic shapes
-  output()->resize(input()->shape());
-}
+  kernels::SISOKernel kernel(cur_op, runtime_graph);
 
-void LeakyRelu::execute() const
-{
-  switch (input()->element_type())
+  const auto *input_data = runtime_graph->getDataByTensor(kernel.input());
+  assert(input_data);
+
+  auto *output_data = runtime_graph->getDataByTensor(kernel.output());
+
+  bool is_inplace = runtime_graph->is_inplace_op(cur_op);
+
+  const auto options = cur_op->builtin_options_as_LeakyReluOptions();
+
+  switch (Tensor::element_type(kernel.input()))
   {
+#ifndef DIS_FLOAT
     case DataType::FLOAT32:
-      evalFloat();
-      break;
-    case DataType::U8:
-      evalQuantized();
+    {
+      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,
+                                       options->alpha(), false);
       break;
+    }
+#endif // DIS_FLOAT
     default:
-      assert(false && "Unsupported type.");
+      assert(false && "Unsupported type");
   }
-}
-
-void LeakyRelu::evalFloat() const
-{
-  tflite::LeakyReluParams op_params{};
-  op_params.alpha = params().alpha;
-  luci_interpreter_pal::LeakyRelu(op_params, getTensorShape(input()), getTensorData<float>(input()),
-                                  getTensorShape(output()), getTensorData<float>(output()));
-}
 
-void LeakyRelu::evalQuantized() const
-{
-  tflite::LeakyReluParams op_params{};
-  op_params.input_offset = input()->zero_point();
-  op_params.output_offset = output()->zero_point();
-  op_params.output_multiplier_alpha = _output_multiplier_alpha;
-  op_params.output_shift_alpha = _output_shift_alpha;
-  op_params.output_multiplier_identity = _output_multiplier_identity;
-  op_params.output_shift_identity = _output_shift_identity;
-
-  tflite::reference_ops::QuantizeLeakyRelu(
-    op_params, getTensorShape(input()), getTensorData<uint8_t>(input()), getTensorShape(output()),
-    getTensorData<uint8_t>(output()));
+  if (is_inplace)
+    runtime_graph->makeInplaceOperation(kernel.input(), kernel.output());
 }
-
-} // namespace kernels
 } // namespace luci_interpreter