blob: 174b75398d1db1b49d476a8d6a1c88db6cf21a7a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arenad304adb2020-10-02 10:20:11 +01002 * Copyright (c) 2016-2020 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 */
Usama Arife2428a02019-05-09 11:03:17 +010024#if defined(ACT) && defined(DATA_TYPE) && defined(VEC_SIZE)
25
Georgios Pinitas00394ae2017-06-22 18:13:55 +010026#define TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
27
Usama Arife2428a02019-05-09 11:03:17 +010028#include "activation_float_helpers.h"
Giorgio Arena99ac60b2018-02-16 15:17:23 +000029
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030/** This performs an activation function floating point inputs.
31 *
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010032 * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
33 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034 * @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 +010035 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
Giorgio Arena53048842020-10-07 16:03:43 +010036 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Georgios Pinitas00394ae2017-06-22 18:13:55 +010037 * @note Activation function should be given as a preprocessor argument using -DACT=name. e.g. -DACT=TANH
38 * @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 +010039 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010040 * @param[in] input_ptr Pointer to the source image. Supported data types: F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041 * @param[in] input_stride_x Stride of the source image in X dimension (in bytes)
42 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
43 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
44 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
45 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
46 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
47 * @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 +010048 * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes)
50 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
51 * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes)
52 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
53 * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes)
54 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
55 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
56 */
57__kernel void activation_layer(
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010058 TENSOR3D_DECLARATION(input)
Anthony Barbierac69aa12017-07-03 17:39:37 +010059#ifndef IN_PLACE
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010060 ,
61 TENSOR3D_DECLARATION(output)
Anthony Barbierac69aa12017-07-03 17:39:37 +010062#endif /* not IN_PLACE */
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +010063)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064{
Giorgio Arenad304adb2020-10-02 10:20:11 +010065 uint x_offs = max((int)(get_global_id(0) * VEC_SIZE * sizeof(DATA_TYPE) - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE * sizeof(DATA_TYPE)), 0);
66
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 // Get pixels pointer
Giorgio Arenad304adb2020-10-02 10:20:11 +010068 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs + get_global_id(1) * input_stride_y + get_global_id(2) * input_stride_z;
Anthony Barbierac69aa12017-07-03 17:39:37 +010069#ifdef IN_PLACE
Giorgio Arenad304adb2020-10-02 10:20:11 +010070 __global uchar *output_addr = input_addr;
Anthony Barbierac69aa12017-07-03 17:39:37 +010071#else /* IN_PLACE */
Giorgio Arenad304adb2020-10-02 10:20:11 +010072 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs + get_global_id(1) * output_stride_y + get_global_id(2) * output_stride_z;
Anthony Barbierac69aa12017-07-03 17:39:37 +010073#endif /* IN_PLACE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074
75 // Load data
Giorgio Arenad304adb2020-10-02 10:20:11 +010076 TYPE data0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input_addr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077
78 // Perform activation
Giorgio Arenad056e572020-10-12 11:53:51 +010079 data0 = ACTIVATION(ACT, DATA_TYPE, VEC_SIZE, data0, A_VAL, B_VAL);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080
81 // Store result
Giorgio Arenad304adb2020-10-02 10:20:11 +010082 STORE_VECTOR_SELECT(data, DATA_TYPE, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083}
Giorgio Arena99ac60b2018-02-16 15:17:23 +000084
Usama Arife2428a02019-05-09 11:03:17 +010085#endif /* defined(ACT) */