1 /*
2 * Copyright (C) 2017 The Android Open Source Project
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 #define LOG_TAG "Operations"
18
19 #include "EmbeddingLookup.h"
20
21 #include "CpuExecutor.h"
22 #include "Operations.h"
23 #include "Tracing.h"
24
25 namespace android {
26 namespace nn {
27
EmbeddingLookup(const Operation & operation,RunTimeOperandInfo * operands)28 EmbeddingLookup::EmbeddingLookup(const Operation& operation, RunTimeOperandInfo* operands) {
29 value_ = GetInput(operation, operands, kValueTensor);
30 lookup_ = GetInput(operation, operands, kLookupTensor);
31
32 output_ = GetOutput(operation, operands, kOutputTensor);
33 }
34
Eval()35 bool EmbeddingLookup::Eval() {
36 NNTRACE_COMP("EmbeddingLookup::Eval");
37 const int row_size = value_->shape().dimensions[0];
38 const int total_bytes = nonExtensionOperandSizeOfData(value_->type, value_->dimensions);
39 const int row_bytes = total_bytes / row_size;
40
41 for (uint32_t i = 0; i < lookup_->shape().dimensions[0]; i++) {
42 int idx = (reinterpret_cast<int*>(lookup_->buffer))[i];
43 if (idx >= row_size || idx < 0) {
44 LOG(ERROR) << "Embedding Lookup: index out of bounds.";
45 return false;
46 } else {
47 memcpy(output_->buffer + i * row_bytes, value_->buffer + idx * row_bytes, row_bytes);
48 }
49 }
50
51 return true;
52 }
53
54 } // namespace nn
55 } // namespace android
56