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