ONE - On-device Neural Engine
Loading...
Searching...
No Matches
arm_compute::CLHashtableLookupKernel Class Reference

Class to perform HashtableLookup operation with opencl kernel. More...

#include <CLHashtableLookupKernel.h>

Collaboration diagram for arm_compute::CLHashtableLookupKernel:

Public Member Functions

 CLHashtableLookupKernel ()
 Construct a CLHashtableLookupKernel object.
 
 CLHashtableLookupKernel (const CLHashtableLookupKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers)
 
CLHashtableLookupKerneloperator= (const CLHashtableLookupKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers)
 
 CLHashtableLookupKernel (CLHashtableLookupKernel &&)=default
 Construct a CLHashtableLookupKernel object by using default move constructor.
 
CLHashtableLookupKerneloperator= (CLHashtableLookupKernel &&)=default
 Move assignment operator.
 
 ~CLHashtableLookupKernel ()=default
 Destruct this object.
 
void configure (const ICLTensor *lookups, const ICLTensor *keys, const ICLTensor *input, ICLTensor *output, ICLTensor *hits)
 Set the input and output of the kernel.
 
void run (const Window &window, cl::CommandQueue &queue) override
 Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.
 

Static Public Member Functions

static Status validate (const ITensorInfo *lookups, const ITensorInfo *keys, const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *hits)
 Static function to check if given info will lead to a valid configuration of CLHashtableLookupKernel.
 

Detailed Description

Class to perform HashtableLookup operation with opencl kernel.

Definition at line 60 of file CLHashtableLookupKernel.h.

Constructor & Destructor Documentation

◆ CLHashtableLookupKernel() [1/3]

CLHashtableLookupKernel::CLHashtableLookupKernel ( )

Construct a CLHashtableLookupKernel object.

Definition at line 71 of file CLHashtableLookupKernel.cpp.

72{
73 // DO NOTHING
74}

◆ CLHashtableLookupKernel() [2/3]

