Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / kernel / 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 kernel
28 {
29
30 void DivLayer::divFloat32()
31 {
32   float output_activation_min, output_activation_max;
33   CalculateActivationRangeFloat(_activation, &output_activation_min, &output_activation_max);
34   nnfw::cker::BinaryArithmeticOpParam op_params;
35   op_params.type = nnfw::cker::BinaryArithmeticOpType::DIV;
36   op_params.float_activation_max = output_activation_max;
37   op_params.float_activation_min = output_activation_min;
38
39   if (!HaveSameShapes(_lhs, _rhs))
40   {
41     nnfw::cker::BroadcastBinaryArithmeticOpSlow(
42         op_params, convertToExtendedCkerShape(_lhs),
43         reinterpret_cast<const float *>(_lhs->buffer()), convertToExtendedCkerShape(_rhs),
44         reinterpret_cast<const float *>(_rhs->buffer()), convertToExtendedCkerShape(_output),
45         reinterpret_cast<float *>(_output->buffer()));
46     return;
47   }
48
49   nnfw::cker::BinaryArithmeticOp(
50       op_params, convertTensorToCkerShape(_lhs), reinterpret_cast<const float *>(_lhs->buffer()),
51       convertTensorToCkerShape(_rhs), reinterpret_cast<const float *>(_rhs->buffer()),
52       convertTensorToCkerShape(_output), reinterpret_cast<float *>(_output->buffer()));
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   // nnfw::cker::BinaryArithmeticOpParam op_params;
61   // op_params.quantized_activation_max = output_activation_max;
62   // op_params.quantized_activation_min = output_activation_min;
63
64   // cker quant8 div is not implemented yet
65   throw std::runtime_error{"Div NYI for quantized"};
66 }
67
68 void DivLayer::configure(const operand::Tensor *lhs, const operand::Tensor *rhs,
69                          const ir::Activation activation, operand::Tensor *output)
70 {
71   _lhs = lhs;
72   _rhs = rhs;
73   _activation = activation;
74   _output = output;
75 }
76
77 void DivLayer::run()
78 {
79   if (_output->data_type() == OperandType::FLOAT32)
80   {
81     divFloat32();
82   }
83   else if (_output->data_type() == OperandType::QUANT8_ASYMM)
84   {
85     divQuant8();
86   }
87 }
88
89 } // namespace kernel
90 } // namespace cpu
91 } // namespace backend
92 } // namespace onert