blob: 1e079cbb06a5d629b25f0a3f28abd67af6e57a88 [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
29namespace arm_compute
30{
31class ICLTensor;
32
33/** Interface for the identifying the max value of 1D Logits */
steniu010d523cc2017-07-13 14:24:23 +010034class CLLogits1DMaxKernel : public ICLSimple3DKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035{
36public:
37 /** Set the input and output tensors.
38 *
Georgios Pinitas09796752017-07-10 16:05:21 +010039 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010040 * @param[out] output Destination tensor. Data types supported: same as @p input
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041 */
42 void configure(const ICLTensor *input, ICLTensor *output);
43};
44
45/** Interface for shifting the logits values around the max value and exponentiating the result */
46class CLLogits1DShiftExpSumKernel : public ICLKernel
47{
48public:
49 /** Default constructor */
50 CLLogits1DShiftExpSumKernel();
51 /** Prevent instances of this class from being copied (As this class contains pointers) */
52 CLLogits1DShiftExpSumKernel(const CLLogits1DShiftExpSumKernel &) = delete;
53 /** Prevent instances of this class from being copied (As this class contains pointers) */
54 CLLogits1DShiftExpSumKernel &operator=(const CLLogits1DShiftExpSumKernel &) = delete;
55 /** Allow instances of this class to be moved */
56 CLLogits1DShiftExpSumKernel(CLLogits1DShiftExpSumKernel &&) = default;
57 /** Allow instances of this class to be moved */
58 CLLogits1DShiftExpSumKernel &operator=(CLLogits1DShiftExpSumKernel &&) = default;
59 /** Set the input and output tensors.
60 *
Georgios Pinitas09796752017-07-10 16:05:21 +010061 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010062 * @param[in] max Max values tensor. Data types supported: same as @p input
Georgios Pinitas388d3ec2017-11-02 12:17:56 +000063 * @param[in] beta A scaling factor for the exponent.
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010064 * @param[out] output Destination tensor. Data types supported: same as @p input
65 * @param[out] sum Sum of 1D logits tensor. Data types supported: same as @p input
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 */
Pablo Palmier48a60f92017-10-18 11:03:08 +010067 void configure(const ICLTensor *input, const ICLTensor *max, ICLTensor *output, ICLTensor *sum, float beta = 1.0f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068
69 // Inherited methods overridden:
70 void run(const Window &window, cl::CommandQueue &queue) override;
71
72private:
73 const ICLTensor *_input;
74 const ICLTensor *_max;
75 ICLTensor *_output;
76 ICLTensor *_sum;
77};
78
79/** 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. */
80class CLLogits1DNormKernel : public ICLKernel
81{
82public:
83 /** Default constructor */
84 CLLogits1DNormKernel();
85 /** Prevent instances of this class from being copied (As this class contains pointers) */
86 CLLogits1DNormKernel(const CLLogits1DNormKernel &) = delete;
87 /** Prevent instances of this class from being copied (As this class contains pointers) */
88 CLLogits1DNormKernel &operator=(const CLLogits1DNormKernel &) = delete;
89 /** Allow instances of this class to be moved */
90 CLLogits1DNormKernel(CLLogits1DNormKernel &&) = default;
91 /** Allow instances of this class to be moved */
92 CLLogits1DNormKernel &operator=(CLLogits1DNormKernel &&) = default;
93 /** Set the input and output tensors.
94 *
Georgios Pinitas09796752017-07-10 16:05:21 +010095 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010096 * @param[in] sum Sum tensor. Dimensions should be dim(input)-1. Data types supported: same as @p input
97 * @param[out] output Destination tensor. Data types supported: same as @p input
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 */
99 void configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output);
100
101 // Inherited methods overridden:
102 void run(const Window &window, cl::CommandQueue &queue) override;
103
104private:
105 const ICLTensor *_input;
106 const ICLTensor *_sum;
107 ICLTensor *_output;
108};
Gian Marco Iodicef670a0a2017-09-18 12:20:45 +0100109} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110#endif /*__ARM_COMPUTE_CLSOFTMAXLAYERKERNEL_H__ */