blob: 373406a6dafa0fd76da6ab66dfa2ea461d68a253 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arena99ac60b2018-02-16 15:17:23 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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
Georgios Pinitas00394ae2017-06-22 18:13:55 +010026#define TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
27
SiCong Li1e5c1572017-07-27 17:58:52 +010028#define CONST_ONE 1.f
Georgios Pinitas00394ae2017-06-22 18:13:55 +010029#define ABS_OP(a) fabs((a))
30#define ADD_OP(a, b) ((a) + (b))
31#define SUB_OP(a, b) ((a) - (b))
32#define MUL_OP(a, b) ((a) * (b))
33#define MLA_OP(a, b, c) ((b) * (c) + (a))
34#define DIV_OP(a, b) ((a) / (b))
35#define EXP_OP(a) exp((a))
36#define LOG_OP(a) log((a))
37#define SQRT_OP(a) sqrt((a))
38#define TANH_OP(a) tanh((a))
39
Georgios Pinitas00394ae2017-06-22 18:13:55 +010040// Logistic Activation
41inline TYPE logistic_op(TYPE x)
42{
Moritz Pflanzera36ccf12017-07-24 15:05:12 +010043 return DIV_OP((TYPE)CONST_ONE, ADD_OP((TYPE)CONST_ONE, EXP_OP(-x)));
Georgios Pinitas00394ae2017-06-22 18:13:55 +010044}
45// Hyperbolic Tangent Activation
46inline TYPE tanh_op(TYPE x)
47{
48 return MUL_OP((TYPE)A_VAL, TANH_OP(MUL_OP((TYPE)B_VAL, x)));
49}
50// RELU Tangent Activation
51inline TYPE relu_op(TYPE x)
52{
53 return max(0, x);
54}
55// Bounded RELU Activation
56inline TYPE brelu_op(TYPE x)
57{
58 return min((TYPE)A_VAL, max(0, x));
59}
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +010060// Lower Upper Bounded RELU Activation
61inline TYPE lu_brelu_op(TYPE x)
62{
63 return min(max(x, (TYPE)B_VAL), (TYPE)A_VAL);
64}
Georgios Pinitas579c0492017-07-12 16:12:12 +010065// Leaky RELU Activation
66inline TYPE lrelu_op(TYPE x)
67{
68 return select(MUL_OP((TYPE)A_VAL, x), x, x > (TYPE)0);
69}
Georgios Pinitas00394ae2017-06-22 18:13:55 +010070// Soft RELU Activation
71inline TYPE srelu_op(TYPE x)
72{
Moritz Pflanzera36ccf12017-07-24 15:05:12 +010073 return LOG_OP(ADD_OP((TYPE)CONST_ONE, EXP_OP(x)));
Georgios Pinitas00394ae2017-06-22 18:13:55 +010074}
75// Absolute Activation
76inline TYPE abs_op(TYPE x)
77{
78 return ABS_OP(x);
79}
80// Square Activation
81inline TYPE square_op(TYPE x)
82{
83 return MUL_OP(x, x);
84}
85// Square-root Activation
86inline TYPE sqrt_op(TYPE x)
87{
88 return SQRT_OP(x);
89}
90// Linear Activation
91inline TYPE linear_op(TYPE x)
92{
93 return MLA_OP((TYPE)B_VAL, (TYPE)A_VAL, x);
94}
95
96#define ACTIVATION_OP2(op, x) op##_op(x)
97#define ACTIVATION_OP(op, x) ACTIVATION_OP2(op, x)
98
Giorgio Arena99ac60b2018-02-16 15:17:23 +000099#if defined(ACT)
100
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101/** This performs an activation function floating point inputs.
102 *
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100103 * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
104 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100106 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
107 * @note Activation function should be given as a preprocessor argument using -DACT=name. e.g. -DACT=TANH
108 * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100110 * @param[in] input_ptr Pointer to the source image. Supported data types: F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 * @param[in] input_stride_x Stride of the source image in X dimension (in bytes)
112 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
113 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
114 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
115 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
116 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
117 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100118 * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes)
120 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
121 * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes)
122 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
123 * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes)
124 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
125 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
126 */
127__kernel void activation_layer(
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100128 TENSOR3D_DECLARATION(input)
Anthony Barbierac69aa12017-07-03 17:39:37 +0100129#ifndef IN_PLACE
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100130 ,
131 TENSOR3D_DECLARATION(output)
Anthony Barbierac69aa12017-07-03 17:39:37 +0100132#endif /* not IN_PLACE */
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100133)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134{
135 // Get pixels pointer
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100136 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100137#ifdef IN_PLACE
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100138 Tensor3D output = input;
Anthony Barbierac69aa12017-07-03 17:39:37 +0100139#else /* IN_PLACE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
Anthony Barbierac69aa12017-07-03 17:39:37 +0100141#endif /* IN_PLACE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142
143 // Load data
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100144 TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145
146 // Perform activation
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100147 data = ACTIVATION_OP(ACT, data);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148
149 // Store result
Georgios Pinitas00394ae2017-06-22 18:13:55 +0100150 VSTORE(VEC_SIZE)
151 (data, 0, (__global DATA_TYPE *)output.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152}
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000153
154#endif /* defined(ACT) */