blob: cce21569d93b333ec769646e24a4f723ae3b6136 [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);
46
47 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +010048 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 BorderSize border_size() const override;
50
51private:
52 using Logits1DMaxFunction = void(const ITensor *in, ITensor *out, const Window &window);
53
54private:
55 Logits1DMaxFunction *_func;
56 BorderSize _border_size;
57};
58
59/** Interface for shifting the logits values around the max value and exponentiating the result */
60class NELogits1DShiftExpSumKernel : public INEKernel
61{
62public:
63 /** Default constructor */
64 NELogits1DShiftExpSumKernel();
65 /** Prevent instances of this class from being copied (As this class contains pointers) */
66 NELogits1DShiftExpSumKernel(const NELogits1DShiftExpSumKernel &) = delete;
67 /** Prevent instances of this class from being copied (As this class contains pointers) */
68 NELogits1DShiftExpSumKernel &operator=(const NELogits1DShiftExpSumKernel &) = delete;
69 /** Allow instances of this class to be moved */
70 NELogits1DShiftExpSumKernel(NELogits1DShiftExpSumKernel &&) = default;
71 /** Allow instances of this class to be moved */
72 NELogits1DShiftExpSumKernel &operator=(NELogits1DShiftExpSumKernel &&) = default;
73 /** Default destructor */
74 ~NELogits1DShiftExpSumKernel() = default;
75 /** Set the input and output tensors.
76 *
Pablo Tellob49a7152017-07-11 16:31:35 +010077 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 * @param[in] max Max values tensor. Data types supported: same as @p input.
79 * @param[out] output Destination tensor. Data types supported: same as @p input.
80 * @param[out] sum Sum of 1D logits tensor. Data types supported: same as @p input.
81 */
82 void configure(const ITensor *input, const ITensor *max, ITensor *output, ITensor *sum);
83
84 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +010085 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
87private:
88 using Logits1DShiftExpSumFunction = void(const ITensor *in, const ITensor *max, ITensor *out, ITensor *sum, const Window &window);
89
90private:
91 Logits1DShiftExpSumFunction *_func;
92 const ITensor *_input;
93 const ITensor *_max;
94 ITensor *_output;
95 ITensor *_sum;
96};
97
98/** 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. */
99class NELogits1DNormKernel : public INEKernel
100{
101public:
102 /** Default constructor */
103 NELogits1DNormKernel();
104 /** Prevent instances of this class from being copied (As this class contains pointers) */
105 NELogits1DNormKernel(const NELogits1DNormKernel &) = delete;
106 /** Prevent instances of this class from being copied (As this class contains pointers) */
107 NELogits1DNormKernel &operator=(const NELogits1DNormKernel &) = delete;
108 /** Allow instances of this class to be moved */
109 NELogits1DNormKernel(NELogits1DNormKernel &&) = default;
110 /** Allow instances of this class to be moved */
111 NELogits1DNormKernel &operator=(NELogits1DNormKernel &&) = default;
112 /** Default destructor */
113 ~NELogits1DNormKernel() = default;
114 /** Set the input and output tensors.
115 *
Pablo Tellob49a7152017-07-11 16:31:35 +0100116 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 * @param[in] sum Sum tensor. The number of dimensions should be dim(input)-1. Data types supported: same as @p input.
118 * @param[out] output Destination tensor. Data types supported: same as @p input.
119 */
120 void configure(const ITensor *input, const ITensor *sum, ITensor *output);
121
122 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100123 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124
125private:
126 using Logits1DNormFunction = void(const ITensor *in, const ITensor *sum, ITensor *out, const Window &window);
127
128private:
129 Logits1DNormFunction *_func;
130 const ITensor *_input;
131 const ITensor *_sum;
132 ITensor *_output;
133};
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100134} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135#endif /*__ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H__ */