Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Relu.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "Builders.h"
18 #include "kernels/Utils.h"
19 #include "SISOKernel.h"
20
21 #include "PALReluCommon.h"
22
23 namespace luci_interpreter
24 {
25
26 void configure_kernel_CircleRelu(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
27 {
28   kernels::SISOKernel kernel(cur_op, runtime_graph);
29
30   LUCI_INTERPRETER_CHECK(Tensor::element_type(kernel.input()) ==
31                          Tensor::element_type(kernel.output()));
32   LUCI_INTERPRETER_CHECK(Tensor::num_dims(kernel.input()) == Tensor::num_dims(kernel.output()));
33   LUCI_INTERPRETER_CHECK(Tensor::num_elements(kernel.input()) ==
34                          Tensor::num_elements(kernel.output()));
35 }
36
37 void execute_kernel_CircleRelu(const circle::Operator *cur_op, BaseRuntimeGraph *runtime_graph)
38 {
39   kernels::SISOKernel kernel(cur_op, runtime_graph);
40
41   const auto *input_data = runtime_graph->getDataByTensor(kernel.input());
42   assert(input_data);
43
44   auto *output_data = runtime_graph->getDataByTensor(kernel.output());
45
46   bool is_inplace = runtime_graph->is_inplace_op(cur_op);
47
48   switch (Tensor::element_type(kernel.input()))
49   {
50 #ifndef DIS_FLOAT
51     case DataType::FLOAT32:
52     {
53       const float *input_data_float = kernels::getTensorData<float>(input_data);
54       float *output_data_float = kernels::getTensorData<float>(output_data);
55       if (is_inplace)
56       {
57         output_data_float = const_cast<float *>(input_data_float);
58       }
59
60       assert(output_data_float);
61       const int flat_size =
62         kernels::getTensorRuntimeShape(kernel.input(), runtime_graph).flatSize();
63
64       luci_interpreter_pal::ReLUCommon(flat_size, input_data_float, output_data_float, 0.0f, false);
65       break;
66     }
67 #endif // DIS_FLOAT
68     default:
69       assert(false && "Unsupported type");
70   }
71
72   if (is_inplace)
73     runtime_graph->makeInplaceOperation(kernel.input(), kernel.output());
74 }
75
76 } // namespace luci_interpreter