blob: f8b31816862e0833ec169500d706d452b76be533 [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 {
76 ARM_COMPUTE_ERROR_ON(value < 1);
77
78 // Make sure all empty dimensions are filled with 1
79 std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
80
81 // Set the specified dimension and increase the number of dimensions if
82 // necessary
83 Dimensions::set(dimension, value);
84
85 // Correct number dimensions to ignore trailing dimensions of size 1
86 apply_dimension_correction();
87 }
88
89 /** Collapse the first n dimensions.
90 *
91 * @param[in] first Dimensions into which the following @p n are collapsed.
92 * @param[in] n Number of dimensions to collapse into @p first.
93 */
94 void collapse(size_t n, size_t first = 0)
95 {
96 Dimensions::collapse(n, first);
97
98 // Make sure all empty dimensions are filled with 1
99 std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
100 }
101
102 /** Collapses all dimensions to a single linear total size.
103 *
104 * @return The total tensor size in terms of elements.
105 */
106 size_t total_size() const
107 {
108 return std::accumulate(_id.begin(), _id.end(), 1, std::multiplies<size_t>());
109 }
110 /** Collapses given dimension and above.
111 *
112 * @note Precondition: dimension < TensorShape::num_max_dimensions
113 *
114 * @param[in] dimension Size of the wanted dimension
115 *
116 * @return The linear size of the collapsed dimensions
117 */
118 size_t total_size_upper(size_t dimension) const
119 {
120 return std::accumulate(_id.begin() + dimension, _id.end(), 1, std::multiplies<size_t>());
121 }
122
123private:
124 /** Remove trailing dimensions of size 1 from the reported number of dimensions. */
125 void apply_dimension_correction()
126 {
127 for(int i = static_cast<int>(_num_dimensions) - 1; i >= 0; --i)
128 {
129 if(_id[i] == 1)
130 {
131 --_num_dimensions;
132 }
133 else
134 {
135 break;
136 }
137 }
138 }
139};
140}
141#endif /*__ARM_COMPUTE_TENSORSHAPE_H__*/