blob: f89add71ee96113cd3ffad7ae8d1c19ac02d1cd8 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini678d83a2019-01-07 16:05:36 +00002 * Copyright (c) 2017-2019 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"
Manuel Bottini678d83a2019-01-07 16:05:36 +000028#include "arm_compute/core/NEON/kernels/NEFlattenLayerKernel.h"
29#include "arm_compute/core/NEON/kernels/NEReshapeLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/NEON/kernels/NESoftmaxLayerKernel.h"
31#include "arm_compute/runtime/IFunction.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010032#include "arm_compute/runtime/MemoryGroup.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 :
42 * @f[ out = \frac{e^{x - max(x)}}{\sum{e^{x - max(x)}}} @f]
43 *
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010044 * Log Softmax is calculated by :
45 * @f[ out = (x - max(x)) - \sum{e^{x - max(x)}} @f]
46 *
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 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010068 * @param[in,out] input Source tensor. Data types supported: QASYMM8/F16/F32. If the width is not a
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000069 * 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.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010072 * @param[in] beta (Optional) A scaling factor for the exponent.
Manuel Bottini678d83a2019-01-07 16:05:36 +000073 * @param[in] axis (Optional) Reduction axis. Defaults to 1. Must be in range [1, input_num_dimensions).
74 * It has the purpose of squashing the first @p axis dimensions together. For instance, given a [4x4x4x4] image,
giuros01efbf6c82018-09-03 09:53:53 +010075 * when @p axis is 2, the Softmax reduction will be applied on each of the [4x4] planes of the input image.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 */
giuros01efbf6c82018-09-03 09:53:53 +010077 void configure(ITensor *input, ITensor *output, float beta = 1.0f, size_t axis = 1);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000078 /** Static function to check if given info will lead to a valid configuration of @ref NESoftmaxLayer
79 *
Manuel Bottini678d83a2019-01-07 16:05:36 +000080 * @param[in] input Source tensor info. Data types supported: QASYMM8/F16/F32.
81 * @param[in] output Destination tensor info. Data types supported: same as @p input
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010082 * @param[in] beta (Optional) A scaling factor for the exponent.
Manuel Bottini678d83a2019-01-07 16:05:36 +000083 * @param[in] axis (Optional) Reduction axis. Defaults to 1. Must be in range [1, input_num_dimensions).
84 * It has the purpose of squashing the first @p axis dimensions together. For instance, given a [4x4x4x4] image,
giuros01efbf6c82018-09-03 09:53:53 +010085 * when @p axis is 2, the Softmax reduction will be applied on each of the [4x4] planes of the input image.
86 *
Michalis Spyrouafa5d812017-11-30 14:25:57 +000087 * @return a status
88 */
giuros01efbf6c82018-09-03 09:53:53 +010089 static Status validate(const ITensorInfo *input, const ITensorInfo *output, float beta = 1.0f, size_t axis = 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91 // Inherited methods overridden:
92 void run() override;
93
94private:
Manuel Bottini678d83a2019-01-07 16:05:36 +000095 /** Utility method to configure the kernels needed to flatten the input
96 * tensor.
97 *
98 * @note This function changes the internal state of this class. In particular,
99 * it initializes the kernel @p _flatten_kernel and the tensors @p _input_flat and
100 * @p _output_flat
101 *
102 * @param[in] input Original source tensor.
103 * @param[in] output Original destination tensor.
104 * @param[in] axis (Optional) Reduction axis. Defaults to 1. Must be in range [1, input_num_dimensions).
105 * It has the purpose of squashing the first @p axis dimensions together. For instance, given a [4x4x4x4] image,
106 * when @p axis is 2, the Softmax reduction will be applied on each of the [4x4] planes of the input image.
107 */
108 void configure_reshape_input_kernel(const ITensor *input, const ITensor *output, size_t axis);
109
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100110 MemoryGroup _memory_group;
111 NELogits1DMaxKernel _max_kernel;
112 NELogits1DSoftmaxKernel<IS_LOG> _softmax_kernel;
113 std::unique_ptr<INEKernel> _flat_or_reshape_kernel_ptr;
114 NEFillBorderKernel _fill_border_kernel;
115 NEReshapeLayerKernel _reshape_kernel;
116 Tensor _max;
117 Tensor _tmp;
118 Tensor _input_flattened;
119 Tensor _output_flattened;
120 bool _needs_flattening;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121};
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100122
123using NESoftmaxLayer = NESoftmaxLayerGeneric<false>;
124using NELogSoftmaxLayer = NESoftmaxLayerGeneric<true>;
125
Manuel Bottini678d83a2019-01-07 16:05:36 +0000126} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000127#endif /* ARM_COMPUTE_NESOFTMAXLAYER_H */