Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / MinLayer.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 "MinLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/MaxMin.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29 namespace ops
30 {
31
32 template <typename T> void MinLayer::minimum()
33 {
34   nnfw::cker::Min<T>(getTensorShape(_lhs), reinterpret_cast<const T *>(_lhs->buffer()),
35                      getTensorShape(_rhs), reinterpret_cast<const T *>(_rhs->buffer()),
36                      getTensorShape(_output), reinterpret_cast<T *>(_output->buffer()));
37 }
38
39 void MinLayer::minQuant8()
40 {
41   if (_lhs->data_scale() == _rhs->data_scale() && _lhs->data_scale() == _output->data_scale())
42   {
43     if (_lhs->data_offset() == _rhs->data_offset() && _lhs->data_offset() == _output->data_offset())
44     {
45       return nnfw::cker::Min<uint8_t>(
46           getTensorShape(_lhs), reinterpret_cast<const uint8_t *>(_lhs->buffer()),
47           getTensorShape(_rhs), reinterpret_cast<const uint8_t *>(_rhs->buffer()),
48           getTensorShape(_output), reinterpret_cast<uint8_t *>(_output->buffer()));
49     }
50   }
51   throw std::runtime_error("Min NYI for quantized");
52 }
53
54 void MinLayer::configure(const IPortableTensor *lhs, const IPortableTensor *rhs,
55                          IPortableTensor *output)
56 {
57   assert(lhs != nullptr);
58   assert(rhs != nullptr);
59   assert(output != nullptr);
60
61   _lhs = lhs;
62   _rhs = rhs;
63   _output = output;
64 }
65
66 void MinLayer::run()
67 {
68   if (_lhs->data_type() == OperandType::FLOAT32)
69   {
70     minimum<float>();
71   }
72   else if (_lhs->data_type() == OperandType::QUANT_UINT8_ASYMM)
73   {
74     minQuant8();
75   }
76   else if (_lhs->data_type() == OperandType::INT32)
77   {
78     minimum<int32_t>();
79   }
80   else
81   {
82     throw std::runtime_error{"Min: unsupported data type"};
83   }
84 }
85
86 } // namespace ops
87 } // namespace cpu
88 } // namespace backend
89 } // namespace onert