Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | */ |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 24 | #include "fixed_point.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | #include "helpers.h" |
| 26 | |
| 27 | /** This OpenCL kernel computes the "vector" 1x4 transposition of input matrix |
| 28 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 29 | * @param[in] src_ptr Pointer to the source matrix. Supported data types: U32/S32/F32 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes) |
| 31 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 32 | * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 33 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 34 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 35 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 37 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 38 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 39 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 40 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 41 | */ |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 42 | __kernel void gemm_transpose1x4(IMAGE_DECLARATION(src), |
| 43 | IMAGE_DECLARATION(dst)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | { |
| 45 | uint x = get_global_id(0); |
| 46 | uint y = get_global_id(1); |
| 47 | |
| 48 | /* Compute address for Matrix B - source */ |
| 49 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 50 | |
| 51 | /* Compute address for Matrix B transposed - destination. X and Y are swapped */ |
| 52 | uint dst_addr_in_bytes = y * 16 + ((x * dst_stride_y + dst_offset_first_element_in_bytes)); |
| 53 | |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 54 | uint4 b0 = vload4(0, (__global uint *)src.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 56 | vstore4(b0, 0, (__global uint *)(dst_ptr + dst_addr_in_bytes)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** This OpenCL kernel computes the "vector" 1x8 transposition of input matrix |
| 60 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 61 | * @param[in] src_ptr Pointer to the source matrix. Supported data types: U16/S16/QS16/F16 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes) |
| 63 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 64 | * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 65 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 66 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 67 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 69 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 70 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 71 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 72 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 73 | */ |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 74 | __kernel void gemm_transpose1x8(IMAGE_DECLARATION(src), |
| 75 | IMAGE_DECLARATION(dst)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | { |
| 77 | uint x = get_global_id(0); |
| 78 | uint y = get_global_id(1); |
| 79 | |
| 80 | /* Compute address for Matrix B - source */ |
| 81 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 82 | |
| 83 | /* Compute address for Matrix B transposed - destination. X and Y are swapped */ |
| 84 | uint dst_addr_in_bytes = y * 16 + ((x * dst_stride_y + dst_offset_first_element_in_bytes)); |
| 85 | |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 86 | ushort8 b0 = vload8(0, (__global ushort *)src.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 88 | vstore8(b0, 0, (__global ushort *)(dst_ptr + dst_addr_in_bytes)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** This OpenCL kernel computes the "vector" 1x16 transposition of input matrix |
| 92 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 93 | * @param[in] src_ptr Pointer to the source matrix. Supported data types: U8/S8/QS8 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes) |
| 95 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 96 | * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 97 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 98 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 99 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 101 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 102 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 103 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 104 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 105 | */ |
Gian Marco Iodice | 9f89bae | 2017-06-22 12:09:49 +0100 | [diff] [blame] | 106 | __kernel void gemm_transpose1x16(IMAGE_DECLARATION(src), |
| 107 | IMAGE_DECLARATION(dst)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | { |
| 109 | uint x = get_global_id(0); |
| 110 | uint y = get_global_id(1); |
| 111 | |
| 112 | /* Compute address for Matrix B - source */ |
| 113 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 114 | |
| 115 | /* Compute address for Matrix B transposed - destination. X and Y are swapped */ |
| 116 | uint dst_addr_in_bytes = y * 16 + ((x * dst_stride_y + dst_offset_first_element_in_bytes)); |
| 117 | |
| 118 | uchar16 b0 = vload16(0, (__global uchar *)src.ptr); |
| 119 | |
| 120 | vstore16(b0, 0, (__global uchar *)(dst_ptr + dst_addr_in_bytes)); |
| 121 | } |
| 122 | |
| 123 | /** This OpenCL kernel reshapes the input matrix transposing each 4x4 block and interleaving the values |
| 124 | * |
| 125 | * @param[in] src_ptr Pointer to the source matrix. Supported data types: U32/S32/F32 |
| 126 | * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes) |
| 127 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 128 | * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 129 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 130 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 131 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 133 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 134 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 135 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 136 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 137 | */ |
| 138 | __kernel void gemm_interleave4x4_32bit(IMAGE_DECLARATION(src), |
| 139 | IMAGE_DECLARATION(dst)) |
| 140 | { |
| 141 | /* Compute source and destination addresses */ |
| 142 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 143 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 144 | |
| 145 | /* Load values from Matrix A */ |
| 146 | float4 a0 = vload4(0, (__global float *)(offset(&src, 0, 0))); |
| 147 | float4 a1 = vload4(0, (__global float *)(offset(&src, 0, 1))); |
| 148 | float4 a2 = vload4(0, (__global float *)(offset(&src, 0, 2))); |
| 149 | float4 a3 = vload4(0, (__global float *)(offset(&src, 0, 3))); |
| 150 | |
| 151 | float4 val0 = (float4)(a0.s0, a1.s0, a2.s0, a3.s0); |
| 152 | vstore4(val0, 0, ((__global float *)dst.ptr) + 0); |
| 153 | |
| 154 | val0 = (float4)(a0.s1, a1.s1, a2.s1, a3.s1); |
| 155 | vstore4(val0, 0, ((__global float *)dst.ptr) + 4); |
| 156 | |
| 157 | val0 = (float4)(a0.s2, a1.s2, a2.s2, a3.s2); |
| 158 | vstore4(val0, 0, ((__global float *)dst.ptr) + 8); |
| 159 | |
| 160 | val0 = (float4)(a0.s3, a1.s3, a2.s3, a3.s3); |
| 161 | vstore4(val0, 0, ((__global float *)dst.ptr) + 12); |
| 162 | } |
| 163 | |
| 164 | /** This OpenCL kernel reshapes the input matrix transposing each 4x4 block and interleaving the values |
| 165 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 166 | * @param[in] src_ptr Pointer to the source matrix. Supported data types: U16/S16/QS16/F16 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes) |
| 168 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 169 | * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 170 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 171 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 172 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 173 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 174 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 175 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 176 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 177 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 178 | */ |
| 179 | __kernel void gemm_interleave4x4_16bit(IMAGE_DECLARATION(src), |
| 180 | IMAGE_DECLARATION(dst)) |
| 181 | { |
| 182 | /* Compute source and destination addresses */ |
| 183 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 184 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 185 | |
| 186 | /* Load values from Matrix A */ |
| 187 | half8 a0 = vload8(0, (__global half *)(offset(&src, 0, 0))); |
| 188 | half8 a1 = vload8(0, (__global half *)(offset(&src, 0, 1))); |
| 189 | half8 a2 = vload8(0, (__global half *)(offset(&src, 0, 2))); |
| 190 | half8 a3 = vload8(0, (__global half *)(offset(&src, 0, 3))); |
| 191 | |
| 192 | half8 val0 = (half8)((half4)(a0.s0, a1.s0, a2.s0, a3.s0), (half4)(a0.s1, a1.s1, a2.s1, a3.s1)); |
| 193 | vstore8(val0, 0, ((__global half *)dst.ptr) + 0); |
| 194 | |
| 195 | val0 = (half8)((half4)(a0.s2, a1.s2, a2.s2, a3.s2), (half4)(a0.s3, a1.s3, a2.s3, a3.s3)); |
| 196 | vstore8(val0, 0, ((__global half *)dst.ptr) + 8); |
| 197 | |
| 198 | val0 = (half8)((half4)(a0.s4, a1.s4, a2.s4, a3.s4), (half4)(a0.s5, a1.s5, a2.s5, a3.s5)); |
| 199 | vstore8(val0, 0, ((__global half *)dst.ptr) + 16); |
| 200 | |
| 201 | val0 = (half8)((half4)(a0.s6, a1.s6, a2.s6, a3.s6), (half4)(a0.s7, a1.s7, a2.s7, a3.s7)); |
| 202 | vstore8(val0, 0, ((__global half *)dst.ptr) + 24); |
| 203 | } |
| 204 | |
| 205 | /** This OpenCL kernel reshapes the input matrix transposing each 4x4 block and interleaving the values |
| 206 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 207 | * @param[in] src_ptr Pointer to the source matrix. Supported data types: U8/S8/QS8 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 208 | * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes) |
| 209 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 210 | * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 211 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 212 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 213 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 215 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 216 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 217 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 218 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 219 | */ |
| 220 | __kernel void gemm_interleave4x4_8bit(IMAGE_DECLARATION(src), |
| 221 | IMAGE_DECLARATION(dst)) |
| 222 | { |
| 223 | /* Compute source and destination addresses */ |
| 224 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 225 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 226 | |
| 227 | /* Load values from Matrix A */ |
| 228 | uchar16 a0 = vload16(0, (__global uchar *)(offset(&src, 0, 0))); |
| 229 | uchar16 a1 = vload16(0, (__global uchar *)(offset(&src, 0, 1))); |
| 230 | uchar16 a2 = vload16(0, (__global uchar *)(offset(&src, 0, 2))); |
| 231 | uchar16 a3 = vload16(0, (__global uchar *)(offset(&src, 0, 3))); |
| 232 | |
| 233 | uchar16 val0 = (uchar16)((uchar4)(a0.s0, a1.s0, a2.s0, a3.s0), (uchar4)(a0.s1, a1.s1, a2.s1, a3.s1), |
| 234 | (uchar4)(a0.s2, a1.s2, a2.s2, a3.s2), (uchar4)(a0.s3, a1.s3, a2.s3, a3.s3)); |
| 235 | vstore16(val0, 0, ((__global uchar *)dst.ptr) + 0); |
| 236 | |
| 237 | val0 = (uchar16)((uchar4)(a0.s4, a1.s4, a2.s4, a3.s4), (uchar4)(a0.s5, a1.s5, a2.s5, a3.s5), |
| 238 | (uchar4)(a0.s6, a1.s6, a2.s6, a3.s6), (uchar4)(a0.s7, a1.s7, a2.s7, a3.s7)); |
| 239 | vstore16(val0, 0, ((__global uchar *)dst.ptr) + 16); |
| 240 | |
| 241 | val0 = (uchar16)((uchar4)(a0.s8, a1.s8, a2.s8, a3.s8), (uchar4)(a0.s9, a1.s9, a2.s9, a3.s9), |
| 242 | (uchar4)(a0.sA, a1.sA, a2.sA, a3.sA), (uchar4)(a0.sB, a1.sB, a2.sB, a3.sB)); |
| 243 | vstore16(val0, 0, ((__global uchar *)dst.ptr) + 32); |
| 244 | |
| 245 | val0 = (uchar16)((uchar4)(a0.sC, a1.sC, a2.sC, a3.sC), (uchar4)(a0.sD, a1.sD, a2.sD, a3.sD), |
| 246 | (uchar4)(a0.sE, a1.sE, a2.sE, a3.sE), (uchar4)(a0.sF, a1.sF, a2.sF, a3.sF)); |
| 247 | vstore16(val0, 0, ((__global uchar *)dst.ptr) + 48); |
| 248 | } |
| 249 | |
| 250 | /** This kernel accumulates each row with the biases vector |
| 251 | * |
Gian Marco Iodice | 578ab61 | 2017-06-23 09:34:33 +0100 | [diff] [blame] | 252 | * @note The data type must be passed at compile time -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
| 253 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 254 | * @param[in, out] accum_ptr Pointer to the accumulate tensor. Supported data type: U8/S8/QS8/U16/S16/F16/U32/S32/F32 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 255 | * @param[in] accum_stride_x Stride of the accmulate tensor in X dimension (in bytes) |
| 256 | * @param[in] accum_step_x accum_stride_x * number of elements along X processed per workitem(in bytes) |
| 257 | * @param[in] accum_stride_y Stride of the accumlulate tensor in Y dimension (in bytes) |
| 258 | * @param[in] accum_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 259 | * @param[in] accum_offset_first_element_in_bytes The offset of the first element in the accumulate tensor |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 260 | * @param[in] biases_ptr Pointer to the biases vector. Same as @p accum_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 261 | * @param[in] biases_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 262 | * @param[in] biases_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 263 | * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 264 | */ |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 265 | #ifdef DATA_TYPE |
Gian Marco Iodice | 578ab61 | 2017-06-23 09:34:33 +0100 | [diff] [blame] | 266 | __kernel void gemm_accumulate_biases( |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 267 | IMAGE_DECLARATION(accum), |
| 268 | VECTOR_DECLARATION(biases)) |
| 269 | { |
| 270 | Image accum = CONVERT_TO_IMAGE_STRUCT(accum); |
| 271 | Vector biases = CONVERT_TO_VECTOR_STRUCT(biases); |
| 272 | |
Gian Marco Iodice | 578ab61 | 2017-06-23 09:34:33 +0100 | [diff] [blame] | 273 | VEC_DATA_TYPE(DATA_TYPE, 16) |
| 274 | accum_value = vload16(0, (__global DATA_TYPE *)accum.ptr); |
| 275 | VEC_DATA_TYPE(DATA_TYPE, 16) |
| 276 | biases_value = vload16(0, (__global DATA_TYPE *)biases.ptr); |
| 277 | accum_value = biases_value + accum_value; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 278 | |
| 279 | // Store result in the accummulate buffer |
Gian Marco Iodice | 578ab61 | 2017-06-23 09:34:33 +0100 | [diff] [blame] | 280 | vstore16(accum_value, 0, (__global DATA_TYPE *)accum.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 281 | } |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 282 | #endif /* DATA_TYPE */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 283 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 284 | #ifdef WIDTH_MATRIX_B |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 285 | /** This OpenCL kernel computes the matrix multiplication between matrix A (src0) and matrix B (src1) |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 286 | * Matrix A and matrix B must be reshaped respectively with @ref gemm_interleave4x4_8bit and @ref gemm_transpose1x16 before running the matrix multiplication |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 287 | * |
| 288 | * @attention The width of matrix B and the alpha's value need to be passed at compile time using -DWIDTH_MATRIX_B |
| 289 | * |
| 290 | * @param[in] src0_ptr Pointer to the source matrix. Supported formats: U8 |
| 291 | * @param[in] src0_stride_x Stride of the source matrix in X dimension (in bytes) |
| 292 | * @param[in] src0_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 293 | * @param[in] src0_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 294 | * @param[in] src0_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 295 | * @param[in] src0_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 296 | * @param[in] src1_ptr Pointer to the source matrix. Supported formats: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 297 | * @param[in] src1_stride_x Stride of the source matrix in X dimension (in bytes) |
| 298 | * @param[in] src1_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 299 | * @param[in] src1_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 300 | * @param[in] src1_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 301 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 302 | * @param[out] dst_ptr Pointer to the destination matrix Supported formats: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 303 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 304 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 305 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 306 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 307 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 308 | * @param[in] a_offset Offset to be added to each element of the matrix A |
| 309 | * @param[in] b_offset Offset to be added to each element of the matrix B. |
| 310 | * @param[in] c_offset Offset to be added to each element of the matrix C. |
| 311 | * @param[in] c_mult_int Multiplied with each element of the matrix C. |
| 312 | * @param[in] shift Number of bits to shift right the result. |
| 313 | */ |
| 314 | __kernel void gemm_mm_u8(IMAGE_DECLARATION(src0), |
| 315 | IMAGE_DECLARATION(src1), |
| 316 | IMAGE_DECLARATION(dst), |
| 317 | int a_offset, |
| 318 | int b_offset, |
| 319 | int c_offset, |
| 320 | int c_mult_int, |
| 321 | int shift) |
| 322 | { |
| 323 | /* src_addr.s0 = address of matrix A */ |
| 324 | /* src_addr.s1 = address of matrix B */ |
| 325 | |
| 326 | /* Compute address for matrix A and B */ |
| 327 | int2 src_addr = (int2)(get_global_id(1), get_global_id(0)) * (int2)((src0_stride_y), |
| 328 | (src1_stride_y)); |
| 329 | |
| 330 | /* Add offset_first_element_in_bytes */ |
| 331 | src_addr = src_addr + ((int2)(src0_offset_first_element_in_bytes, src1_offset_first_element_in_bytes)); |
| 332 | |
| 333 | /* Compute end row address for matrix B */ |
| 334 | int end_row_mtx_b = src_addr.s1 + WIDTH_MATRIX_B; |
| 335 | |
| 336 | /* Reset accumulators */ |
| 337 | int16 c00 = 0.0f; |
| 338 | int16 c10 = 0.0f; |
| 339 | int16 c20 = 0.0f; |
| 340 | int16 c30 = 0.0f; |
| 341 | |
| 342 | for(; src_addr.s1 <= (end_row_mtx_b - 8); src_addr += (int2)(8, 32)) |
| 343 | { |
| 344 | /* Load values from matrix A (interleaved) and matrix B (transposed) */ |
| 345 | int8 a0 = (int8)a_offset + convert_int8(vload8(0, ((__global uchar *)src0_ptr) + src_addr.s0)); |
| 346 | int16 b0 = (int16)b_offset + convert_int16(vload16(0, ((__global uchar *)src1_ptr) + src_addr.s1)); |
| 347 | |
| 348 | c00 += (int16)a0.s0 * b0; |
| 349 | c10 += (int16)a0.s1 * b0; |
| 350 | c20 += (int16)a0.s2 * b0; |
| 351 | c30 += (int16)a0.s3 * b0; |
| 352 | |
| 353 | int16 b1 = (int16)b_offset + convert_int16(vload16(0, ((__global uchar *)src1_ptr) + src_addr.s1 + 16)); |
| 354 | |
| 355 | c00 += (int16)a0.s4 * b1; |
| 356 | c10 += (int16)a0.s5 * b1; |
| 357 | c20 += (int16)a0.s6 * b1; |
| 358 | c30 += (int16)a0.s7 * b1; |
| 359 | } |
| 360 | |
| 361 | for(; src_addr.s1 < end_row_mtx_b; src_addr += (int2)(4, 16)) |
| 362 | { |
| 363 | /* Load values from matrix A (interleaved) and matrix B (transposed) */ |
| 364 | int4 a0 = (int4)a_offset + convert_int4(vload4(0, ((__global uchar *)src0_ptr) + src_addr.s0)); |
| 365 | int16 b0 = (int16)b_offset + convert_int16(vload16(0, ((__global uchar *)src1_ptr) + src_addr.s1)); |
| 366 | |
| 367 | c00 += (int16)a0.s0 * b0; |
| 368 | c10 += (int16)a0.s1 * b0; |
| 369 | c20 += (int16)a0.s2 * b0; |
| 370 | c30 += (int16)a0.s3 * b0; |
| 371 | } |
| 372 | |
| 373 | /* Compute destination address */ |
| 374 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 375 | |
| 376 | /* Multiply by the weight of matrix product */ |
| 377 | c00 = (((int16)c_offset + c00) * (int16)c_mult_int) >> shift; |
| 378 | c10 = (((int16)c_offset + c10) * (int16)c_mult_int) >> shift; |
| 379 | c20 = (((int16)c_offset + c20) * (int16)c_mult_int) >> shift; |
| 380 | c30 = (((int16)c_offset + c30) * (int16)c_mult_int) >> shift; |
| 381 | |
| 382 | /* Store 4x16 block */ |
| 383 | vstore16(convert_uchar16_sat(c00), 0, (__global uchar *)(offset(&dst, 0, 0))); |
| 384 | vstore16(convert_uchar16_sat(c10), 0, (__global uchar *)(offset(&dst, 0, 1))); |
| 385 | vstore16(convert_uchar16_sat(c20), 0, (__global uchar *)(offset(&dst, 0, 2))); |
| 386 | vstore16(convert_uchar16_sat(c30), 0, (__global uchar *)(offset(&dst, 0, 3))); |
| 387 | } |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 388 | #endif /* WIDTH_MATRIX_B */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 389 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 390 | #if defined(WIDTH_MATRIX_B) && defined(ALPHA) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 391 | /** This OpenCL kernel is optimised for Midgard. It computes the matrix multiplication between matrix A (src0) and matrix B (src1) |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 392 | * Matrix A and matrix B must be reshaped respectively with @ref gemm_interleave4x4_32bit and @ref gemm_transpose1x4 before running the matrix multiplication |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 393 | * |
| 394 | * @attention The width of matrix B and the alpha's value need to be passed at compile time using -DWIDTH_MATRIX_B and -DALPHA |
| 395 | * |
| 396 | * @param[in] src0_ptr Pointer to the source matrix. Supported data types: F32 |
| 397 | * @param[in] src0_stride_x Stride of the source matrix in X dimension (in bytes) |
| 398 | * @param[in] src0_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 399 | * @param[in] src0_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 400 | * @param[in] src0_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 401 | * @param[in] src0_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 402 | * @param[in] src1_ptr Pointer to the source matrix. Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 403 | * @param[in] src1_stride_x Stride of the source matrix in X dimension (in bytes) |
| 404 | * @param[in] src1_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 405 | * @param[in] src1_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 406 | * @param[in] src1_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 407 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 408 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 409 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 410 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 411 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 412 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 413 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 414 | */ |
| 415 | __kernel void gemm_mm_f32_midgard(IMAGE_DECLARATION(src0), |
| 416 | IMAGE_DECLARATION(src1), |
| 417 | IMAGE_DECLARATION(dst)) |
| 418 | { |
| 419 | /* src_addr.s0 = address of matrix A */ |
| 420 | /* src_addr.s1 = address of matrix B */ |
| 421 | |
| 422 | /* Compute address for matrix A and B */ |
| 423 | int2 src_addr = (int2)(get_global_id(1), get_global_id(0)) * (int2)((src0_stride_y), |
| 424 | (src1_stride_y)); |
| 425 | |
| 426 | /* Add offset_first_element_in_bytes */ |
| 427 | src_addr = src_addr + ((int2)(src0_offset_first_element_in_bytes, src1_offset_first_element_in_bytes)); |
| 428 | |
| 429 | /* Divide by 4 in order to get the src_addr in unit of float */ |
| 430 | src_addr = src_addr >> 2; |
| 431 | |
| 432 | /* Compute end row address for matrix B */ |
| 433 | int end_row_mtx_b = src_addr.s1 + WIDTH_MATRIX_B; |
| 434 | |
| 435 | /* Reset accumulators */ |
| 436 | float4 c00 = 0.0f; |
| 437 | float4 c10 = 0.0f; |
| 438 | float4 c20 = 0.0f; |
| 439 | float4 c30 = 0.0f; |
| 440 | |
| 441 | for(; src_addr.s1 <= (end_row_mtx_b - 8); src_addr += (int2)(8, 8)) |
| 442 | { |
| 443 | /* Load values from matrix A (interleaved) and matrix B (transposed) */ |
| 444 | float4 a0 = vload4(0, ((__global float *)src0_ptr) + src_addr.s0); |
| 445 | float4 b0 = vload4(0, ((__global float *)src1_ptr) + src_addr.s1); |
| 446 | |
| 447 | c00 += (float4)a0.s0 * b0; |
| 448 | c10 += (float4)a0.s1 * b0; |
| 449 | c20 += (float4)a0.s2 * b0; |
| 450 | c30 += (float4)a0.s3 * b0; |
| 451 | |
| 452 | /* Load values from matrix A (interleaved) and matrix B (transposed) */ |
| 453 | a0 = vload4(0, ((__global float *)src0_ptr) + src_addr.s0 + 4); |
| 454 | b0 = vload4(0, ((__global float *)src1_ptr) + src_addr.s1 + 4); |
| 455 | |
| 456 | c00 += (float4)a0.s0 * b0; |
| 457 | c10 += (float4)a0.s1 * b0; |
| 458 | c20 += (float4)a0.s2 * b0; |
| 459 | c30 += (float4)a0.s3 * b0; |
| 460 | } |
| 461 | |
| 462 | for(; src_addr.s1 < end_row_mtx_b; src_addr += (int2)(4, 4)) |
| 463 | { |
| 464 | /* Load values from matrix A (interleaved) and matrix B (transposed) */ |
| 465 | float4 a0 = vload4(0, ((__global float *)src0_ptr) + src_addr.s0); |
| 466 | float4 b0 = vload4(0, ((__global float *)src1_ptr) + src_addr.s1); |
| 467 | |
| 468 | c00 += (float4)a0.s0 * b0; |
| 469 | c10 += (float4)a0.s1 * b0; |
| 470 | c20 += (float4)a0.s2 * b0; |
| 471 | c30 += (float4)a0.s3 * b0; |
| 472 | } |
| 473 | |
| 474 | /* Compute destination address */ |
| 475 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 476 | |
| 477 | /* Multiply by the weight of matrix product */ |
| 478 | c00 = c00 * (float4)ALPHA; |
| 479 | c10 = c10 * (float4)ALPHA; |
| 480 | c20 = c20 * (float4)ALPHA; |
| 481 | c30 = c30 * (float4)ALPHA; |
| 482 | |
| 483 | /* Store 4x4 block */ |
| 484 | vstore4(c00, 0, (__global float *)(offset(&dst, 0, 0))); |
| 485 | vstore4(c10, 0, (__global float *)(offset(&dst, 0, 1))); |
| 486 | vstore4(c20, 0, (__global float *)(offset(&dst, 0, 2))); |
| 487 | vstore4(c30, 0, (__global float *)(offset(&dst, 0, 3))); |
| 488 | } |
| 489 | |
| 490 | /** This OpenCL kernel is optimised for Bifrost. It computes the matrix multiplication between matrix A (src0) and matrix B (src1) |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 491 | * Matrix A and matrix B must be reshaped respectively with @ref gemm_interleave4x4_32bit and @ref gemm_transpose1x4 before running the matrix multiplication |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 492 | * |
| 493 | * @attention The width of matrix B and the alpha's value need to be passed at compile time using -DWIDTH_MATRIX_B and -DALPHA |
| 494 | * |
| 495 | * @param[in] src0_ptr Pointer to the source matrix. Supported data types: F32 |
| 496 | * @param[in] src0_stride_x Stride of the source matrix in X dimension (in bytes) |
| 497 | * @param[in] src0_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 498 | * @param[in] src0_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 499 | * @param[in] src0_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 500 | * @param[in] src0_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 501 | * @param[in] src1_ptr Pointer to the source matrix. Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 502 | * @param[in] src1_stride_x Stride of the source matrix in X dimension (in bytes) |
| 503 | * @param[in] src1_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 504 | * @param[in] src1_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 505 | * @param[in] src1_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 506 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 507 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 508 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 509 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 510 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 511 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 512 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 513 | */ |
| 514 | __kernel void gemm_mm_f32_bifrost(IMAGE_DECLARATION(src0), |
| 515 | IMAGE_DECLARATION(src1), |
| 516 | IMAGE_DECLARATION(dst)) |
| 517 | { |
| 518 | // src_addr_a = address of matrix A |
| 519 | // src_addr_b = address of matrix B |
| 520 | __global float *src_addr_a = (__global float *)(src0_ptr + get_global_id(1) * src0_stride_y + src0_offset_first_element_in_bytes); |
| 521 | __global float *src_addr_b = (__global float *)(src1_ptr + get_global_id(0) * src1_stride_y + src1_offset_first_element_in_bytes); |
| 522 | |
| 523 | // Compute end row address for matrix B |
| 524 | __global float *src_end_addr_b = src_addr_b + WIDTH_MATRIX_B; |
| 525 | |
| 526 | // Reset accumulators |
| 527 | float c00 = 0.0f; |
| 528 | float c01 = 0.0f; |
| 529 | float c02 = 0.0f; |
| 530 | float c03 = 0.0f; |
| 531 | float c10 = 0.0f; |
| 532 | float c11 = 0.0f; |
| 533 | float c12 = 0.0f; |
| 534 | float c13 = 0.0f; |
| 535 | float c20 = 0.0f; |
| 536 | float c21 = 0.0f; |
| 537 | float c22 = 0.0f; |
| 538 | float c23 = 0.0f; |
| 539 | float c30 = 0.0f; |
| 540 | float c31 = 0.0f; |
| 541 | float c32 = 0.0f; |
| 542 | float c33 = 0.0f; |
| 543 | |
| 544 | for(; src_addr_b <= (src_end_addr_b - 16); src_addr_a += 16, src_addr_b += 16) |
| 545 | { |
| 546 | // Load values from matrix A (interleaved) and matrix B (transposed) |
| 547 | float4 a0 = vload4(0, src_addr_a); |
| 548 | float4 b0 = vload4(0, src_addr_b); |
| 549 | |
| 550 | c00 = fma(a0.s0, b0.s0, c00); |
| 551 | c01 = fma(a0.s0, b0.s1, c01); |
| 552 | c02 = fma(a0.s0, b0.s2, c02); |
| 553 | c03 = fma(a0.s0, b0.s3, c03); |
| 554 | |
| 555 | c10 = fma(a0.s1, b0.s0, c10); |
| 556 | c11 = fma(a0.s1, b0.s1, c11); |
| 557 | c12 = fma(a0.s1, b0.s2, c12); |
| 558 | c13 = fma(a0.s1, b0.s3, c13); |
| 559 | |
| 560 | c20 = fma(a0.s2, b0.s0, c20); |
| 561 | c21 = fma(a0.s2, b0.s1, c21); |
| 562 | c22 = fma(a0.s2, b0.s2, c22); |
| 563 | c23 = fma(a0.s2, b0.s3, c23); |
| 564 | |
| 565 | c30 = fma(a0.s3, b0.s0, c30); |
| 566 | c31 = fma(a0.s3, b0.s1, c31); |
| 567 | c32 = fma(a0.s3, b0.s2, c32); |
| 568 | c33 = fma(a0.s3, b0.s3, c33); |
| 569 | |
| 570 | // Load values from matrix A (interleaved) and matrix B (transposed) |
| 571 | a0 = vload4(0, src_addr_a + 4); |
| 572 | b0 = vload4(0, src_addr_b + 4); |
| 573 | |
| 574 | c00 = fma(a0.s0, b0.s0, c00); |
| 575 | c01 = fma(a0.s0, b0.s1, c01); |
| 576 | c02 = fma(a0.s0, b0.s2, c02); |
| 577 | c03 = fma(a0.s0, b0.s3, c03); |
| 578 | |
| 579 | c10 = fma(a0.s1, b0.s0, c10); |
| 580 | c11 = fma(a0.s1, b0.s1, c11); |
| 581 | c12 = fma(a0.s1, b0.s2, c12); |
| 582 | c13 = fma(a0.s1, b0.s3, c13); |
| 583 | |
| 584 | c20 = fma(a0.s2, b0.s0, c20); |
| 585 | c21 = fma(a0.s2, b0.s1, c21); |
| 586 | c22 = fma(a0.s2, b0.s2, c22); |
| 587 | c23 = fma(a0.s2, b0.s3, c23); |
| 588 | |
| 589 | c30 = fma(a0.s3, b0.s0, c30); |
| 590 | c31 = fma(a0.s3, b0.s1, c31); |
| 591 | c32 = fma(a0.s3, b0.s2, c32); |
| 592 | c33 = fma(a0.s3, b0.s3, c33); |
| 593 | |
| 594 | // Load values from matrix A (interleaved) and matrix B (transposed) |
| 595 | a0 = vload4(0, src_addr_a + 8); |
| 596 | b0 = vload4(0, src_addr_b + 8); |
| 597 | |
| 598 | c00 = fma(a0.s0, b0.s0, c00); |
| 599 | c01 = fma(a0.s0, b0.s1, c01); |
| 600 | c02 = fma(a0.s0, b0.s2, c02); |
| 601 | c03 = fma(a0.s0, b0.s3, c03); |
| 602 | |
| 603 | c10 = fma(a0.s1, b0.s0, c10); |
| 604 | c11 = fma(a0.s1, b0.s1, c11); |
| 605 | c12 = fma(a0.s1, b0.s2, c12); |
| 606 | c13 = fma(a0.s1, b0.s3, c13); |
| 607 | |
| 608 | c20 = fma(a0.s2, b0.s0, c20); |
| 609 | c21 = fma(a0.s2, b0.s1, c21); |
| 610 | c22 = fma(a0.s2, b0.s2, c22); |
| 611 | c23 = fma(a0.s2, b0.s3, c23); |
| 612 | |
| 613 | c30 = fma(a0.s3, b0.s0, c30); |
| 614 | c31 = fma(a0.s3, b0.s1, c31); |
| 615 | c32 = fma(a0.s3, b0.s2, c32); |
| 616 | c33 = fma(a0.s3, b0.s3, c33); |
| 617 | |
| 618 | // Load values from matrix A (interleaved) and matrix B (transposed) |
| 619 | a0 = vload4(0, src_addr_a + 12); |
| 620 | b0 = vload4(0, src_addr_b + 12); |
| 621 | |
| 622 | c00 = fma(a0.s0, b0.s0, c00); |
| 623 | c01 = fma(a0.s0, b0.s1, c01); |
| 624 | c02 = fma(a0.s0, b0.s2, c02); |
| 625 | c03 = fma(a0.s0, b0.s3, c03); |
| 626 | |
| 627 | c10 = fma(a0.s1, b0.s0, c10); |
| 628 | c11 = fma(a0.s1, b0.s1, c11); |
| 629 | c12 = fma(a0.s1, b0.s2, c12); |
| 630 | c13 = fma(a0.s1, b0.s3, c13); |
| 631 | |
| 632 | c20 = fma(a0.s2, b0.s0, c20); |
| 633 | c21 = fma(a0.s2, b0.s1, c21); |
| 634 | c22 = fma(a0.s2, b0.s2, c22); |
| 635 | c23 = fma(a0.s2, b0.s3, c23); |
| 636 | |
| 637 | c30 = fma(a0.s3, b0.s0, c30); |
| 638 | c31 = fma(a0.s3, b0.s1, c31); |
| 639 | c32 = fma(a0.s3, b0.s2, c32); |
| 640 | c33 = fma(a0.s3, b0.s3, c33); |
| 641 | } |
| 642 | |
| 643 | for(; src_addr_b < src_end_addr_b; src_addr_a += 4, src_addr_b += 4) |
| 644 | { |
| 645 | // Load values from matrix A (interleaved) and matrix B (transposed) |
| 646 | float4 a0 = vload4(0, src_addr_a); |
| 647 | float4 b0 = vload4(0, src_addr_b); |
| 648 | |
| 649 | c00 = fma(a0.s0, b0.s0, c00); |
| 650 | c01 = fma(a0.s0, b0.s1, c01); |
| 651 | c02 = fma(a0.s0, b0.s2, c02); |
| 652 | c03 = fma(a0.s0, b0.s3, c03); |
| 653 | |
| 654 | c10 = fma(a0.s1, b0.s0, c10); |
| 655 | c11 = fma(a0.s1, b0.s1, c11); |
| 656 | c12 = fma(a0.s1, b0.s2, c12); |
| 657 | c13 = fma(a0.s1, b0.s3, c13); |
| 658 | |
| 659 | c20 = fma(a0.s2, b0.s0, c20); |
| 660 | c21 = fma(a0.s2, b0.s1, c21); |
| 661 | c22 = fma(a0.s2, b0.s2, c22); |
| 662 | c23 = fma(a0.s2, b0.s3, c23); |
| 663 | |
| 664 | c30 = fma(a0.s3, b0.s0, c30); |
| 665 | c31 = fma(a0.s3, b0.s1, c31); |
| 666 | c32 = fma(a0.s3, b0.s2, c32); |
| 667 | c33 = fma(a0.s3, b0.s3, c33); |
| 668 | } |
| 669 | |
| 670 | // Compute destination address |
| 671 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 672 | |
| 673 | // Multiply by the weight of matrix product |
| 674 | c00 = c00 * ALPHA; |
| 675 | c01 = c01 * ALPHA; |
| 676 | c02 = c02 * ALPHA; |
| 677 | c03 = c03 * ALPHA; |
| 678 | c10 = c10 * ALPHA; |
| 679 | c11 = c11 * ALPHA; |
| 680 | c12 = c12 * ALPHA; |
| 681 | c13 = c13 * ALPHA; |
| 682 | c20 = c20 * ALPHA; |
| 683 | c21 = c21 * ALPHA; |
| 684 | c22 = c22 * ALPHA; |
| 685 | c23 = c23 * ALPHA; |
| 686 | c30 = c30 * ALPHA; |
| 687 | c31 = c31 * ALPHA; |
| 688 | c32 = c32 * ALPHA; |
| 689 | c33 = c33 * ALPHA; |
| 690 | |
| 691 | barrier(CLK_GLOBAL_MEM_FENCE); |
| 692 | |
| 693 | // Store 4x4 block |
| 694 | vstore4((float4)(c00, c01, c02, c03), 0, (__global float *)(offset(&dst, 0, 0))); |
| 695 | vstore4((float4)(c10, c11, c12, c13), 0, (__global float *)(offset(&dst, 0, 1))); |
| 696 | vstore4((float4)(c20, c21, c22, c23), 0, (__global float *)(offset(&dst, 0, 2))); |
| 697 | vstore4((float4)(c30, c31, c32, c33), 0, (__global float *)(offset(&dst, 0, 3))); |
| 698 | } |
| 699 | |
| 700 | /** This OpenCL kernel computes the matrix multiplication between matrix A (src0) and matrix B (src1) |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 701 | * Matrix A and matrix B must be reshaped respectively with @ref gemm_interleave4x4_16bit and @ref gemm_transpose1x8 before running the matrix multiplication |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 702 | * |
| 703 | * @attention The width of matrix B and the alpha's value need to be passed at compile time using -DWIDTH_MATRIX_B and -DALPHA |
| 704 | * |
| 705 | * @param[in] src0_ptr Pointer to the source matrix. Supported data types: F16 |
| 706 | * @param[in] src0_stride_x Stride of the source matrix in X dimension (in bytes) |
| 707 | * @param[in] src0_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 708 | * @param[in] src0_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 709 | * @param[in] src0_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 710 | * @param[in] src0_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 711 | * @param[in] src1_ptr Pointer to the source matrix. Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 712 | * @param[in] src1_stride_x Stride of the source matrix in X dimension (in bytes) |
| 713 | * @param[in] src1_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 714 | * @param[in] src1_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 715 | * @param[in] src1_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 716 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 717 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 718 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 719 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 720 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 721 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 722 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 723 | */ |
| 724 | __kernel void gemm_mm_f16(IMAGE_DECLARATION(src0), |
| 725 | IMAGE_DECLARATION(src1), |
| 726 | IMAGE_DECLARATION(dst)) |
| 727 | { |
| 728 | /* src_addr.s0 = address of matrix A */ |
| 729 | /* src_addr.s1 = address of matrix B */ |
| 730 | |
| 731 | /* Compute address for matrix A and B */ |
| 732 | int2 src_addr = (int2)(get_global_id(1), get_global_id(0)) * (int2)((src0_stride_y), |
| 733 | (src1_stride_y)); |
| 734 | |
| 735 | /* Add offset_first_element_in_bytes */ |
| 736 | src_addr = src_addr + ((int2)(src0_offset_first_element_in_bytes, src1_offset_first_element_in_bytes)); |
| 737 | |
| 738 | /* Divide by 2 in order to get the src_addr in unit of half */ |
| 739 | src_addr = src_addr >> 1; |
| 740 | |
| 741 | /* Compute end row address for matrix B */ |
| 742 | int end_row_mtx_b = src_addr.s1 + WIDTH_MATRIX_B; |
| 743 | |
| 744 | /* Reset accumulators */ |
| 745 | half8 c00 = 0.0f; |
| 746 | half8 c10 = 0.0f; |
| 747 | half8 c20 = 0.0f; |
| 748 | half8 c30 = 0.0f; |
| 749 | |
| 750 | for(; src_addr.s1 <= (end_row_mtx_b - 8); src_addr += (int2)(8, 16)) |
| 751 | { |
| 752 | /* Load values from matrix A (interleaved) and matrix B (transposed) */ |
| 753 | half4 a0 = vload4(0, ((__global half *)src0_ptr) + src_addr.s0); |
| 754 | half8 b0 = vload8(0, ((__global half *)src1_ptr) + src_addr.s1); |
| 755 | |
| 756 | c00 += (half8)a0.s0 * b0; |
| 757 | c10 += (half8)a0.s1 * b0; |
| 758 | c20 += (half8)a0.s2 * b0; |
| 759 | c30 += (half8)a0.s3 * b0; |
| 760 | |
| 761 | /* Load values from matrix A (interleaved) and matrix B (transposed) */ |
| 762 | a0 = vload4(0, ((__global half *)src0_ptr) + src_addr.s0 + 4); |
| 763 | b0 = vload8(0, ((__global half *)src1_ptr) + src_addr.s1 + 8); |
| 764 | |
| 765 | c00 += (half8)a0.s0 * b0; |
| 766 | c10 += (half8)a0.s1 * b0; |
| 767 | c20 += (half8)a0.s2 * b0; |
| 768 | c30 += (half8)a0.s3 * b0; |
| 769 | } |
| 770 | |
| 771 | for(; src_addr.s1 < end_row_mtx_b; src_addr += (int2)(4, 8)) |
| 772 | { |
| 773 | /* Load values from matrix A (interleaved) and matrix B (transposed) */ |
| 774 | half4 a0 = vload4(0, ((__global half *)src0_ptr) + src_addr.s0); |
| 775 | half8 b0 = vload8(0, ((__global half *)src1_ptr) + src_addr.s1); |
| 776 | |
| 777 | c00 += (half8)a0.s0 * b0; |
| 778 | c10 += (half8)a0.s1 * b0; |
| 779 | c20 += (half8)a0.s2 * b0; |
| 780 | c30 += (half8)a0.s3 * b0; |
| 781 | } |
| 782 | |
| 783 | /* Compute destination address */ |
| 784 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 785 | |
| 786 | /* Multiply by the weight of matrix product */ |
| 787 | c00 = c00 * (half8)ALPHA; |
| 788 | c10 = c10 * (half8)ALPHA; |
| 789 | c20 = c20 * (half8)ALPHA; |
| 790 | c30 = c30 * (half8)ALPHA; |
| 791 | |
| 792 | /* Store 4x8 block */ |
| 793 | vstore8(c00, 0, (__global half *)(offset(&dst, 0, 0))); |
| 794 | vstore8(c10, 0, (__global half *)(offset(&dst, 0, 1))); |
| 795 | vstore8(c20, 0, (__global half *)(offset(&dst, 0, 2))); |
| 796 | vstore8(c30, 0, (__global half *)(offset(&dst, 0, 3))); |
| 797 | } |
| 798 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 799 | #ifdef FIXED_POINT_POSITION |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 800 | /** This OpenCL kernel computes the matrix multiplication between matrix A (src0) and matrix B (src1) in 8 bit fixed point precision |
| 801 | * Matrix A and matrix B must be reshaped respectively with @ref gemm_interleave4x4_8bit and @ref gemm_transpose1x16 before running the matrix multiplication |
| 802 | * |
| 803 | * @attention The width of matrix B, the alpha's value and fixed point position need to be passed at compile time using -DWIDTH_MATRIX_B -DALPHA and -DFIXED_POINT_POSITION |
| 804 | * |
| 805 | * @note: ALPHA must be passed in 8 bit fixed point format |
| 806 | * |
| 807 | * @param[in] src0_ptr Pointer to the source matrix. Supported data types: QS8 |
| 808 | * @param[in] src0_stride_x Stride of the source matrix in X dimension (in bytes) |
| 809 | * @param[in] src0_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 810 | * @param[in] src0_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 811 | * @param[in] src0_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 812 | * @param[in] src0_offset_first_element_in_bytes The offset of the first element in the source matrix |
| 813 | * @param[in] src1_ptr Pointer to the source matrix. Supported data types: same as @p src0_ptr |
| 814 | * @param[in] src1_stride_x Stride of the source matrix in X dimension (in bytes) |
| 815 | * @param[in] src1_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 816 | * @param[in] src1_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 817 | * @param[in] src1_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 818 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the source matrix |
| 819 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src0_ptr |
| 820 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 821 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 822 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 823 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 824 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 825 | */ |
| 826 | __kernel void gemm_mm_qs8(IMAGE_DECLARATION(src0), |
| 827 | IMAGE_DECLARATION(src1), |
| 828 | IMAGE_DECLARATION(dst)) |
| 829 | { |
| 830 | /* src_addr.s0 = address of matrix A */ |
| 831 | /* src_addr.s1 = address of matrix B */ |
| 832 | |
| 833 | /* Compute address for matrix A and B */ |
| 834 | int2 src_addr = (int2)(get_global_id(1), get_global_id(0)) * (int2)((src0_stride_y), |
| 835 | (src1_stride_y)); |
| 836 | |
| 837 | /* Add offset_first_element_in_bytes */ |
| 838 | src_addr = src_addr + ((int2)(src0_offset_first_element_in_bytes, src1_offset_first_element_in_bytes)); |
| 839 | |
| 840 | /* Compute end row address for matrix B */ |
| 841 | int end_row_mtx_b = src_addr.s1 + WIDTH_MATRIX_B; |
| 842 | |
| 843 | /* Reset accumulators */ |
| 844 | short8 c00 = 0.0f; |
| 845 | short8 c10 = 0.0f; |
| 846 | short8 c20 = 0.0f; |
| 847 | short8 c30 = 0.0f; |
| 848 | short8 c01 = 0.0f; |
| 849 | short8 c11 = 0.0f; |
| 850 | short8 c21 = 0.0f; |
| 851 | short8 c31 = 0.0f; |
| 852 | |
| 853 | /* This for loop performs 1 accumulation for each iteration */ |
| 854 | for(; src_addr.s1 <= (end_row_mtx_b - 16); src_addr += (int2)(4, 16)) |
| 855 | { |
| 856 | /* Load values from matrix A (interleaved) and matrix B (transposed) */ |
| 857 | char4 a0 = vload4(0, ((__global char *)src0_ptr) + src_addr.s0); |
| 858 | char16 b0 = vload16(0, ((__global char *)src1_ptr) + src_addr.s1); |
| 859 | |
| 860 | c00 = mlal_sat_qs8x8(c00, (char8)a0.s0, b0.s01234567, FIXED_POINT_POSITION); |
| 861 | c10 = mlal_sat_qs8x8(c10, (char8)a0.s1, b0.s01234567, FIXED_POINT_POSITION); |
| 862 | c20 = mlal_sat_qs8x8(c20, (char8)a0.s2, b0.s01234567, FIXED_POINT_POSITION); |
| 863 | c30 = mlal_sat_qs8x8(c30, (char8)a0.s3, b0.s01234567, FIXED_POINT_POSITION); |
| 864 | |
| 865 | c01 = mlal_sat_qs8x8(c01, (char8)a0.s0, b0.s89ABCDEF, FIXED_POINT_POSITION); |
| 866 | c11 = mlal_sat_qs8x8(c11, (char8)a0.s1, b0.s89ABCDEF, FIXED_POINT_POSITION); |
| 867 | c21 = mlal_sat_qs8x8(c21, (char8)a0.s2, b0.s89ABCDEF, FIXED_POINT_POSITION); |
| 868 | c31 = mlal_sat_qs8x8(c31, (char8)a0.s3, b0.s89ABCDEF, FIXED_POINT_POSITION); |
| 869 | } |
| 870 | |
| 871 | /* Compute destination address */ |
| 872 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 873 | |
| 874 | /* Multiply by the weight of matrix product */ |
| 875 | char16 c00_qs8 = convert_char16_sat((short16)(c00, c01)); |
| 876 | char16 c10_qs8 = convert_char16_sat((short16)(c10, c11)); |
| 877 | char16 c20_qs8 = convert_char16_sat((short16)(c20, c21)); |
| 878 | char16 c30_qs8 = convert_char16_sat((short16)(c30, c31)); |
| 879 | |
| 880 | c00_qs8 = mul_sat_qs8x16(c00_qs8, (char16)ALPHA, FIXED_POINT_POSITION); |
| 881 | c10_qs8 = mul_sat_qs8x16(c10_qs8, (char16)ALPHA, FIXED_POINT_POSITION); |
| 882 | c20_qs8 = mul_sat_qs8x16(c20_qs8, (char16)ALPHA, FIXED_POINT_POSITION); |
| 883 | c30_qs8 = mul_sat_qs8x16(c30_qs8, (char16)ALPHA, FIXED_POINT_POSITION); |
| 884 | |
| 885 | /* Store 16x4 block */ |
| 886 | vstore16(c00_qs8, 0, (__global char *)(offset(&dst, 0, 0))); |
| 887 | vstore16(c10_qs8, 0, (__global char *)(offset(&dst, 0, 1))); |
| 888 | vstore16(c20_qs8, 0, (__global char *)(offset(&dst, 0, 2))); |
| 889 | vstore16(c30_qs8, 0, (__global char *)(offset(&dst, 0, 3))); |
| 890 | } |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 891 | #endif /* FIXED_POINT_POSITION */ |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 892 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 893 | #ifdef WIDTH_VECTOR_A |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 894 | /** This OpenCL kernel computes the vector by matrix multiplication between the vector A (src0) and matrix B (src1) |
| 895 | * |
| 896 | * @attention The width of vector A, the width of matrix B and the alpha's value need to be passed at compile time using -DWIDTH_VECTOR_A -DWIDTH_MATRIX_B and -DALPHA |
| 897 | * |
| 898 | * @attention The input vector A and matrix B must not be reshaped |
| 899 | * |
| 900 | * @param[in] src0_ptr Pointer to the source matrix. Supported data types: F32 |
| 901 | * @param[in] src0_stride_x Stride of the source matrix in X dimension (in bytes) |
| 902 | * @param[in] src0_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 903 | * @param[in] src0_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 904 | * @param[in] src0_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 905 | * @param[in] src0_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 906 | * @param[in] src1_ptr Pointer to the source matrix. Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 907 | * @param[in] src1_stride_x Stride of the source matrix in X dimension (in bytes) |
| 908 | * @param[in] src1_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 909 | * @param[in] src1_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 910 | * @param[in] src1_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 911 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 912 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 913 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 914 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 915 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 916 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 917 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 918 | */ |
| 919 | __kernel void gemm_vm_f32(IMAGE_DECLARATION(src0), |
| 920 | IMAGE_DECLARATION(src1), |
| 921 | IMAGE_DECLARATION(dst)) |
| 922 | { |
| 923 | int idx = get_global_id(0) * 4; |
| 924 | |
| 925 | /* Compute the address for the vector A and matrix B */ |
| 926 | int2 src_addr = ((int2)(src0_offset_first_element_in_bytes, src1_offset_first_element_in_bytes)); |
| 927 | src_addr.s1 += idx * sizeof(float); |
| 928 | |
| 929 | int end_row_vec_a = src_addr.s0 + (WIDTH_VECTOR_A * sizeof(float)); |
| 930 | |
| 931 | float4 acc = 0.0f; |
| 932 | |
| 933 | for(; src_addr.s0 <= (end_row_vec_a - 2 * sizeof(float)); src_addr += (int2)(2 * sizeof(float), 2 * src1_stride_y)) |
| 934 | { |
| 935 | float2 a0 = vload2(0, (__global float *)(src0_ptr + src_addr.s0)); |
| 936 | float4 b0 = vload4(0, (__global float *)(src1_ptr + src_addr.s1)); |
| 937 | float4 b1 = vload4(0, (__global float *)(src1_ptr + src_addr.s1 + src1_stride_y)); |
| 938 | |
| 939 | acc += b0 * (float4)a0.s0; |
| 940 | acc += b1 * (float4)a0.s1; |
| 941 | } |
| 942 | |
| 943 | for(; src_addr.s0 < end_row_vec_a; src_addr += (int2)(sizeof(float), src1_stride_y)) |
| 944 | { |
| 945 | float a0 = *((__global float *)(src0_ptr + src_addr.s0)); |
| 946 | float4 b0 = vload4(0, (__global float *)(src1_ptr + src_addr.s1)); |
| 947 | |
| 948 | acc += b0 * (float4)a0; |
| 949 | } |
| 950 | |
| 951 | /* Compute destination address */ |
| 952 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 953 | |
| 954 | /* Multiply by the weight of vector-matrix product */ |
| 955 | acc = acc * (float4)ALPHA; |
| 956 | |
| 957 | vstore4(acc, 0, (__global float *)(offset(&dst, 0, 0))); |
| 958 | } |
| 959 | |
| 960 | /** This OpenCL kernel computes the vector by matrix multiplication between the vector A (src0) and matrix B (src1) |
| 961 | * |
| 962 | * @attention The width of vector A, the width of matrix B and the alpha's value need to be passed at compile time using -DWIDTH_VECTOR_A -DWIDTH_MATRIX_B and -DALPHA |
| 963 | * |
| 964 | * @attention The input vector A and matrix B must not be reshaped |
| 965 | * |
| 966 | * @param[in] src0_ptr Pointer to the source matrix. Supported data types: F16 |
| 967 | * @param[in] src0_stride_x Stride of the source matrix in X dimension (in bytes) |
| 968 | * @param[in] src0_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 969 | * @param[in] src0_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 970 | * @param[in] src0_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 971 | * @param[in] src0_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 972 | * @param[in] src1_ptr Pointer to the source matrix. Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 973 | * @param[in] src1_stride_x Stride of the source matrix in X dimension (in bytes) |
| 974 | * @param[in] src1_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 975 | * @param[in] src1_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 976 | * @param[in] src1_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 977 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 978 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 979 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 980 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 981 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 982 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 983 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 984 | */ |
| 985 | __kernel void gemm_vm_f16(IMAGE_DECLARATION(src0), |
| 986 | IMAGE_DECLARATION(src1), |
| 987 | IMAGE_DECLARATION(dst)) |
| 988 | { |
| 989 | int idx = get_global_id(0) * 8; |
| 990 | |
| 991 | /* Compute the address for the vector A and matrix B */ |
| 992 | int2 src_addr = ((int2)(src0_offset_first_element_in_bytes, src1_offset_first_element_in_bytes)); |
| 993 | src_addr.s1 += idx * sizeof(half); |
| 994 | |
| 995 | int end_row_vec_a = src_addr.s0 + (WIDTH_VECTOR_A * sizeof(half)); |
| 996 | |
| 997 | half8 acc = 0.0f; |
| 998 | |
| 999 | for(; src_addr.s0 <= (end_row_vec_a - 4 * sizeof(half)); src_addr += (int2)(4 * sizeof(half), 4 * src1_stride_y)) |
| 1000 | { |
| 1001 | half4 a0 = vload4(0, (__global half *)(src0_ptr + src_addr.s0)); |
| 1002 | half8 b0 = vload8(0, (__global half *)(src1_ptr + src_addr.s1 + 0 * src1_stride_y)); |
| 1003 | half8 b1 = vload8(0, (__global half *)(src1_ptr + src_addr.s1 + 1 * src1_stride_y)); |
| 1004 | half8 b2 = vload8(0, (__global half *)(src1_ptr + src_addr.s1 + 2 * src1_stride_y)); |
| 1005 | half8 b3 = vload8(0, (__global half *)(src1_ptr + src_addr.s1 + 3 * src1_stride_y)); |
| 1006 | |
| 1007 | acc += b0 * (half8)a0.s0; |
| 1008 | acc += b1 * (half8)a0.s1; |
| 1009 | acc += b2 * (half8)a0.s2; |
| 1010 | acc += b3 * (half8)a0.s3; |
| 1011 | } |
| 1012 | |
| 1013 | for(; src_addr.s0 < end_row_vec_a; src_addr += (int2)(sizeof(half), src1_stride_y)) |
| 1014 | { |
| 1015 | half a0 = *((__global half *)(src0_ptr + src_addr.s0)); |
| 1016 | half8 b0 = vload8(0, (__global half *)(src1_ptr + src_addr.s1)); |
| 1017 | |
| 1018 | acc += b0 * (half8)a0; |
| 1019 | } |
| 1020 | |
| 1021 | /* Compute destination address */ |
| 1022 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 1023 | |
| 1024 | /* Multiply by the weight of vector-matrix product */ |
| 1025 | acc = acc * (half8)ALPHA; |
| 1026 | |
| 1027 | vstore8(acc, 0, (__global half *)(offset(&dst, 0, 0))); |
| 1028 | } |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 1029 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 1030 | #ifdef FIXED_POINT_POSITION |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 1031 | /** This OpenCL kernel computes the vector by matrix multiplication between the vector A (src0) and matrix B (src1) in 8 bit fixed point |
| 1032 | * |
| 1033 | * @attention The width of vector A, the width of matrix B, the alpha's value and the fixed point position need to be passed at compile time using -DWIDTH_VECTOR_A -DWIDTH_MATRIX_B, -DALPHA and -DFIXED_POINT_POSITION |
| 1034 | * |
| 1035 | * @attention The input vector A and matrix B must not be reshaped |
| 1036 | * |
| 1037 | * @note: ALPHA must be passed in 8 bit fixed point format |
| 1038 | * |
| 1039 | * @param[in] src0_ptr Pointer to the source matrix. Supported data types: QS8 |
| 1040 | * @param[in] src0_stride_x Stride of the source matrix in X dimension (in bytes) |
| 1041 | * @param[in] src0_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1042 | * @param[in] src0_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 1043 | * @param[in] src0_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1044 | * @param[in] src0_offset_first_element_in_bytes The offset of the first element in the source matrix |
| 1045 | * @param[in] src1_ptr Pointer to the source matrix. Supported data types: same as @p src0_ptr |
| 1046 | * @param[in] src1_stride_x Stride of the source matrix in X dimension (in bytes) |
| 1047 | * @param[in] src1_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1048 | * @param[in] src1_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 1049 | * @param[in] src1_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1050 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the source matrix |
| 1051 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src0_ptr |
| 1052 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 1053 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1054 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 1055 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1056 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 1057 | */ |
| 1058 | __kernel void gemm_vm_qs8(IMAGE_DECLARATION(src0), |
| 1059 | IMAGE_DECLARATION(src1), |
| 1060 | IMAGE_DECLARATION(dst)) |
| 1061 | { |
| 1062 | int idx = get_global_id(0) * 16; |
| 1063 | |
| 1064 | /* Compute the address for the vector A and matrix B */ |
| 1065 | int2 src_addr = ((int2)(src0_offset_first_element_in_bytes, src1_offset_first_element_in_bytes)); |
| 1066 | src_addr.s1 += idx; |
| 1067 | |
| 1068 | int end_row_vec_a = src_addr.s0 + WIDTH_VECTOR_A; |
| 1069 | |
| 1070 | short8 acc0 = 0; |
| 1071 | short8 acc1 = 0; |
| 1072 | |
| 1073 | /* This for loop performs 4 accumulations per iteration */ |
| 1074 | for(; src_addr.s0 <= (end_row_vec_a - 4); src_addr += (int2)(4, 4 * src1_stride_y)) |
| 1075 | { |
| 1076 | char4 a0 = vload4(0, (__global char *)(src0_ptr + src_addr.s0)); |
| 1077 | char16 b0 = vload16(0, (__global char *)(src1_ptr + src_addr.s1 + 0 * src1_stride_y)); |
| 1078 | char16 b1 = vload16(0, (__global char *)(src1_ptr + src_addr.s1 + 1 * src1_stride_y)); |
| 1079 | char16 b2 = vload16(0, (__global char *)(src1_ptr + src_addr.s1 + 2 * src1_stride_y)); |
| 1080 | char16 b3 = vload16(0, (__global char *)(src1_ptr + src_addr.s1 + 3 * src1_stride_y)); |
| 1081 | |
| 1082 | acc0 = mlal_sat_qs8x8(acc0, (char8)a0.s0, b0.s01234567, FIXED_POINT_POSITION); |
| 1083 | acc0 = mlal_sat_qs8x8(acc0, (char8)a0.s1, b1.s01234567, FIXED_POINT_POSITION); |
| 1084 | acc0 = mlal_sat_qs8x8(acc0, (char8)a0.s2, b2.s01234567, FIXED_POINT_POSITION); |
| 1085 | acc0 = mlal_sat_qs8x8(acc0, (char8)a0.s3, b3.s01234567, FIXED_POINT_POSITION); |
| 1086 | |
| 1087 | acc1 = mlal_sat_qs8x8(acc1, (char8)a0.s0, b0.s89ABCDEF, FIXED_POINT_POSITION); |
| 1088 | acc1 = mlal_sat_qs8x8(acc1, (char8)a0.s1, b1.s89ABCDEF, FIXED_POINT_POSITION); |
| 1089 | acc1 = mlal_sat_qs8x8(acc1, (char8)a0.s2, b2.s89ABCDEF, FIXED_POINT_POSITION); |
| 1090 | acc1 = mlal_sat_qs8x8(acc1, (char8)a0.s3, b3.s89ABCDEF, FIXED_POINT_POSITION); |
| 1091 | } |
| 1092 | |
| 1093 | /* Left-over accumulations */ |
| 1094 | for(; src_addr.s0 < end_row_vec_a; src_addr += (int2)(1, src1_stride_y)) |
| 1095 | { |
| 1096 | char a0 = *((__global char *)(src0_ptr + src_addr.s0)); |
| 1097 | char16 b0 = vload16(0, (__global char *)(src1_ptr + src_addr.s1)); |
| 1098 | |
| 1099 | acc0 = mlal_sat_qs8x8(acc0, (char8)a0, b0.s01234567, FIXED_POINT_POSITION); |
| 1100 | acc1 = mlal_sat_qs8x8(acc1, (char8)a0, b0.s89ABCDEF, FIXED_POINT_POSITION); |
| 1101 | } |
| 1102 | |
| 1103 | /* Compute destination address */ |
| 1104 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 1105 | |
| 1106 | /* Multiply by the weight of matrix product */ |
| 1107 | char16 acc_qs8 = convert_char16_sat((short16)(acc0, acc1)); |
| 1108 | |
| 1109 | acc_qs8 = mul_sat_qs8x16(acc_qs8, (char16)ALPHA, FIXED_POINT_POSITION); |
| 1110 | |
| 1111 | /* Store 16 values */ |
| 1112 | vstore16(acc_qs8, 0, (__global char *)(offset(&dst, 0, 0))); |
| 1113 | } |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 1114 | #endif /* FIXED_POINT_POSITION */ |
| 1115 | #endif /* WIDTH_VECTOR_A */ |
| 1116 | #endif /* WIDTH_MATRIX_B && ALPHA */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1117 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 1118 | #ifdef BETA |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1119 | /** This OpenCL kernel performs the in-place matrix addition between 2 matrices taking into account that the second matrix might be weighted by a scalar value beta: |
| 1120 | * |
| 1121 | * @attention The beta's value need to be passed at compile time using -DBETA |
| 1122 | * |
| 1123 | * @param[in] src_ptr Pointer to the source matrix. Supported data types: F32 |
| 1124 | * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes) |
| 1125 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1126 | * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 1127 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1128 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 1129 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1130 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 1131 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1132 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 1133 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1134 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 1135 | */ |
| 1136 | __kernel void gemm_ma_f32(IMAGE_DECLARATION(src), |
| 1137 | IMAGE_DECLARATION(dst)) |
| 1138 | { |
| 1139 | /* Compute source and destination addresses */ |
| 1140 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 1141 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 1142 | |
| 1143 | /* Load values from A x B */ |
| 1144 | float4 alpha_ab = vload4(0, (__global float *)dst.ptr); |
| 1145 | |
| 1146 | /* Load values from Matrix C */ |
| 1147 | float4 c = vload4(0, (__global float *)src.ptr); |
| 1148 | |
| 1149 | /* Computes alpha * axb + beta * c */ |
| 1150 | float4 out = alpha_ab + (float4)BETA * c; |
| 1151 | |
| 1152 | /* Store final result in axb matrix */ |
| 1153 | vstore4(out, 0, (__global float *)dst.ptr); |
| 1154 | } |
| 1155 | |
| 1156 | /** This OpenCL kernel performs the in-place matrix addition between 2 matrices taking into account that the second matrix might be weighted by a scalar value beta: |
| 1157 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 1158 | * @attention The beta's value need to be passed at compile time using -DBETA |
| 1159 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1160 | * @param[in] src_ptr Pointer to the source matrix. Supported data types: F16 |
| 1161 | * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes) |
| 1162 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1163 | * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 1164 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1165 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 1166 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1167 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 1168 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1169 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 1170 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1171 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 1172 | */ |
| 1173 | __kernel void gemm_ma_f16(IMAGE_DECLARATION(src), |
| 1174 | IMAGE_DECLARATION(dst)) |
| 1175 | { |
| 1176 | /* Compute source and destination addresses */ |
| 1177 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 1178 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 1179 | |
| 1180 | /* Load values from A x B */ |
| 1181 | half8 alpha_ab = vload8(0, (__global half *)dst.ptr); |
| 1182 | |
| 1183 | /* Load values from Matrix C */ |
| 1184 | half8 c = vload8(0, (__global half *)src.ptr); |
| 1185 | |
| 1186 | /* Computes alpha * axb + beta * c */ |
| 1187 | half8 out = alpha_ab + (half8)BETA * c; |
| 1188 | |
| 1189 | /* Store final result in axb matrix */ |
| 1190 | vstore8(out, 0, (__global half *)dst.ptr); |
| 1191 | } |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 1192 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 1193 | #ifdef FIXED_POINT_POSITION |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 1194 | /** This OpenCL kernel performs the in-place matrix addition between 2 matrices in 8 bit fixed point taking into account that the second matrix might be weighted by a scalar value beta: |
| 1195 | * |
| 1196 | * @attention The beta's value and the fixed point position need to be passed at compile time using -DBETA and -DFIXED_POINT_POSITION |
| 1197 | * |
| 1198 | * @note: BETA must be passed in 8 bit fixed point format |
| 1199 | * |
| 1200 | * @param[in] src_ptr Pointer to the source matrix. Supported data types: QS8 |
| 1201 | * @param[in] src_stride_x Stride of the source matrix in X dimension (in bytes) |
| 1202 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1203 | * @param[in] src_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 1204 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1205 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source matrix |
| 1206 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src_ptr |
| 1207 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 1208 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1209 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 1210 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1211 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 1212 | */ |
| 1213 | __kernel void gemm_ma_qs8(IMAGE_DECLARATION(src), |
| 1214 | IMAGE_DECLARATION(dst)) |
| 1215 | { |
| 1216 | /* Compute source and destination addresses */ |
| 1217 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 1218 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 1219 | |
| 1220 | /* Load values from A x B */ |
| 1221 | char16 alpha_ab = vload16(0, (__global char *)dst.ptr); |
| 1222 | |
| 1223 | /* Load values from Matrix C */ |
| 1224 | char16 c = vload16(0, (__global char *)src.ptr); |
| 1225 | |
| 1226 | /* Computes alpha * axb + beta * c */ |
| 1227 | char16 out = mla_sat_qs8x16(alpha_ab, (char16)BETA, c, FIXED_POINT_POSITION); |
| 1228 | |
| 1229 | /* Store final result in axb matrix */ |
| 1230 | vstore16(out, 0, (__global char *)dst.ptr); |
| 1231 | } |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 1232 | #endif /* FIXED_POINT_POSITION */ |
| 1233 | #endif /* BETA */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1234 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 1235 | #ifdef WIDTH_VECTOR_A |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1236 | /** This OpenCL kernel computes the vector by matrix multiplication between each row of A (src0) and matrix B (src1) used for locally connected layer |
| 1237 | * |
| 1238 | * @attention The width of A need to be passed at compile time using -DWIDTH_VECTOR_A |
| 1239 | * |
| 1240 | * @attention The input A and matrix B must not be reshaped |
| 1241 | * |
| 1242 | * @param[in] src0_ptr Pointer to the source matrix. Supported data types: F32 |
| 1243 | * @param[in] src0_stride_x Stride of the source matrix in X dimension (in bytes) |
| 1244 | * @param[in] src0_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1245 | * @param[in] src0_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 1246 | * @param[in] src0_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1247 | * @param[in] src0_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 1248 | * @param[in] src1_ptr Pointer to the source matrix. Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1249 | * @param[in] src1_stride_x Stride of the source matrix in X dimension (in bytes) |
| 1250 | * @param[in] src1_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 1251 | * @param[in] src1_stride_y Stride of the source matrix in Y dimension (in bytes) |
| 1252 | * @param[in] src1_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1253 | * @param[in] src1_stride_z Stride of the source matrix in Z dimension (in bytes) |
| 1254 | * @param[in] src1_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 1255 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the source matrix |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 1256 | * @param[out] dst_ptr Pointer to the destination matrix Supported data types: same as @p src0_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1257 | * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes) |
| 1258 | * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes) |
| 1259 | * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes) |
| 1260 | * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes) |
| 1261 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix |
| 1262 | */ |
| 1263 | __kernel void gemm_lc_vm_f32(IMAGE_DECLARATION(src0), |
| 1264 | TENSOR3D_DECLARATION(src1), |
| 1265 | IMAGE_DECLARATION(dst)) |
| 1266 | { |
| 1267 | int idx = get_global_id(0) * 4; |
| 1268 | int idy = get_global_id(1); |
| 1269 | |
| 1270 | /* Compute the address for the vector A and matrix B */ |
| 1271 | int2 src_addr = ((int2)(src0_offset_first_element_in_bytes + src0_stride_y * idy, src1_offset_first_element_in_bytes + src1_stride_z * idy)); |
| 1272 | src_addr.s1 += idx * sizeof(float); |
| 1273 | |
| 1274 | int end_row_vec_a = src_addr.s0 + (WIDTH_VECTOR_A * sizeof(float)); |
| 1275 | |
| 1276 | float4 acc = 0.0f; |
| 1277 | |
| 1278 | for(; src_addr.s0 <= (end_row_vec_a - 2 * sizeof(float)); src_addr += (int2)(2 * sizeof(float), 2 * src1_stride_y)) |
| 1279 | { |
| 1280 | float2 a0 = vload2(0, (__global float *)(src0_ptr + src_addr.s0)); |
| 1281 | float4 b0 = vload4(0, (__global float *)(src1_ptr + src_addr.s1)); |
| 1282 | float4 b1 = vload4(0, (__global float *)(src1_ptr + src_addr.s1 + src1_stride_y)); |
| 1283 | |
| 1284 | acc += b0 * (float4)a0.s0; |
| 1285 | acc += b1 * (float4)a0.s1; |
| 1286 | } |
| 1287 | |
| 1288 | for(; src_addr.s0 < end_row_vec_a; src_addr += (int2)(sizeof(float), src1_stride_y)) |
| 1289 | { |
| 1290 | float a0 = *((__global float *)(src0_ptr + src_addr.s0)); |
| 1291 | float4 b0 = vload4(0, (__global float *)(src1_ptr + src_addr.s1)); |
| 1292 | |
| 1293 | acc += b0 * (float4)a0; |
| 1294 | } |
| 1295 | |
| 1296 | /* Compute destination address */ |
| 1297 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 1298 | |
| 1299 | vstore4(acc, 0, (__global float *)(offset(&dst, 0, 0))); |
| 1300 | } |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 1301 | #endif /* WIDTH_VECTOR_A */ |