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 #include "EmbeddingLookup.h"
18
19 #include "CpuExecutor.h"
20 #include "Operations.h"
21
22 #include "Tracing.h"
23
24 namespace android {
25 namespace nn {
26
EmbeddingLookup(const Operation & operation,std::vector<RunTimeOperandInfo> & operands)27 EmbeddingLookup::EmbeddingLookup(const Operation& operation,
28 std::vector<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,
48 row_bytes);
49 }
50 }
51
52 return true;
53 }
54
55 } // namespace nn
56 } // namespace android
57