Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef __ARM_COMPUTE_TENSORSHAPE_H__ |
| 25 | #define __ARM_COMPUTE_TENSORSHAPE_H__ |
| 26 | |
| 27 | #include "arm_compute/core/Dimensions.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <array> |
| 32 | #include <functional> |
| 33 | #include <numeric> |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | /** Shape of a tensor */ |
| 38 | class TensorShape : public Dimensions<size_t> |
| 39 | { |
| 40 | public: |
| 41 | /** Constructor to initialize the tensor shape. |
| 42 | * |
| 43 | * @param[in] dims Values to initialize the dimensions. |
| 44 | */ |
| 45 | template <typename... Ts> |
| 46 | TensorShape(Ts... dims) |
| 47 | : Dimensions{ dims... } |
| 48 | { |
| 49 | // Initialize unspecified dimensions to 1 |
| 50 | if(_num_dimensions > 0) |
| 51 | { |
| 52 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
| 53 | } |
| 54 | |
| 55 | // Correct number dimensions to ignore trailing dimensions of size 1 |
| 56 | apply_dimension_correction(); |
| 57 | } |
| 58 | /** Allow instances of this class to be copy constructed */ |
| 59 | TensorShape(const TensorShape &) = default; |
| 60 | /** Allow instances of this class to be copied */ |
| 61 | TensorShape &operator=(const TensorShape &) = default; |
| 62 | /** Allow instances of this class to be move constructed */ |
| 63 | TensorShape(TensorShape &&) = default; |
| 64 | /** Allow instances of this class to be moved */ |
| 65 | TensorShape &operator=(TensorShape &&) = default; |
| 66 | /** Default destructor */ |
| 67 | ~TensorShape() = default; |
| 68 | |
| 69 | /** Accessor to set the value of one of the dimensions. |
| 70 | * |
| 71 | * @param[in] dimension Dimension for which the value is set. |
| 72 | * @param[in] value Value to be set for the dimension. |
| 73 | */ |
| 74 | void set(size_t dimension, size_t value) |
| 75 | { |
Moritz Pflanzer | 0745a98 | 2017-07-05 16:34:28 +0100 | [diff] [blame] | 76 | // Clear entire shape if one dimension is zero |
| 77 | if(value == 0) |
| 78 | { |
| 79 | _num_dimensions = 0; |
| 80 | std::fill(_id.begin(), _id.end(), 0); |
| 81 | return; |
| 82 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | |
| 84 | // Make sure all empty dimensions are filled with 1 |
| 85 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
| 86 | |
| 87 | // Set the specified dimension and increase the number of dimensions if |
| 88 | // necessary |
| 89 | Dimensions::set(dimension, value); |
| 90 | |
| 91 | // Correct number dimensions to ignore trailing dimensions of size 1 |
| 92 | apply_dimension_correction(); |
| 93 | } |
| 94 | |
| 95 | /** Collapse the first n dimensions. |
| 96 | * |
| 97 | * @param[in] first Dimensions into which the following @p n are collapsed. |
| 98 | * @param[in] n Number of dimensions to collapse into @p first. |
| 99 | */ |
| 100 | void collapse(size_t n, size_t first = 0) |
| 101 | { |
| 102 | Dimensions::collapse(n, first); |
| 103 | |
| 104 | // Make sure all empty dimensions are filled with 1 |
| 105 | std::fill(_id.begin() + _num_dimensions, _id.end(), 1); |
| 106 | } |
| 107 | |
| 108 | /** Collapses all dimensions to a single linear total size. |
| 109 | * |
| 110 | * @return The total tensor size in terms of elements. |
| 111 | */ |
| 112 | size_t total_size() const |
| 113 | { |
| 114 | return std::accumulate(_id.begin(), _id.end(), 1, std::multiplies<size_t>()); |
| 115 | } |
| 116 | /** Collapses given dimension and above. |
| 117 | * |
| 118 | * @note Precondition: dimension < TensorShape::num_max_dimensions |
| 119 | * |
| 120 | * @param[in] dimension Size of the wanted dimension |
| 121 | * |
| 122 | * @return The linear size of the collapsed dimensions |
| 123 | */ |
| 124 | size_t total_size_upper(size_t dimension) const |
| 125 | { |
| 126 | return std::accumulate(_id.begin() + dimension, _id.end(), 1, std::multiplies<size_t>()); |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | /** Remove trailing dimensions of size 1 from the reported number of dimensions. */ |
| 131 | void apply_dimension_correction() |
| 132 | { |
| 133 | for(int i = static_cast<int>(_num_dimensions) - 1; i >= 0; --i) |
| 134 | { |
| 135 | if(_id[i] == 1) |
| 136 | { |
| 137 | --_num_dimensions; |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | }; |
| 146 | } |
| 147 | #endif /*__ARM_COMPUTE_TENSORSHAPE_H__*/ |