2 * Copyright (c) 2016-2018 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
26 #define TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
29 inline TYPE relu_op(TYPE x)
31 return max((TYPE)CONST_0, x);
33 // Bounded RELU Activation
34 inline TYPE brelu_op(TYPE x)
36 return min((TYPE)A_VAL, max(CONST_0, x));
38 // Lower Upper Bounded RELU Activation
39 inline TYPE lu_brelu_op(TYPE x)
41 return min(max(x, (TYPE)B_VAL), (TYPE)A_VAL);
44 #define ACTIVATION_OP2(op, x) op##_op(x)
45 #define ACTIVATION_OP(op, x) ACTIVATION_OP2(op, x)
47 /** This performs an activation function on QASYMM8 inputs.
49 * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
51 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
52 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
53 * @note Activation function should be given as a preprocessor argument using -DACT=name. e.g. -DACT=TANH
54 * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively.
55 * @note Quantization scales of the input/output tensors are passed in with -DS1_VAL= and -DS2_VAL= respectively.
56 * @note Quantization offsets of the input/output tensors are passed in with -DO1_VAL= and -DO2_VAL= respectively.
57 * @note Quantized value of constant zero should be given as a preprocessor argument using -DCONST_0=value. e.g. -DCONST_0=128.
59 * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8
60 * @param[in] input_stride_x Stride of the source image in X dimension (in bytes)
61 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
62 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
63 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
64 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
65 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
66 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image
67 * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr
68 * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes)
69 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
70 * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes)
71 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
72 * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes)
73 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
74 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
76 __kernel void activation_layer_qa8(
77 TENSOR3D_DECLARATION(input)
80 TENSOR3D_DECLARATION(output)
81 #endif /* not IN_PLACE */
85 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
87 Tensor3D output = input;
89 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
93 TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr);
96 data = ACTIVATION_OP(ACT, data);
98 #if defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL)
99 // requantize to output space
100 VEC_DATA_TYPE(float, VEC_SIZE)
101 fdata = CONVERT(data, VEC_DATA_TYPE(float, VEC_SIZE));
103 fdata = round((fdata - (float)O1_VAL) * ((float)S1_VAL / (float)S2_VAL) + (float)O2_VAL);
104 data = CONVERT_SAT(fdata, VEC_DATA_TYPE(uchar, VEC_SIZE));
105 #endif // defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL)
109 (data, 0, (__global DATA_TYPE *)output.ptr);