Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +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 | */ |
| 24 | #include "helpers.h" |
| 25 | |
| 26 | /** This kernel applies dot product to each plane on the input tensor and the corrispective column of the reshaped weight tensor. |
| 27 | * |
| 28 | * @note Datatype and source width and height should be given as a preprocessor argument using -DDATA_TYPE=type, -DSRC_WIDTH=width and -DSRC_HEIGHT=height. e.g. -DDATA_TYPE=short |
| 29 | * |
| 30 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32 |
| 31 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 32 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 33 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 34 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 35 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 36 | * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes) |
| 37 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 38 | * @param[out] weights_ptr Pointer to the weights tensor. Same as @p src_ptr |
| 39 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 40 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 41 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 42 | * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes) |
| 43 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 44 | * @param[out] dst_ptr Pointer to the destination tensor. Same as @p src_ptr |
| 45 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 46 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 47 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 48 | */ |
| 49 | __kernel void gemm_mv(TENSOR3D_DECLARATION(src), IMAGE_DECLARATION(weights), VECTOR_DECLARATION(dst)) |
| 50 | { |
| 51 | Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src); |
| 52 | |
| 53 | int y = get_global_id(1) * 4; |
| 54 | int z = get_global_id(2); |
| 55 | |
| 56 | __global uchar *current_weights = weights_ptr + weights_offset_first_element_in_bytes + z * weights_stride_y; |
| 57 | __global uchar *input_ptr = src.ptr; |
| 58 | |
| 59 | DATA_TYPE acc0 = (DATA_TYPE)0; |
| 60 | DATA_TYPE acc1 = (DATA_TYPE)0; |
| 61 | DATA_TYPE acc2 = (DATA_TYPE)0; |
| 62 | DATA_TYPE acc3 = (DATA_TYPE)0; |
| 63 | |
| 64 | // This kernel handle 4 rows in per thread so that it can reuse the weights |
| 65 | for(int i = 0; i < SRC_WIDTH; i += 4) |
| 66 | { |
| 67 | VEC_DATA_TYPE(DATA_TYPE, 4) |
| 68 | weights = vload4(0, (__global DATA_TYPE *)(current_weights + i * weights_stride_x)); |
| 69 | |
| 70 | int4 offset = (int4)i * (int4)src_stride_x + (int4)(0, 1, 2, 3) * (int4)src_stride_y; |
| 71 | |
| 72 | VEC_DATA_TYPE(DATA_TYPE, 4) |
| 73 | tmp0 = vload4(0, (__global DATA_TYPE *)(input_ptr + offset.s0)); |
| 74 | VEC_DATA_TYPE(DATA_TYPE, 4) |
| 75 | tmp1 = vload4(0, (__global DATA_TYPE *)(input_ptr + offset.s1)); |
| 76 | VEC_DATA_TYPE(DATA_TYPE, 4) |
| 77 | tmp2 = vload4(0, (__global DATA_TYPE *)(input_ptr + offset.s2)); |
| 78 | VEC_DATA_TYPE(DATA_TYPE, 4) |
| 79 | tmp3 = vload4(0, (__global DATA_TYPE *)(input_ptr + offset.s3)); |
| 80 | |
| 81 | acc0 += dot(weights, tmp0); |
| 82 | acc1 += dot(weights, tmp1); |
| 83 | acc2 += dot(weights, tmp2); |
| 84 | acc3 += dot(weights, tmp3); |
| 85 | } |
| 86 | |
| 87 | __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + (y + z * SRC_HEIGHT) * dst_stride_x; |
| 88 | |
| 89 | int rows_left = SRC_HEIGHT - (y + 4); |
| 90 | |
| 91 | // This if check is used to handle the last few rows when it can't be divided by the four |
| 92 | if(rows_left >= 0) |
| 93 | { |
| 94 | VEC_DATA_TYPE(DATA_TYPE, 4) |
| 95 | out = (VEC_DATA_TYPE(DATA_TYPE, 4))(acc0, acc1, acc2, acc3); |
| 96 | vstore4(out, 0, (__global DATA_TYPE *)output_ptr); |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | switch(rows_left) |
| 101 | { |
| 102 | case -1: // three rows left; one is padding |
| 103 | *((__global DATA_TYPE *)(output_ptr + 2 * dst_stride_x)) = acc2; |
| 104 | case -2: // two rows left; two are padding |
| 105 | *((__global DATA_TYPE *)(output_ptr + 1 * dst_stride_x)) = acc1; |
| 106 | case -3: // one row left; three are padding |
| 107 | *((__global DATA_TYPE *)(output_ptr + 0 * dst_stride_x)) = acc0; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | } |