blob: 721c43c0170432f1f6d1856bb07fff396602cf28 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
3 *
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/** This performs an activation function floating point inputs.
27 *
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010028 * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
29 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
31 * @note Activation function should be given as a preprocessor argument using -DNAME. e.g. -DTANH
32 * @note Distinction between floating point and integer is done using -DTYPE_FP and -DTYPE_INT preprocessor argument
33 * @note A, B variables required by some activation functions are set using -DA= and -DB= respectively.
34 *
35 * @param[in] input_ptr Pointer to the source image. Supported data types: F16, F32
36 * @param[in] input_stride_x Stride of the source image in X dimension (in bytes)
37 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
38 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
39 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
40 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
41 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
42 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image
43 * @param[out] output_ptr Pointer to the destination image. Supported data types: F16, F32
44 * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes)
45 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
46 * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes)
47 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
48 * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes)
49 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
50 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
51 */
52__kernel void activation_layer(
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010053 TENSOR3D_DECLARATION(input)
Anthony Barbierac69aa12017-07-03 17:39:37 +010054#ifndef IN_PLACE
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010055 ,
56 TENSOR3D_DECLARATION(output)
Anthony Barbierac69aa12017-07-03 17:39:37 +010057#endif /* not IN_PLACE */
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010058)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059{
60 // Get pixels pointer
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010061 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
Anthony Barbierac69aa12017-07-03 17:39:37 +010062#ifdef IN_PLACE
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010063 Tensor3D output = input;
Anthony Barbierac69aa12017-07-03 17:39:37 +010064#else /* IN_PLACE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
Anthony Barbierac69aa12017-07-03 17:39:37 +010066#endif /* IN_PLACE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067
68 // Load data
69 VEC_DATA_TYPE(DATA_TYPE, 16)
70 data = vload16(0, (__global DATA_TYPE *)input.ptr);
71
72 // Perform activation
Anthony Barbierac69aa12017-07-03 17:39:37 +010073#ifdef LOGISTIC
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 data = 1 / (1 + exp(-data));
Anthony Barbierac69aa12017-07-03 17:39:37 +010075#elif defined(TANH)
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010076 data = (VEC_DATA_TYPE(DATA_TYPE, 16))A * tanh((VEC_DATA_TYPE(DATA_TYPE, 16))B * data);
Anthony Barbierac69aa12017-07-03 17:39:37 +010077#elif defined(RELU)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 data = max(0, data);
Anthony Barbierac69aa12017-07-03 17:39:37 +010079#elif defined(BRELU)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 data = min((VEC_DATA_TYPE(DATA_TYPE, 16))A, max(0, data));
Anthony Barbierac69aa12017-07-03 17:39:37 +010081#elif defined(SRELU)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 data = log(1 + exp(data));
Anthony Barbierac69aa12017-07-03 17:39:37 +010083#elif defined(ABS)
84#ifdef TYPE_INT
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 data = abs(data);
Anthony Barbierac69aa12017-07-03 17:39:37 +010086#else /* TYPE_INT */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 data = fabs(data);
Anthony Barbierac69aa12017-07-03 17:39:37 +010088#endif /* TYPE_INT */
89#elif defined(SQUARE)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 data = data * data;
Anthony Barbierac69aa12017-07-03 17:39:37 +010091#elif defined(SQRT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092 data = sqrt(data);
Anthony Barbierac69aa12017-07-03 17:39:37 +010093#elif defined(LINEAR)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 data = (VEC_DATA_TYPE(DATA_TYPE, 16))A * data + (VEC_DATA_TYPE(DATA_TYPE, 16))B;
Anthony Barbierac69aa12017-07-03 17:39:37 +010095#endif /* switch TANH, RELU, BRELU, SRELU, ABS, SQUARE, SQRT, LINEAR */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
97 // Store result
98 vstore16(data, 0, (__global DATA_TYPE *)output.ptr);
99}