blob: 9fb4d85262f1c43c9877128fbde50062b652f7df [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +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_NESOFTMAXLAYER_H
25#define ARM_COMPUTE_NESOFTMAXLAYER_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h"
28#include "arm_compute/core/NEON/kernels/NESoftmaxLayerKernel.h"
29#include "arm_compute/runtime/IFunction.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010030#include "arm_compute/runtime/MemoryGroup.h"
Michalis Spyroubcd23522020-05-21 15:02:36 +010031#include "arm_compute/runtime/NEON/functions/NEFlattenLayer.h"
32#include "arm_compute/runtime/NEON/functions/NEReshapeLayer.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/runtime/Tensor.h"
34
35namespace arm_compute
36{
37class ITensor;
38
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010039/** Basic function to compute a SoftmaxLayer and a Log SoftmaxLayer.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 *
41 * Softmax is calculated by :
Sang-Hoon Parka0205b92020-07-07 09:36:09 +010042 * @f[ out = exp((x - max(x)) * beta) / sum(exp((x - max(x)) * beta)) @f]
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 *
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010044 * Log Softmax is calculated by :
Sang-Hoon Parka0205b92020-07-07 09:36:09 +010045 * @f[ out = (x - max(x) * beta) - log(\sum{e^{x - max(x) * beta}}) @f]
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010046 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 * This function runs the following kernels:
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000048 * -# @ref NEFillBorderKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 * -# @ref NELogits1DMaxKernel
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000050 * -# @ref NELogits1DSoftmaxKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 */
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010052template <bool IS_LOG = false>
53class NESoftmaxLayerGeneric : public IFunction
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054{
55public:
56 /** Constructor */
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010057 NESoftmaxLayerGeneric(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Manuel Bottini678d83a2019-01-07 16:05:36 +000058 /** Prevent instances of this class from being copied (As this class contains pointers) */
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010059 NESoftmaxLayerGeneric(const NESoftmaxLayerGeneric &) = delete;
Manuel Bottini678d83a2019-01-07 16:05:36 +000060 /** Default move constructor */
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010061 NESoftmaxLayerGeneric(NESoftmaxLayerGeneric &&) = default;
Manuel Bottini678d83a2019-01-07 16:05:36 +000062 /** Prevent instances of this class from being copied (As this class contains pointers) */
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010063 NESoftmaxLayerGeneric &operator=(const NESoftmaxLayerGeneric &) = delete;
Manuel Bottini678d83a2019-01-07 16:05:36 +000064 /** Default move assignment operator */
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010065 NESoftmaxLayerGeneric &operator=(NESoftmaxLayerGeneric &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 /** Set the input and output tensors.
67 *
morgolock9c7fed82020-08-05 12:30:56 +010068 * @param[in,out] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. If the width is not a
69 * multiple of the internal processing block size, @ref NEFillBorderKernel replicates the
70 * last value of each row to the nearest multiple.
71 * @param[out] output Destination tensor. Data types supported: same as @p input.
72 * @param[in] beta (Optional) A scaling factor for the exponent.
73 * @param[in] axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Only supports axis 0.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 */
morgolock9c7fed82020-08-05 12:30:56 +010075 void configure(ITensor *input, ITensor *output, float beta = 1.0f, int32_t axis = 0);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000076 /** Static function to check if given info will lead to a valid configuration of @ref NESoftmaxLayer
77 *
morgolock9c7fed82020-08-05 12:30:56 +010078 * @param[in] input Source tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
79 * @param[in] output Destination tensor info. Data types supported: same as @p input
80 * @param[in] beta (Optional) A scaling factor for the exponent.
81 * @param[in] axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Only supports axis 0.
82 *
Michalis Spyrouafa5d812017-11-30 14:25:57 +000083 * @return a status
84 */
morgolock9c7fed82020-08-05 12:30:56 +010085 static Status validate(const ITensorInfo *input, const ITensorInfo *output, float beta = 1.0f, int32_t axis = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
87 // Inherited methods overridden:
88 void run() override;
89
90private:
Manuel Bottini678d83a2019-01-07 16:05:36 +000091 /** Utility method to configure the kernels needed to flatten the input
92 * tensor.
93 *
94 * @note This function changes the internal state of this class. In particular,
95 * it initializes the kernel @p _flatten_kernel and the tensors @p _input_flat and
96 * @p _output_flat
97 *
morgolock9c7fed82020-08-05 12:30:56 +010098 * @param[in] input Original source tensor.
99 * @param[in] output Original destination tensor.
100 * @param[in] axis (Optional) The last axis of the first n dimensions (inclusive)to reduce. Only supports axis 0.
Manuel Bottini678d83a2019-01-07 16:05:36 +0000101 */
morgolock9c7fed82020-08-05 12:30:56 +0100102 void configure_reshape_input_kernel(const ITensor *input, const ITensor *output, int32_t axis);
Manuel Bottini678d83a2019-01-07 16:05:36 +0000103
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100104 MemoryGroup _memory_group;
105 NELogits1DMaxKernel _max_kernel;
106 NELogits1DSoftmaxKernel<IS_LOG> _softmax_kernel;
Michalis Spyroubcd23522020-05-21 15:02:36 +0100107 std::unique_ptr<IFunction> _flat_or_reshape_ptr;
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100108 NEFillBorderKernel _fill_border_kernel;
Michalis Spyroubcd23522020-05-21 15:02:36 +0100109 NEReshapeLayer _reshape;
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100110 Tensor _max;
111 Tensor _tmp;
112 Tensor _input_flattened;
113 Tensor _output_flattened;
114 bool _needs_flattening;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115};
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100116
117using NESoftmaxLayer = NESoftmaxLayerGeneric<false>;
118using NELogSoftmaxLayer = NESoftmaxLayerGeneric<true>;
119
Manuel Bottini678d83a2019-01-07 16:05:36 +0000120} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000121#endif /* ARM_COMPUTE_NESOFTMAXLAYER_H */