blob: c072d2a6de243746974237061ba86e3b3778d9cb [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_CLSOFTMAXLAYERKERNEL_H__
25#define __ARM_COMPUTE_CLSOFTMAXLAYERKERNEL_H__
26
steniu010d523cc2017-07-13 14:24:23 +010027#include "arm_compute/core/CL/ICLSimple3DKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028
Chunosovd6afedc2017-11-06 22:09:45 +070029#include <tuple>
30
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031namespace arm_compute
32{
33class ICLTensor;
34
35/** Interface for the identifying the max value of 1D Logits */
steniu010d523cc2017-07-13 14:24:23 +010036class CLLogits1DMaxKernel : public ICLSimple3DKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037{
38public:
39 /** Set the input and output tensors.
40 *
Chunosovf450caa2017-11-08 16:09:35 +070041 * @param[in] input Source tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010042 * @param[out] output Destination tensor. Data types supported: same as @p input
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 */
44 void configure(const ICLTensor *input, ICLTensor *output);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000045 /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DMaxKernel
46 *
47 * @param[in] input Source tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32
48 * @param[in] output Destination tensor. Data types supported: same as @p input
49 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +000050 * @return a status
Georgios Pinitas30902ed2017-11-14 15:32:57 +000051 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000052 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053};
54
Chunosovd6afedc2017-11-06 22:09:45 +070055/** Interface for shifting, exponentiating and summing the logits */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056class CLLogits1DShiftExpSumKernel : public ICLKernel
57{
58public:
59 /** Default constructor */
60 CLLogits1DShiftExpSumKernel();
61 /** Prevent instances of this class from being copied (As this class contains pointers) */
62 CLLogits1DShiftExpSumKernel(const CLLogits1DShiftExpSumKernel &) = delete;
63 /** Prevent instances of this class from being copied (As this class contains pointers) */
64 CLLogits1DShiftExpSumKernel &operator=(const CLLogits1DShiftExpSumKernel &) = delete;
65 /** Allow instances of this class to be moved */
66 CLLogits1DShiftExpSumKernel(CLLogits1DShiftExpSumKernel &&) = default;
67 /** Allow instances of this class to be moved */
68 CLLogits1DShiftExpSumKernel &operator=(CLLogits1DShiftExpSumKernel &&) = default;
69 /** Set the input and output tensors.
70 *
Chunosovf450caa2017-11-08 16:09:35 +070071 * @param[in] input Source tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010072 * @param[in] max Max values tensor. Data types supported: same as @p input
Chunosovf450caa2017-11-08 16:09:35 +070073 * @param[out] output Destination tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input
74 * @param[out] sum Sum of 1D logits tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input
75 * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.0
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 */
Pablo Palmier48a60f92017-10-18 11:03:08 +010077 void configure(const ICLTensor *input, const ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta = 1.0f);
Georgios Pinitas30902ed2017-11-14 15:32:57 +000078 /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DShiftExpSumKernel
79 *
80 * @param[in] input Source tensor. Data types supported: QS8/QASYMM8/QS16/F16/F32
81 * @param[in] max Max values tensor. Data types supported: same as @p input
82 * @param[in] output Destination tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input
83 * @param[in] sum Sum of 1D logits tensor. Data types supported: S32 for QASYMM8 @p input, or same as @p input
84 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +000085 * @return a status
Georgios Pinitas30902ed2017-11-14 15:32:57 +000086 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +000087 static Status validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
89 // Inherited methods overridden:
90 void run(const Window &window, cl::CommandQueue &queue) override;
91
92private:
93 const ICLTensor *_input;
94 const ICLTensor *_max;
95 ICLTensor *_output;
96 ICLTensor *_sum;
97};
98
Chunosovd6afedc2017-11-06 22:09:45 +070099/** Interface for max, shifting, exponentiating and summing the logits */
100class CLLogits1DMaxShiftExpSumKernel : public ICLKernel
101{
102public:
103 using ParallelReductionInfo = std::tuple<bool, unsigned int>;
104
105public:
106 /** Default constructor */
107 CLLogits1DMaxShiftExpSumKernel();
108 /** Prevent instances of this class from being copied (As this class contains pointers) */
109 CLLogits1DMaxShiftExpSumKernel(const CLLogits1DMaxShiftExpSumKernel &) = delete;
110 /** Prevent instances of this class from being copied (As this class contains pointers) */
111 CLLogits1DMaxShiftExpSumKernel &operator=(const CLLogits1DMaxShiftExpSumKernel &) = delete;
112 /** Allow instances of this class to be moved */
113 CLLogits1DMaxShiftExpSumKernel(CLLogits1DMaxShiftExpSumKernel &&) = default;
114 /** Allow instances of this class to be moved */
115 CLLogits1DMaxShiftExpSumKernel &operator=(CLLogits1DMaxShiftExpSumKernel &&) = default;
116 /** Set the input and output tensors.
117 *
118 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32
119 * @param[in,out] max Max values tensor. Data types supported: same as @p input
120 * @param[out] output Destination tensor. Data types supported: same as @p input
121 * @param[out] sum Sum of 1D logits tensor. Data types supported: same as @p input
122 * @param[in] beta (Optional) A scaling factor for the exponent. Defaults to 1.f
123 */
124 void configure(const ICLTensor *input, ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta = 1.0f);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000125 /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DMaxShiftExpSumKernel
126 *
127 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32
128 * @param[in] max Max values tensor. Data types supported: same as @p input
129 * @param[in] output Destination tensor. Data types supported: same as @p input
130 * @param[in] sum Sum of 1D logits tensor. Data types supported: same as @p input
131 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000132 * @return a status
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000133 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000134 static Status validate(const ITensorInfo *input, const ITensorInfo *max, const ITensorInfo *output, const ITensorInfo *sum);
Chunosovd6afedc2017-11-06 22:09:45 +0700135 /** Checks if the given size is eligible for parallel reduction
136 *
137 * @note Serial reduction is launched for width < (_grid_size * _serial_vector_size).
138 * @note Parallel reduction is launched for width >= (_grid_size * _serial_vector_size) and vector_size is forced to 4.
139 *
140 * @param[in] size Size to check
141 *
142 * @return A two-element tuple where the first element is a boolean specifying is a parallel reduction will be run,
143 * while the second elements is the vector size of the execution.
144 */
145 static ParallelReductionInfo is_parallel_reduction(size_t size);
146
147 // Inherited methods overridden:
148 void run(const Window &window, cl::CommandQueue &queue) override;
149
150private:
151 const ICLTensor *_input;
152 ICLTensor *_max;
153 ICLTensor *_output;
154 ICLTensor *_sum;
155
156private:
157 static const unsigned int _grid_size;
158 static const unsigned int _serial_vector_size;
159 static const unsigned int _parallel_vector_size;
160};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161/** 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. */
162class CLLogits1DNormKernel : public ICLKernel
163{
164public:
165 /** Default constructor */
166 CLLogits1DNormKernel();
167 /** Prevent instances of this class from being copied (As this class contains pointers) */
168 CLLogits1DNormKernel(const CLLogits1DNormKernel &) = delete;
169 /** Prevent instances of this class from being copied (As this class contains pointers) */
170 CLLogits1DNormKernel &operator=(const CLLogits1DNormKernel &) = delete;
171 /** Allow instances of this class to be moved */
172 CLLogits1DNormKernel(CLLogits1DNormKernel &&) = default;
173 /** Allow instances of this class to be moved */
174 CLLogits1DNormKernel &operator=(CLLogits1DNormKernel &&) = default;
175 /** Set the input and output tensors.
176 *
Chunosovf450caa2017-11-08 16:09:35 +0700177 * @param[in] input Source tensor. Data types supported: QS8/QS16/S32/F16/F32
Georgios Pinitase5f8fd62017-06-23 18:03:44 +0100178 * @param[in] sum Sum tensor. Dimensions should be dim(input)-1. Data types supported: same as @p input
Chunosovf450caa2017-11-08 16:09:35 +0700179 * @param[out] output Destination tensor. Data types supported: QASYMM8 for S32 @p input, or same as @p input
180 * @param[in] beta (Optional) A scaling factor for the exponent. (Default = 1.0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 */
Chunosovf450caa2017-11-08 16:09:35 +0700182 void configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output, float beta = 1.0f);
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000183 /** Static function to check if given info will lead to a valid configuration of @ref CLLogits1DNormKernel
184 *
185 * @param[in] input Source tensor. Data types supported: QS8/QS16/S32/F16/F32
186 * @param[in] sum Sum tensor. Dimensions should be dim(input)-1. Data types supported: same as @p input
187 * @param[in] output Destination tensor. Data types supported: QASYMM8 for S32 @p input, or same as @p input
188 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000189 * @return a status
Georgios Pinitas30902ed2017-11-14 15:32:57 +0000190 */
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000191 static Status validate(const ITensorInfo *input, const ITensorInfo *sum, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192
193 // Inherited methods overridden:
194 void run(const Window &window, cl::CommandQueue &queue) override;
195
196private:
197 const ICLTensor *_input;
198 const ICLTensor *_sum;
199 ICLTensor *_output;
200};
Gian Marco Iodicef670a0a2017-09-18 12:20:45 +0100201} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202#endif /*__ARM_COMPUTE_CLSOFTMAXLAYERKERNEL_H__ */