Michele Di Giorgio | 56dd726 | 2017-07-27 09:53:49 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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 | * |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 36 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/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) |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 93 | /** This performs per channel dequantization of 8-bit signed integers to floating point. (NCHW) |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 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 | * |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 99 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: QSYMM8_PER_CHANNEL |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 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 |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 116 | */ |
| 117 | __kernel void dequantization_layer_per_channel_nchw( |
| 118 | TENSOR3D_DECLARATION(input), |
| 119 | TENSOR3D_DECLARATION(output), |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 120 | __global float *scale) |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 121 | { |
| 122 | // Get pixels pointer |
| 123 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 124 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 125 | |
| 126 | #if defined(LAST_ACCESSED_X) |
| 127 | // Check if access on width gets out of bounds |
| 128 | // If it does shift access vector to access elements within bounds |
| 129 | const int xi = (int)(get_global_id(0) * VEC_SIZE); |
| 130 | input.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * input_stride_x; |
| 131 | output.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * output_stride_x; |
| 132 | |
| 133 | // Load data |
| 134 | VEC_DATA_TYPE(int, VEC_SIZE) |
| 135 | val = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE_SRC *)input.ptr), VEC_DATA_TYPE(int, VEC_SIZE)); |
| 136 | |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 137 | // Create scale vectors |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 138 | const VEC_DATA_TYPE(float, VEC_SIZE) |
| 139 | vscale = scale[get_global_id(2)]; |
| 140 | |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 141 | // Dequantize |
| 142 | VEC_DATA_TYPE(float, VEC_SIZE) |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 143 | res = vscale * CONVERT((val), VEC_DATA_TYPE(float, VEC_SIZE)); |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 144 | |
| 145 | // Store result |
| 146 | VSTORE(VEC_SIZE) |
| 147 | (CONVERT(res, VEC_DATA_TYPE(DATA_TYPE_DST, VEC_SIZE)), 0, (__global DATA_TYPE_DST *)output.ptr); |
| 148 | #else // !defined(LAST_ACCESSED_X) |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 149 | *((__global DATA_TYPE_DST *)(output.ptr)) = (DATA_TYPE_DST)((float)((int)(*((__global DATA_TYPE_SRC *)(input.ptr)))) * scale[get_global_id(2)]); |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 150 | #endif // defined(LAST_ACCESSED_X) |
| 151 | } |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 152 | /** This performs per channel dequantization of 8-bit signed integers to floating point. (NHWC) |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 153 | * |
| 154 | * @note Source datatype should be given as a preprocessor argument using -DDATA_TYPE_SRC=type. e.g. -DDATA_TYPE_SRC=char |
| 155 | * @note Destination datatype should be given as a preprocessor argument using -DDATA_TYPE_DST=type. e.g. -DDATA_TYPE_DST=float |
| 156 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 |
| 157 | * |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 158 | * @param[in] input_ptr Pointer to the source tensor. Supported data types: QSYMM8_PER_CHANNEL |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 159 | * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes) |
| 160 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 161 | * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes) |
| 162 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 163 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 164 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 165 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor |
| 166 | * @param[out] output_ptr Pointer to the destination tensor. Supported data types: F16/F32 |
| 167 | * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 168 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 169 | * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 170 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 171 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 172 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 173 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 174 | * @param[in] scale Pointer to buffer with the per channel quantized scales |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 175 | */ |
| 176 | __kernel void dequantization_layer_per_channel_nhwc( |
| 177 | TENSOR3D_DECLARATION(input), |
| 178 | TENSOR3D_DECLARATION(output), |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 179 | __global float *scale) |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 180 | { |
| 181 | // Get pixels pointer |
| 182 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 183 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 184 | |
| 185 | #if defined(LAST_ACCESSED_X) |
| 186 | // Check if access on width gets out of bounds |
| 187 | // If it does shift access vector to access elements within bounds |
| 188 | const int xi = (int)(get_global_id(0) * VEC_SIZE); |
| 189 | input.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * input_stride_x; |
| 190 | output.ptr -= max(xi - (int)LAST_ACCESSED_X, 0) * output_stride_x; |
| 191 | scale -= max(xi - (int)LAST_ACCESSED_X, 0); |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 192 | |
| 193 | // Load data |
| 194 | VEC_DATA_TYPE(int, VEC_SIZE) |
| 195 | val = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE_SRC *)input.ptr), VEC_DATA_TYPE(int, VEC_SIZE)); |
| 196 | |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 197 | // Create scale vectors |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 198 | const VEC_DATA_TYPE(float, VEC_SIZE) |
| 199 | vscale = VLOAD(VEC_SIZE)(0, &scale[xi]); |
| 200 | |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 201 | // Dequantize |
| 202 | VEC_DATA_TYPE(float, VEC_SIZE) |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 203 | res = vscale * CONVERT((val), VEC_DATA_TYPE(float, VEC_SIZE)); |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 204 | |
| 205 | // Store result |
| 206 | VSTORE(VEC_SIZE) |
| 207 | (CONVERT(res, VEC_DATA_TYPE(DATA_TYPE_DST, VEC_SIZE)), 0, (__global DATA_TYPE_DST *)output.ptr); |
| 208 | #else // !defined(LAST_ACCESSED_X) |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 209 | *((__global DATA_TYPE_DST *)(output.ptr)) = (DATA_TYPE_DST)((float)((int)(*((__global DATA_TYPE_SRC *)(input.ptr)))) * scale[get_global_id(0)]); |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 210 | #endif // defined(LAST_ACCESSED_X) |
| 211 | } |
| 212 | #endif // defined(VEC_SIZE) && defined(DATA_TYPE_SRC) && defined(DATA_TYPE_DST) |