c23cbb782e3fcadbe0d65e097ecc3e4c3564a7c9
[platform/core/ml/nnfw.git] / runtime / onert / core / src / interp / operations / Logistic.cc
1 /*
2  * Copyright (c) 2019 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 <cker/operation/Logistic.h>
18
19 #include "OperationUtil.h"
20
21 #include "interp/Registration.h"
22 #include "ir/operation/Logistic.h"
23
24 namespace onert
25 {
26 namespace interp
27 {
28 namespace
29 {
30
31 void prepareLogistic(ExecEnv *env, const ir::Operation &node)
32 {
33   const auto input_index = node.getInputs().at(0);
34   const auto output_index = node.getOutputs().at(0);
35
36   const auto input_tensor = env->tensorAt(input_index);
37
38   const auto output_info = env->graph().operands().at(output_index).info();
39
40   // Check shape and type lhs is same with rhs
41   // TODO Util function to compare TensorInfo
42   if (output_info.total_size() == 0)
43   {
44     throw std::runtime_error{"Interp(TConv): NYI unspecified output shape"};
45   }
46   else
47   {
48     env->allocateIfNeeded(output_index, output_info);
49   }
50
51   const auto output_tensor = env->tensorAt(output_index);
52   if (input_tensor->data_type() != output_tensor->data_type())
53   {
54     throw std::runtime_error{"Interp(Logistic): Invalid output type"};
55   }
56 }
57
58 void invoke(const ITensor *input_tensor, const ITensor *output_tensor)
59 {
60   const auto input_buffer = input_tensor->bufferRO();
61   auto output_buffer = output_tensor->buffer();
62
63   const auto cker_input_shape = convertShape(input_tensor->tensorInfo().shape());
64   const auto cker_output_shape = convertShape(output_tensor->tensorInfo().shape());
65   const float *input_ptr = reinterpret_cast<const float *>(input_buffer);
66   float *output_ptr = reinterpret_cast<float *>(output_buffer);
67
68   nnfw::cker::Logistic(cker_input_shape, input_ptr, cker_output_shape, output_ptr);
69 }
70
71 void invokeLogistic(const ExecEnv *env, const ir::Operation &node)
72 {
73   const auto input_index = node.getInputs().at(0);
74   const auto output_index = node.getOutputs().at(0);
75
76   const auto input_tensor = env->tensorAt(input_index);
77   const auto output_tensor = env->tensorAt(output_index);
78
79   const auto data_type = input_tensor->data_type();
80
81   if (data_type == ir::DataType::FLOAT32)
82   {
83     invoke(input_tensor, output_tensor);
84   }
85   else
86   {
87     throw std::runtime_error{"Interp(Logistic): NYI - Unsupported data type"};
88   }
89 }
90 } // namespace
91
92 OpKernel *getLogistic()
93 {
94   static OpKernel kernel = {prepareLogistic, invokeLogistic};
95   return &kernel;
96 }
97
98 } // namespace interp
99 } // namespace onert