blob: 40b6743031da2b43d3c128ebdcbdddafbd5039f2 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini2b84be52020-04-08 10:15:51 +01002 * Copyright (c) 2017-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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLSOFTMAXLAYER_H
25#define ARM_COMPUTE_CLSOFTMAXLAYER_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h"
28#include "arm_compute/runtime/CL/CLTensor.h"
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010029#include "arm_compute/runtime/CL/functions/CLFlattenLayer.h"
30#include "arm_compute/runtime/CL/functions/CLReshapeLayer.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/runtime/IFunction.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010032#include "arm_compute/runtime/IMemoryManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010033#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010034
35#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37namespace arm_compute
38{
39class ICLTensor;
40
41/** Basic function to compute a SoftmaxLayer.
42 *
43 * Softmax is calculated by :
Georgios Pinitas388d3ec2017-11-02 12:17:56 +000044 * @f[ out = exp((x - max(x)) * beta) / sum(exp((x - max(x)) * beta)) @f]
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 *
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000046 * Log Softmax is calculated by :
Sang-Hoon Parka0205b92020-07-07 09:36:09 +010047 * @f[ out = (x - max(x) * beta) - log(\sum{e^{x - max(x) * beta}}) @f]
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000048 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 * This function runs the following kernels:
50 * -# @ref CLLogits1DMaxKernel
51 * -# @ref CLLogits1DShiftExpSumKernel
52 * -# @ref CLLogits1DNormKernel
SiCong Lid004a7a2020-05-28 15:26:41 +010053 * And if the reduce_end_axis is not 0, the function will use one of the the following kernels to reshape the input and
54 * perform softmax on the reshaped input:
55 * -# @ref CLFlattenLayerKernel
56 * -# @ref CLReshapeLayerKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 */
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000058template <bool IS_LOG = false>
59class CLSoftmaxLayerGeneric : public IFunction
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060{
61public:
62 /** Constructor */
Sang-Hoon Park62eeb532019-10-29 13:13:19 +000063 CLSoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 /** Set the input and output tensors.
65 *
Sang-Hoon Parka0205b92020-07-07 09:36:09 +010066 * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 for Softmax and F16/F32 for Log Softmax
SiCong Lid004a7a2020-05-28 15:26:41 +010067 * @param[out] output Destination tensor. Data types supported: same as @p input
68 * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.f
69 * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0.
70 * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image,
71 * when @p reduce_end_axis is 1, the reduction will be applied to axes 0 and 1, and the Softmax op will be applied on each of the [2x3] planes of the input image.
72 * Must be in range [0, input_num_dimensions).
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 */
SiCong Lid004a7a2020-05-28 15:26:41 +010074 void configure(const ICLTensor *input, ICLTensor *output, float beta = 1.0f, size_t reduce_end_axis = 0);
Manuel Bottini2b84be52020-04-08 10:15:51 +010075 /** Set the input and output tensors.
76 *
77 * @param[in] compile_context The compile context to be used.
Sang-Hoon Parka0205b92020-07-07 09:36:09 +010078 * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 for Softmax and F16/F32 for Log Softmax
Manuel Bottini2b84be52020-04-08 10:15:51 +010079 * @param[out] output Destination tensor. Data types supported: same as @p input
80 * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.f
SiCong Lid004a7a2020-05-28 15:26:41 +010081 * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0.
82 * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image,
83 * when @p reduce_end_axis is 1, the reduction will be applied to axes 0 and 1, and the Softmax op will be applied on each of the [2x3] planes of the input image.
84 * Must be in range [0, input_num_dimensions).
Manuel Bottini2b84be52020-04-08 10:15:51 +010085 */
SiCong Lid004a7a2020-05-28 15:26:41 +010086 void configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, float beta = 1.0f, size_t reduce_end_axis = 0);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000087 /** Static function to check if given info will lead to a valid configuration of @ref CLSoftmaxLayer
88 *
Sang-Hoon Parka0205b92020-07-07 09:36:09 +010089 * @param[in] input Source tensor. Data types supported: QASYMM8/F16/F32 for Softmax and F16/F32 for Log Softmax
SiCong Lid004a7a2020-05-28 15:26:41 +010090 * @param[in] output Destination tensor. Data types supported: same as @p input
91 * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.f
92 * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0.
93 * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image,
94 * when @p reduce_end_axis is 1, the reduction will be applied to axes 0 and 1, and the Softmax op will be applied on each of the [2x3] planes of the input image.
95 * Must be in range [0, input_num_dimensions).
Georgios Pinitas631c41a2017-12-06 11:53:03 +000096 * @return a status
Georgios Pinitas30902ed2017-11-14 15:32:57 +000097 */
SiCong Lid004a7a2020-05-28 15:26:41 +010098 static Status validate(const ITensorInfo *input, const ITensorInfo *output, float beta = 1.0f, size_t reduce_end_axis = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099
100 // Inherited methods overridden:
101 void run() override;
102
103private:
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100104 /** Utility method to configure the kernels needed to flatten the input
105 * tensor.
106 *
107 * @note This function changes the internal state of this class. In particular,
108 * it initializes the kernel @p _flatten_kernel and the tensors @p _input_flat and
109 * @p _output_flat
110 *
SiCong Lid004a7a2020-05-28 15:26:41 +0100111 * @param[in] input Original source tensor.
112 * @param[in] output Original destination tensor.
113 * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0.
114 * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image,
115 * when @p reduce_end_axis is 1, the reduction will be applied to axes 0 and 1, and the Softmax op will be applied on each of the [2x3] planes of the input image.
116 * Must be in range [0, input_num_dimensions).
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100117 */
SiCong Lid004a7a2020-05-28 15:26:41 +0100118 void configure_reshape_input_kernel(const ICLTensor *input, const ICLTensor *output, size_t reduce_end_axis);
Manuel Bottini2b84be52020-04-08 10:15:51 +0100119 /** Utility method to configure the kernels needed to flatten the input
120 * tensor.
121 *
122 * @note This function changes the internal state of this class. In particular,
123 * it initializes the kernel @p _flatten_kernel and the tensors @p _input_flat and
124 * @p _output_flat
125 *
126 * @param[in] compile_context The compile context to be used.
127 * @param[in] input Original source tensor.
128 * @param[in] output Original destination tensor.
SiCong Lid004a7a2020-05-28 15:26:41 +0100129 * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0.
130 * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image,
131 * when @p reduce_end_axis is 1, the reduction will be applied to axes 0 and 1, and the Softmax op will be applied on each of the [2x3] planes of the input image.
132 * Must be in range [0, input_num_dimensions).
Manuel Bottini2b84be52020-04-08 10:15:51 +0100133 */
SiCong Lid004a7a2020-05-28 15:26:41 +0100134 void configure_reshape_input_kernel(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *output, size_t reduce_end_axis);
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100135
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100136 MemoryGroup _memory_group;
Chunosovd6afedc2017-11-06 22:09:45 +0700137 CLLogits1DMaxShiftExpSumKernel _max_shift_exp_sum_kernel;
138 CLLogits1DNormKernel _norm_kernel;
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100139 std::unique_ptr<IFunction> _flatten_ptr;
140 CLReshapeLayer _reshape;
Chunosovd6afedc2017-11-06 22:09:45 +0700141 CLTensor _max;
142 CLTensor _sum;
143 CLTensor _tmp;
giuros01efbf6c82018-09-03 09:53:53 +0100144 CLTensor _input_flattened;
145 CLTensor _output_flattened;
Giuseppe Rossini87e896a2018-08-24 10:24:12 +0100146 bool _needs_flattening;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147};
Sang-Hoon Park62eeb532019-10-29 13:13:19 +0000148
149using CLSoftmaxLayer = CLSoftmaxLayerGeneric<false>;
150using CLLogSoftmaxLayer = CLSoftmaxLayerGeneric<true>;
151} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000152#endif /* ARM_COMPUTE_CLSOFTMAXLAYER_H */