blob: 0fecfac15abb2cdaf73d1d6fc8039143fdef9266 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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:
38 /** Default constructor */
39 NELogits1DMaxKernel();
40 /** Set the input and output tensors.
41 *
Pablo Tellob49a7152017-07-11 16:31:35 +010042 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 * @param[out] output Destination tensor. Data types supported: same as @p input
44 */
45 void configure(const ITensor *input, ITensor *output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000046 /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DMaxKernel
47 *
48 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32
49 * @param[in] output Destination tensor. Data types supported: same as @p input
50 *
51 * @return a status
52 */
53 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054
55 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +010056 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 BorderSize border_size() const override;
58
59private:
60 using Logits1DMaxFunction = void(const ITensor *in, ITensor *out, const Window &window);
61
62private:
63 Logits1DMaxFunction *_func;
64 BorderSize _border_size;
65};
66
67/** Interface for shifting the logits values around the max value and exponentiating the result */
68class NELogits1DShiftExpSumKernel : public INEKernel
69{
70public:
71 /** Default constructor */
72 NELogits1DShiftExpSumKernel();
73 /** Prevent instances of this class from being copied (As this class contains pointers) */
74 NELogits1DShiftExpSumKernel(const NELogits1DShiftExpSumKernel &) = delete;
75 /** Prevent instances of this class from being copied (As this class contains pointers) */
76 NELogits1DShiftExpSumKernel &operator=(const NELogits1DShiftExpSumKernel &) = delete;
77 /** Allow instances of this class to be moved */
78 NELogits1DShiftExpSumKernel(NELogits1DShiftExpSumKernel &&) = default;
79 /** Allow instances of this class to be moved */
80 NELogits1DShiftExpSumKernel &operator=(NELogits1DShiftExpSumKernel &&) = default;
81 /** Default destructor */
82 ~NELogits1DShiftExpSumKernel() = default;
83 /** Set the input and output tensors.
84 *
Pablo Tellob49a7152017-07-11 16:31:35 +010085 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 * @param[in] max Max values tensor. Data types supported: same as @p input.
87 * @param[out] output Destination tensor. Data types supported: same as @p input.
88 * @param[out] sum Sum of 1D logits tensor. Data types supported: same as @p input.
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010089 * @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 +010090 */
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010091 void configure(const ITensor *input, const ITensor *max, ITensor *output, ITensor *sum, float beta = 1.0f);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000092 /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DShiftExpSumKernel
93 *
94 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32
95 * @param[in] max Max values tensor. Data types supported: same as @p input
96 * @param[in] output Destination tensor. Data types supported: same as @p input.
97 * @param[in] sum Sum of 1D logits tensor. Data types supported: same as @p input.
98 * @param[in] beta (Optional) A scaling factor for the exponent. QS8/QS16 only support a beta value of 1.
99 *
100 * @return a status
101 */
102 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 +0100103
104 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100105 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106
107private:
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100108 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 +0100109
110private:
111 Logits1DShiftExpSumFunction *_func;
112 const ITensor *_input;
113 const ITensor *_max;
114 ITensor *_output;
115 ITensor *_sum;
Pablo Palmiera2b89ca2017-10-05 15:01:34 +0100116 float _beta;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117};
118
119/** 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. */
120class NELogits1DNormKernel : public INEKernel
121{
122public:
123 /** Default constructor */
124 NELogits1DNormKernel();
125 /** Prevent instances of this class from being copied (As this class contains pointers) */
126 NELogits1DNormKernel(const NELogits1DNormKernel &) = delete;
127 /** Prevent instances of this class from being copied (As this class contains pointers) */
128 NELogits1DNormKernel &operator=(const NELogits1DNormKernel &) = delete;
129 /** Allow instances of this class to be moved */
130 NELogits1DNormKernel(NELogits1DNormKernel &&) = default;
131 /** Allow instances of this class to be moved */
132 NELogits1DNormKernel &operator=(NELogits1DNormKernel &&) = default;
133 /** Default destructor */
134 ~NELogits1DNormKernel() = default;
135 /** Set the input and output tensors.
136 *
Pablo Tellob49a7152017-07-11 16:31:35 +0100137 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 * @param[in] sum Sum tensor. The number of dimensions should be dim(input)-1. Data types supported: same as @p input.
139 * @param[out] output Destination tensor. Data types supported: same as @p input.
140 */
141 void configure(const ITensor *input, const ITensor *sum, ITensor *output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000142 /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DNormKernel
143 *
144 * @param[in] input Source tensor. Data types supported: QS8/QS16/S32/F16/F32
145 * @param[in] sum Sum tensor. The number of dimensions should be dim(input)-1. Data types supported: same as @p input.
146 * @param[in] output Destination tensor. Data types supported: same as @p input.
147 *
148 * @return a status
149 */
150 static Status validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151
152 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100153 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154
155private:
156 using Logits1DNormFunction = void(const ITensor *in, const ITensor *sum, ITensor *out, const Window &window);
157
158private:
159 Logits1DNormFunction *_func;
160 const ITensor *_input;
161 const ITensor *_sum;
162 ITensor *_output;
163};
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100164} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165#endif /*__ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H__ */