blob: 0806974ad656f38727d3979786817f4517506927 [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
27#include "arm_compute/core/CL/ICLSimple2DKernel.h"
28
29namespace arm_compute
30{
31class ICLTensor;
32
33/** Interface for the identifying the max value of 1D Logits */
34class CLLogits1DMaxKernel : public ICLSimple2DKernel
35{
36public:
37 /** Set the input and output tensors.
38 *
39 * @param[in] input Source tensor. Data types supported: F16, F32. Number of channels must be 1.
40 * @param[out] output Destination tensor. Matching input type and channel number.
41 */
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 *
61 * @param[in] input Source tensor. Data types supported: F16, F32. Number of channels must be 1.
62 * @param[in] max Max values tensor. Matching input type and channel number.
63 * @param[out] output Destination tensor. Matching input type and channel number.
64 * @param[out] sum Sum of 1D logits tensor. Matching input type and channel number.
65 */
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 *
94 * @param[in] input Source tensor. Data types supported: F16, F32. Number of channels must be 1.
95 * @param[in] sum Sum tensor. Dimensions should be dim(input)-1. Matching input type and channel number.
96 * @param[out] output Destination tensor. Matching input type and channel number.
97 */
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__ */