blob: 4ccfe2684ef76106e045b85eb070a7a7a4dca090 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +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_GCSOFTMAXLAYER_H
25#define ARM_COMPUTE_GCSOFTMAXLAYER_H
Anthony Barbier7068f992017-10-26 15:23:08 +010026
27#include "arm_compute/core/GLES_COMPUTE/kernels/GCSoftmaxLayerKernel.h"
28#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
29#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010030#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010031
32namespace arm_compute
33{
34class IGCTensor;
35
36/** Basic function to compute a SoftmaxLayer.
37 *
38 * Softmax is calculated by :
39 * @f[ out = exp(x - max(x)) / sum(exp(x - max(x))) @f]
40 *
41 * This function runs the following kernels:
42 * -# @ref GCLogits1DMaxKernel
43 * -# @ref GCLogits1DShiftExpSumKernel
44 * -# @ref GCLogits1DNormKernel
45 */
46class GCSoftmaxLayer : public IFunction
47{
48public:
49 /** Constructor */
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000050 GCSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Anthony Barbier7068f992017-10-26 15:23:08 +010051 /** Set the input and output tensors.
52 *
SiCong Lid004a7a2020-05-28 15:26:41 +010053 * @param[in] input Source tensor. Data types supported: F16/F32
54 * @param[out] output Destination tensor. Data types supported: same as @p input
55 * @param[in] beta (Optional) A scaling factor for the exponent. Only beta = 1 is supported
56 * @param[in] reduce_end_axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Defaults to 0.
57 * It has the purpose of squashing together the first n dimensions till (including) the @p reduce_end_axis. For instance, given a [2x3x4x5] image,
58 * 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.
59 * Must be in range [0, input_num_dimensions).
giuros01efbf6c82018-09-03 09:53:53 +010060 *
SiCong Lid004a7a2020-05-28 15:26:41 +010061 * @note The value of @p reduce_end_axis must be always 0 for GLES
Anthony Barbier7068f992017-10-26 15:23:08 +010062 */
SiCong Lid004a7a2020-05-28 15:26:41 +010063 void configure(const IGCTensor *input, IGCTensor *output, float beta = 1.0f, size_t reduce_end_axis = 0);
Anthony Barbier7068f992017-10-26 15:23:08 +010064
65 // Inherited methods overridden:
66 void run() override;
67
68private:
Georgios Pinitas26014cf2019-09-09 19:00:57 +010069 MemoryGroup _memory_group;
Anthony Barbier7068f992017-10-26 15:23:08 +010070 GCLogits1DMaxKernel _max_kernel;
71 GCLogits1DShiftExpSumKernel _shift_exp_sum_kernel;
72 GCLogits1DNormKernel _norm_kernel;
73 GCTensor _max;
74 GCTensor _sum;
75 GCTensor _tmp;
76};
Georgios Pinitas26014cf2019-09-09 19:00:57 +010077} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +000078#endif /* ARM_COMPUTE_GCSOFTMAXLAYER_H */