1 /*
2  * Copyright (c) 2021-2022 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 #include "common/experimental/gemm_fused_post_ops/fp_elementwise_op_helpers.h"
25 #include "gemm_helpers.h"
26 #include "load_store_utility.h"
27 
28 /** (EXPERIMENTAL_POST_OPS) Convenience macros for automatically handling mixed precision (fp16 and fp32) operations
29  * -DMIXED_PRECISION toggles mixed precision mode
30  */
31 
32 /** Mixed-Precision-Aware Activation Block
33  * @name MIXED_PRECISION_ACTIVATION_BLOCK
34  * params N ... B_VAL: same as those in @ref ACTIVATION_BLOCK
35  *
36  * @param[in] DATA_TYPE_ACCUMULATR Higher-precision accumulator data type in case of mixed-precision op
37  * @{
38  */
39 #if defined(MIXED_PRECISION)
40 #define MIXED_PRECISION_ACTIVATION_BLOCK(N, ACTIVATION_TYPE, DATA_TYPE, VEC_SIZE, BASENAME, A_VAL, B_VAL, DATA_TYPE_ACCUMULATOR) \
41     ACTIVATION_BLOCK(N, ACTIVATION_TYPE, DATA_TYPE_ACCUMULATOR, VEC_SIZE, BASENAME, A_VAL, B_VAL);
42 #else // defined(MIXED_PRECISION)
43 #define MIXED_PRECISION_ACTIVATION_BLOCK(N, ACTIVATION_TYPE, DATA_TYPE, VEC_SIZE, BASENAME, A_VAL, B_VAL, DATA_TYPE_ACCUMULATOR) \
44     ACTIVATION_BLOCK(N, ACTIVATION_TYPE, DATA_TYPE, VEC_SIZE, BASENAME, A_VAL, B_VAL);
45 #endif    // defined(MIXED_PRECISION)
46 /** @} */ // end of group MIXED_PRECISION_ACTIVATION_BLOCK
47 
48 /** Mixed-Precision-Aware Elementwise Op Block
49  * Performs OPERAND1 = OP(OPERAND1, OPERAND2)
50  * @name MIXED_PRECISION_ELTWISE_OP_BLOCK
51  *
52  * @param[in] OP                   The elementwise post op
53  * @param[in] M0                   The number of consecutive rows
54  * @param[in] N0                   The number of consecutive columns
55  * @param[in] OPERAND1             The basename of the first and result operand variables
56  * @param[in] OPERAND2             The basename of the second operand variables
57  * @param[in] DATA_TYPE_ACCUMULATR Higher-precision accumulator data type in case of mixed-precision op
58  * @param[in] CONVERTED_OPERAND2   The basename of the second operand variables converted to higher-precision in case of mixed-precision op
59  * @{
60  */
61 #if defined(MIXED_PRECISION)
62 #define MIXED_PRECISION_ELTWISE_OP_BLOCK(OP, M0, N0, OPERAND1, OPERAND2, DATA_TYPE_ACCUMULATOR, CONVERTED_OPERAND2) \
63     CONVERT_BLOCK(M0, N0, DATA_TYPE_ACCUMULATOR, OPERAND2, CONVERTED_OPERAND2);                                     \
64     ELTWISE_OP_BLOCK(OP, M0, OPERAND1, CONVERTED_OPERAND2);
65 #else // defined(MIXED_PRECISION)
66 #define MIXED_PRECISION_ELTWISE_OP_BLOCK(OP, M0, N0, OPERAND1, OPERAND2, DATA_TYPE_ACCUMULATOR, CONVERTED_OPERAND2) \
67     ELTWISE_OP_BLOCK(OP, M0, OPERAND1, OPERAND2);
68 #endif    // defined(MIXED_PRECISION)
69 /** @} */ // end of group MIXED_PRECISION_ELTWISE_OP_BLOCK
70 
71 /** Mixed-Precision-Aware Elementwise Op Broadcast Block
72  * Performs OPERAND1 = OP(OPERAND1, OPERAND2)
73  * @name MIXED_PRECISION_ELTWISE_OP_BLOCK_BROADCAST
74  * @note Only support:
75  *      case 1 broadcast in Y dimension : Operand1 [YxX] + Operand2 [1xX]; this means @p N0 > 1
76  *      case 2 broadcast in both Y and X dimensions : Operand1 [YxX] + Operand2 [1x1] (scalar) ; this means @p N0 == 1
77  *      Does NOT support broad cast in X dimension: Operand1 [YxX] + Operand2 [Yx1]; this means @p M0 should never == 1
78  *
79  * @param[in] OP                   The elementwise post op
80  * @param[in] M0                   The number of consecutive rows, > 1
81  * @param[in] N0                   The number of consecutive columns, >= 1
82  * @param[in] OPERAND1             The basename of the first and result operand variables
83  * @param[in] OPERAND2             The basename of the second operand variables
84  * @param[in] DATA_TYPE_ACCUMULATR Higher-precision accumulator data type in case of mixed-precision op
85  * @param[in] CONVERTED_OPERAND2   The basename of the second operand variables converted to higher-precision in case of mixed-precision op
86  * @{
87  */
88 #if defined(MIXED_PRECISION)
89 #define MIXED_PRECISION_ELTWISE_OP_BLOCK_BROADCAST(OP, M0, N0, OPERAND1, OPERAND2, DATA_TYPE_ACCUMULATOR, CONVERTED_OPERAND2) \
90     CONVERT_BLOCK(1, N0, DATA_TYPE_ACCUMULATOR, OPERAND2, CONVERTED_OPERAND2);                                                \
91     ELTWISE_OP_BLOCK_BROADCAST(OP, M0, OPERAND1, CONVERTED_OPERAND2##0);
92 #else // defined(MIXED_PRECISION)
93 #define MIXED_PRECISION_ELTWISE_OP_BLOCK_BROADCAST(OP, M0, N0, OPERAND1, OPERAND2, DATA_TYPE_ACCUMULATOR, CONVERTED_OPERAND2) \
94     ELTWISE_OP_BLOCK_BROADCAST(OP, M0, OPERAND1, OPERAND2##0);
95 #endif    // defined(MIXED_PRECISION)
96 /** @} */ // end of group MIXED_PRECISION_ELTWISE_OP_BLOCK_BROADCAST
97 
98 /** Mixed-Precision-Aware Boundary-Aware Store Block
99  * @name MIXED_PRECISION_STORE_BLOCK_BOUNDARY_AWARE
100  * params M0 ... PARTIAL_COND_X, same as those in STORE_BLOCK_BOUNDARY_AWARE
101  *
102  * @param[in] BASENAME_LP The name of the low precision variables, converted from BASENAME, in case of mixed-precision op
103  * @{
104  */
105 #if defined(MIXED_PRECISION)
106 #define MIXED_PRECISION_STORE_BLOCK_BOUNDARY_AWARE(M0, N0, DATA_TYPE, BASENAME, PTR, STRIDE_Y, Z, PARTIAL_STORE_M0, PARTIAL_STORE_N0, PARTIAL_COND_Y, PARTIAL_COND_X, BASENAME_LP) \
107     CONVERT_BLOCK(M0, N0, DATA_TYPE, BASENAME, BASENAME_LP);                                                                                                                       \
108     STORE_BLOCK_BOUNDARY_AWARE(M0, N0, DATA_TYPE, BASENAME_LP, PTR, STRIDE_Y, Z, PARTIAL_STORE_M0, PARTIAL_STORE_N0, PARTIAL_COND_Y, PARTIAL_COND_X);
109 #else // defined(MIXED_PRECISION)
110 #define MIXED_PRECISION_STORE_BLOCK_BOUNDARY_AWARE(M0, N0, DATA_TYPE, BASENAME, PTR, STRIDE_Y, Z, PARTIAL_STORE_M0, PARTIAL_STORE_N0, PARTIAL_COND_Y, PARTIAL_COND_X, BASENAME_LP) \
111     STORE_BLOCK_BOUNDARY_AWARE(M0, N0, DATA_TYPE, BASENAME, PTR, STRIDE_Y, Z, PARTIAL_STORE_M0, PARTIAL_STORE_N0, PARTIAL_COND_Y, PARTIAL_COND_X);
112 #endif    // defined(MIXED_PRECISION)
113 /** @} */ // end of group MIXED_PRECISION_STORE_BLOCK_BOUNDARY_AWARE