IndexedShapeWrapper.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2018 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. #define LOG_TAG "IndexedShapeWrapper"
  17. #include "IndexedShapeWrapper.h"
  18. namespace android {
  19. namespace nn {
  20. IndexedShapeWrapper::IndexedShapeWrapper(const Shape& wrapped_shape) : shape(&wrapped_shape) {
  21. strides.resize(shape->dimensions.size());
  22. strides.back() = 1;
  23. for (int i = strides.size() - 2; i >= 0; --i) {
  24. strides[i] = shape->dimensions[i + 1] * strides[i + 1];
  25. }
  26. }
  27. bool IndexedShapeWrapper::nextIndexInplace(std::vector<uint32_t>* index, bool* lastIndex) const {
  28. NN_CHECK(isValid(*index));
  29. bool anyIndicesLeft = false;
  30. for (int i = 0; i < index->size(); ++i) {
  31. if (index->at(i) < shape->dimensions[i] - 1) {
  32. anyIndicesLeft = true;
  33. break;
  34. }
  35. }
  36. if (!anyIndicesLeft) {
  37. *lastIndex = true;
  38. return true;
  39. }
  40. for (int i = index->size() - 1; i >= 0; --i) {
  41. ++index->at(i);
  42. if (index->at(i) == shape->dimensions[i]) {
  43. index->at(i) = 0;
  44. } else {
  45. break;
  46. }
  47. }
  48. return true;
  49. }
  50. bool IndexedShapeWrapper::indexToFlatIndex(const std::vector<uint32_t>& index,
  51. uint32_t* flatIndex) const {
  52. NN_CHECK(isValid(index));
  53. *flatIndex = 0;
  54. for (int i = 0; i < index.size(); ++i) {
  55. *flatIndex += strides[i] * index[i];
  56. }
  57. return true;
  58. }
  59. bool IndexedShapeWrapper::broadcastedIndexToFlatIndex(const std::vector<uint32_t>& index,
  60. uint32_t* flatIndex) const {
  61. NN_CHECK(index.size() >= strides.size());
  62. *flatIndex = 0;
  63. for (int i = 1; i <= strides.size(); ++i) {
  64. uint32_t currentIndex = index[index.size() - i];
  65. uint32_t currentDimSize = shape->dimensions[shape->dimensions.size() - i];
  66. NN_CHECK(currentIndex < currentDimSize || currentDimSize == 1);
  67. if (currentDimSize != 1) {
  68. *flatIndex += strides[strides.size() - i] * index[index.size() - i];
  69. }
  70. }
  71. return true;
  72. }
  73. bool IndexedShapeWrapper::isValid(const std::vector<uint32_t>& index) const {
  74. if (index.size() != shape->dimensions.size()) {
  75. LOG(ERROR) << "Index: " << toString(index)
  76. << " has a different number of dimensions from shape: "
  77. << toString(shape->dimensions);
  78. return false;
  79. }
  80. for (int i = 0; i < index.size(); ++i) {
  81. if (index[i] >= shape->dimensions[i]) {
  82. LOG(ERROR) << "Invalid index: " << toString(index)
  83. << " is out of range for shape: " << toString(shape->dimensions);
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. } // namespace nn
  90. } // namespace android