2 * Copyright (c) 2019 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) 2018-2019 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/NEON/kernels/NEEmbeddingLookupKernel.h"
43 #include "arm_compute/core/Error.h"
44 #include "arm_compute/core/Helpers.h"
45 #include "arm_compute/core/ITensor.h"
46 #include "arm_compute/core/TensorInfo.h"
47 #include "arm_compute/core/Validate.h"
48 #include "arm_compute/core/Window.h"
50 using namespace arm_compute;
52 NEEmbeddingLookupKernel::NEEmbeddingLookupKernel()
53 : _input(nullptr), _lookups(nullptr), _output(nullptr)
57 void NEEmbeddingLookupKernel::configure(const ITensor *input, ITensor *output,
58 const ITensor *lookups)
60 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
61 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), lookups->info()));
67 // Auto initialize output if not initialized
68 auto out_shape = input->info()->tensor_shape();
69 out_shape.set(out_shape.num_dimensions() - 1, lookups->info()->num_dimensions());
70 auto_init_if_empty(*output->info(), out_shape, 1, input->info()->data_type(),
71 input->info()->quantization_info());
73 INEKernel::configure(calculate_max_window(*output->info()));
76 Status NEEmbeddingLookupKernel::validate(const arm_compute::ITensorInfo *input,
77 const arm_compute::ITensorInfo *output,
78 const arm_compute::ITensorInfo *lookups)
80 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, lookups);
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);
86 ARM_COMPUTE_ERROR_ON(input->num_dimensions() < 2 && input->num_dimensions() > 4);
87 ARM_COMPUTE_ERROR_ON(lookups->num_dimensions() > 1);
89 // Validate in case of configured output
90 if (output->total_size() > 0)
92 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
93 ARM_COMPUTE_ERROR_ON(input->num_dimensions() != output->num_dimensions());
94 ARM_COMPUTE_ERROR_ON(output->dimension(output->num_dimensions() - 1) != lookups->dimension(0));
95 for (size_t i = 0; i < output->num_dimensions() - 1; ++i)
97 ARM_COMPUTE_ERROR_ON(input->dimension(i) != output->dimension(i));
104 void NEEmbeddingLookupKernel::run(const Window &window, const ThreadInfo &info)
106 ARM_COMPUTE_UNUSED(info);
107 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
108 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
110 const size_t lookup_dim = _output->info()->num_dimensions() - 1;
112 Window output_window{window};
113 output_window.set(Window::DimX,
114 Window::Dimension(output_window.x().start(), output_window.x().end(),
115 _input->info()->dimension(0)));
117 Window out_slice = output_window.first_slice_window_4D();
120 Iterator output_it(_output, out_slice);
124 [&](const Coordinates &id) {
125 const int32_t lookup =
126 *reinterpret_cast<int32_t *>(_lookups->ptr_to_element(Coordinates{id[lookup_dim]}));
127 Coordinates input_id{id};
128 input_id.set(lookup_dim, lookup);
129 memcpy(output_it.ptr(), _input->ptr_to_element(input_id),
130 _output->info()->dimension(0) * _output->info()->element_size());
134 } while (window.slide_window_slice_4D(out_slice));