556c55e338c0f5cb8b0e1582c1faf5cce2777175
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / ops / DivLayer.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 "DivLayer.h"
18
19 #include <cker/operation/BinaryArithmeticOps.h>
20
21 namespace onert
22 {
23 namespace backend
24 {
25 namespace cpu
26 {
27 namespace ops
28 {
29
30 void DivLayer::divFloat32()
31 {
32   float output_activation_min = 0, output_activation_max = 0;
33   CalculateActivationRange(_activation, &output_activation_min, &output_activation_max);
34   nnfw::cker::BinaryArithmeticOpParam op_params;
35   op_params.float_activation_max = output_activation_max;
36   op_params.float_activation_min = output_activation_min;
37
38   const bool requires_broadcast = !HaveSameShapes(_lhs, _rhs);
39   if (requires_broadcast)
40   {
41     nnfw::cker::BroadcastBinaryArithmeticOp<nnfw::cker::BinaryArithmeticOpType::DIV>(
42         op_params, getTensorShape(_lhs), reinterpret_cast<const float *>(_lhs->buffer()),
43         getTensorShape(_rhs), reinterpret_cast<const float *>(_rhs->buffer()),
44         getTensorShape(_output), reinterpret_cast<float *>(_output->buffer()));
45   }
46   else
47   {
48     nnfw::cker::BinaryArithmeticOp<nnfw::cker::BinaryArithmeticOpType::DIV>(
49         op_params, getTensorShape(_lhs), reinterpret_cast<const float *>(_lhs->buffer()),
50         getTensorShape(_rhs), reinterpret_cast<const float *>(_rhs->buffer()),
51         getTensorShape(_output), reinterpret_cast<float *>(_output->buffer()));
52   }
53 }
54
55 void DivLayer::divQuant8()
56 {
57   int32_t output_activation_min, output_activation_max;
58   CalculateActivationRangeUint8(_activation, _output, &output_activation_min,
59                                 &output_activation_max);
60   // op_params.quantized_activation_max = output_activation_max;
61   // op_params.quantized_activation_min = output_activation_min;
62
63   // cker quant8 div is not implemented yet
64   throw std::runtime_error{"Div NYI for quantized"};
65 }
66
67 void DivLayer::configure(const IPortableTensor *lhs, const IPortableTensor *rhs,
68                          const ir::Activation activation, IPortableTensor *output)
69 {
70   _lhs = lhs;
71   _rhs = rhs;
72   _activation = activation;
73   _output = output;
74 }
75
76 void DivLayer::run()
77 {
78   if (_output->data_type() == OperandType::FLOAT32)
79   {
80     divFloat32();
81   }
82   else if (_output->data_type() == OperandType::QUANT_UINT8_ASYMM)
83   {
84     divQuant8();
85   }
86   else
87   {
88     throw std::runtime_error{"Div: unsupported data type"};
89   }
90 }
91
92 } // namespace ops
93 } // namespace cpu
94 } // namespace backend
95 } // namespace onert