blob: 127f67d940316ba6f4c7aaed782c36847623bda0 [file] [log] [blame]
Michele Di Giorgio56dd7262017-07-27 09:53:49 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Michele Di Giorgio56dd7262017-07-27 09:53:49 +01003 *
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 Pinitas3d13af82019-06-04 13:04:16 +010026#if defined(VEC_SIZE) && defined(DATA_TYPE_SRC) && defined(DATA_TYPE_DST) && defined(SCALE) && defined(OFFSET)
Georgios Pinitas574775c2019-02-18 20:08:02 +000027
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010028/** This performs the dequantization of 8-bit unsigned integers to floating point.
29 *
Georgios Pinitas3d13af82019-06-04 13:04:16 +010030 * @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 Pinitas574775c2019-02-18 20:08:02 +000032 * @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 Giorgiocbbed282019-12-20 13:26:08 +000036 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/QSYMM8
Georgios Pinitas574775c2019-02-18 20:08:02 +000037 * @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 Giorgio56dd7262017-07-27 09:53:49 +010052 */
53__kernel void dequantization_layer(
54 TENSOR3D_DECLARATION(input),
Georgios Pinitas574775c2019-02-18 20:08:02 +000055 TENSOR3D_DECLARATION(output))
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010056{
57 // Get pixels pointer
Georgios Pinitas574775c2019-02-18 20:08:02 +000058 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
59 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010060
Michele Di Giorgiocbe39052019-07-02 11:39:10 +010061#if defined(LAST_ACCESSED_X)
Georgios Pinitas574775c2019-02-18 20:08:02 +000062 // 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 Giorgio56dd7262017-07-27 09:53:49 +010067
68 // Load data
Georgios Pinitas574775c2019-02-18 20:08:02 +000069 VEC_DATA_TYPE(int, VEC_SIZE)
Georgios Pinitas3d13af82019-06-04 13:04:16 +010070 val = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE_SRC *)input.ptr), VEC_DATA_TYPE(int, VEC_SIZE));
Georgios Pinitas574775c2019-02-18 20:08:02 +000071
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 Giorgio56dd7262017-07-27 09:53:49 +010078
79 // Dequantize
Georgios Pinitas574775c2019-02-18 20:08:02 +000080 VEC_DATA_TYPE(float, VEC_SIZE)
81 res = vscale * CONVERT((val - voffset), VEC_DATA_TYPE(float, VEC_SIZE));
Michele Di Giorgio56dd7262017-07-27 09:53:49 +010082
83 // Store result
Georgios Pinitas574775c2019-02-18 20:08:02 +000084 VSTORE(VEC_SIZE)
Georgios Pinitas3d13af82019-06-04 13:04:16 +010085 (CONVERT(res, VEC_DATA_TYPE(DATA_TYPE_DST, VEC_SIZE)), 0, (__global DATA_TYPE_DST *)output.ptr);
Michele Di Giorgiocbe39052019-07-02 11:39:10 +010086#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 Giorgio56dd7262017-07-27 09:53:49 +010089}
Georgios Pinitas3d13af82019-06-04 13:04:16 +010090#endif // defined(VEC_SIZE) && defined(DATA_TYPE_SRC) && defined(DATA_TYPE_DST) && defined(SCALE) && defined(OFFSET)
Michalis Spyrou3f632f32019-08-22 16:52:00 +010091
92#if defined(VEC_SIZE) && defined(DATA_TYPE_SRC) && defined(DATA_TYPE_DST)
Georgios Pinitas8217c8e2019-11-11 18:24:22 +000093/** This performs per channel dequantization of 8-bit signed integers to floating point. (NCHW)
Michalis Spyrou3f632f32019-08-22 16:52:00 +010094 *
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 Pinitas8217c8e2019-11-11 18:24:22 +000099 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QSYMM8_PER_CHANNEL
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100100 * @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 Spyrou3f632f32019-08-22 16:52:00 +0100116 */
117__kernel void dequantization_layer_per_channel_nchw(
118 TENSOR3D_DECLARATION(input),
119 TENSOR3D_DECLARATION(output),
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000120 __global float *scale)
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100121{
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 Pinitas8217c8e2019-11-11 18:24:22 +0000137 // Create scale vectors
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100138 const VEC_DATA_TYPE(float, VEC_SIZE)
139 vscale = scale[get_global_id(2)];
140
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100141 // Dequantize
142 VEC_DATA_TYPE(float, VEC_SIZE)
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000143 res = vscale * CONVERT((val), VEC_DATA_TYPE(float, VEC_SIZE));
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100144
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 Pinitas8217c8e2019-11-11 18:24:22 +0000149 *((__global DATA_TYPE_DST *)(output.ptr)) = (DATA_TYPE_DST)((float)((int)(*((__global DATA_TYPE_SRC *)(input.ptr)))) * scale[get_global_id(2)]);
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100150#endif // defined(LAST_ACCESSED_X)
151}
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000152/** This performs per channel dequantization of 8-bit signed integers to floating point. (NHWC)
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100153 *
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 Pinitas8217c8e2019-11-11 18:24:22 +0000158 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QSYMM8_PER_CHANNEL
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100159 * @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 Spyrou3f632f32019-08-22 16:52:00 +0100175 */
176__kernel void dequantization_layer_per_channel_nhwc(
177 TENSOR3D_DECLARATION(input),
178 TENSOR3D_DECLARATION(output),
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000179 __global float *scale)
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100180{
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 Spyrou3f632f32019-08-22 16:52:00 +0100192
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 Pinitas8217c8e2019-11-11 18:24:22 +0000197 // Create scale vectors
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100198 const VEC_DATA_TYPE(float, VEC_SIZE)
199 vscale = VLOAD(VEC_SIZE)(0, &scale[xi]);
200
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100201 // Dequantize
202 VEC_DATA_TYPE(float, VEC_SIZE)
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000203 res = vscale * CONVERT((val), VEC_DATA_TYPE(float, VEC_SIZE));
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100204
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 Pinitas8217c8e2019-11-11 18:24:22 +0000209 *((__global DATA_TYPE_DST *)(output.ptr)) = (DATA_TYPE_DST)((float)((int)(*((__global DATA_TYPE_SRC *)(input.ptr)))) * scale[get_global_id(0)]);
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100210#endif // defined(LAST_ACCESSED_X)
211}
212#endif // defined(VEC_SIZE) && defined(DATA_TYPE_SRC) && defined(DATA_TYPE_DST)