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