blob: e80cd222c56616a5dd043bbc8c84179ae477f85c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H
25#define ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/NEON/INEKernel.h"
28#include "arm_compute/core/NEON/INESimpleKernel.h"
29
30namespace arm_compute
31{
32class ITensor;
33
34/** Interface for the identifying the max value of 1D Logits */
35class NELogits1DMaxKernel : public INESimpleKernel
36{
37public:
Anthony Barbiere8a49832018-01-18 10:04:05 +000038 const char *name() const override
39 {
40 return "NELogits1DMaxKernel";
41 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042 /** Default constructor */
43 NELogits1DMaxKernel();
44 /** Set the input and output tensors.
45 *
Sheri Zhang1f567af2020-05-05 11:47:36 +010046 * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 * @param[out] output Destination tensor. Data types supported: same as @p input
48 */
49 void configure(const ITensor *input, ITensor *output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +000050 /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DMaxKernel
51 *
Sheri Zhang1f567af2020-05-05 11:47:36 +010052 * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Michalis Spyrouafa5d812017-11-30 14:25:57 +000053 * @param[in] output Destination tensor. Data types supported: same as @p input
54 *
55 * @return a status
56 */
57 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058
59 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +010060 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 BorderSize border_size() const override;
62
63private:
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000064 using Logits1DMaxFunction = void(const ITensor &in, ITensor &out, const Window &window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065
66private:
67 Logits1DMaxFunction *_func;
68 BorderSize _border_size;
69};
70
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000071/** Interface for softmax computation for QASYMM8 with pre-computed max. */
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010072template <bool IS_LOG = false>
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000073class NELogits1DSoftmaxKernel : public INEKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074{
75public:
Anthony Barbiere8a49832018-01-18 10:04:05 +000076 const char *name() const override
77 {
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010078 if(IS_LOG)
79 {
80 return "NELogits1DSoftmaxKernel";
81 }
82 else
83 {
84 return "NELogits1DLogSoftmaxKernel";
85 }
Anthony Barbiere8a49832018-01-18 10:04:05 +000086 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 /** Default constructor */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000088 NELogits1DSoftmaxKernel();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 /** Prevent instances of this class from being copied (As this class contains pointers) */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000090 NELogits1DSoftmaxKernel(const NELogits1DSoftmaxKernel &) = delete;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 /** Prevent instances of this class from being copied (As this class contains pointers) */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000092 NELogits1DSoftmaxKernel &operator=(const NELogits1DSoftmaxKernel &) = delete;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 /** Allow instances of this class to be moved */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000094 NELogits1DSoftmaxKernel(NELogits1DSoftmaxKernel &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 /** Allow instances of this class to be moved */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000096 NELogits1DSoftmaxKernel &operator=(NELogits1DSoftmaxKernel &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 /** Default destructor */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000098 ~NELogits1DSoftmaxKernel() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 /** Set the input and output tensors.
100 *
Sheri Zhang1f567af2020-05-05 11:47:36 +0100101 * @param[in] input Source tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000102 * @param[in] max Max values tensor. Same shape as input with dimension 0 set to 1.
103 * Data types supported: same as @p input.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 * @param[out] output Destination tensor. Data types supported: same as @p input.
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000105 * @param[in] beta A scaling factor for the exponent.
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000106 *
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000107 * @param tmp Auxiliary tensor. Must be type F32 and same shape as the input.
108 */
109 void configure(const ITensor *input, const ITensor *max, ITensor *output, const float beta, ITensor *tmp);
110 /** Static function to check if given info will lead to a valid configuration of @ref NELogits1DSoftmaxKernel
111 *
Sheri Zhang1f567af2020-05-05 11:47:36 +0100112 * @param[in] input Source tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000113 * @param[in] max Max values tensor info. Same shape as input with dimension 0 set to 1.
114 * Data types supported: same as @p input.
115 * @param[in] output Destination tensor info. Data types supported: same as @p input.
116 * @param[in] beta A scaling factor for the exponent.
117 * @param[in] tmp Tensor info of auxiliary. Must be type F32 and same shape as the input.
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000118 *
119 * @return a status
120 */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000121 static Status validate(const ITensorInfo *input, const ITensorInfo *max,
122 const ITensorInfo *output, const float beta, const ITensorInfo *tmp);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123
124 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100125 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126
127private:
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000128 using LogitsSoftmaxFunction = void(const ITensor &in, const ITensor &max, void *const tmp, ITensor &out, const float beta,
129 const Window &window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000131 LogitsSoftmaxFunction *_func;
132 const ITensor *_input;
133 const ITensor *_max;
134 ITensor *_output;
135 float _beta;
136 ITensor *_tmp; //Temporary. Used internally
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137};
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100138} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000139#endif /*ARM_COMPUTE_NESOFTMAXLAYERKERNEL_H */