blob: 5c0d05f5861c6064363f2a8ff6cef2d7f9508063 [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
63 * @param[out] output Destination tensor. Data types supported: same as @p input
64 * @param[out] sum Sum of 1D logits tensor. Data types supported: same as @p input
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065 */
66 void configure(const ICLTensor *input, const ICLTensor *max, ICLTensor *output, ICLTensor *sum);
67
68 // Inherited methods overridden:
69 void run(const Window &window, cl::CommandQueue &queue) override;
70
71private:
72 const ICLTensor *_input;
73 const ICLTensor *_max;
74 ICLTensor *_output;
75 ICLTensor *_sum;
76};
77
78/** 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. */
79class CLLogits1DNormKernel : public ICLKernel
80{
81public:
82 /** Default constructor */
83 CLLogits1DNormKernel();
84 /** Prevent instances of this class from being copied (As this class contains pointers) */
85 CLLogits1DNormKernel(const CLLogits1DNormKernel &) = delete;
86 /** Prevent instances of this class from being copied (As this class contains pointers) */
87 CLLogits1DNormKernel &operator=(const CLLogits1DNormKernel &) = delete;
88 /** Allow instances of this class to be moved */
89 CLLogits1DNormKernel(CLLogits1DNormKernel &&) = default;
90 /** Allow instances of this class to be moved */
91 CLLogits1DNormKernel &operator=(CLLogits1DNormKernel &&) = default;
92 /** Set the input and output tensors.
93 *
Georgios Pinitas09796752017-07-10 16:05:21 +010094 * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32
Georgios Pinitase5f8fd62017-06-23 18:03:44 +010095 * @param[in] sum Sum tensor. Dimensions should be dim(input)-1. Data types supported: same as @p input
96 * @param[out] output Destination tensor. Data types supported: same as @p input
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 */
98 void configure(const ICLTensor *input, const ICLTensor *sum, ICLTensor *output);
99
100 // Inherited methods overridden:
101 void run(const Window &window, cl::CommandQueue &queue) override;
102
103private:
104 const ICLTensor *_input;
105 const ICLTensor *_sum;
106 ICLTensor *_output;
107};
108}
109#endif /*__ARM_COMPUTE_CLSOFTMAXLAYERKERNEL_H__ */