Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLSimple3DKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | class ICLTensor; |
| 32 | |
| 33 | /** Interface for the identifying the max value of 1D Logits */ |
steniu01 | 0d523cc | 2017-07-13 14:24:23 +0100 | [diff] [blame] | 34 | class CLLogits1DMaxKernel : public ICLSimple3DKernel |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | { |
| 36 | public: |
| 37 | /** Set the input and output tensors. |
| 38 | * |
Georgios Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame] | 39 | * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32 |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 40 | * @param[out] output Destination tensor. Data types supported: same as @p input |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 */ |
| 46 | class CLLogits1DShiftExpSumKernel : public ICLKernel |
| 47 | { |
| 48 | public: |
| 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 Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame] | 61 | * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32 |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 62 | * @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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
| 71 | private: |
| 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. */ |
| 79 | class CLLogits1DNormKernel : public ICLKernel |
| 80 | { |
| 81 | public: |
| 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 Pinitas | 0979675 | 2017-07-10 16:05:21 +0100 | [diff] [blame] | 94 | * @param[in] input Source tensor. Data types supported: QS8/QS16/F16/F32 |
Georgios Pinitas | e5f8fd6 | 2017-06-23 18:03:44 +0100 | [diff] [blame] | 95 | * @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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
| 103 | private: |
| 104 | const ICLTensor *_input; |
| 105 | const ICLTensor *_sum; |
| 106 | ICLTensor *_output; |
| 107 | }; |
| 108 | } |
| 109 | #endif /*__ARM_COMPUTE_CLSOFTMAXLAYERKERNEL_H__ */ |