blob: bd0e642d76ad9e89bdd5668c1f5d6370dc113658 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbiere8a49832018-01-18 10:04:05 +00002 * Copyright (c) 2017-2018 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 */
24#ifndef __ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H__
25#define __ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H__
26
27#include "arm_compute/core/NEON/INEKernel.h"
28#include "arm_compute/core/NEON/INESimpleKernel.h"
29
30namespace arm_compute
31{
32class ITensor;
33
34/** Interface for the identifying the max value of 1D Logits */
35class NELogits1DMaxKernel : public INESimpleKernel
36{
37public:
Anthony Barbiere8a49832018-01-18 10:04:05 +000038 const char *name() const override
39 {
40 return "NELogits1DMaxKernel";
41 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042 /** Default constructor */
43 NELogits1DMaxKernel();
44 /** Set the input and output tensors.
45 *
Pablo Tellob49a7152017-07-11 16:31:35 +010046 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 * @param[out] output Destination tensor. Data types supported: same as @p input
48 */
49 void configure(const ITensor *input, ITensor *output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000050 /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DMaxKernel
51 *
52 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32
53 * @param[in] output Destination tensor. Data types supported: same as @p input
54 *
55 * @return a status
56 */
57 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058
59 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +010060 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 BorderSize border_size() const override;
62
63private:
64 using Logits1DMaxFunction = void(const ITensor *in, ITensor *out, const Window &window);
65
66private:
67 Logits1DMaxFunction *_func;
68 BorderSize _border_size;
69};
70
71/** Interface for shifting the logits values around the max value and exponentiating the result */
72class NELogits1DShiftExpSumKernel : public INEKernel
73{
74public:
Anthony Barbiere8a49832018-01-18 10:04:05 +000075 const char *name() const override
76 {
77 return "NELogits1DShiftExpSumKernel";
78 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 /** Default constructor */
80 NELogits1DShiftExpSumKernel();
81 /** Prevent instances of this class from being copied (As this class contains pointers) */
82 NELogits1DShiftExpSumKernel(const NELogits1DShiftExpSumKernel &) = delete;
83 /** Prevent instances of this class from being copied (As this class contains pointers) */
84 NELogits1DShiftExpSumKernel &operator=(const NELogits1DShiftExpSumKernel &) = delete;
85 /** Allow instances of this class to be moved */
86 NELogits1DShiftExpSumKernel(NELogits1DShiftExpSumKernel &&) = default;
87 /** Allow instances of this class to be moved */
88 NELogits1DShiftExpSumKernel &operator=(NELogits1DShiftExpSumKernel &&) = default;
89 /** Default destructor */
90 ~NELogits1DShiftExpSumKernel() = default;
91 /** Set the input and output tensors.
92 *
Pablo Tellob49a7152017-07-11 16:31:35 +010093 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 * @param[in] max Max values tensor. Data types supported: same as @p input.
95 * @param[out] output Destination tensor. Data types supported: same as @p input.
96 * @param[out] sum Sum of 1D logits tensor. Data types supported: same as @p input.
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010097 * @param[in] beta (Optional) A scaling factor for the exponent. QS8/QS16 only support a beta value of 1.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 */
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010099 void configure(const ITensor *input, const ITensor *max, ITensor *output, ITensor *sum, float beta = 1.0f);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000100 /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DShiftExpSumKernel
101 *
102 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32
103 * @param[in] max Max values tensor. Data types supported: same as @p input
104 * @param[in] output Destination tensor. Data types supported: same as @p input.
105 * @param[in] sum Sum of 1D logits tensor. Data types supported: same as @p input.
106 * @param[in] beta (Optional) A scaling factor for the exponent. QS8/QS16 only support a beta value of 1.
107 *
108 * @return a status
109 */
110 static Status validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum, float beta = 1.0f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111
112 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100113 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114
115private:
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100116 using Logits1DShiftExpSumFunction = void(const ITensor *in, const ITensor *max, ITensor *out, ITensor *sum, const Window &window, float beta);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117
118private:
119 Logits1DShiftExpSumFunction *_func;
120 const ITensor *_input;
121 const ITensor *_max;
122 ITensor *_output;
123 ITensor *_sum;
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100124 float _beta;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125};
126
127/** Interface for calculating the final step of the Softmax Layer where each logit value is multiplied by the inverse of the sum of the logits. */
128class NELogits1DNormKernel : public INEKernel
129{
130public:
Anthony Barbiere8a49832018-01-18 10:04:05 +0000131 const char *name() const override
132 {
133 return "NELogits1DNormKernel";
134 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 /** Default constructor */
136 NELogits1DNormKernel();
137 /** Prevent instances of this class from being copied (As this class contains pointers) */
138 NELogits1DNormKernel(const NELogits1DNormKernel &) = delete;
139 /** Prevent instances of this class from being copied (As this class contains pointers) */
140 NELogits1DNormKernel &operator=(const NELogits1DNormKernel &) = delete;
141 /** Allow instances of this class to be moved */
142 NELogits1DNormKernel(NELogits1DNormKernel &&) = default;
143 /** Allow instances of this class to be moved */
144 NELogits1DNormKernel &operator=(NELogits1DNormKernel &&) = default;
145 /** Default destructor */
146 ~NELogits1DNormKernel() = default;
147 /** Set the input and output tensors.
148 *
Pablo Tellob49a7152017-07-11 16:31:35 +0100149 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 * @param[in] sum Sum tensor. The number of dimensions should be dim(input)-1. Data types supported: same as @p input.
151 * @param[out] output Destination tensor. Data types supported: same as @p input.
152 */
153 void configure(const ITensor *input, const ITensor *sum, ITensor *output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000154 /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DNormKernel
155 *
156 * @param[in] input Source tensor. Data types supported: QS8/QS16/S32/F16/F32
157 * @param[in] sum Sum tensor. The number of dimensions should be dim(input)-1. Data types supported: same as @p input.
158 * @param[in] output Destination tensor. Data types supported: same as @p input.
159 *
160 * @return a status
161 */
162 static Status validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163
164 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100165 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166
167private:
168 using Logits1DNormFunction = void(const ITensor *in, const ITensor *sum, ITensor *out, const Window &window);
169
170private:
171 Logits1DNormFunction *_func;
172 const ITensor *_input;
173 const ITensor *_sum;
174 ITensor *_output;
175};
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100176} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177#endif /*__ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H__ */