blob: 50f1211c18263bc6f01b9a74109b521d9f5d16b3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
35namespace arm_compute
36{
37/** Shape of a tensor */
38class TensorShape : public Dimensions<size_t>
39{
40public:
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.
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000073 *
74 * @return *this.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000076 TensorShape &set(size_t dimension, size_t value)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 {
Moritz Pflanzer0745a982017-07-05 16:34:28 +010078 // Clear entire shape if one dimension is zero
79 if(value == 0)
80 {
81 _num_dimensions = 0;
82 std::fill(_id.begin(), _id.end(), 0);
Moritz Pflanzer0745a982017-07-05 16:34:28 +010083 }
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000084 else
85 {
86 // Make sure all empty dimensions are filled with 1
87 std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000089 // Set the specified dimension and increase the number of dimensions if
90 // necessary
91 Dimensions::set(dimension, value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000093 // Correct number dimensions to ignore trailing dimensions of size 1
94 apply_dimension_correction();
95 }
96 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 }
98
Gian Marco Iodice42ab8992017-08-04 10:54:55 +010099 /** Accessor to remove the dimension n from the tensor shape.
100 *
101 * @note The upper dimensions of the tensor shape will be shifted down by 1
102 *
103 * @param[in] n Dimension to remove
104 */
105 void remove_dimension(size_t n)
106 {
107 ARM_COMPUTE_ERROR_ON(_num_dimensions < 1);
108 ARM_COMPUTE_ERROR_ON(n >= _num_dimensions);
109
110 std::copy(_id.begin() + n + 1, _id.end(), _id.begin() + n);
111
112 // Reduce number of dimensions
113 _num_dimensions--;
114
115 // Make sure all empty dimensions are filled with 1
116 std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
117
118 // Correct number dimensions to ignore trailing dimensions of size 1
119 apply_dimension_correction();
120 }
121
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 /** Collapse the first n dimensions.
123 *
Gian Marco Iodiceab182122017-10-09 15:05:40 +0100124 * @param[in] n Number of dimensions to collapse into @p first
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 * @param[in] first Dimensions into which the following @p n are collapsed.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 */
127 void collapse(size_t n, size_t first = 0)
128 {
129 Dimensions::collapse(n, first);
130
131 // Make sure all empty dimensions are filled with 1
132 std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
133 }
134
135 /** Collapses all dimensions to a single linear total size.
136 *
137 * @return The total tensor size in terms of elements.
138 */
139 size_t total_size() const
140 {
141 return std::accumulate(_id.begin(), _id.end(), 1, std::multiplies<size_t>());
142 }
143 /** Collapses given dimension and above.
144 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 * @param[in] dimension Size of the wanted dimension
146 *
147 * @return The linear size of the collapsed dimensions
148 */
149 size_t total_size_upper(size_t dimension) const
150 {
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100151 ARM_COMPUTE_ERROR_ON(dimension >= TensorShape::num_max_dimensions);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 return std::accumulate(_id.begin() + dimension, _id.end(), 1, std::multiplies<size_t>());
153 }
154
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100155 /** Compute size of dimensions lower than the given one.
156 *
157 * @param[in] dimension Upper boundary.
158 *
159 * @return The linear size of the collapsed dimensions.
160 */
161 size_t total_size_lower(size_t dimension) const
162 {
163 ARM_COMPUTE_ERROR_ON(dimension > TensorShape::num_max_dimensions);
164 return std::accumulate(_id.begin(), _id.begin() + dimension, 1, std::multiplies<size_t>());
165 }
166
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167private:
168 /** Remove trailing dimensions of size 1 from the reported number of dimensions. */
169 void apply_dimension_correction()
170 {
Anthony Barbiera3b4ce22017-10-09 11:04:30 +0100171 for(int i = static_cast<int>(_num_dimensions) - 1; i > 0; --i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 {
173 if(_id[i] == 1)
174 {
175 --_num_dimensions;
176 }
177 else
178 {
179 break;
180 }
181 }
182 }
183};
184}
185#endif /*__ARM_COMPUTE_TENSORSHAPE_H__*/