blob: cfb61376ca6c9c00d1c43daab890bedfb6cd6109 [file] [log] [blame]
Michel Iwaniec00633802017-10-12 14:14:15 +01001/*
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +00002 * Copyright (c) 2016-2019 ARM Limited.
Michel Iwaniec00633802017-10-12 14:14:15 +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
26#define TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Michele Di Giorgiod304e802018-07-06 10:17:33 +010027#define VEC_FLOAT VEC_DATA_TYPE(float, VEC_SIZE)
Michel Iwaniec00633802017-10-12 14:14:15 +010028
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +000029// RELU Activation
30inline TYPE relu_op(TYPE x)
31{
32 return max((TYPE)CONST_0, x);
33}
Michel Iwaniec00633802017-10-12 14:14:15 +010034// Bounded RELU Activation
35inline TYPE brelu_op(TYPE x)
36{
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +000037 return min((TYPE)A_VAL, max(CONST_0, x));
Michel Iwaniec00633802017-10-12 14:14:15 +010038}
39// Lower Upper Bounded RELU Activation
40inline TYPE lu_brelu_op(TYPE x)
41{
42 return min(max(x, (TYPE)B_VAL), (TYPE)A_VAL);
43}
44
45#define ACTIVATION_OP2(op, x) op##_op(x)
46#define ACTIVATION_OP(op, x) ACTIVATION_OP2(op, x)
47
Giorgio Arena99ac60b2018-02-16 15:17:23 +000048#if defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL)
49#define PERFORM_ACTIVATION_QA8(act, data) \
50 ({ \
51 data = ACTIVATION_OP(act, data); \
52 \
53 VEC_DATA_TYPE(float, VEC_SIZE) \
54 fdata = CONVERT(data, VEC_DATA_TYPE(float, VEC_SIZE)); \
55 \
56 fdata = round((fdata - (float)O1_VAL) * ((float)S1_VAL / (float)S2_VAL) + (float)O2_VAL); \
57 data = CONVERT_SAT(fdata, VEC_DATA_TYPE(uchar, VEC_SIZE)); \
58 })
59#else /* defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL) */
60#define PERFORM_ACTIVATION_QA8(act, data) \
61 ({ \
62 data = ACTIVATION_OP(act, data); \
63 })
64#endif /* defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL) */
65
66#if defined(ACT)
67
Michel Iwaniec00633802017-10-12 14:14:15 +010068/** This performs an activation function on QASYMM8 inputs.
69 *
70 * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
71 *
72 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
73 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
74 * @note Activation function should be given as a preprocessor argument using -DACT=name. e.g. -DACT=TANH
75 * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively.
76 * @note Quantization scales of the input/output tensors are passed in with -DS1_VAL= and -DS2_VAL= respectively.
77 * @note Quantization offsets of the input/output tensors are passed in with -DO1_VAL= and -DO2_VAL= respectively.
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +000078 * @note Quantized value of constant zero should be given as a preprocessor argument using -DCONST_0=value. e.g. -DCONST_0=128.
Michel Iwaniec00633802017-10-12 14:14:15 +010079 *
80 * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8
81 * @param[in] input_stride_x Stride of the source image in X dimension (in bytes)
82 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
83 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
84 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
85 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
86 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
87 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +000088 * @param[out] output_ptr (Optional) Pointer to the destination image. Supported data types: same as @p input_ptr
89 * @param[in] output_stride_x (Optional) Stride of the destination image in X dimension (in bytes)
90 * @param[in] output_step_x (Optional) output_stride_x * number of elements along X processed per workitem(in bytes)
91 * @param[in] output_stride_y (Optional) Stride of the destination image in Y dimension (in bytes)
92 * @param[in] output_step_y (Optional) output_stride_y * number of elements along Y processed per workitem(in bytes)
93 * @param[in] output_stride_z (Optional) Stride of the source tensor in Z dimension (in bytes)
94 * @param[in] output_step_z (Optional) output_stride_z * number of elements along Z processed per workitem(in bytes)
95 * @param[in] output_offset_first_element_in_bytes (Optional) The offset of the first element in the destination image
Michel Iwaniec00633802017-10-12 14:14:15 +010096 */
97__kernel void activation_layer_qa8(
98 TENSOR3D_DECLARATION(input)
99#ifndef IN_PLACE
100 ,
101 TENSOR3D_DECLARATION(output)
102#endif /* not IN_PLACE */
103)
104{
105 // Get pixels pointer
106 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
107#ifdef IN_PLACE
108 Tensor3D output = input;
109#else /* IN_PLACE */
110 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
111#endif /* IN_PLACE */
112
113 // Load data
114 TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr);
115
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000116 data = PERFORM_ACTIVATION_QA8(ACT, data);
Michel Iwaniec00633802017-10-12 14:14:15 +0100117
118 // Store result
119 VSTORE(VEC_SIZE)
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000120 (data, 0, (__global DATA_TYPE *)output.ptr);
Michel Iwaniec00633802017-10-12 14:14:15 +0100121}
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000122
Michele Di Giorgiod304e802018-07-06 10:17:33 +0100123#endif /* defined(ACT) */
Michele Di Giorgioe5bf4c52019-02-14 17:47:33 +0000124
125#if defined(O2_VAL) && defined(S2_VAL)
126#define OFFSET_OUT O2_VAL
127#define SCALE_OUT S2_VAL
128#else // defined(O2_VAL) && defined(S2_VAL)
129#define OFFSET_OUT O1_VAL
130#define SCALE_OUT S1_VAL
131#endif // defined(O2_VAL) && defined(S2_VAL)
132
133/** This performs a Logistic activation function on QASYMM8 inputs.
134 *
135 * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
136 *
137 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
138 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
139 * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively.
140 * @note Quantization scales of the input/output tensors are passed in with -DS1_VAL= and -DS2_VAL= respectively.
141 * @note Quantization offsets of the input/output tensors are passed in with -DO1_VAL= and -DO2_VAL= respectively.
142 * @note Quantized value of constant zero should be given as a preprocessor argument using -DCONST_0=value. e.g. -DCONST_0=128.
143 *
144 * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8
145 * @param[in] input_stride_x Stride of the source image in X dimension (in bytes)
146 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
147 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
148 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
149 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
150 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
151 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image
152 * @param[out] output_ptr (Optional) Pointer to the destination image. Supported data types: same as @p input_ptr
153 * @param[in] output_stride_x (Optional) Stride of the destination image in X dimension (in bytes)
154 * @param[in] output_step_x (Optional) output_stride_x * number of elements along X processed per workitem(in bytes)
155 * @param[in] output_stride_y (Optional) Stride of the destination image in Y dimension (in bytes)
156 * @param[in] output_step_y (Optional) output_stride_y * number of elements along Y processed per workitem(in bytes)
157 * @param[in] output_stride_z (Optional) Stride of the source tensor in Z dimension (in bytes)
158 * @param[in] output_step_z (Optional) output_stride_z * number of elements along Z processed per workitem(in bytes)
159 * @param[in] output_offset_first_element_in_bytes (Optional) The offset of the first element in the destination image
160 */
161__kernel void activation_layer_logistic_qa8(
162 TENSOR3D_DECLARATION(input)
163#ifndef IN_PLACE
164 ,
165 TENSOR3D_DECLARATION(output)
166#endif /* not IN_PLACE */
167)
168{
169 // Get pixels pointer
170 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
171#ifdef IN_PLACE
172 Tensor3D output = input;
173#else /* IN_PLACE */
174 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
175#endif /* IN_PLACE */
176
177 // Load data
178 TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr);
179
180 VEC_FLOAT data_flt = CONVERT(data, VEC_FLOAT);
181 data_flt = round(data_flt - (float)O1_VAL) * ((float)S1_VAL);
182 data_flt = 1.f / (1.f + exp(-data_flt));
183
184 data = CONVERT_SAT(round(data_flt / ((float)SCALE_OUT)) + (float)OFFSET_OUT, TYPE);
185
186 // Store result
187 VSTORE(VEC_SIZE)
188 (data, 0, (__global DATA_TYPE *)output.ptr);
189}