steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 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 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 26 | #if STRIDE_X == 1 |
| 27 | #define CONVOLUTION1x3(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x3_STRIDE1(acc, src_row_ptr, weights_row_ptr) |
| 28 | #elif STRIDE_X == 2 /* STRIDE_X == 1 */ |
| 29 | #define CONVOLUTION1x3(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x3_STRIDE2(acc, src_row_ptr, weights_row_ptr) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 30 | #else /* STRIDE_X not equals 1 or 2 */ |
| 31 | #error "STRIDE_X larger than 2 is not supported" |
| 32 | #endif /* STRIDE_X == 2 */ |
| 33 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 34 | #define CONVOLUTION1x3_STRIDE1(acc, src_row_ptr, weights_row_ptr) \ |
| 35 | ({ \ |
| 36 | VEC_DATA_TYPE(DATA_TYPE, 4) \ |
| 37 | weights_values0 = vload4(0, weights_row_ptr); \ |
| 38 | VEC_DATA_TYPE(DATA_TYPE, 8) \ |
| 39 | src0 = vload8(0, src_row_ptr); \ |
| 40 | VEC_DATA_TYPE(DATA_TYPE, 2) \ |
| 41 | src1 = vload2(0, src_row_ptr + 8); \ |
| 42 | \ |
| 43 | acc += src0 * (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s0; \ |
| 44 | acc += (VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s1234, src0.s567, src1.s0) * (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s1; \ |
| 45 | acc += (VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s234, src0.s567, src1.s01) * (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s2; \ |
| 46 | }) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 47 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 48 | #define CONVOLUTION1x3_STRIDE2(acc, src_row_ptr, weights_row_ptr) \ |
| 49 | ({ \ |
| 50 | VEC_DATA_TYPE(DATA_TYPE, 4) \ |
| 51 | weights_values0 = vload4(0, weights_row_ptr); \ |
| 52 | VEC_DATA_TYPE(DATA_TYPE, 16) \ |
| 53 | src0 = vload16(0, src_row_ptr); \ |
| 54 | DATA_TYPE src1 = *(src_row_ptr + 16); \ |
| 55 | \ |
| 56 | acc += src0.even * (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s0; \ |
| 57 | acc += (VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s1357, src0.s9BDF) * (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s1; \ |
| 58 | acc += (VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s2468, src0.sACE, src1) * (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s2; \ |
| 59 | }) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 60 | |
| 61 | /** This kernel performs a direct convolution to convolve the low three dimensions. |
| 62 | * |
| 63 | * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 64 | * @note The third dimensions of the weights tensors must be passed at compile time using -DWEIGHTS_DEPTH |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 65 | * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row. |
| 66 | * |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 67 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32 |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 68 | * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes) |
| 69 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 70 | * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 71 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 72 | * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 73 | * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes) |
| 74 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 75 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 76 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 77 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 78 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 79 | * @param[in] dst_step_y dst_stride_y * number of elements along Z processed per workitem(in bytes) |
| 80 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 81 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
| 82 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 83 | * @param[out] weights_ptr Pointer to the weights tensor. Supported data types: same as @p weights_ptr |
| 84 | * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes) |
| 85 | * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes) |
| 86 | * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes) |
| 87 | * @param[in] weights_step_y weights_stride_y * number of elements along y processed per workitem(in bytes) |
| 88 | * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes) |
| 89 | * @param[in] weights_step_z weights_stride_z * number of elements along Z processed per workitem(in bytes) |
| 90 | * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor |
| 91 | * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr |
| 92 | * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes) |
| 93 | * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes) |
| 94 | * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 95 | * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 96 | */ |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 97 | #if defined(DATA_TYPE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 98 | __kernel void direct_convolution3x3( |
| 99 | TENSOR3D_DECLARATION(src), |
| 100 | TENSOR3D_DECLARATION(dst), |
| 101 | TENSOR3D_DECLARATION(weights), |
| 102 | #ifdef HAS_BIAS |
| 103 | VECTOR_DECLARATION(biases), |
| 104 | #endif /* defined(HAS_BIAS) */ |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 105 | unsigned int weights_stride_w) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 106 | { |
| 107 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 108 | Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights); |
| 109 | Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst); |
| 110 | |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 111 | VEC_DATA_TYPE(DATA_TYPE, 8) |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 112 | pixels0 = 0; |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 113 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 114 | __global uchar *weights_addr = (__global uchar *)tensor3D_offset(&weights, 0, 0, 0); |
| 115 | __global uchar *src_addr = (__global uchar *)offset(&src, 0, 0); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 116 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 117 | const int kernel_index = get_global_id(2); |
| 118 | weights_addr += kernel_index * weights_stride_w; |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 119 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 120 | for(int d = 0; d < WEIGHTS_DEPTH; ++d) |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 121 | { |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 122 | CONVOLUTION1x3(pixels0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_y)); |
| 123 | CONVOLUTION1x3(pixels0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_y)); |
| 124 | CONVOLUTION1x3(pixels0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_y)); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 125 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 126 | src_addr += src_stride_z; |
| 127 | weights_addr += weights_stride_z; |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | #ifdef HAS_BIAS |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 131 | Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases); |
| 132 | |
| 133 | pixels0 += (VEC_DATA_TYPE(DATA_TYPE, 8)) * ((__global DATA_TYPE *)(vector_offset(&biases, kernel_index))); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 134 | #endif /* defined(HAS_BIAS) */ |
| 135 | |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 136 | vstore8(pixels0, 0, (__global DATA_TYPE *)dst.ptr); |
steniu01 | 27b386c | 2017-07-18 17:37:43 +0100 | [diff] [blame] | 137 | } |
Gian Marco Iodice | 5cb4d6a | 2017-08-08 10:53:00 +0100 | [diff] [blame^] | 138 | #endif // defined(DATA_TYPE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH) |