aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/core/ITensorInfo.h
blob: f2b4c155aad992bdd5c8b616ee44ff7e317472a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (c) 2016-2019 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef ARM_COMPUTE_ITENSORINFO_H
#define ARM_COMPUTE_ITENSORINFO_H

#include "arm_compute/core/Coordinates.h"
#include "arm_compute/core/Strides.h"
#include "arm_compute/core/TensorShape.h"
#include "arm_compute/core/Types.h"
#include "arm_compute/core/Utils.h"
#include "arm_compute/core/utils/misc/ICloneable.h"
#include "arm_compute/core/utils/misc/Utility.h"

#include <cstddef>

namespace arm_compute
{
/** Store the tensor's metadata */
class ITensorInfo : public misc::ICloneable<ITensorInfo>
{
public:
    /** Default virtual destructor */
    virtual ~ITensorInfo() = default;
    /** Set the data type to the specified value.
     *
     * @warning This resets the format to UNKNOWN.
     *
     * @param[in] data_type The new data type.
     *
     * @return Reference to this ITensorInfo object
     */
    virtual ITensorInfo &set_data_type(DataType data_type) = 0;
    /** Set the number of channels to the specified value.
     *
     * @warning This resets the format to UNKNOWN.
     *
     * @param[in] num_channels New number of channels.
     *
     * @return Reference to this ITensorInfo object
     */
    virtual ITensorInfo &set_num_channels(int num_channels) = 0;
    /** Set the format of an already initialized tensor.
     *
     * @note If the data type has already been configured (i.e. not UNKNOWN) it
     * must match the new format. If data type hasn't been configured it will
     * be based on the format.
     *
     * @param[in] format Single-plane format of the tensor.
     *
     * @return Reference to this ITensorInfo object
     */
    virtual ITensorInfo &set_format(Format format) = 0;
    /** Set the shape of an already initialized tensor.
     *
     * @warning Changing the shape requires to recompute the strides and is
     * therefore only possible if the tensor hasn't been allocated yet.
     *
     * @param[in] shape New tensor shape.
     *
     * @return Reference to this ITensorInfo object
     */
    virtual ITensorInfo &set_tensor_shape(const TensorShape &shape) = 0;
    /** Set the quantization settings (scale and offset) of the tensor.
     *
     * @param[in] quantization_info QuantizationInfo containing the scale and offset
     *
     * @return Reference to this ITensorInfo object
     */
    virtual ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) = 0;
    /** Set the data layout of the tensor.
     *
     * @param[in] data_layout DataLayout containing the layout data information.
     *
     * @return Reference to this ITensorInfo object
     */
    virtual ITensorInfo &set_data_layout(const DataLayout &data_layout) = 0;
    /** Resets the padding settings of the tensor.
    *
    * @return Reference to this ITensorInfo object
    */
    virtual ITensorInfo &reset_padding() = 0;
    /** Update the offset to the first element and the strides to automatically computed values.
     *
     * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
     *
     * @return True if the strides or the offset to the first element have changed.
     */
    virtual bool auto_padding() = 0;
    /** Update the offset to the first element, the strides and the total size.
     *
     * @note This function can only increase the offset, strides and total size.
     *
     * @param[in] padding Padding around the XY plane in number of elements.
     *
     * @return True if the strides, offset and total size have changed.
     */
    virtual bool extend_padding(const PaddingSize &padding) = 0;
    /** Return the size of the requested dimension
     *
     * @param[in] index Index of the dimension
     *
     * @return Dimension of the requested dimension
     */
    virtual size_t dimension(size_t index) const = 0;
    /** Return the size of the requested data layout dimension
     *
     * @param[in] dimension DataLayoutDimension of the dimension
     *
     * @return Dimension of the requested dimension
     */
    virtual size_t dimension(DataLayoutDimension dimension) const = 0;
    /** The strides in bytes for accessing each dimension of the tensor
     *
     * @return Strides in bytes for each tensor dimension
     */
    virtual const Strides &strides_in_bytes() const = 0;
    /** The offset from the beginning of the memory allocation to the first element of the tensor.
     *  This can be used to access efficiently elements in a 2D tensor
     *
     * @return The offset in bytes to access the first element of the tensor.
     */
    virtual size_t offset_first_element_in_bytes() const = 0;
    /** The offset in bytes from the beginning of the memory allocation to access the element at position (x, y, z ...)
     *
     * @param[in] pos Vector with the coordinates of the element to access.
     *                The size of this vector must be equal to the number of dimensions of the tensor
     *
     * @return Offset in bytes from the beginning of the memory allocation to access the element (x, y, z, ...)
     */
    virtual int32_t offset_element_in_bytes(const Coordinates &pos) const = 0;

