SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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 "fp_post_ops_act_eltwise_op_act.h" |
| 26 | #include "gemm_helpers.h" |
| 27 | #include "repeat.h" |
| 28 | |
| 29 | /** (EXPERIMENTAL_POST_OPS) gemm_mm_native kernel */ |
| 30 | #if defined(M0) && defined(N0) && defined(K0) && defined(K) && defined(DATA_TYPE) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0) |
| 31 | #if defined(P2_ELTWISE_OP) && defined(P2_ELTWISE_ARG1_HEIGHT) && defined(P2_ELTWISE_ARG1_WIDTH) |
| 32 | |
| 33 | #define VFMA(a, b, c) \ |
| 34 | ({ \ |
| 35 | c = fma(a, b, c); \ |
| 36 | }) |
| 37 | |
| 38 | #if M0 == 1 |
| 39 | #define RHS_VFMA_M0xN0(i, a, b, c) \ |
| 40 | ({ \ |
| 41 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ |
| 42 | }) |
| 43 | #elif M0 == 2 // M0 == 2 |
| 44 | #define RHS_VFMA_M0xN0(i, a, b, c) \ |
| 45 | ({ \ |
| 46 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ |
| 47 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ |
| 48 | }) |
| 49 | #elif M0 == 3 // M0 == 3 |
| 50 | #define RHS_VFMA_M0xN0(i, a, b, c) \ |
| 51 | ({ \ |
| 52 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ |
| 53 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ |
| 54 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ |
| 55 | }) |
| 56 | #elif M0 == 4 // M0 == 4 |
| 57 | #define RHS_VFMA_M0xN0(i, a, b, c) \ |
| 58 | ({ \ |
| 59 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ |
| 60 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ |
| 61 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ |
| 62 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##3).s##i), b, (c##3)); \ |
| 63 | }) |
| 64 | #elif M0 == 5 // M0 == 5 |
| 65 | #define RHS_VFMA_M0xN0(i, a, b, c) \ |
| 66 | ({ \ |
| 67 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ |
| 68 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ |
| 69 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ |
| 70 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##3).s##i), b, (c##3)); \ |
| 71 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##4).s##i), b, (c##4)); \ |
| 72 | }) |
| 73 | #elif M0 == 6 // M0 == 6 |
| 74 | #define RHS_VFMA_M0xN0(i, a, b, c) \ |
| 75 | ({ \ |
| 76 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ |
| 77 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ |
| 78 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ |
| 79 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##3).s##i), b, (c##3)); \ |
| 80 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##4).s##i), b, (c##4)); \ |
| 81 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##5).s##i), b, (c##5)); \ |
| 82 | }) |
| 83 | #elif M0 == 7 // M0 == 7 |
| 84 | #define RHS_VFMA_M0xN0(i, a, b, c) \ |
| 85 | ({ \ |
| 86 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ |
| 87 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ |
| 88 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ |
| 89 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##3).s##i), b, (c##3)); \ |
| 90 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##4).s##i), b, (c##4)); \ |
| 91 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##5).s##i), b, (c##5)); \ |
| 92 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##6).s##i), b, (c##6)); \ |
| 93 | }) |
| 94 | #elif M0 == 8 // M0 == 8 |
| 95 | #define RHS_VFMA_M0xN0(i, a, b, c) \ |
| 96 | ({ \ |
| 97 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##0).s##i), b, (c##0)); \ |
| 98 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##1).s##i), b, (c##1)); \ |
| 99 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##2).s##i), b, (c##2)); \ |
| 100 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##3).s##i), b, (c##3)); \ |
| 101 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##4).s##i), b, (c##4)); \ |
| 102 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##5).s##i), b, (c##5)); \ |
| 103 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##6).s##i), b, (c##6)); \ |
| 104 | VFMA((VEC_DATA_TYPE(DATA_TYPE, N0))((a##7).s##i), b, (c##7)); \ |
| 105 | }) |
| 106 | #else // M0 not supported |
| 107 | #error "M0 not supported" |
| 108 | #endif // M0 not supported |
| 109 | |
| 110 | /** This OpenCL kernel computes the matrix multiplication between 2 matrices plus 3 post ops: |
| 111 | * Post op 1: activation (optional) |
| 112 | * Post op 2: elementwise op |
| 113 | * Post op 3: activation (optional) |
| 114 | * |
| 115 | * @note (Optional) -DP1_ACTIVATION_TYPE, -DP1_ACTIVATION_A_VAL, -DP1_ACTIVATION_B_VAL: The activation type, alpha and beta values of the activation post op at slot 3 |
| 116 | * @note (Required) -DP2_ELTWISE_OP: The (binary) elementwise post op to perform |
| 117 | * @note (Required) -DP2_ELTWISE_ARG1_HEIGHT: The height (Y dimension) of the eltwise operand matrix of the eltwise post op at slot 2 |
| 118 | * @note (Required) -DP2_ELTWISE_ARG1_WIDTH: The width (X dimension) of the eltwise operand matrix of the eltwise post op at slot 2 |
| 119 | * @note (Optional) -DP3_ACTIVATION_TYPE, -DP3_ACTIVATION_A_VAL, -DP3_ACTIVATION_B_VAL: The activation type, alpha and beta values of the activation post op at slot 3 |
| 120 | * |
| 121 | * All parameters are similarly defined in kernel gemm_mm_native, with these additions: |
| 122 | * |
| 123 | * @param[in] eltwise_operand_ptr Pointer to the eltwise operand matrix. Supported data type: F16/F32 |
| 124 | * @param[in] eltwise_operand_stride_x Stride of the eltwise operand matrix in X dimension (in bytes) |
| 125 | * @param[in] eltwise_operand_step_x eltwise_operand_stride_x * number of elements along X processed per workitem(in bytes) |
| 126 | * @param[in] eltwise_operand_stride_y Stride of the eltwise operand matrix in Y dimension (in bytes) |
| 127 | * @param[in] eltwise_operand_step_y eltwise_operand_stride_y * number of elements along Y processed per workitem(in bytes) |
| 128 | * @param[in] eltwise_operand_stride_z Stride of the eltwise operand tensor in Z dimension (in bytes) |
| 129 | */ |
| 130 | __kernel void gemm_mm_native_post_act_eltwise_op_act(IMAGE_DECLARATION(lhs), |
| 131 | IMAGE_DECLARATION(rhs), |
| 132 | #if defined(BETA) |
| 133 | IMAGE_DECLARATION(bias), |
| 134 | #endif // defined(BETA) |
| 135 | IMAGE_DECLARATION(dst), |
SiCongLi | d928735 | 2021-11-03 19:01:22 +0000 | [diff] [blame^] | 136 | // Post Op arguments |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 137 | IMAGE_DECLARATION(eltwise_operand), |
| 138 | uint lhs_stride_z, |
| 139 | uint rhs_stride_z, |
| 140 | #if defined(BETA) |
| 141 | uint bias_stride_z, |
| 142 | #endif //defined(BETA) |
| 143 | uint dst_stride_z, |
| 144 | uint eltwise_operand_stride_z |
| 145 | #if defined(REINTERPRET_INPUT_AS_3D) |
| 146 | , |
| 147 | uint lhs_cross_plane_pad |
| 148 | #endif // REINTERPRET_INPUT_AS_3D |
| 149 | #if defined(REINTERPRET_OUTPUT_AS_3D) |
| 150 | , |
| 151 | uint dst_cross_plane_pad |
| 152 | #endif // REINTERPRET_OUTPUT_AS_3D |
| 153 | ) |
| 154 | { |
| 155 | // Block size |
| 156 | #define RHS_BLOCK_SIZE ((K0) * (N0)) |
| 157 | |
| 158 | // RHS offset and step X |
| 159 | #define RHS_OFFSET_X (RHS_BLOCK_SIZE) |
| 160 | |
| 161 | uint x = get_global_id(0); |
| 162 | uint y = get_global_id(1); |
| 163 | uint z = get_global_id(2); |
| 164 | |
| 165 | #if defined(DUMMY_WORK_ITEMS) |
| 166 | if((x * N0 >= N) || (y * M0 >= M)) |
| 167 | { |
| 168 | return; |
| 169 | } |
| 170 | #endif // defined(DUMMY_WORK_ITEMS) |
| 171 | |
| 172 | // Compute LHS matrix address |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 173 | uint lhs_offset = lhs_offset_first_element_in_bytes + COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * (uint)lhs_stride_y; |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 174 | |
| 175 | // Compute RHS matrix address |
| 176 | uint rhs_offset = rhs_offset_first_element_in_bytes + x * N0 * sizeof(DATA_TYPE); |
| 177 | |
| 178 | #if defined(MATRIX_B_DEPTH) |
| 179 | // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3 |
| 180 | rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z; |
| 181 | #else // defined(MATRIX_B_DEPTH) |
| 182 | rhs_offset += z * rhs_stride_z; |
| 183 | #endif // defined(MATRIX_B_DEPTH) |
| 184 | |
| 185 | REPEAT_VAR_INIT_TO_CONST(M0, uint, zlhs, 0); |
| 186 | REPEAT_VAR_INIT_TO_CONST(16, uint, zero, 0); |
| 187 | |
| 188 | #if defined(REINTERPRET_INPUT_AS_3D) |
| 189 | // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 190 | CALCULATE_Z_OFFSET(M0, uint, zlhs, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y); |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 191 | |
| 192 | // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we |
| 193 | // multiply lhs_stride_z by DEPTH_GEMM3D |
| 194 | lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D; |
| 195 | |
| 196 | #else // defined(REINTERPRET_INPUT_AS_3D) |
| 197 | |
| 198 | // Add offset for batched GEMM |
| 199 | lhs_offset += z * lhs_stride_z; |
| 200 | |
| 201 | #endif // defined(REINTERPRET_INPUT_AS_3D) |
| 202 | |
| 203 | // Initialize the accumulators |
| 204 | REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(M0-1)=0; |
| 205 | |
| 206 | int i = 0; |
| 207 | #if K0 > 1 |
| 208 | for(; i <= (K - K0); i += K0) |
| 209 | { |
| 210 | // Supported cases (M0, K0): |
| 211 | // 1,2 - 1,3 - 1,4 - 1,8 - 1,16 |
| 212 | // 2,2 - 2,3 - 2,4 - 2,8 - 2,16 |
| 213 | // 3,2 - 3,3 - 3,4 - 3,8 - 3,16 |
| 214 | // 4,2 - 4,3 - 4,4 - 4,8 - 4,16 |
| 215 | // 5,2 - 5,3 - 5,4 - 5,8 - 5,16 |
| 216 | // 6,2 - 6,3 - 6,4 - 6,8 - 6,16 |
| 217 | // 7,2 - 7,3 - 7,4 - 7,8 - 7,16 |
| 218 | // 8,2 - 8,3 - 8,4 - 8,8 - 8,16 |
| 219 | // Load values from LHS matrix |
| 220 | LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs); |
| 221 | |
| 222 | // Load values from RHS matrix |
| 223 | LOAD_BLOCK(K0, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zero); |
| 224 | |
| 225 | RHS_VFMA_M0xN0(0, a, b0, c); |
| 226 | RHS_VFMA_M0xN0(1, a, b1, c); |
| 227 | #if K0 > 2 |
| 228 | RHS_VFMA_M0xN0(2, a, b2, c); |
| 229 | #endif // K0 > 2 |
| 230 | #if K0 > 3 |
| 231 | RHS_VFMA_M0xN0(3, a, b3, c); |
| 232 | #endif // K0 > 3 |
| 233 | #if K0 > 4 |
| 234 | RHS_VFMA_M0xN0(4, a, b4, c); |
| 235 | RHS_VFMA_M0xN0(5, a, b5, c); |
| 236 | RHS_VFMA_M0xN0(6, a, b6, c); |
| 237 | RHS_VFMA_M0xN0(7, a, b7, c); |
| 238 | #endif // K0 > 4 |
| 239 | #if K0 > 8 |
| 240 | RHS_VFMA_M0xN0(8, a, b8, c); |
| 241 | RHS_VFMA_M0xN0(9, a, b9, c); |
| 242 | RHS_VFMA_M0xN0(A, a, bA, c); |
| 243 | RHS_VFMA_M0xN0(B, a, bB, c); |
| 244 | RHS_VFMA_M0xN0(C, a, bC, c); |
| 245 | RHS_VFMA_M0xN0(D, a, bD, c); |
| 246 | RHS_VFMA_M0xN0(E, a, bE, c); |
| 247 | RHS_VFMA_M0xN0(F, a, bF, c); |
| 248 | #endif // K0 > 8 |
| 249 | |
| 250 | lhs_offset += K0 * sizeof(DATA_TYPE); |
| 251 | rhs_offset += K0 * rhs_stride_y; |
| 252 | } |
| 253 | #endif // K0 > 1 |
| 254 | // Left-over accumulations |
| 255 | for(; i < K; ++i) |
| 256 | { |
| 257 | // Load values from LHS matrix |
| 258 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 259 | a0 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 0 * lhs_stride_y + zlhs0)); |
| 260 | #if M0 > 1 |
| 261 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 262 | a1 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 1 * lhs_stride_y + zlhs1)); |
| 263 | #endif // M0 > 1 |
| 264 | #if M0 > 2 |
| 265 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 266 | a2 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 2 * lhs_stride_y + zlhs2)); |
| 267 | #endif // M0 > 2 |
| 268 | #if M0 > 3 |
| 269 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 270 | a3 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 3 * lhs_stride_y + zlhs3)); |
| 271 | #endif // M0 > 3 |
| 272 | #if M0 > 4 |
| 273 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 274 | a4 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 4 * lhs_stride_y + zlhs4)); |
| 275 | #endif // M0 > 4 |
| 276 | #if M0 > 5 |
| 277 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 278 | a5 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 5 * lhs_stride_y + zlhs5)); |
| 279 | #endif // M0 > 5 |
| 280 | #if M0 > 6 |
| 281 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 282 | a6 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 6 * lhs_stride_y + zlhs6)); |
| 283 | #endif // M0 > 6 |
| 284 | #if M0 > 7 |
| 285 | VEC_DATA_TYPE(DATA_TYPE, 2) |
| 286 | a7 = *((__global DATA_TYPE *)(lhs_ptr + lhs_offset + 7 * lhs_stride_y + zlhs7)); |
| 287 | #endif // M0 > 7 |
| 288 | |
| 289 | VEC_DATA_TYPE(DATA_TYPE, N0) |
| 290 | b = VLOAD(N0)(0, (__global DATA_TYPE *)(rhs_ptr + rhs_offset + 0 * rhs_stride_y)); |
| 291 | RHS_VFMA_M0xN0(0, a, b, c); |
| 292 | |
| 293 | lhs_offset += sizeof(DATA_TYPE); |
| 294 | rhs_offset += rhs_stride_y; |
| 295 | } |
| 296 | |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 297 | __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0 * sizeof(DATA_TYPE)) + (COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * dst_stride_y); |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 298 | |
| 299 | REPEAT_VAR_INIT_TO_CONST(M0, uint, zout, 0); |
| 300 | |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 301 | #if defined(REINTERPRET_OUTPUT_AS_3D) |
| 302 | // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 303 | CALCULATE_Z_OFFSET(M0, uint, zout, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y); |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 304 | |
| 305 | // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we |
| 306 | // multiply dst_stride_z by DEPTH_GEMM3D |
| 307 | dst_addr += z * dst_stride_z * DEPTH_GEMM3D; |
| 308 | |
| 309 | #else // defined(REINTERPRET_OUTPUT_AS_3D) |
| 310 | |
| 311 | // Add offset for batched GEMM |
| 312 | dst_addr += z * dst_stride_z; |
| 313 | |
| 314 | #endif // defined(REINTERPRET_OUTPUT_AS_3D) |
| 315 | |
| 316 | // Multiply by the weight of matrix-matrix product and store the result |
| 317 | #if defined(ALPHA) |
| 318 | SCALE_BLOCK(M0, DATA_TYPE, c, ALPHA); |
| 319 | #endif // defined(ALPHA) |
| 320 | |
| 321 | // Add beta*bias |
| 322 | #if defined(BETA) |
| 323 | #if defined(BROADCAST_BIAS) |
| 324 | __global uchar *bias_addr = bias_ptr + bias_offset_first_element_in_bytes + (get_global_id(0) * (uint)N0 * sizeof(DATA_TYPE)); |
| 325 | |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 326 | LOAD_BLOCK(1, N0, DATA_TYPE, bias, bias_addr, 0, bias_stride_y, zero); |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 327 | |
| 328 | #ifndef UNIT_BETA |
| 329 | SCALE_BLOCK(1, DATA_TYPE, bias, BETA); |
| 330 | #endif // UNIT_BIAS |
| 331 | |
| 332 | // c = c + bias[broadcasted] |
| 333 | ADD_BLOCK_BROADCAST(M0, c, bias0); |
| 334 | |
| 335 | #else // defined(BROADCAST_BIAS) |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 336 | __global uchar *bias_addr = bias_ptr + bias_offset_first_element_in_bytes + (x * (uint)N0 * sizeof(DATA_TYPE)) + (COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * bias_stride_y) + z * bias_stride_z; |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 337 | |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 338 | LOAD_BLOCK(M0, N0, DATA_TYPE, bias, bias_addr, 0, bias_stride_y, zero); |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 339 | |
| 340 | #ifndef UNIT_BETA |
| 341 | SCALE_BLOCK(M0, DATA_TYPE, bias, BETA); |
| 342 | #endif // UNIT_BIAS |
| 343 | |
| 344 | // c = c + bias |
| 345 | ADD_BLOCK(M0, c, bias); |
| 346 | |
| 347 | #endif // defined(BROADCAST_BIAS) |
| 348 | #endif // defined(BETA) |
| 349 | |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 350 | const bool cond_y = y == 0; |
| 351 | const bool cond_x = ((x + 1) * N0 >= N); |
| 352 | |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 353 | // c = act(c) |
| 354 | POST_OP1_ACTIVATION_OPTIONAL(M0, DATA_TYPE, DATA_TYPE_ACCUMULATOR, N0, c); |
| 355 | // c = c + eltwise_operand (mix-precision, broadcast, boundary aware) |
SiCongLi | 71cbd28 | 2021-11-03 12:17:06 +0000 | [diff] [blame] | 356 | POST_OP2_ELTWISE_OP(P2_ELTWISE_OP, M0, N0, c, eltwise_operand, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), DATA_TYPE, DATA_TYPE_ACCUMULATOR, zero, 1, PARTIAL_STORE_N0, false, cond_x); |
SiCongLi | afa1972 | 2021-10-24 19:12:33 +0100 | [diff] [blame] | 357 | // c = act(c) |
| 358 | POST_OP3_ACTIVATION_OPTIONAL(M0, DATA_TYPE, DATA_TYPE_ACCUMULATOR, N0, c); |
| 359 | |
| 360 | // Store output block |
| 361 | STORE_BLOCK_BOUNDARY_AWARE(M0, N0, DATA_TYPE, c, dst_addr, dst_stride_y, zout, PARTIAL_STORE_M0, PARTIAL_STORE_N0, cond_y, cond_x); |
| 362 | } |
| 363 | #endif // defined(P2_ELTWISE_OP) && defined(P2_ELTWISE_ARG1_HEIGHT) && defined(P2_ELTWISE_ARG1_WIDTH) |
| 364 | #endif // defined(M0) && defined(N0) && defined(K0) && defined(K) && defined(DATA_TYPE) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0) |