blob: 3b395e74ce1cfd38ed6853181fa19d0ca4285434 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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
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.
73 */
74 void set(size_t dimension, size_t value)
75 {
Moritz Pflanzer0745a982017-07-05 16:34:28 +010076 // 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 Barbier6ff3b192017-09-04 18:44:23 +010083
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
Gian Marco Iodice42ab8992017-08-04 10:54:55 +010095 /** Accessor to remove the dimension n from the tensor shape.
96 *
97 * @note The upper dimensions of the tensor shape will be shifted down by 1
98 *
99 * @param[in] n Dimension to remove
100 */
101 void remove_dimension(size_t n)
102 {
103 ARM_COMPUTE_ERROR_ON(_num_dimensions < 1);
104 ARM_COMPUTE_ERROR_ON(n >= _num_dimensions);
105
106 std::copy(_id.begin() + n + 1, _id.end(), _id.begin() + n);
107
108 // Reduce number of dimensions
109 _num_dimensions--;
110
111 // Make sure all empty dimensions are filled with 1
112 std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
113
114 // Correct number dimensions to ignore trailing dimensions of size 1
115 apply_dimension_correction();
116 }
117
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 /** Collapse the first n dimensions.
119 *
120 * @param[in] first Dimensions into which the following @p n are collapsed.
121 * @param[in] n Number of dimensions to collapse into @p first.
122 */
123 void collapse(size_t n, size_t first = 0)
124 {
125 Dimensions::collapse(n, first);
126
127 // Make sure all empty dimensions are filled with 1
128 std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
129 }
130
131 /** Collapses all dimensions to a single linear total size.
132 *
133 * @return The total tensor size in terms of elements.
134 */
135 size_t total_size() const
136 {
137 return std::accumulate(_id.begin(), _id.end(), 1, std::multiplies<size_t>());
138 }
139 /** Collapses given dimension and above.
140 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141 * @param[in] dimension Size of the wanted dimension
142 *
143 * @return The linear size of the collapsed dimensions
144 */
145 size_t total_size_upper(size_t dimension) const
146 {
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100147 ARM_COMPUTE_ERROR_ON(dimension >= TensorShape::num_max_dimensions);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 return std::accumulate(_id.begin() + dimension, _id.end(), 1, std::multiplies<size_t>());
149 }
150
Moritz Pflanzer484e7b32017-08-09 11:43:18 +0100151 /** Compute size of dimensions lower than the given one.
152 *
153 * @param[in] dimension Upper boundary.
154 *
155 * @return The linear size of the collapsed dimensions.
156 */
157 size_t total_size_lower(size_t dimension) const
158 {
159 ARM_COMPUTE_ERROR_ON(dimension > TensorShape::num_max_dimensions);
160 return std::accumulate(_id.begin(), _id.begin() + dimension, 1, std::multiplies<size_t>());
161 }
162
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163private:
164 /** Remove trailing dimensions of size 1 from the reported number of dimensions. */
165 void apply_dimension_correction()
166 {
Anthony Barbiera3b4ce22017-10-09 11:04:30 +0100167 for(int i = static_cast<int>(_num_dimensions) - 1; i > 0; --i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 {
169 if(_id[i] == 1)
170 {
171 --_num_dimensions;
172 }
173 else
174 {
175 break;
176 }
177 }
178 }
179};
180}
181#endif /*__ARM_COMPUTE_TENSORSHAPE_H__*/