    /** Element size in bytes calculated as data_size() * num_channels()
     *
     * @return The size of one element in bytes
     */
    virtual size_t element_size() const = 0;
    /** The number of dimensions of the tensor (rank)
     *
     * @return The number of dimensions of the tensor (rank)
     */
    virtual size_t num_dimensions() const = 0;
    /** The number of channels for each tensor element
     *
     * @return The number of channels for each tensor element
     */
    virtual size_t num_channels() const = 0;
    /** Size for each dimension of the tensor
     *
     * @return A vector with the size for each dimension of the tensor
     */
    virtual const TensorShape &tensor_shape() const = 0;
    /** Data type used for each element of the tensor
     *
     * @return Tensor data type
     */
    virtual DataType data_type() const = 0;
    /** Colour format of the image
     *
     * @return Colour format of the image
     */
    virtual Format format() const = 0;
    /** Returns the total size of the tensor in bytes.
     *
     * @return Total size of the tensor in bytes.
     */
    virtual size_t total_size() const = 0;
    /** Padding of tensor.
     *
     * @return Padding.
     */
    virtual PaddingSize padding() const = 0;
    /** Checks if the tensor has been allocated with padding or not.
     *
     * @return True if padding is allocated in the tensor, otherwise false.
     */
    virtual bool has_padding() const = 0;
    /** Flag indicating whether the size of the tensor can be changed.
     *
     * @return True if the tensor size can be changed.
     */
    virtual bool is_resizable() const = 0;
    /** Flag indicating whether the shape of the tensor is dynamic, meaning that it can change on kernel/function execution.
     *
     * @return True if its dynamic else false
     */
    virtual bool is_dynamic() const = 0;
    /** Set the flag whether the tensor size can be changed.
     *
     * @param[in] is_resizable Flag that marks the tensor if it can be changed or not.
     *
     * @return Reference to this ITensorInfo object
     */
    virtual ITensorInfo &set_is_resizable(bool is_resizable) = 0;
    /** Set the flag whether the tensor size is dynamic.
     *
     * @param[in] is_dynamic Flag that marks the tensor if it's dynamic.
     *
     * @return Reference to this ITensorInfo object
     */
    virtual ITensorInfo &set_is_dynamic(bool is_dynamic) = 0;
    /** Valid region of the tensor. All elements in the valid region have defined values, i.e. are not undefined.
     *
     * @return The valid region.
     */
    virtual ValidRegion valid_region() const = 0;
    /** Set the valid region of the tensor.
     *
     * @param[in] valid_region Valid region to set.
     */
    virtual void set_valid_region(const ValidRegion &valid_region) = 0;

    /** Get the quantization settings (scale and offset) of the tensor.
    *
    * @return A QuantizationInfo containing the scale and offset.
    */
    virtual QuantizationInfo quantization_info() const = 0;
    /** Get the data layout of the tensor.
    *
    * @return A DataLayout containing the layout data information.
    */
    virtual DataLayout data_layout() const = 0;

    /** If infos are broadcast compatible tensor info's, return the broadcasted shape and the intersection of
     * the broadcasted valid regions of the tensors.
     *
     * Two tensor info's are broadcast compatible if their shapes are broadcast compatible.
     *
     * Two tensor shapes are broadcast compatible if for each dimension, they're equal or one of them is 1.
     *
     * If two shapes are compatible, each dimension in the broadcasted shape is the max of the original dimensions.
     *
     * @param[in] infos Tensor info's.
     *
     * @return The broadcasted shape and valid region, or an empty shape and valid region if the info's are
     * not broadcast compatible.
     */
    template <typename... Infos>
    static std::pair<TensorShape, ValidRegion> broadcast_shape_and_valid_region(const Infos &... infos)
    {
        TensorShape bc_shape = TensorShape::broadcast_shape(infos.tensor_shape()...);
        ValidRegion bc_valid_region{ Coordinates(), bc_shape };

        auto broadcast_valid_region = [&bc_valid_region](const ITensorInfo & info)
        {
            if(info.num_dimensions() != 0)
            {
                for(size_t d = 0; d < bc_valid_region.shape.num_dimensions(); ++d)
                {
                    const bool is_broadcast = (info.tensor_shape()[d] == 1);

                    const int    anchor_max = std::max(bc_valid_region.anchor[d], info.valid_region().anchor[d]);
                    const size_t valid_min  = std::min(bc_valid_region.shape[d], info.valid_region().shape[d]);

                    if(!is_broadcast || (valid_min == 0))
                    {
                        bc_valid_region.anchor.set(d, anchor_max);
                        bc_valid_region.shape.set(d, valid_min);
                    }
                }
            }
        };

        utility::for_each(broadcast_valid_region, infos...);

        return std::pair<TensorShape, ValidRegion>(bc_shape, bc_valid_region);
    }
};
} // namespace arm_compute
#endif /*ARM_COMPUTE_TENSORINFO_H */