99bb351bcdbfd915b8631348640fcb1bb19a1bd7
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / arm_compute / core / NEON / kernels / NEOneHotKernel.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Copyright (c) 2019 Arm Limited.
19  *
20  * SPDX-License-Identifier: MIT
21  *
22  * Permission is hereby granted, free of charge, to any person obtaining a copy
23  * of this software and associated documentation files (the "Software"), to
24  * deal in the Software without restriction, including without limitation the
25  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26  * sell copies of the Software, and to permit persons to whom the Software is
27  * furnished to do so, subject to the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be included in all
30  * copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38  * SOFTWARE.
39  */
40 #ifndef __ARM_COMPUTE_NEONEHOTKERNEL_H__
41 #define __ARM_COMPUTE_NEONEHOTKERNEL_H__
42 #include "arm_compute/core/NEON/INEKernel.h"
43 #include "arm_compute/core/Types.h"
44 namespace arm_compute
45 {
46 // Forward declarations
47 class ITensor;
48 /** Kernel to perform other operation on NEON */
49 class NEOneHotKernel : public INEKernel
50 {
51 public:
52   /** Default constructor. */
53   NEOneHotKernel();
54   /** Prevent instances of this class from being copied (As this class contains pointers). */
55   NEOneHotKernel(const NEOneHotKernel &) = delete;
56   /** Prevent instances of this class from being copied (As this class contains pointers). */
57   NEOneHotKernel &operator=(const NEOneHotKernel &) = delete;
58   /** Allow instances of this class to be moved. */
59   NEOneHotKernel(NEOneHotKernel &&) = default;
60   /** Allow instances of this class to be moved. */
61   NEOneHotKernel &operator=(NEOneHotKernel &&) = default;
62   /** Default detructor */
63   ~NEOneHotKernel() = default;
64   /** Name of the kernel
65    *
66    * @return Kernel name
67    */
68   const char *name() const override { return "NEOneHotKernel"; }
69   /** Initialise the kernel's inputs and outputs
70    *
71  * @param[in]  indices   Indices tensor. Supported tensor rank: up to 3. Must be one of the
72  * following types: U32/S32
73  * @param[in]  depth     The tensor for depth of the one hot dimension. Supported tensor rank: up to
74  * 3. Must be one of the following types: U32/S32
75  * @param[in]  on_value  On value tensor. Supported tensor rank: only 1. Data type supported:
76  * U8/S8/U16/S16/F16/U32/S32/F32
77  * @param[in]  off_value Off value tensor. Supported tensor rank: only 1. Data type supported: Same
78  * as @p on_value
79  * @param[out] output    Destination tensor. Data type supported: Same as @p on_value
80  * @param[in]  axis      (Optional) The axis to fill. Negative values wrap around. Defaults to -1.
81  * The value must be in range [-indices.rank , indices.rank)
82    */
83   void configure(const ITensor *indices, const ITensor *depth, const ITensor *on_value,
84                  const ITensor *off_value, ITensor *output, int axis = -1);
85   /** Static function to check if given info will lead to a valid configuration of @ref
86  * NEOneHotKernel
87    *
88  * @param[in]  indices   Indices tensor info. Supported tensor rank: up to 3. Must be one of the
89  * following types: U32/S32
90  * @param[in]  depth     The tensor info for depth of the one hot dimension. Supported tensor rank:
91  * up to 3. Must be one of the following types: U32/S32
92  * @param[in]  on_value  On value tensor info. Supported tensor rank: only 1. Data type supported:
93  * U8/S8/U16/S16/F16/U32/S32/F32
94  * @param[in]  off_value Off value tensor info. Supported tensor rank: only 1. Data type supported:
95  * Same as @p on_value
96  * @param[out] output    Destination tensor info. Data type supported: Same as @p on_value
97  * @param[in]  axis      (Optional) The axis to fill. Negative values wrap around. Defaults to -1.
98  * The value must be in range [-indices.rank , indices.rank)
99    *
100    * @return a status
101    */
102   static Status validate(const ITensorInfo *indices, const ITensorInfo *depth,
103                          const ITensorInfo *on_value, const ITensorInfo *off_value,
104                          const ITensorInfo *output, int axis = -1);
105   // Inherited methods overridden:
106   void run(const Window &window, const ThreadInfo &info) override;
107
108 private:
109   /** Implementation of the onehot operation for 0 axis.
110    *
111    * For onehot on the 0 axis an element by element copy is performed.
112    *
113    * @param[in] window Region on which to execute the kernel. (Must be a region of the window
114    * returned by window())
115    * @param[in] info   Info about executing thread and CPU.
116    */
117   template <typename U> void onehot_0_axis(const Window &window, const ThreadInfo &info);
118   /** Implementation of the onehot operation.
119    *
120    * For 1<=axis a row-wise copy is taking place.
121    *
122    * @param[in] window Region on which to execute the kernel. (Must be a region of the window
123    * returned by window())
124    * @param[in] info   Info about executing thread and CPU.
125    */
126   template <typename U> void onehot_n_axis(const Window &window, const ThreadInfo &info);
127   using kernel_ptr = void (NEOneHotKernel::*)(const Window &window, const ThreadInfo &info);
128   const ITensor *_indices;
129   const ITensor *_depth;
130   const ITensor *_on_value;
131   const ITensor *_off_value;
132   int _axis;
133   ITensor *_output;
134   kernel_ptr _func;
135 };
136 } // namespace arm_compute
137 #endif /* __ARM_COMPUTE_NEONEHOTKERNEL_H__ */