EmbeddingLookup.cpp 1.7 KB

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