Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / xnnpack / ops / OperationUtils.h
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 #ifndef __ONERT_BACKEND_XNNPACK_OPS_OPERATION_UTILS_H__
18 #define __ONERT_BACKEND_XNNPACK_OPS_OPERATION_UTILS_H__
19
20 // duplicated from cpu/ops/OperationUtils.h
21 #include <ir/InternalType.h>
22 #include <ir/Padding.h>
23 #include <ir/DataType.h>
24
25 namespace onert
26 {
27 namespace backend
28 {
29 namespace xnnpack
30 {
31 namespace ops
32 {
33
34 using OperandType = ir::DataType;
35
36 template <typename T>
37 void CalculateActivationRange(ir::Activation activation, T *activation_min, T *activation_max)
38 {
39   if (activation == ir::Activation::RELU)
40   {
41     *activation_min = 0;
42     *activation_max = std::numeric_limits<T>::max();
43   }
44   else if (activation == ir::Activation::RELU6)
45   {
46     *activation_min = 0;
47     *activation_max = 6;
48   }
49   else if (activation == ir::Activation::RELU1)
50   {
51     *activation_min = -1;
52     *activation_max = 1;
53   }
54   else if (activation == ir::Activation::SIGMOID)
55   {
56     *activation_min = 0;
57     *activation_max = 1;
58   }
59   else if (activation == ir::Activation::NONE)
60   {
61     *activation_min = std::numeric_limits<T>::lowest();
62     *activation_max = std::numeric_limits<T>::max();
63   }
64   else
65   {
66     throw std::runtime_error{"Unsupported fused activation function"};
67   }
68 }
69
70 } // namespace ops
71 } // namespace xnnpack
72 } // namespace backend
73 } // namespace onert
74
75 #endif // __ONERT_BACKEND_XNNPACK_OPS_OPERATION_UTILS_H__