blob: f9e49bc85b71dc118849de2dc546ad6473a05722 [file] [log] [blame]
Moritz Pflanzerc7d15032017-07-18 16:21:16 +01001/*
Ioan-Cristian Szabo91d20d92017-10-27 17:35:40 +01002 * Copyright (c) 2017-2018 ARM Limited.
Moritz Pflanzerc7d15032017-07-18 16:21:16 +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_TEST_SIMPLE_TENSOR_H__
25#define __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/core/Utils.h"
30#include "support/ToolchainSupport.h"
31#include "tests/IAccessor.h"
32#include "tests/Utils.h"
33
34#include <algorithm>
35#include <array>
36#include <cstddef>
37#include <cstdint>
38#include <functional>
39#include <memory>
40#include <stdexcept>
41#include <utility>
42
43namespace arm_compute
44{
45namespace test
46{
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010047class RawTensor;
48
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010049/** Simple tensor object that stores elements in a consecutive chunk of memory.
50 *
51 * It can be created by either loading an image from a file which also
52 * initialises the content of the tensor or by explcitly specifying the size.
53 * The latter leaves the content uninitialised.
54 *
55 * Furthermore, the class provides methods to convert the tensor's values into
56 * different image format.
57 */
58template <typename T>
Moritz Pflanzer82e70a12017-08-08 16:20:45 +010059class SimpleTensor : public IAccessor
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010060{
61public:
62 /** Create an uninitialised tensor. */
63 SimpleTensor() = default;
64
65 /** Create an uninitialised tensor of the given @p shape and @p format.
66 *
67 * @param[in] shape Shape of the new raw tensor.
68 * @param[in] format Format of the new raw tensor.
69 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
70 */
71 SimpleTensor(TensorShape shape, Format format, int fixed_point_position = 0);
72
73 /** Create an uninitialised tensor of the given @p shape and @p data type.
74 *
75 * @param[in] shape Shape of the new raw tensor.
76 * @param[in] data_type Data type of the new raw tensor.
77 * @param[in] num_channels (Optional) Number of channels (default = 1).
78 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers (default = 0).
Chunosovd621bca2017-11-03 17:33:15 +070079 * @param[in] quantization_info (Optional) Quantization info for asymmetric quantization (default = empty).
Michele Di Giorgio4a65b982018-03-02 11:21:38 +000080 * @param[in] data_layout (Optional) Data layout of the tensor (default = NCHW).
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010081 */
Chunosovd621bca2017-11-03 17:33:15 +070082 SimpleTensor(TensorShape shape, DataType data_type,
Michele Di Giorgio4a65b982018-03-02 11:21:38 +000083 int num_channels = 1,
84 int fixed_point_position = 0,
85 QuantizationInfo quantization_info = QuantizationInfo(),
86 DataLayout data_layout = DataLayout::NCHW);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010087
88 /** Create a deep copy of the given @p tensor.
89 *
90 * @param[in] tensor To be copied tensor.
91 */
92 SimpleTensor(const SimpleTensor &tensor);
93
94 /** Create a deep copy of the given @p tensor.
95 *
96 * @param[in] tensor To be copied tensor.
97 */
98 SimpleTensor &operator =(SimpleTensor tensor);
99 SimpleTensor(SimpleTensor &&) = default;
100 ~SimpleTensor() = default;
101
102 using value_type = T;
103 using Buffer = std::unique_ptr<value_type[]>;
104
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100105 friend class RawTensor;
106
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100107 /** Return value at @p offset in the buffer.
108 *
109 * @param[in] offset Offset within the buffer.
110 */
111 T &operator[](size_t offset);
112
113 /** Return constant value at @p offset in the buffer.
114 *
115 * @param[in] offset Offset within the buffer.
116 */
117 const T &operator[](size_t offset) const;
118
119 /** Shape of the tensor. */
120 TensorShape shape() const override;
121
122 /** Size of each element in the tensor in bytes. */
123 size_t element_size() const override;
124
125 /** Total size of the tensor in bytes. */
126 size_t size() const override;
127
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000128 /** Data layout of the tensor. */
129 DataLayout data_layout() const override;
130
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100131 /** Image format of the tensor. */
132 Format format() const override;
133
134 /** Data type of the tensor. */
135 DataType data_type() const override;
136
137 /** Number of channels of the tensor. */
138 int num_channels() const override;
139
140 /** Number of elements of the tensor. */
141 int num_elements() const override;
142
Giorgio Arenaa2611812017-07-21 10:08:48 +0100143 /** Available padding around the tensor. */
144 PaddingSize padding() const override;
145
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100146 /** The number of bits for the fractional part of the fixed point numbers. */
147 int fixed_point_position() const override;
148
Chunosovd621bca2017-11-03 17:33:15 +0700149 /** Quantization info in case of asymmetric quantized type */
150 QuantizationInfo quantization_info() const override;
151
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100152 /** Constant pointer to the underlying buffer. */
153 const T *data() const;
154
155 /** Pointer to the underlying buffer. */
156 T *data();
157
158 /** Read only access to the specified element.
159 *
160 * @param[in] coord Coordinates of the desired element.
161 *
162 * @return A pointer to the desired element.
163 */
164 const void *operator()(const Coordinates &coord) const override;
165
166 /** Access to the specified element.
167 *
168 * @param[in] coord Coordinates of the desired element.
169 *
170 * @return A pointer to the desired element.
171 */
172 void *operator()(const Coordinates &coord) override;
173
174 /** Swaps the content of the provided tensors.
175 *
176 * @param[in, out] tensor1 Tensor to be swapped.
177 * @param[in, out] tensor2 Tensor to be swapped.
178 */
179 template <typename U>
180 friend void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2);
181
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100182protected:
Chunosovd621bca2017-11-03 17:33:15 +0700183 Buffer _buffer{ nullptr };
184 TensorShape _shape{};
185 Format _format{ Format::UNKNOWN };
186 DataType _data_type{ DataType::UNKNOWN };
187 int _num_channels{ 0 };
188 int _fixed_point_position{ 0 };
189 QuantizationInfo _quantization_info{};
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000190 DataLayout _data_layout{ DataLayout::UNKNOWN };
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100191};
192
193template <typename T>
194SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format, int fixed_point_position)
195 : _buffer(nullptr),
196 _shape(shape),
197 _format(format),
Chunosovd621bca2017-11-03 17:33:15 +0700198 _fixed_point_position(fixed_point_position),
199 _quantization_info()
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100200{
John Richardson25f23682017-11-27 14:35:09 +0000201 _num_channels = num_channels();
202 _buffer = support::cpp14::make_unique<T[]>(num_elements() * _num_channels);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100203}
204
205template <typename T>
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000206SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, int fixed_point_position, QuantizationInfo quantization_info, DataLayout data_layout)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100207 : _buffer(nullptr),
208 _shape(shape),
209 _data_type(data_type),
210 _num_channels(num_channels),
Chunosovd621bca2017-11-03 17:33:15 +0700211 _fixed_point_position(fixed_point_position),
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000212 _quantization_info(quantization_info),
213 _data_layout(data_layout)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100214{
215 _buffer = support::cpp14::make_unique<T[]>(num_elements() * this->num_channels());
216}
217
218template <typename T>
219SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor)
220 : _buffer(nullptr),
221 _shape(tensor.shape()),
222 _format(tensor.format()),
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100223 _data_type(tensor.data_type()),
224 _num_channels(tensor.num_channels()),
Chunosovd621bca2017-11-03 17:33:15 +0700225 _fixed_point_position(tensor.fixed_point_position()),
226 _quantization_info(tensor.quantization_info())
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100227{
228 _buffer = support::cpp14::make_unique<T[]>(tensor.num_elements() * num_channels());
229 std::copy_n(tensor.data(), num_elements() * num_channels(), _buffer.get());
230}
231
232template <typename T>
233SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor)
234{
235 swap(*this, tensor);
236
237 return *this;
238}
239
240template <typename T>
241T &SimpleTensor<T>::operator[](size_t offset)
242{
243 return _buffer[offset];
244}
245
246template <typename T>
247const T &SimpleTensor<T>::operator[](size_t offset) const
248{
249 return _buffer[offset];
250}
251
252template <typename T>
253TensorShape SimpleTensor<T>::shape() const
254{
255 return _shape;
256}
257
258template <typename T>
259size_t SimpleTensor<T>::element_size() const
260{
261 return num_channels() * element_size_from_data_type(data_type());
262}
263
264template <typename T>
265int SimpleTensor<T>::fixed_point_position() const
266{
267 return _fixed_point_position;
268}
269
270template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700271QuantizationInfo SimpleTensor<T>::quantization_info() const
272{
273 return _quantization_info;
274}
275
276template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100277size_t SimpleTensor<T>::size() const
278{
279 const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>());
280 return size * element_size();
281}
282
283template <typename T>
284Format SimpleTensor<T>::format() const
285{
286 return _format;
287}
288
289template <typename T>
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000290DataLayout SimpleTensor<T>::data_layout() const
291{
292 return _data_layout;
293}
294
295template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100296DataType SimpleTensor<T>::data_type() const
297{
298 if(_format != Format::UNKNOWN)
299 {
300 return data_type_from_format(_format);
301 }
302 else
303 {
304 return _data_type;
305 }
306}
307
308template <typename T>
309int SimpleTensor<T>::num_channels() const
310{
311 switch(_format)
312 {
313 case Format::U8:
Anthony Barbier1fbb8122018-02-19 19:36:02 +0000314 case Format::U16:
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100315 case Format::S16:
Anthony Barbier1fbb8122018-02-19 19:36:02 +0000316 case Format::U32:
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100317 case Format::S32:
318 case Format::F16:
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100319 case Format::F32:
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100320 return 1;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100321 // Because the U and V channels are subsampled
322 // these formats appear like having only 2 channels:
323 case Format::YUYV422:
324 case Format::UYVY422:
325 return 2;
326 case Format::UV88:
327 return 2;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100328 case Format::RGB888:
329 return 3;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100330 case Format::RGBA8888:
331 return 4;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100332 case Format::UNKNOWN:
333 return _num_channels;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100334 //Doesn't make sense for planar formats:
335 case Format::NV12:
336 case Format::NV21:
337 case Format::IYUV:
338 case Format::YUV444:
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100339 default:
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100340 return 0;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100341 }
342}
343
344template <typename T>
345int SimpleTensor<T>::num_elements() const
346{
347 return _shape.total_size();
348}
349
350template <typename T>
Giorgio Arenaa2611812017-07-21 10:08:48 +0100351PaddingSize SimpleTensor<T>::padding() const
352{
353 return PaddingSize(0);
354}
355
356template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100357const T *SimpleTensor<T>::data() const
358{
359 return _buffer.get();
360}
361
362template <typename T>
363T *SimpleTensor<T>::data()
364{
365 return _buffer.get();
366}
367
368template <typename T>
369const void *SimpleTensor<T>::operator()(const Coordinates &coord) const
370{
John Richardson25f23682017-11-27 14:35:09 +0000371 return _buffer.get() + coord2index(_shape, coord) * _num_channels;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100372}
373
374template <typename T>
375void *SimpleTensor<T>::operator()(const Coordinates &coord)
376{
John Richardson25f23682017-11-27 14:35:09 +0000377 return _buffer.get() + coord2index(_shape, coord) * _num_channels;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100378}
379
380template <typename U>
381void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2)
382{
383 // Use unqualified call to swap to enable ADL. But make std::swap available
384 // as backup.
385 using std::swap;
386 swap(tensor1._shape, tensor2._shape);
387 swap(tensor1._format, tensor2._format);
388 swap(tensor1._data_type, tensor2._data_type);
389 swap(tensor1._num_channels, tensor2._num_channels);
390 swap(tensor1._buffer, tensor2._buffer);
391}
392} // namespace test
393} // namespace arm_compute
394#endif /* __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ */