diff options
author | Georgios Pinitas <georgios.pinitas@arm.com> | 2017-06-22 18:13:55 +0100 |
---|---|---|
committer | Anthony Barbier <anthony.barbier@arm.com> | 2018-09-17 14:16:42 +0100 |
commit | 00394ae1015c1eaa73f4d98fad31b7771063cd3a (patch) | |
tree | 673634921bc8e9d5781787f2a46fdbc9aa1b0dd8 /src/core/CL/cl_kernels/activation_layer.cl | |
parent | b797fa235f714440ffa7a2ad4eef7ae14ee45da4 (diff) | |
download | ComputeLibrary-00394ae1015c1eaa73f4d98fad31b7771063cd3a.tar.gz |
COMPMID-406: Port CLActivationLayer to use QS8/QS16.
Change-Id: Ia4114984c38e1d2027ad97335b3c6c11f5754e23
Reviewed-on: http://mpd-gerrit.cambridge.arm.com/78727
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
Tested-by: Kaizen <jeremy.johnson+kaizengerrit@arm.com>
Diffstat (limited to 'src/core/CL/cl_kernels/activation_layer.cl')
-rw-r--r-- | src/core/CL/cl_kernels/activation_layer.cl | 123 |
1 files changed, 92 insertions, 31 deletions
diff --git a/src/core/CL/cl_kernels/activation_layer.cl b/src/core/CL/cl_kernels/activation_layer.cl index 721c43c017..5f812cf5b3 100644 --- a/src/core/CL/cl_kernels/activation_layer.cl +++ b/src/core/CL/cl_kernels/activation_layer.cl @@ -23,16 +23,99 @@ */ #include "helpers.h" +#define TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) + +#if defined(FIXED_POINT_POSITION) +#include "fixed_point.h" + +#define CONST_ONE (1 << FIXED_POINT_POSITION) +#define ABS_OP(a) ABS_SAT_OP_EXPAND((a), DATA_TYPE, VEC_SIZE) +#define ADD_OP(a, b) ADD_SAT_OP_EXPAND((a), (b), DATA_TYPE, VEC_SIZE) +#define SUB_OP(a, b) SUB_SAT_OP_EXPAND((a), (b), DATA_TYPE, VEC_SIZE) +#define MUL_OP(a, b) MUL_SAT_OP_EXPAND((a), (b), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION) +#define MLA_OP(a, b, c) MLA_SAT_OP_EXPAND((a), (b), (c), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION) +#define DIV_OP(a, b) DIV_SAT_OP_EXPAND((a), (b), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION) +#define EXP_OP(a) EXP_OP_EXPAND((a), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION) +#define LOG_OP(a) LOG_OP_EXPAND((a), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION) +#define SQRT_OP(a) DIV_OP(CONST_ONE, INVSQRT_OP_EXPAND((a), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION)) +#define TANH_OP(a) TANH_OP_EXPAND((a), DATA_TYPE, VEC_SIZE, FIXED_POINT_POSITION) + +#else /* FIXED_POINT_POSITION */ + +#define CONST_ONE (1.f) +#define ABS_OP(a) fabs((a)) +#define ADD_OP(a, b) ((a) + (b)) +#define SUB_OP(a, b) ((a) - (b)) +#define MUL_OP(a, b) ((a) * (b)) +#define MLA_OP(a, b, c) ((b) * (c) + (a)) +#define DIV_OP(a, b) ((a) / (b)) +#define EXP_OP(a) exp((a)) +#define LOG_OP(a) log((a)) +#define SQRT_OP(a) sqrt((a)) +#define TANH_OP(a) tanh((a)) + +#endif /* FIXED_POINT_POSITION */ + +// Logistic Activation +inline TYPE logistic_op(TYPE x) +{ + return DIV_OP(CONST_ONE, ADD_OP(CONST_ONE, EXP_OP(-x))); +} +// Hyperbolic Tangent Activation +inline TYPE tanh_op(TYPE x) +{ + return MUL_OP((TYPE)A_VAL, TANH_OP(MUL_OP((TYPE)B_VAL, x))); +} +// RELU Tangent Activation +inline TYPE relu_op(TYPE x) +{ + return max(0, x); +} +// Bounded RELU Activation +inline TYPE brelu_op(TYPE x) +{ + return min((TYPE)A_VAL, max(0, x)); +} +// Soft RELU Activation +inline TYPE srelu_op(TYPE x) +{ + return LOG_OP(ADD_OP(CONST_ONE, EXP_OP(x))); +} +// Absolute Activation +inline TYPE abs_op(TYPE x) +{ + return ABS_OP(x); +} +// Square Activation +inline TYPE square_op(TYPE x) +{ + return MUL_OP(x, x); +} +// Square-root Activation +inline TYPE sqrt_op(TYPE x) +{ + return SQRT_OP(x); +} +// Linear Activation +inline TYPE linear_op(TYPE x) +{ + return MLA_OP((TYPE)B_VAL, (TYPE)A_VAL, x); +} + +#define ACTIVATION_OP2(op, x) op##_op(x) +#define ACTIVATION_OP(op, x) ACTIVATION_OP2(op, x) + /** This performs an activation function floating point inputs. * * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time * * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short - * @note Activation function should be given as a preprocessor argument using -DNAME. e.g. -DTANH - * @note Distinction between floating point and integer is done using -DTYPE_FP and -DTYPE_INT preprocessor argument - * @note A, B variables required by some activation functions are set using -DA= and -DB= respectively. + * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 + * @note Activation function should be given as a preprocessor argument using -DACT=name. e.g. -DACT=TANH + * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively. + * @note In case of fixed point calculations the fixed point position is passed using -DFIXED_POINT_POSITION=position. e.g. -DFIXED_POINT_POSITION=3. * - * @param[in] input_ptr Pointer to the source image. Supported data types: F16, F32 + * @param[in] input_ptr Pointer to the source image. Supported data types: QS8/QS16/F16/F32 * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) @@ -40,7 +123,7 @@ * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image - * @param[out] output_ptr Pointer to the destination image. Supported data types: F16, F32 + * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes) * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes) @@ -66,34 +149,12 @@ __kernel void activation_layer( #endif /* IN_PLACE */ // Load data - VEC_DATA_TYPE(DATA_TYPE, 16) - data = vload16(0, (__global DATA_TYPE *)input.ptr); + TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr); // Perform activation -#ifdef LOGISTIC - data = 1 / (1 + exp(-data)); -#elif defined(TANH) - data = (VEC_DATA_TYPE(DATA_TYPE, 16))A * tanh((VEC_DATA_TYPE(DATA_TYPE, 16))B * data); -#elif defined(RELU) - data = max(0, data); -#elif defined(BRELU) - data = min((VEC_DATA_TYPE(DATA_TYPE, 16))A, max(0, data)); -#elif defined(SRELU) - data = log(1 + exp(data)); -#elif defined(ABS) -#ifdef TYPE_INT - data = abs(data); -#else /* TYPE_INT */ - data = fabs(data); -#endif /* TYPE_INT */ -#elif defined(SQUARE) - data = data * data; -#elif defined(SQRT) - data = sqrt(data); -#elif defined(LINEAR) - data = (VEC_DATA_TYPE(DATA_TYPE, 16))A * data + (VEC_DATA_TYPE(DATA_TYPE, 16))B; -#endif /* switch TANH, RELU, BRELU, SRELU, ABS, SQUARE, SQRT, LINEAR */ + data = ACTIVATION_OP(ACT, data); // Store result - vstore16(data, 0, (__global DATA_TYPE *)output.ptr); + VSTORE(VEC_SIZE) + (data, 0, (__global DATA_TYPE *)output.ptr); } |