2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Copyright (c) 2017 ARM Limited.
20 * SPDX-License-Identifier: MIT
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:
29 * The above copyright notice and this permission notice shall be included in all
30 * copies or substantial portions of the Software.
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
41 #include "arm_compute/core/CL/kernels/CLHashtableLookupKernel.h"
43 #include "arm_compute/core/CL/CLHelpers.h"
44 #include "arm_compute/core/CL/CLKernelLibraryEx.h"
45 #include "arm_compute/core/CL/ICLTensor.h"
46 #include "support/StringSupport.h"
48 using namespace arm_compute;
52 constexpr unsigned int num_elems_processed_per_iteration = 16;
54 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
56 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
57 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
58 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
60 bool window_changed = update_window_and_padding(win, input_access, output_access);
61 input_access.set_valid_region(win, output->valid_region());
63 Status err = (window_changed)
64 ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!")
66 return std::make_pair(err, win);
70 CLHashtableLookupKernel::CLHashtableLookupKernel()
75 Status CLHashtableLookupKernel::validate(const ITensorInfo *lookups, const ITensorInfo *keys,
76 const ITensorInfo *input, const ITensorInfo *output,
77 const ITensorInfo *hits)
79 ARM_COMPUTE_ERROR_ON_NULLPTR(lookups, keys, input, output, hits);
80 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
81 input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16,
82 DataType::U32, DataType::S32, DataType::F16, DataType::F32);
83 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lookups, 1, DataType::S32);
84 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(keys, 1, DataType::S32);
85 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(hits, 1, DataType::U8, DataType::QASYMM8);
86 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
88 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->tensor_shape().total_size() == 0,
89 "Output's shape was not set");
91 ARM_COMPUTE_ERROR_ON(lookups->dimension(0) != hits->dimension(0) ||
92 output->dimension(output->num_dimensions() - 1) != lookups->dimension(0));
93 ARM_COMPUTE_ERROR_ON(input->num_dimensions() < 2 && input->num_dimensions() > 4);
94 ARM_COMPUTE_ERROR_ON(lookups->num_dimensions() > 1);
95 ARM_COMPUTE_ERROR_ON(keys->num_dimensions() > 1);
96 ARM_COMPUTE_ERROR_ON(hits->num_dimensions() > 1);
101 void CLHashtableLookupKernel::configure(const ICLTensor *lookups, const ICLTensor *keys,
102 const ICLTensor *input, ICLTensor *output, ICLTensor *hits)
104 ARM_COMPUTE_ERROR_THROW_ON(
105 validate(lookups->info(), keys->info(), input->info(), output->info(), hits->info()));
113 // Make _lookup_indices tensor
114 _lookup_indices = support::cpp14::make_unique<CLTensor>();
115 _lookup_indices->allocator()->init(
116 TensorInfo(lookups->info()->tensor_shape(), lookups->info()->num_channels(), DataType::S32));
117 _lookup_indices->allocator()->allocate();
119 // Set kernel build options
120 std::stringstream kernel_name;
121 std::set<std::string> build_opts;
122 kernel_name << "hashtable_lookup";
124 build_opts.emplace("-DDEPTH_OUT=" + support::cpp11::to_string(output->info()->dimension(2)));
125 build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
126 build_opts.emplace("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
127 build_opts.emplace("-DNUM_DIMS=" + support::cpp11::to_string(_input->info()->num_dimensions()));
130 _kernel = static_cast<cl::Kernel>(
131 CLKernelLibraryEx::get().create_kernel(kernel_name.str(), build_opts));
133 // Configure kernel window
134 auto win_config = validate_and_configure_window(input->info(), output->info());
135 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
136 ICLKernel::configure_internal(win_config.second);
139 void CLHashtableLookupKernel::run(const Window &window, cl::CommandQueue &queue)
141 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
142 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
144 const_cast<ICLTensor *>(_lookups)->map(queue);
145 const_cast<ICLTensor *>(_keys)->map(queue);
147 _lookup_indices->map(queue);
149 // Set values of hits
150 const int32_t *lookups_buf =
151 reinterpret_cast<int32_t *>(const_cast<ICLTensor *>(_lookups)->buffer());
152 const int32_t *keys_buf = reinterpret_cast<int32_t *>(const_cast<ICLTensor *>(_keys)->buffer());
153 uint8_t *hits_buf = reinterpret_cast<uint8_t *>(_hits->buffer());
154 int32_t *lookup_indices_buf = reinterpret_cast<int32_t *>(_lookup_indices->buffer());
156 std::map<int32_t, size_t> key_map;
157 const size_t keys_num = _keys->info()->dimension(0);
158 for (size_t key_index = 0; key_index < keys_num; key_index++)
160 key_map[keys_buf[key_index]] = key_index;
163 const size_t lookups_num = _lookups->info()->dimension(0);
164 for (size_t i = 0; i < lookups_num; ++i)
166 const auto lookup_value = lookups_buf[i];
167 const auto it = key_map.find(lookup_value);
168 if (it != key_map.end())
170 #if defined(ARM_COMPUTE_DEBUG_ENABLED)
171 if (it->second >= lookups_num)
172 ARM_COMPUTE_ERROR("HashTable Lookup: index out of bounds.");
173 #endif // defined(ARM_COMPUTE_DEBUG_ENABLED)
174 lookup_indices_buf[i] = static_cast<int32_t>(it->second);
175 hits_buf[i] = static_cast<uint8_t>(1);
179 lookup_indices_buf[i] = -1;
180 hits_buf[i] = static_cast<uint8_t>(0);
184 const_cast<ICLTensor *>(_lookups)->unmap(queue);
185 const_cast<ICLTensor *>(_keys)->unmap(queue);
187 _lookup_indices->unmap(queue);
189 Window win = window.collapse(ICLKernel::window(), 2, 4);
192 win_lookup.set(Window::DimX, Window::Dimension(0, 0, 0));
196 unsigned int idx = 0;
197 add_4D_tensor_argument(idx, _input, win);
198 add_4D_tensor_argument(idx, _output, win);
199 add_1D_tensor_argument(idx, _lookup_indices.get(), win_lookup);
201 enqueue(queue, *this, win);
202 } while (window.slide_window_slice_4D(win) && window.slide_window_slice_1D(win_lookup));