Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 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 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 26 | #if defined(VEC_SIZE) && defined(DATA_TYPE_SRC) && defined(DATA_TYPE_DST) && defined(SCALE) && defined(OFFSET) |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 27 | |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 28 | /** This performs the dequantization of 8-bit unsigned integers to floating point. |
| 29 | * |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 30 | * @note Source datatype should be given as a preprocessor argument using -DDATA_TYPE_SRC=type. e.g. -DDATA_TYPE_SRC=char |
| 31 | * @note Destination datatype should be given as a preprocessor argument using -DDATA_TYPE_DST=type. e.g. -DDATA_TYPE_DST=float |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 32 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 |
| 33 | * @note Quantization scale of input tensor is passed in with -DSCALE=scale. |
| 34 | * @note Quantization offset of input tensor is passed in with -DOFFSET=offset. |
| 35 | * |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 36 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/QSYMM8 |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 37 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
| 38 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 39 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 40 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 41 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 42 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 43 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 44 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F16/F32 |
| 45 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 46 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 47 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 48 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 49 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 50 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 51 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 52 | */ |
| 53 | __kernel void dequantization_layer( |
| 54 | TENSOR3D_DECLARATION(input), |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 55 | TENSOR3D_DECLARATION(output)) |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 56 | { |
| 57 | // Get pixels pointer |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 58 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 59 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 60 | |
Michele Di Giorgio | cbe3905 | 2019-07-02 11:39:10 +0100 | [diff] [blame] | 61 | #if defined(LAST_ACCESSED_X) |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 62 | // Check if access on width gets out of bounds |
| 63 | // If it does shift access vector to access elements within bounds |
| 64 | const int xi = (int)(get_global_id(0) * VEC_SIZE); |
| 65 | input.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * input_stride_x; |
| 66 | output.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * output_stride_x; |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 67 | |
| 68 | // Load data |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 69 | VEC_DATA_TYPE(int, VEC_SIZE) |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 70 | val = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE_SRC *)input.ptr), VEC_DATA_TYPE(int, VEC_SIZE)); |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 71 | |
| 72 | // Create scale and offset vectors |
| 73 | const VEC_DATA_TYPE(float, VEC_SIZE) |
| 74 | vscale = SCALE; |
| 75 | |
| 76 | const VEC_DATA_TYPE(int, VEC_SIZE) |
| 77 | voffset = OFFSET; |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 78 | |
| 79 | // Dequantize |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 80 | VEC_DATA_TYPE(float, VEC_SIZE) |
| 81 | res = vscale * CONVERT((val - voffset), VEC_DATA_TYPE(float, VEC_SIZE)); |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 82 | |
| 83 | // Store result |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 84 | VSTORE(VEC_SIZE) |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 85 | (CONVERT(res, VEC_DATA_TYPE(DATA_TYPE_DST, VEC_SIZE)), 0, (__global DATA_TYPE_DST *)output.ptr); |
Michele Di Giorgio | cbe3905 | 2019-07-02 11:39:10 +0100 | [diff] [blame] | 86 | #else // !defined(LAST_ACCESSED_X) |
| 87 | *((__global DATA_TYPE_DST *)(output.ptr)) = (DATA_TYPE_DST)((float)((int)(*((__global DATA_TYPE_SRC *)(input.ptr))) - (int)(OFFSET)) * (float)(SCALE)); |
| 88 | #endif // defined(LAST_ACCESSED_X) |
Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 89 | } |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 90 | #endif // defined(VEC_SIZE) && defined(DATA_TYPE_SRC) && defined(DATA_TYPE_DST) && defined(SCALE) && defined(OFFSET) |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 91 | |
| 92 | #if defined(VEC_SIZE) && defined(DATA_TYPE_SRC) && defined(DATA_TYPE_DST) |
| 93 | /** This performs per channel dequantization of 8-bit unsigned integers to floating point. (NCHW) |
| 94 | * |
| 95 | * @note Source datatype should be given as a preprocessor argument using -DDATA_TYPE_SRC=type. e.g. -DDATA_TYPE_SRC=char |
| 96 | * @note Destination datatype should be given as a preprocessor argument using -DDATA_TYPE_DST=type. e.g. -DDATA_TYPE_DST=float |
| 97 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 |
| 98 | * |
| 99 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8_PER_CHANNEL |
| 100 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
| 101 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 102 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 103 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 104 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 105 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 106 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 107 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F16/F32 |
| 108 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 109 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 110 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 111 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 112 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 113 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 114 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 115 | * @param[in] scale Pointer to buffer with the per channel quantized scales |
| 116 | * @param[in] offset Pointer to buffer with the per channel quantized offsets |
| 117 | */ |
| 118 | __kernel void dequantization_layer_per_channel_nchw( |
| 119 | TENSOR3D_DECLARATION(input), |
| 120 | TENSOR3D_DECLARATION(output), |
| 121 | __global float *scale, |
| 122 | __global int *offset) |
| 123 | { |
| 124 | // Get pixels pointer |
| 125 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 126 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 127 | |
| 128 | #if defined(LAST_ACCESSED_X) |
| 129 | // Check if access on width gets out of bounds |
| 130 | // If it does shift access vector to access elements within bounds |
| 131 | const int xi = (int)(get_global_id(0) * VEC_SIZE); |
| 132 | input.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * input_stride_x; |
| 133 | output.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * output_stride_x; |
| 134 | |
| 135 | // Load data |
| 136 | VEC_DATA_TYPE(int, VEC_SIZE) |
| 137 | val = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE_SRC *)input.ptr), VEC_DATA_TYPE(int, VEC_SIZE)); |
| 138 | |
| 139 | // Create scale and offset vectors |
| 140 | const VEC_DATA_TYPE(float, VEC_SIZE) |
| 141 | vscale = scale[get_global_id(2)]; |
| 142 | |
| 143 | const VEC_DATA_TYPE(int, VEC_SIZE) |
| 144 | voffset = offset[get_global_id(2)]; |
| 145 | |
| 146 | // Dequantize |
| 147 | VEC_DATA_TYPE(float, VEC_SIZE) |
| 148 | res = vscale * CONVERT((val - voffset), VEC_DATA_TYPE(float, VEC_SIZE)); |
| 149 | |
| 150 | // Store result |
| 151 | VSTORE(VEC_SIZE) |
| 152 | (CONVERT(res, VEC_DATA_TYPE(DATA_TYPE_DST, VEC_SIZE)), 0, (__global DATA_TYPE_DST *)output.ptr); |
| 153 | #else // !defined(LAST_ACCESSED_X) |
| 154 | *((__global DATA_TYPE_DST *)(output.ptr)) = (DATA_TYPE_DST)((float)((int)(*((__global DATA_TYPE_SRC *)(input.ptr))) - offset[get_global_id(2)]) * scale[get_global_id(2)]); |
| 155 | #endif // defined(LAST_ACCESSED_X) |
| 156 | } |
| 157 | /** This performs per channel dequantization of 8-bit unsigned integers to floating point. (NHWC) |
| 158 | * |
| 159 | * @note Source datatype should be given as a preprocessor argument using -DDATA_TYPE_SRC=type. e.g. -DDATA_TYPE_SRC=char |
| 160 | * @note Destination datatype should be given as a preprocessor argument using -DDATA_TYPE_DST=type. e.g. -DDATA_TYPE_DST=float |
| 161 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 |
| 162 | * |
| 163 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8_PER_CHANNEL |
| 164 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
| 165 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 166 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 167 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 168 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 169 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 170 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 171 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F16/F32 |
| 172 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 173 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 174 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 175 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 176 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 177 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 178 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 179 | * @param[in] scale Pointer to buffer with the per channel quantized scales |
| 180 | * @param[in] offset Pointer to buffer with the per channel quantized offsets |
| 181 | */ |
| 182 | __kernel void dequantization_layer_per_channel_nhwc( |
| 183 | TENSOR3D_DECLARATION(input), |
| 184 | TENSOR3D_DECLARATION(output), |
| 185 | __global float *scale, |
| 186 | __global int *offset) |
| 187 | { |
| 188 | // Get pixels pointer |
| 189 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 190 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 191 | |
| 192 | #if defined(LAST_ACCESSED_X) |
| 193 | // Check if access on width gets out of bounds |
| 194 | // If it does shift access vector to access elements within bounds |
| 195 | const int xi = (int)(get_global_id(0) * VEC_SIZE); |
| 196 | input.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * input_stride_x; |
| 197 | output.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * output_stride_x; |
| 198 | scale -= max(xi - (int)LAST_ACCESSED_X, 0); |
| 199 | offset -= max(xi - (int)LAST_ACCESSED_X, 0); |
| 200 | |
| 201 | // Load data |
| 202 | VEC_DATA_TYPE(int, VEC_SIZE) |
| 203 | val = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE_SRC *)input.ptr), VEC_DATA_TYPE(int, VEC_SIZE)); |
| 204 | |
| 205 | // Create scale and offset vectors |
| 206 | const VEC_DATA_TYPE(float, VEC_SIZE) |
| 207 | vscale = VLOAD(VEC_SIZE)(0, &scale[xi]); |
| 208 | |
| 209 | const VEC_DATA_TYPE(int, VEC_SIZE) |
| 210 | voffset = VLOAD(VEC_SIZE)(0, &offset[xi]); |
| 211 | |
| 212 | // Dequantize |
| 213 | VEC_DATA_TYPE(float, VEC_SIZE) |
| 214 | res = vscale * CONVERT((val - voffset), VEC_DATA_TYPE(float, VEC_SIZE)); |
| 215 | |
| 216 | // Store result |
| 217 | VSTORE(VEC_SIZE) |
| 218 | (CONVERT(res, VEC_DATA_TYPE(DATA_TYPE_DST, VEC_SIZE)), 0, (__global DATA_TYPE_DST *)output.ptr); |
| 219 | #else // !defined(LAST_ACCESSED_X) |
| 220 | *((__global DATA_TYPE_DST *)(output.ptr)) = (DATA_TYPE_DST)((float)((int)(*((__global DATA_TYPE_SRC *)(input.ptr))) - offset[get_global_id(0)]) * scale[get_global_id(0)]); |
| 221 | #endif // defined(LAST_ACCESSED_X) |
| 222 | } |
| 223 | #endif // defined(VEC_SIZE) && defined(DATA_TYPE_SRC) && defined(DATA_TYPE_DST) |