091d38c56dcf2fd513456681baad1bf5973513bf
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / core / NEON / kernels / NEEmbeddingLookupKernel.cpp
1 /*
2  * Copyright (c) 2019 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) 2018-2019 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 #include "arm_compute/core/NEON/kernels/NEEmbeddingLookupKernel.h"
42
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"
49
50 using namespace arm_compute;
51
52 NEEmbeddingLookupKernel::NEEmbeddingLookupKernel()
53     : _input(nullptr), _lookups(nullptr), _output(nullptr)
54 {
55 }
56
57 void NEEmbeddingLookupKernel::configure(const ITensor *input, ITensor *output,
58                                         const ITensor *lookups)
59 {
60   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
61   ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), lookups->info()));
62
63   _input = input;
64   _output = output;
65   _lookups = lookups;
66
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());
72
73   INEKernel::configure(calculate_max_window(*output->info()));
74 }
75
76 Status NEEmbeddingLookupKernel::validate(const arm_compute::ITensorInfo *input,
77                                          const arm_compute::ITensorInfo *output,
78                                          const arm_compute::ITensorInfo *lookups)
79 {
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);
85
86   ARM_COMPUTE_ERROR_ON(input->num_dimensions() < 2 && input->num_dimensions() > 4);
87   ARM_COMPUTE_ERROR_ON(lookups->num_dimensions() > 1);
88
89   // Validate in case of configured output
90   if (output->total_size() > 0)
91   {
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)
96     {
97       ARM_COMPUTE_ERROR_ON(input->dimension(i) != output->dimension(i));
98     }
99   }
100
101   return Status{};
102 }
103
104 void NEEmbeddingLookupKernel::run(const Window &window, const ThreadInfo &info)
105 {
106   ARM_COMPUTE_UNUSED(info);
107   ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
108   ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
109
110   const size_t lookup_dim = _output->info()->num_dimensions() - 1;
111
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)));
116
117   Window out_slice = output_window.first_slice_window_4D();
118   do
119   {
120     Iterator output_it(_output, out_slice);
121
122     execute_window_loop(out_slice,
123                         [&](const Coordinates &id) {
124                           const int32_t lookup = *reinterpret_cast<int32_t *>(
125                               _lookups->ptr_to_element(Coordinates{id[lookup_dim]}));
126                           Coordinates input_id{id};
127                           input_id.set(lookup_dim, lookup);
128                           memcpy(output_it.ptr(), _input->ptr_to_element(input_id),
129                                  _output->info()->dimension(0) * _output->info()->element_size());
130                         },
131                         output_it);
132
133   } while (window.slide_window_slice_4D(out_slice));
134 }