blob: 21daee82305325bd7028d20af38850a6f70252dd [file] [log] [blame]
Dmitry Savenkod7295b72017-11-20 22:00:08 +07001/*
Giorgio Arena944d3f72018-01-16 15:38:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Dmitry Savenkod7295b72017-11-20 22:00:08 +07003 *
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
25#include "helpers_asymm.h"
26
Giorgio Arena287b5702018-02-16 11:01:04 +000027#if defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && defined(OUTPUT_OFFSET) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT)
28
Giorgio Arena99ac60b2018-02-16 15:17:23 +000029#if defined(FUSED_ACTIVATION)
30#define DATA_TYPE uchar
31#define VEC_SIZE 8
32#include "activation_layer_qa8.cl"
33#define ACTIVATION_FUNC(x) PERFORM_ACTIVATION_QA8(FUSED_ACTIVATION, x)
34#else /* defined(FUSED_ACTIVATION) */
35#define ACTIVATION_FUNC(x) (x)
36#endif /* defined(FUSED_ACTIVATION) */
37
Giorgio Arena287b5702018-02-16 11:01:04 +000038#if CONV_STRIDE_X > 3
39#error "Stride X not supported"
40#endif /* CONV_STRIDE_X > 3 */
Dmitry Savenkod7295b72017-11-20 22:00:08 +070041
42#if CONV_STRIDE_X == 1
Giorgio Arena287b5702018-02-16 11:01:04 +000043#define GET_VALUES(first_value, left, middle, right) \
44 ({ \
45 int8 temp0 = CONVERT(vload8(0, first_value), int8); \
46 int2 temp1 = CONVERT(vload2(0, (first_value + 8 * sizeof(uchar))), int2); \
47 \
48 left = CONVERT(temp0.s01234567, int8); \
49 middle = CONVERT((int8)(temp0.s1234, temp0.s567, temp1.s0), int8); \
50 right = CONVERT((int8)(temp0.s2345, temp0.s67, temp1.s01), int8); \
51 })
Dmitry Savenkod7295b72017-11-20 22:00:08 +070052#elif CONV_STRIDE_X == 2
Giorgio Arena287b5702018-02-16 11:01:04 +000053#define GET_VALUES(first_value, left, middle, right) \
54 ({ \
55 int16 temp0 = CONVERT(vload16(0, first_value), int16); \
56 int temp1 = CONVERT(*(first_value + 16 * sizeof(uchar)), int); \
57 \
58 left = CONVERT(temp0.s02468ace, int8); \
59 middle = CONVERT(temp0.s13579bdf, int8); \
60 right = CONVERT((int8)(temp0.s2468, temp0.sace, temp1), int8); \
61 })
Dmitry Savenkod7295b72017-11-20 22:00:08 +070062#else /* CONV_STRIDE_X */
Giorgio Arena287b5702018-02-16 11:01:04 +000063#define GET_VALUES(first_value, left, middle, right) \
64 ({ \
65 int16 temp0 = CONVERT(vload16(0, first_value), int16); \
66 int8 temp1 = CONVERT(vload8(0, (first_value + 16 * sizeof(uchar))), int8); \
67 \
68 left = CONVERT((int8)(temp0.s0369, temp0.scf, temp1.s25), int8); \
69 middle = CONVERT((int8)(temp0.s147a, temp0.sd, temp1.s036), int8); \
70 right = CONVERT((int8)(temp0.s258b, temp0.se, temp1.s147), int8); \
71 })
Dmitry Savenkod7295b72017-11-20 22:00:08 +070072#endif /* CONV_STRIDE_X */
73
Giorgio Arena287b5702018-02-16 11:01:04 +000074/** This function computes the horizontal integral of the image and adds offsets.
Anthony Barbierf202e502017-11-23 18:02:04 +000075 *
76 * @param[in] src_ptr Pointer to the source image. Supported data types: QASYMM8
77 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes)
78 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
79 * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes)
80 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
81 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image
82 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
83 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
84 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: QASYMM8
85 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
86 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
87 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
88 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
89 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
90 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
91 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
92 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8
93 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
94 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
95 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
96 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
97 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
98 * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes)
99 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
100 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: QASYMM8
101 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
102 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
103 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector
Anthony Barbierf202e502017-11-23 18:02:04 +0000104 */
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700105
106__kernel void depthwise_convolution_3x3_quantized(
107 TENSOR3D_DECLARATION(src),
108 TENSOR3D_DECLARATION(dst),
Georgios Pinitas5b2191e2018-02-22 12:56:51 +0000109 TENSOR3D_DECLARATION(weights)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700110#if defined(HAS_BIAS)
Georgios Pinitas5b2191e2018-02-22 12:56:51 +0000111 ,
Giorgio Arena287b5702018-02-16 11:01:04 +0000112 VECTOR_DECLARATION(biases)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700113#endif //defined(HAS_BIAS)
Giorgio Arena287b5702018-02-16 11:01:04 +0000114)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700115{
116 Image src = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(src);
117 Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst);
118 Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT(weights);
119#if defined(HAS_BIAS)
120 Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700121
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700122 int bias_value = *((__global int *)(vector_offset(&biases, get_global_id(2))));
123#endif //defined(HAS_BIAS)
124
Giorgio Arena287b5702018-02-16 11:01:04 +0000125 uchar3 w0 = vload3(0, weights.ptr + 0 * weights_stride_y);
126 uchar3 w1 = vload3(0, weights.ptr + 1 * weights_stride_y);
127 uchar3 w2 = vload3(0, weights.ptr + 2 * weights_stride_y);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700128
Giorgio Arena287b5702018-02-16 11:01:04 +0000129 int8 values0 = 0;
130 int8 sum0 = 0;
131#if CONV_STRIDE_Y == 1
132 int8 values1 = 0;
133 int8 sum1 = 0;
134#endif /* CONV_STRIDE_Y */
135
136 // Row0
137 int8 left, middle, right;
138 GET_VALUES(src.ptr + 0 * src_stride_y, left, middle, right);
139 values0 += left * (int8)(w0.s0);
140 values0 += middle * (int8)(w0.s1);
141 values0 += right * (int8)(w0.s2);
142
143#if WEIGHTS_OFFSET != 0
144 sum0 += left + middle + right;
145#endif /* WEIGHTS_OFFSET != 0 */
146
147 // Row1
148 GET_VALUES(src.ptr + 1 * src_stride_y, left, middle, right);
149 values0 += left * (int8)(w1.s0);
150 values0 += middle * (int8)(w1.s1);
151 values0 += right * (int8)(w1.s2);
152#if CONV_STRIDE_Y == 1
153 values1 += left * (int8)(w0.s0);
154 values1 += middle * (int8)(w0.s1);
155 values1 += right * (int8)(w0.s2);
156#endif /* CONV_STRIDE_Y == 1 */
157
158#if WEIGHTS_OFFSET != 0
159 int8 tmp = left + middle + right;
160 sum0 += tmp;
161#if CONV_STRIDE_Y == 1
162 sum1 += tmp;
163#endif /* CONV_STRIDE_Y == 1 */
164#endif /* WEIGHTS_OFFSET != 0 */
165
166 // Row2
167 GET_VALUES(src.ptr + 2 * src_stride_y, left, middle, right);
168 values0 += left * (int8)(w2.s0);
169 values0 += middle * (int8)(w2.s1);
170 values0 += right * (int8)(w2.s2);
171#if CONV_STRIDE_Y == 1
172 values1 += left * (int8)(w1.s0);
173 values1 += middle * (int8)(w1.s1);
174 values1 += right * (int8)(w1.s2);
175#endif /* CONV_STRIDE_Y == 1 */
176
177#if WEIGHTS_OFFSET != 0
178 tmp = left + middle + right;
179 sum0 += tmp;
180#if CONV_STRIDE_Y == 1
181 sum1 += tmp;
182#endif /* CONV_STRIDE_Y == 1 */
183#endif /* WEIGHTS_OFFSET != 0 */
184
185#if CONV_STRIDE_Y == 1
186 // Row3
187 GET_VALUES(src.ptr + 3 * src_stride_y, left, middle, right);
188 values1 += left * (int8)(w2.s0);
189 values1 += middle * (int8)(w2.s1);
190 values1 += right * (int8)(w2.s2);
191
192#if WEIGHTS_OFFSET != 0
193 sum1 += left + middle + right;
194#endif /* WEIGHTS_OFFSET != 0 */
195#endif /* CONV_STRIDE_Y == 1 */
196
197#if defined(HAS_BIAS)
198 values0 += (int8)(bias_value);
199#if CONV_STRIDE_Y == 1
200 values1 += (int8)(bias_value);
201#endif /* CONV_STRIDE_Y == 1 */
202#endif //defined(HAS_BIAS)
203
204#if WEIGHTS_OFFSET != 0
205 values0 += sum0 * (int8)(WEIGHTS_OFFSET);
206#if CONV_STRIDE_Y == 1
207 values1 += sum1 * (int8)(WEIGHTS_OFFSET);
208#endif /* CONV_STRIDE_Y == 1 */
209#endif /* WEIGHTS_OFFSET != 0 */
210
211#if INPUT_OFFSET != 0
212 ushort sum_weights = 0;
213 ushort3 tmp_we = convert_ushort3(w0) + convert_ushort3(w1) + convert_ushort3(w2);
214 sum_weights += tmp_we.s0 + tmp_we.s1 + tmp_we.s2;
215 values0 += sum_weights * (int8)(INPUT_OFFSET);
216#if CONV_STRIDE_Y == 1
217 values1 += sum_weights * (int8)(INPUT_OFFSET);
218#endif /* CONV_STRIDE_Y == 1 */
219#endif /* INPUT_OFFSET != 0 */
220
221#if K_OFFSET != 0
222 values0 += (int8)(K_OFFSET);
223#if CONV_STRIDE_Y == 1
224 values1 += (int8)(K_OFFSET);
225#endif /* CONV_STRIDE_Y == 1 */
226#endif /* K_OFFSET != 0 */
227
228 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
229 values0 += (int8)OUTPUT_OFFSET;
230 uchar8 res0 = convert_uchar8_sat(values0);
231 res0 = max(res0, (uchar8)0);
232 res0 = min(res0, (uchar8)255);
233
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000234 vstore8(ACTIVATION_FUNC(res0), 0, dst.ptr);
Giorgio Arena287b5702018-02-16 11:01:04 +0000235#if CONV_STRIDE_Y == 1
236
237 values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
238 values1 += (int8)OUTPUT_OFFSET;
239 uchar8 res1 = convert_uchar8_sat(values1);
240 res1 = max(res1, (uchar8)0);
241 res1 = min(res1, (uchar8)255);
242
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000243 vstore8(ACTIVATION_FUNC(res1), 0, dst.ptr + dst_stride_y);
Giorgio Arena287b5702018-02-16 11:01:04 +0000244#endif /* CONV_STRIDE_Y == 1 */
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700245}
Giorgio Arena287b5702018-02-16 11:01:04 +0000246
247#endif /* defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && defined(OUTPUT_OFFSET) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT) */