Compute Library
 22.11
activation_quant_helpers.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "helpers.h"
26 
27 #define TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
28 
29 #if defined(S1_VAL) && !defined(S2_VAL)
30 #define S2_VAL S1_VAL
31 #endif // defined(S1_VAL) && !defined(S2_VAL)
32 #if defined(O1_VAL) && !defined(O2_VAL)
33 #define O2_VAL O1_VAL
34 #endif // defined(O1_VAL) && !defined(O2_VAL)
35 
36 // RELU Activation
37 inline TYPE relu_op(TYPE x)
38 {
39  return max((TYPE)CONST_0, x);
40 }
41 // Bounded RELU Activation
42 inline TYPE brelu_op(TYPE x)
43 {
44  return min((TYPE)A_VAL, max((TYPE)CONST_0, x));
45 }
46 // Lower Upper Bounded RELU Activation
48 {
49  return min(max(x, (TYPE)B_VAL), (TYPE)A_VAL);
50 }
51 // Hard Swish Activation
53 {
54  return (x * ((min(max((TYPE)(x + (TYPE)3.f), (TYPE)0.f), (TYPE)6.f)) * (TYPE)0.166666667f));
55 }
56 
57 inline TYPE identiy_op(TYPE x)
58 {
59  return x;
60 }
61 
62 #define ACTIVATION_OP2(op, x) op##_op(x)
63 #define ACTIVATION_OP(op, x) ACTIVATION_OP2(op, x)
64 
65 #if defined(S1_VAL) && defined(S2_VAL)
66 #if defined(O1_VAL) && defined(O2_VAL)
67 #define PERFORM_ACTIVATION_QUANT(act, data) \
68  ({ \
69  data = ACTIVATION_OP(act, data); \
70  \
71  VEC_DATA_TYPE(float, VEC_SIZE) \
72  fdata = CONVERT(data, VEC_DATA_TYPE(float, VEC_SIZE)); \
73  \
74  fdata = round((fdata - (float)O1_VAL) * ((float)S1_VAL / (float)S2_VAL) + (float)O2_VAL); \
75  data = CONVERT_SAT(fdata, VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)); \
76  })
77 #else // defined(O1_VAL) && defined(O2_VAL)
78 #define PERFORM_ACTIVATION_QUANT(act, data) \
79  ({ \
80  data = ACTIVATION_OP(act, data); \
81  \
82  VEC_DATA_TYPE(float, VEC_SIZE) \
83  fdata = CONVERT(data, VEC_DATA_TYPE(float, VEC_SIZE)); \
84  \
85  fdata = round((fdata) * ((float)S1_VAL / (float)S2_VAL)); \
86  data = CONVERT_SAT(fdata, VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)); \
87  })
88 #endif /* defined(O1_VAL) && defined(O2_VAL) */
89 #else /* defined(S1_VAL) && defined(S2_VAL) */
90 #define PERFORM_ACTIVATION_QUANT(act, data) \
91  ({ \
92  data = ACTIVATION_OP(act, data); \
93  })
94 #endif /* defined(S1_VAL) && defined(S2_VAL) */
DATA_TYPEVEC_SIZE identiy_op(DATA_TYPEVEC_SIZE x)
DATA_TYPEVEC_SIZE hard_swish_op(DATA_TYPEVEC_SIZE x)
#define TYPE
DATA_TYPEVEC_SIZE brelu_op(DATA_TYPEVEC_SIZE x)
DATA_TYPEVEC_SIZE relu_op(DATA_TYPEVEC_SIZE x)
DATA_TYPEVEC_SIZE lu_brelu_op(DATA_TYPEVEC_SIZE x)