ea3c1e7cd345031cd81d3717891e81c14fbc3abe
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / ElementwiseBinaryLayer.cc
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 "ElementwiseBinaryLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/LogicalOr.h>
22 #include <cker/operation/MaxMin.h>
23
24 namespace onert
25 {
26 namespace backend
27 {
28 namespace cpu
29 {
30 namespace ops
31 {
32
33 namespace
34 {
35 template <typename T>
36 void logicalOrGeneric(const IPortableTensor *lhs, const IPortableTensor *rhs,
37                       IPortableTensor *output)
38 {
39   if (!HaveSameShapes(lhs, rhs))
40   {
41     nnfw::cker::LogicalOrBroadcast<T>(
42         getTensorShape(lhs), reinterpret_cast<const T *>(lhs->buffer()), getTensorShape(rhs),
43         reinterpret_cast<const T *>(rhs->buffer()), getTensorShape(output),
44         reinterpret_cast<T *>(output->buffer()));
45   }
46   else
47   {
48     nnfw::cker::LogicalOrElementwise<T>(
49         getTensorShape(lhs), reinterpret_cast<const T *>(lhs->buffer()),
50         reinterpret_cast<const T *>(rhs->buffer()), reinterpret_cast<T *>(output->buffer()));
51   }
52 }
53
54 template <typename T>
55 void maximumGeneric(const IPortableTensor *lhs, const IPortableTensor *rhs, IPortableTensor *output)
56 {
57   nnfw::cker::Max<T>(getTensorShape(lhs), reinterpret_cast<const T *>(lhs->buffer()),
58                      getTensorShape(rhs), reinterpret_cast<const T *>(rhs->buffer()),
59                      getTensorShape(output), reinterpret_cast<T *>(output->buffer()));
60 }
61
62 template <typename T>
63 void minimumGeneric(const IPortableTensor *lhs, const IPortableTensor *rhs, IPortableTensor *output)
64 {
65   nnfw::cker::Min<T>(getTensorShape(lhs), reinterpret_cast<const T *>(lhs->buffer()),
66                      getTensorShape(rhs), reinterpret_cast<const T *>(rhs->buffer()),
67                      getTensorShape(output), reinterpret_cast<T *>(output->buffer()));
68 }
69
70 bool haveSameQauntInfo(const IPortableTensor *lhs, const IPortableTensor *rhs,
71                        const IPortableTensor *output)
72 {
73   return (lhs->data_scale() == rhs->data_scale() && lhs->data_scale() == output->data_scale()) &&
74          (lhs->data_offset() == rhs->data_offset() && lhs->data_offset() == output->data_offset());
75 }
76 } // namespace
77
78 void ElementwiseBinaryLayer::configure(const IPortableTensor *lhs, const IPortableTensor *rhs,
79                                        IPortableTensor *output, const ElementwiseBinaryType op_type)
80 {
81   assert(lhs != nullptr);
82   assert(rhs != nullptr);
83   assert(output != nullptr);
84
85   _lhs = lhs;
86   _rhs = rhs;
87   _output = output;
88
89   switch (op_type)
90   {
91     case ElementwiseBinaryType::kLogicalOr:
92       if ((_lhs->data_type() == OperandType::BOOL8) && (_rhs->data_type() == OperandType::BOOL8))
93       {
94         _kernel = logicalOrGeneric<bool>;
95       }
96       else
97       {
98         throw std::runtime_error{"LogicalOr: Unsupported data type"};
99       }
100       break;
101     case ElementwiseBinaryType::kMax:
102       if (_lhs->data_type() == OperandType::QUANT_UINT8_ASYMM)
103       {
104         if (!haveSameQauntInfo(_lhs, _rhs, _output))
105         {
106           throw std::runtime_error("Max NYI for quantized");
107         }
108         _kernel = maximumGeneric<uint8_t>;
109       }
110       else if (_lhs->data_type() == OperandType::FLOAT32)
111       {
112         _kernel = maximumGeneric<float>;
113       }
114       else
115       {
116         throw std::runtime_error{"Max: unsupported data type"};
117       }
118       break;
119     case ElementwiseBinaryType::kMin:
120       if (_lhs->data_type() == OperandType::QUANT_UINT8_ASYMM)
121       {
122         if (!haveSameQauntInfo(_lhs, _rhs, _output))
123         {
124           throw std::runtime_error("Min NYI for quantized");
125         }
126         _kernel = minimumGeneric<uint8_t>;
127       }
128       else if (_lhs->data_type() == OperandType::INT32)
129       {
130         _kernel = minimumGeneric<int32_t>;
131       }
132       else if (_lhs->data_type() == OperandType::FLOAT32)
133       {
134         _kernel = minimumGeneric<float>;
135       }
136       else
137       {
138         throw std::runtime_error{"Min: unsupported data type"};
139       }
140       break;
141     default:
142       throw std::runtime_error{"ElementwiseBinary: Unsupported ElementwiseBinary type"};
143   }
144 }
145
146 void ElementwiseBinaryLayer::run() { _kernel(_lhs, _rhs, _output); }
147
148 } // namespace ops
149 } // namespace cpu
150 } // namespace backend
151 } // namespace onert