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