Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / MaxLayer.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 "MaxLayer.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 MaxLayer::maximum()
33 {
34   nnfw::cker::Max<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 MaxLayer::maxQuant8()
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::Max<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("Max NYI for quantized");
52 }
53
54 void MaxLayer::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 MaxLayer::run()
67 {
68   if (_lhs->data_type() == OperandType::FLOAT32)
69   {
70     maximum<float>();
71   }
72   else if (_lhs->data_type() == OperandType::QUANT_UINT8_ASYMM)
73   {
74     maxQuant8();
75   }
76   else
77   {
78     throw std::runtime_error{"Max: unsupported data type"};
79   }
80 }
81
82 } // namespace ops
83 } // namespace cpu
84 } // namespace backend
85 } // namespace onert