blob: 8f6a80761330088c0fe0f296f14705ed80f0d2da [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)
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 Giorgiod304e802018-07-06 10:17:33 +010029// Logistic Activation
30inline TYPE logistic_op(TYPE x)
31{
32 VEC_FLOAT x_flt = CONVERT(x, VEC_FLOAT);
33 x_flt = round(x_flt - (float)O1_VAL) * ((float)S1_VAL);
34 x_flt = 1.f / (1.f + exp(-x_flt));
35
36 const TYPE x_u8 = CONVERT_SAT(round(x_flt / ((float)S1_VAL)) + (float)O1_VAL, TYPE);
37 return x_u8;
38}
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +000039// RELU Activation
40inline TYPE relu_op(TYPE x)
41{
42 return max((TYPE)CONST_0, x);
43}
Michel Iwaniec00633802017-10-12 14:14:15 +010044// Bounded RELU Activation
45inline TYPE brelu_op(TYPE x)
46{
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +000047 return min((TYPE)A_VAL, max(CONST_0, x));
Michel Iwaniec00633802017-10-12 14:14:15 +010048}
49// Lower Upper Bounded RELU Activation
50inline TYPE lu_brelu_op(TYPE x)
51{
52 return min(max(x, (TYPE)B_VAL), (TYPE)A_VAL);
53}
54
55#define ACTIVATION_OP2(op, x) op##_op(x)
56#define ACTIVATION_OP(op, x) ACTIVATION_OP2(op, x)
57
Giorgio Arena99ac60b2018-02-16 15:17:23 +000058#if 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 VEC_DATA_TYPE(float, VEC_SIZE) \
64 fdata = CONVERT(data, VEC_DATA_TYPE(float, VEC_SIZE)); \
65 \
66 fdata = round((fdata - (float)O1_VAL) * ((float)S1_VAL / (float)S2_VAL) + (float)O2_VAL); \
67 data = CONVERT_SAT(fdata, VEC_DATA_TYPE(uchar, VEC_SIZE)); \
68 })
69#else /* defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL) */
70#define PERFORM_ACTIVATION_QA8(act, data) \
71 ({ \
72 data = ACTIVATION_OP(act, data); \
73 })
74#endif /* defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL) */
75
76#if defined(ACT)
77
Michel Iwaniec00633802017-10-12 14:14:15 +010078/** This performs an activation function on QASYMM8 inputs.
79 *
80 * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
81 *
82 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
83 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
84 * @note Activation function should be given as a preprocessor argument using -DACT=name. e.g. -DACT=TANH
85 * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively.
86 * @note Quantization scales of the input/output tensors are passed in with -DS1_VAL= and -DS2_VAL= respectively.
87 * @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 +000088 * @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 +010089 *
90 * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8
91 * @param[in] input_stride_x Stride of the source image in X dimension (in bytes)
92 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
93 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
94 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
95 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
96 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
97 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image
98 * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr
99 * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes)
100 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
101 * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes)
102 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
103 * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes)
104 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
105 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
106 */
107__kernel void activation_layer_qa8(
108 TENSOR3D_DECLARATION(input)
109#ifndef IN_PLACE
110 ,
111 TENSOR3D_DECLARATION(output)
112#endif /* not IN_PLACE */
113)
114{
115 // Get pixels pointer
116 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
117#ifdef IN_PLACE
118 Tensor3D output = input;
119#else /* IN_PLACE */
120 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
121#endif /* IN_PLACE */
122
123 // Load data
124 TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr);
125
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000126 data = PERFORM_ACTIVATION_QA8(ACT, data);
Michel Iwaniec00633802017-10-12 14:14:15 +0100127
128 // Store result
129 VSTORE(VEC_SIZE)
Giorgio Arenaa0d11832018-01-17 16:13:46 +0000130 (data, 0, (__global DATA_TYPE *)output.ptr);
Michel Iwaniec00633802017-10-12 14:14:15 +0100131}
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000132
Michele Di Giorgiod304e802018-07-06 10:17:33 +0100133#endif /* defined(ACT) */