blob: 66e54ed6adb3312f5fc61f5ac90514bb996f7c45 [file] [log] [blame]
Michel Iwaniec00633802017-10-12 14:14:15 +01001/*
Giorgio Arenaa0d11832018-01-17 16:13:46 +00002 * Copyright (c) 2016-2018 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)
27
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +000028// RELU Activation
29inline TYPE relu_op(TYPE x)
30{
31 return max((TYPE)CONST_0, x);
32}
Michel Iwaniec00633802017-10-12 14:14:15 +010033// Bounded RELU Activation
34inline TYPE brelu_op(TYPE x)
35{
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +000036 return min((TYPE)A_VAL, max(CONST_0, x));
Michel Iwaniec00633802017-10-12 14:14:15 +010037}
38// Lower Upper Bounded RELU Activation
39inline TYPE lu_brelu_op(TYPE x)
40{
41 return min(max(x, (TYPE)B_VAL), (TYPE)A_VAL);
42}
43
44#define ACTIVATION_OP2(op, x) op##_op(x)
45#define ACTIVATION_OP(op, x) ACTIVATION_OP2(op, x)
46
Giorgio Arena99ac60b2018-02-16 15:17:23 +000047#if defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL)
48#define PERFORM_ACTIVATION_QA8(act, data) \
49 ({ \
50 data = ACTIVATION_OP(act, data); \
51 \
52 VEC_DATA_TYPE(float, VEC_SIZE) \
53 fdata = CONVERT(data, VEC_DATA_TYPE(float, VEC_SIZE)); \
54 \
55 fdata = round((fdata - (float)O1_VAL) * ((float)S1_VAL / (float)S2_VAL) + (float)O2_VAL); \
56 data = CONVERT_SAT(fdata, VEC_DATA_TYPE(uchar, VEC_SIZE)); \
57 })
58#else /* defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL) */
59#define PERFORM_ACTIVATION_QA8(act, data) \
60 ({ \
61 data = ACTIVATION_OP(act, data); \
62 })
63#endif /* defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL) */
64
65#if defined(ACT)
66
Michel Iwaniec00633802017-10-12 14:14:15 +010067/** This performs an activation function on QASYMM8 inputs.
68 *
69 * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
70 *
71 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
72 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
73 * @note Activation function should be given as a preprocessor argument using -DACT=name. e.g. -DACT=TANH
74 * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively.
75 * @note Quantization scales of the input/output tensors are passed in with -DS1_VAL= and -DS2_VAL= respectively.
76 * @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 +000077 * @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 +010078 *
79 * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8
80 * @param[in] input_stride_x Stride of the source image in X dimension (in bytes)
81 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
82 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
83 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
84 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
85 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
86 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image
87 * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr
88 * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes)
89 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
90 * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes)
91 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
92 * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes)
93 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
94 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
95 */
96__kernel void activation_layer_qa8(
97 TENSOR3D_DECLARATION(input)
98#ifndef IN_PLACE
99 ,
100 TENSOR3D_DECLARATION(output)
101#endif /* not IN_PLACE */
102)
103{
104 // Get pixels pointer
105 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
106#ifdef IN_PLACE
107 Tensor3D output = input;
108#else /* IN_PLACE */
109 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
110#endif /* IN_PLACE */
111
112 // Load data
113 TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr);
114
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000115 data = PERFORM_ACTIVATION_QA8(ACT, data);
Michel Iwaniec00633802017-10-12 14:14:15 +0100116
117 // Store result
118 VSTORE(VEC_SIZE)
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000119 (data, 0, (__global DATA_TYPE *)output.ptr);
Michel Iwaniec00633802017-10-12 14:14:15 +0100120}
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000121
122#endif /* defined(ACT) */