99cfa61ecd8c5d5643fb876880b42f869bfd6dfa
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / arm_compute / core / CL / kernels / CLHashtableLookupKernel.h
1 /*
2  * Copyright (c) 2018 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) 2016-2018 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
41 /**
42  * @file      CLHashtableLookupKernel.h
43  * @ingroup   COM_AI_RUNTIME
44  * @brief     This file defines CLHashtableLookupKernel class
45  */
46
47 #ifndef __ARM_COMPUTE_CLHASHTABLELOOKUPKERNEL_H__
48 #define __ARM_COMPUTE_CLHASHTABLELOOKUPKERNEL_H__
49
50 #include "arm_compute/core/CL/ICLKernel.h"
51 #include "arm_compute/runtime/CL/CLTensor.h"
52
53 namespace arm_compute
54 {
55 class ICLTensor;
56
57 /**
58 * @brief Class to perform HashtableLookup operation with opencl kernel
59 */
60 class CLHashtableLookupKernel : public ICLKernel
61 {
62 public:
63   /**
64    * @brief Construct a CLHashtableLookupKernel object
65    * */
66   CLHashtableLookupKernel();
67
68   /**
69    * @brief Prevent instances of this class from being copied (As this class contains pointers)
70    * */
71   CLHashtableLookupKernel(const CLHashtableLookupKernel &) = delete;
72
73   /**
74    * @brief Prevent instances of this class from being copied (As this class contains pointers)
75    * */
76   CLHashtableLookupKernel &operator=(const CLHashtableLookupKernel &) = delete;
77
78   /**
79    * @brief Construct a CLHashtableLookupKernel object by using default move constructor
80    * @param[in] CLHashtableLookupKernel object to move
81    * */
82   CLHashtableLookupKernel(CLHashtableLookupKernel &&) = default;
83
84   /**
85    * @brief Move assignment operator
86    * @param[in] CLHashtableLookupKernel object to move
87    * */
88   CLHashtableLookupKernel &operator=(CLHashtableLookupKernel &&) = default;
89
90   /**
91    * @brief Destruct this object
92    * */
93   ~CLHashtableLookupKernel() = default;
94
95   /**
96    * @brief Set the input and output of the kernel
97    * @param[in]  lookups  Lookups 1D tensor that values are indices into the first dimension of
98    *                      input.
99    * @param[in]  keys     Keys 1D tensor. keys and input pair represent a map.
100    *                      Data types supported: S32
101    * @param[in]  input    Source tensor.
102    *                      Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32
103    * @param[out] output   Destination tensor. Data types and data layouts supported: Same as @p
104    *                      input.
105    * @param[out] hits     Hits 1D tensor. A boolean tensor that indicates whether the lookup hits
106    *                      (True) or not (False). Data types supported: U8/QASYMM8
107    * @return N/A
108    */
109   void configure(const ICLTensor *lookups, const ICLTensor *keys, const ICLTensor *input,
110                  ICLTensor *output, ICLTensor *hits);
111
112   /**
113    * @brief Static function to check if given info will lead to a valid configuration of @ref
114    *        CLHashtableLookupKernel
115    * @param[in]  lookups  The lookups tensor info. Data types supported: S32.
116    * @param[in]  keys     The keys tensor info. keys and input pair represent a map.
117    *                      Data types supported: S32
118    * @param[in]  input    The input tensor info.
119    *                      Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32
120    * @param[out] output   The output tensor. Data types and data layouts supported: Same as @p
121    *                      input.
122    * @param[out] hits     The hits tensor info. A boolean tensor that indicates whether the lookup
123    *                      hits
124    *                      (True) or not (False). Data types supported: U8/QASYMM8
125    * @return a status
126    */
127   static Status validate(const ITensorInfo *lookups, const ITensorInfo *keys,
128                          const ITensorInfo *input, const ITensorInfo *output,
129                          const ITensorInfo *hits);
130
131   /**
132    * @brief Enqueue the OpenCL kernel to process the given window on the passed OpenCL command
133    *        queue.
134    * @note  The queue is *not* flushed by this method, and therefore the kernel will not have
135    *        been executed by the time this method returns.
136    * @param[in]     window  Region on which to execute the kernel. (Must be a valid region of
137    *                        the window returned by window()).
138    * @param[in,out] queue   Command queue on which to enqueue the kernel.@return N/A
139    * @return N/A
140    */
141   void run(const Window &window, cl::CommandQueue &queue) override;
142
143 private:
144   const ICLTensor *_lookups{nullptr};                 /** Lookups tensor */
145   const ICLTensor *_keys{nullptr};                    /** Keys tensor */
146   const ICLTensor *_input{nullptr};                   /** Source tensor */
147   ICLTensor *_output{nullptr};                        /** Destination tensor */
148   ICLTensor *_hits{nullptr};                          /** Hits tensor */
149   std::unique_ptr<CLTensor> _lookup_indices{nullptr}; /** Lookup indices tensor */
150 };
151 } // namespace arm_compute
152 #endif /*__ARM_COMPUTE_CLHASHTABLELOOKUPKERNEL_H__ */