arm_compute::CLHashtableLookupKernel::CLHashtableLookupKernel ( const CLHashtableLookupKernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ CLHashtableLookupKernel() [3/3]

arm_compute::CLHashtableLookupKernel::CLHashtableLookupKernel ( CLHashtableLookupKernel &&  )
default

Construct a CLHashtableLookupKernel object by using default move constructor.

Parameters
[in]CLHashtableLookupKernelobject to move

◆ ~CLHashtableLookupKernel()

arm_compute::CLHashtableLookupKernel::~CLHashtableLookupKernel ( )
default

Destruct this object.

References validate().

Member Function Documentation

◆ configure()

void CLHashtableLookupKernel::configure ( const ICLTensor *  lookups,
const ICLTensor *  keys,
const ICLTensor *  input,
ICLTensor *  output,
ICLTensor *  hits 
)

Set the input and output of the kernel.

Parameters
[in]lookupsLookups 1D tensor that values are indices into the first dimension of input.
[in]keysKeys 1D tensor. keys and input pair represent a map. Data types supported: S32
[in]inputSource tensor. Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32
[out]outputDestination tensor. Data types and data layouts supported: Same as input.
[out]hitsHits 1D tensor. A boolean tensor that indicates whether the lookup hits (True) or not (False). Data types supported: U8/QASYMM8
Returns
N/A

Definition at line 102 of file CLHashtableLookupKernel.cpp.

104{
105 ARM_COMPUTE_ERROR_THROW_ON(
106 validate(lookups->info(), keys->info(), input->info(), output->info(), hits->info()));
107
108 _lookups = lookups;
109 _keys = keys;
110 _input = input;
111 _output = output;
112 _hits = hits;
113
114 // Make _lookup_indices tensor
115 _lookup_indices = std::make_unique<CLTensor>();
116 _lookup_indices->allocator()->init(
117 TensorInfo(lookups->info()->tensor_shape(), lookups->info()->num_channels(), DataType::S32));
118 _lookup_indices->allocator()->allocate();
119
120 // Set kernel build options
121 std::stringstream kernel_name;
122 std::set<std::string> build_opts;
123 kernel_name << "hashtable_lookup";
124
125 build_opts.emplace("-DDEPTH_OUT=" + support::cpp11::to_string(output->info()->dimension(2)));
126 build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
127 build_opts.emplace("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
128 build_opts.emplace("-DNUM_DIMS=" + support::cpp11::to_string(_input->info()->num_dimensions()));
129
130 // Create kernel
131 _kernel =
132 static_cast<cl::Kernel>(CLKernelLibraryEx::get().create_kernel(kernel_name.str(), build_opts));
133
134 // Configure kernel window
135 auto win_config = validate_and_configure_window(input->info(), output->info());
136 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
137 ICLKernel::configure_internal(win_config.second);
138}
static Status validate(const ITensorInfo *lookups, const ITensorInfo *keys, const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *hits)
Static function to check if given info will lead to a valid configuration of CLHashtableLookupKernel.
static CLKernelLibraryEx & get()
Get the KernelLibrary singleton.
Kernel create_kernel(const std::string &kernel_name, const StringSet &build_options_set={}) const
Create a kernel from the kernel library.

References arm_compute::CLKernelLibraryEx::create_kernel(), arm_compute::CLKernelLibraryEx::get(), and validate().

◆ operator=() [1/2]

CLHashtableLookupKernel & arm_compute::CLHashtableLookupKernel::operator= ( CLHashtableLookupKernel &&  )
default

Move assignment operator.

Parameters
[in]CLHashtableLookupKernelobject to move

◆ operator=() [2/2]

CLHashtableLookupKernel & arm_compute::CLHashtableLookupKernel::operator= ( const CLHashtableLookupKernel )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ run()

void CLHashtableLookupKernel::run ( const Window &  window,
cl::CommandQueue &  queue 
)
override

Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue.

Note
The queue is not flushed by this method, and therefore the kernel will not have been executed by the time this method returns.
Parameters
[in]windowRegion on which to execute the kernel. (Must be a valid region of the window returned by window()).
[in,out]queueCommand queue on which to enqueue the kernel.
Returns
N/A
N/A

Definition at line 140 of file CLHashtableLookupKernel.cpp.

141{
142 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
143 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
144
145 const_cast<ICLTensor *>(_lookups)->map(queue);
146 const_cast<ICLTensor *>(_keys)->map(queue);
147 _hits->map(queue);
148 _lookup_indices->map(queue);
149
150 // Set values of hits
151 const int32_t *lookups_buf =
152 reinterpret_cast<int32_t *>(const_cast<ICLTensor *>(_lookups)->buffer());
153 const int32_t *keys_buf = reinterpret_cast<int32_t *>(const_cast<ICLTensor *>(_keys)->buffer());
154 uint8_t *hits_buf = reinterpret_cast<uint8_t *>(_hits->buffer());
155 int32_t *lookup_indices_buf = reinterpret_cast<int32_t *>(_lookup_indices->buffer());
156
157 std::map<int32_t, size_t> key_map;
158 const size_t keys_num = _keys->info()->dimension(0);
159 for (size_t key_index = 0; key_index < keys_num; key_index++)
160 {
161 key_map[keys_buf[key_index]] = key_index;
162 }
163
164 const size_t lookups_num = _lookups->info()->dimension(0);
165 for (size_t i = 0; i < lookups_num; ++i)
166 {
167 const auto lookup_value = lookups_buf[i];
168 const auto it = key_map.find(lookup_value);
169 if (it != key_map.end())
170 {
171#if defined(ARM_COMPUTE_DEBUG_ENABLED)
172 if (it->second >= lookups_num)
173 ARM_COMPUTE_ERROR("HashTable Lookup: index out of bounds.");
174#endif // defined(ARM_COMPUTE_DEBUG_ENABLED)
175 lookup_indices_buf[i] = static_cast<int32_t>(it->second);
176 hits_buf[i] = static_cast<uint8_t>(1);
177 }
178 else
179 {
180 lookup_indices_buf[i] = -1;
181 hits_buf[i] = static_cast<uint8_t>(0);
182 }
183 }
184
185 const_cast<ICLTensor *>(_lookups)->unmap(queue);
186 const_cast<ICLTensor *>(_keys)->unmap(queue);
187 _hits->unmap(queue);
188 _lookup_indices->unmap(queue);
189
190 Window win = window.collapse(ICLKernel::window(), 2, 4);
191
192 Window win_lookup;
193 win_lookup.set(Window::DimX, Window::Dimension(0, 0, 0));
194
195 do
196 {
197 unsigned int idx = 0;
198 add_4D_tensor_argument(idx, _input, win);
199 add_4D_tensor_argument(idx, _output, win);
200 add_1D_tensor_argument(idx, _lookup_indices.get(), win_lookup);
201
202 enqueue(queue, *this, win);
203 } while (window.slide_window_slice_4D(win) && window.slide_window_slice_1D(win_lookup));
204}

Referenced by package.infer.session::inference().

◆ validate()

Status CLHashtableLookupKernel::validate ( const ITensorInfo *  lookups,
const ITensorInfo *  keys,
const ITensorInfo *  input,
const ITensorInfo *  output,
const ITensorInfo *  hits 
)
static

Static function to check if given info will lead to a valid configuration of CLHashtableLookupKernel.

Parameters
[in]lookupsThe lookups tensor info. Data types supported: S32.
[in]keysThe keys tensor info. keys and input pair represent a map. Data types supported: S32
[in]inputThe input tensor info. Data types supported: U8/S8/QASYMM8/U16/S16/U32/S32/F16/F32
[out]outputThe output tensor. Data types and data layouts supported: Same as input.
[out]hitsThe hits tensor info. A boolean tensor that indicates whether the lookup hits (True) or not (False). Data types supported: U8/QASYMM8
Returns
a status

Definition at line 76 of file CLHashtableLookupKernel.cpp.

79{
80 ARM_COMPUTE_ERROR_ON_NULLPTR(lookups, keys, input, output, hits);
81 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
82 input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16,
83 DataType::U32, DataType::S32, DataType::F16, DataType::F32);
84 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lookups, 1, DataType::S32);
85 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(keys, 1, DataType::S32);
86 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(hits, 1, DataType::U8, DataType::QASYMM8);
87 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
88
89 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->tensor_shape().total_size() == 0,
90 "Output's shape was not set");
91
92 ARM_COMPUTE_ERROR_ON(lookups->dimension(0) != hits->dimension(0) ||
93 output->dimension(output->num_dimensions() - 1) != lookups->dimension(0));
94 ARM_COMPUTE_ERROR_ON(input->num_dimensions() < 2 && input->num_dimensions() > 4);
95 ARM_COMPUTE_ERROR_ON(lookups->num_dimensions() > 1);
96 ARM_COMPUTE_ERROR_ON(keys->num_dimensions() > 1);
97 ARM_COMPUTE_ERROR_ON(hits->num_dimensions() > 1);
98
99 return Status{};
100}

Referenced by configure(), and ~CLHashtableLookupKernel().


The documentation for this class was generated from the following files: