Home | History | Annotate | Download | only in include
      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 #ifndef FRAMEWORKS_ML_NN_INDEXED_SHAPE_WRAPPER_H
     18 #define FRAMEWORKS_ML_NN_INDEXED_SHAPE_WRAPPER_H
     19 
     20 #include "OperationsUtils.h"
     21 
     22 namespace android {
     23 namespace nn {
     24 
     25 // A wrapper over a Shape class implementing some indexing logic for a wrapped
     26 // shape.
     27 // To get an offset for an element in a tensor from vector index, one needs to
     28 // calculate strides first. This class removes the need to recalculate strides
     29 // for every indexing and also provides some utility functions.
     30 class IndexedShapeWrapper {
     31    public:
     32     IndexedShapeWrapper(const Shape& wrapped_shape);
     33 
     34     // Calculates the next index in a lexicograpical order for a wrapped shape
     35     // inplace. Only accepts valid index for a given shape as an input.
     36     // Sets lastIndex to true if the received index was the last in a
     37     // lexicographical order for a given shape. In this case, index stays the
     38     // same.
     39     bool nextIndexInplace(std::vector<uint32_t>* index, bool* lastIndex) const;
     40 
     41     // Given an index as a vector with per-dimension indices, calculates an
     42     // offset of the element in a flattened tensor.
     43     bool indexToFlatIndex(const std::vector<uint32_t>& index, uint32_t* flatIndex) const;
     44 
     45     // Same as indexToFlatIndex, only ignores first dimensions of an index if
     46     // they are not present in the shape. Also ignores dimensions of a shape of
     47     // size 1.
     48     // For example:
     49     //    for shape:    [3, 1, 2]
     50     //    and index: [4, 2, 5, 1]
     51     // the function will ignore dimensions with indices 4 and 5 and set
     52     // flatIndex to 5 as a result.
     53     bool broadcastedIndexToFlatIndex(const std::vector<uint32_t>& index, uint32_t* flatIndex) const;
     54 
     55    private:
     56     const Shape* const shape;
     57     std::vector<uint32_t> strides;
     58 
     59     bool isValid(const std::vector<uint32_t>& index) const;
     60 };
     61 
     62 }  // namespace nn
     63 }  // namespace android
     64 
     65 #endif  // FRAMEWORKS_ML_NN_INDEXED_SHAPE_WRAPPER_H
     66