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/CLEmbeddingLookupKernel.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 CLEmbeddingLookupKernel::CLEmbeddingLookupKernel()
71 : _input(nullptr), _output(nullptr), _lookups(nullptr)
75 Status CLEmbeddingLookupKernel::validate(const ITensorInfo *input, const ITensorInfo *output,
76 const ITensorInfo *lookups)
78 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, lookups);
79 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(
80 input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16,
81 DataType::U32, DataType::S32, DataType::F16, DataType::F32);
82 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(lookups, 1, DataType::S32);
83 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
85 ARM_COMPUTE_ERROR_ON(input->num_dimensions() < 2 && input->num_dimensions() > 4);
86 ARM_COMPUTE_ERROR_ON(lookups->num_dimensions() > 1);
91 void CLEmbeddingLookupKernel::configure(const ICLTensor *input, ICLTensor *output,
92 const ICLTensor *lookups)
94 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), lookups->info()));
100 // Set kernel build options
101 std::stringstream kernel_name;
102 std::set<std::string> build_opts;
103 kernel_name << "embedding_lookup";
105 build_opts.emplace("-DDEPTH_OUT=" + support::cpp11::to_string(output->info()->dimension(2)));
106 build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
107 build_opts.emplace("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
108 build_opts.emplace("-DNUM_DIMS=" + support::cpp11::to_string(_input->info()->num_dimensions()));
112 static_cast<cl::Kernel>(CLKernelLibraryEx::get().create_kernel(kernel_name.str(), build_opts));
114 // Configure kernel window
115 auto win_config = validate_and_configure_window(input->info(), output->info());
116 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
117 ICLKernel::configure_internal(win_config.second);
120 void CLEmbeddingLookupKernel::run(const Window &window, cl::CommandQueue &queue)
122 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
123 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
125 Window slice_in = window.first_slice_window_4D().collapse(ICLKernel::window(), 2, 4);
128 win_lookup.set(Window::DimX, Window::Dimension(0, 0, 0));
132 unsigned int idx = 0;
133 add_4D_tensor_argument(idx, _input, slice_in);
134 add_4D_tensor_argument(idx, _output, slice_in);
135 add_1D_tensor_argument(idx, _lookups, win_lookup);
137 enqueue(queue, *this, slice_in);
138 } while (window.slide_window_slice_4D(slice_in) && window.slide_window_slice_1D(win_lookup));