blob: 9ea171d492e94434db766bdb42361f97c96e3721 [file] [log] [blame]
Moritz Pflanzerc7d15032017-07-18 16:21:16 +01001/*
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +01002 * Copyright (c) 2017-2020, 2023 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_TEST_SIMPLE_TENSOR_H
25#define ARM_COMPUTE_TEST_SIMPLE_TENSOR_H
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010026
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/core/Utils.h"
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010030#include "tests/IAccessor.h"
31#include "tests/Utils.h"
32
33#include <algorithm>
34#include <array>
35#include <cstddef>
36#include <cstdint>
37#include <functional>
38#include <memory>
39#include <stdexcept>
40#include <utility>
41
42namespace arm_compute
43{
44namespace test
45{
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010046class RawTensor;
47
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010048/** Simple tensor object that stores elements in a consecutive chunk of memory.
49 *
50 * It can be created by either loading an image from a file which also
51 * initialises the content of the tensor or by explcitly specifying the size.
52 * The latter leaves the content uninitialised.
53 *
54 * Furthermore, the class provides methods to convert the tensor's values into
55 * different image format.
56 */
57template <typename T>
Moritz Pflanzer82e70a12017-08-08 16:20:45 +010058class SimpleTensor : public IAccessor
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010059{
60public:
61 /** Create an uninitialised tensor. */
62 SimpleTensor() = default;
63
64 /** Create an uninitialised tensor of the given @p shape and @p format.
65 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010066 * @param[in] shape Shape of the new raw tensor.
67 * @param[in] format Format of the new raw tensor.
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010068 */
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010069 SimpleTensor(TensorShape shape, Format format);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010070
71 /** Create an uninitialised tensor of the given @p shape and @p data type.
72 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010073 * @param[in] shape Shape of the new raw tensor.
74 * @param[in] data_type Data type of the new raw tensor.
75 * @param[in] num_channels (Optional) Number of channels (default = 1).
76 * @param[in] quantization_info (Optional) Quantization info for asymmetric quantization (default = empty).
77 * @param[in] data_layout (Optional) Data layout of the tensor (default = NCHW).
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010078 */
Chunosovd621bca2017-11-03 17:33:15 +070079 SimpleTensor(TensorShape shape, DataType data_type,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010080 int num_channels = 1,
81 QuantizationInfo quantization_info = QuantizationInfo(),
82 DataLayout data_layout = DataLayout::NCHW);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010083
84 /** Create a deep copy of the given @p tensor.
85 *
86 * @param[in] tensor To be copied tensor.
87 */
88 SimpleTensor(const SimpleTensor &tensor);
89
90 /** Create a deep copy of the given @p tensor.
91 *
92 * @param[in] tensor To be copied tensor.
Alex Gildayc357c472018-03-21 13:54:09 +000093 *
94 * @return a copy of the given tensor.
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010095 */
Alex Gildayc357c472018-03-21 13:54:09 +000096 SimpleTensor &operator=(SimpleTensor tensor);
97 /** Allow instances of this class to be move constructed */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010098 SimpleTensor(SimpleTensor &&) = default;
Alex Gildayc357c472018-03-21 13:54:09 +000099 /** Default destructor. */
100 ~SimpleTensor() = default;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100101
Alex Gildayc357c472018-03-21 13:54:09 +0000102 /** Tensor value type */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100103 using value_type = T;
Alex Gildayc357c472018-03-21 13:54:09 +0000104 /** Tensor buffer pointer type */
105 using Buffer = std::unique_ptr<value_type[]>;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100106
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100107 friend class RawTensor;
108
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100109 /** Return value at @p offset in the buffer.
110 *
111 * @param[in] offset Offset within the buffer.
Alex Gildayc357c472018-03-21 13:54:09 +0000112 *
113 * @return value in the buffer.
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100114 */
115 T &operator[](size_t offset);
116
117 /** Return constant value at @p offset in the buffer.
118 *
119 * @param[in] offset Offset within the buffer.
Alex Gildayc357c472018-03-21 13:54:09 +0000120 *
121 * @return constant value in the buffer.
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100122 */
123 const T &operator[](size_t offset) const;
124
Alex Gildayc357c472018-03-21 13:54:09 +0000125 /** Shape of the tensor.
126 *
127 * @return the shape of the tensor.
128 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100129 TensorShape shape() const override;
Alex Gildayc357c472018-03-21 13:54:09 +0000130 /** Size of each element in the tensor in bytes.
131 *
132 * @return the size of each element in the tensor in bytes.
133 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100134 size_t element_size() const override;
Alex Gildayc357c472018-03-21 13:54:09 +0000135 /** Total size of the tensor in bytes.
136 *
137 * @return the total size of the tensor in bytes.
138 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100139 size_t size() const override;
Alex Gildayc357c472018-03-21 13:54:09 +0000140 /** Image format of the tensor.
141 *
142 * @return the format of the tensor.
143 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100144 Format format() const override;
Alex Gildayc357c472018-03-21 13:54:09 +0000145 /** Data layout of the tensor.
146 *
147 * @return the data layout of the tensor.
148 */
149 DataLayout data_layout() const override;
150 /** Data type of the tensor.
151 *
152 * @return the data type of the tensor.
153 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100154 DataType data_type() const override;
Alex Gildayc357c472018-03-21 13:54:09 +0000155 /** Number of channels of the tensor.
156 *
157 * @return the number of channels of the tensor.
158 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100159 int num_channels() const override;
Alex Gildayc357c472018-03-21 13:54:09 +0000160 /** Number of elements of the tensor.
161 *
162 * @return the number of elements of the tensor.
163 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100164 int num_elements() const override;
Alex Gildayc357c472018-03-21 13:54:09 +0000165 /** Available padding around the tensor.
166 *
167 * @return the available padding around the tensor.
168 */
Giorgio Arenaa2611812017-07-21 10:08:48 +0100169 PaddingSize padding() const override;
Alex Gildayc357c472018-03-21 13:54:09 +0000170 /** Quantization info in case of asymmetric quantized type
171 *
172 * @return
173 */
Chunosovd621bca2017-11-03 17:33:15 +0700174 QuantizationInfo quantization_info() const override;
175
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100176 /** Set the quantization information of the tensor.
177 *
178 * This function does not have any effect on the raw quantized data of the tensor.
179 * It simply changes the quantization information, hence changes the dequantized values.
180 *
181 * @return A reference to the current object.
182 */
183 SimpleTensor<T> &quantization_info(const QuantizationInfo &qinfo);
184
Alex Gildayc357c472018-03-21 13:54:09 +0000185 /** Constant pointer to the underlying buffer.
186 *
187 * @return a constant pointer to the data.
188 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100189 const T *data() const;
190
Alex Gildayc357c472018-03-21 13:54:09 +0000191 /** Pointer to the underlying buffer.
192 *
193 * @return a pointer to the data.
194 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100195 T *data();
196
197 /** Read only access to the specified element.
198 *
199 * @param[in] coord Coordinates of the desired element.
200 *
201 * @return A pointer to the desired element.
202 */
203 const void *operator()(const Coordinates &coord) const override;
204
205 /** Access to the specified element.
206 *
207 * @param[in] coord Coordinates of the desired element.
208 *
209 * @return A pointer to the desired element.
210 */
211 void *operator()(const Coordinates &coord) override;
212
213 /** Swaps the content of the provided tensors.
214 *
215 * @param[in, out] tensor1 Tensor to be swapped.
216 * @param[in, out] tensor2 Tensor to be swapped.
217 */
218 template <typename U>
219 friend void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2);
220
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100221protected:
Chunosovd621bca2017-11-03 17:33:15 +0700222 Buffer _buffer{ nullptr };
223 TensorShape _shape{};
224 Format _format{ Format::UNKNOWN };
225 DataType _data_type{ DataType::UNKNOWN };
226 int _num_channels{ 0 };
Chunosovd621bca2017-11-03 17:33:15 +0700227 QuantizationInfo _quantization_info{};
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000228 DataLayout _data_layout{ DataLayout::UNKNOWN };
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100229};
230
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000231template <typename T1, typename T2>
232SimpleTensor<T1> copy_tensor(const SimpleTensor<T2> &tensor)
233{
234 SimpleTensor<T1> st(tensor.shape(), tensor.data_type(),
235 tensor.num_channels(),
236 tensor.quantization_info(),
237 tensor.data_layout());
238 for(size_t n = 0; n < size_t(st.num_elements()); n++)
239 {
240 st.data()[n] = static_cast<T1>(tensor.data()[n]);
241 }
242 return st;
243}
244
245template <typename T1, typename T2, typename std::enable_if<std::is_same<T1, T2>::value, int>::type = 0>
246SimpleTensor<T1> copy_tensor(const SimpleTensor<half> &tensor)
247{
248 SimpleTensor<T1> st(tensor.shape(), tensor.data_type(),
249 tensor.num_channels(),
250 tensor.quantization_info(),
251 tensor.data_layout());
252 memcpy((void *)st.data(), (const void *)tensor.data(), size_t(st.num_elements() * sizeof(T1)));
253 return st;
254}
255
256template < typename T1, typename T2, typename std::enable_if < (std::is_same<T1, half>::value || std::is_same<T2, half>::value), int >::type = 0 >
257SimpleTensor<T1> copy_tensor(const SimpleTensor<half> &tensor)
258{
259 SimpleTensor<T1> st(tensor.shape(), tensor.data_type(),
260 tensor.num_channels(),
261 tensor.quantization_info(),
262 tensor.data_layout());
263 for(size_t n = 0; n < size_t(st.num_elements()); n++)
264 {
265 st.data()[n] = half_float::detail::half_cast<T1, T2>(tensor.data()[n]);
266 }
267 return st;
268}
269
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100270template <typename T>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100271SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100272 : _buffer(nullptr),
273 _shape(shape),
274 _format(format),
Giorgio Arena563494c2018-04-30 17:29:41 +0100275 _quantization_info(),
276 _data_layout(DataLayout::NCHW)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100277{
John Richardson25f23682017-11-27 14:35:09 +0000278 _num_channels = num_channels();
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000279 _buffer = std::make_unique<T[]>(num_elements() * _num_channels);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100280}
281
282template <typename T>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100283SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, QuantizationInfo quantization_info, DataLayout data_layout)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100284 : _buffer(nullptr),
285 _shape(shape),
286 _data_type(data_type),
287 _num_channels(num_channels),
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000288 _quantization_info(quantization_info),
289 _data_layout(data_layout)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100290{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000291 _buffer = std::make_unique<T[]>(this->_shape.total_size() * _num_channels);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100292}
293
294template <typename T>
295SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor)
296 : _buffer(nullptr),
297 _shape(tensor.shape()),
298 _format(tensor.format()),
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100299 _data_type(tensor.data_type()),
300 _num_channels(tensor.num_channels()),
Giorgio Arena563494c2018-04-30 17:29:41 +0100301 _quantization_info(tensor.quantization_info()),
302 _data_layout(tensor.data_layout())
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100303{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000304 _buffer = std::make_unique<T[]>(tensor.num_elements() * _num_channels);
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100305 std::copy_n(tensor.data(), this->_shape.total_size() * _num_channels, _buffer.get());
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100306}
307
308template <typename T>
309SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor)
310{
311 swap(*this, tensor);
312
313 return *this;
314}
315
316template <typename T>
317T &SimpleTensor<T>::operator[](size_t offset)
318{
319 return _buffer[offset];
320}
321
322template <typename T>
323const T &SimpleTensor<T>::operator[](size_t offset) const
324{
325 return _buffer[offset];
326}
327
328template <typename T>
329TensorShape SimpleTensor<T>::shape() const
330{
331 return _shape;
332}
333
334template <typename T>
335size_t SimpleTensor<T>::element_size() const
336{
337 return num_channels() * element_size_from_data_type(data_type());
338}
339
340template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700341QuantizationInfo SimpleTensor<T>::quantization_info() const
342{
343 return _quantization_info;
344}
345
346template <typename T>
Viet-Hoa Do9c7c2d22023-04-11 17:16:27 +0100347SimpleTensor<T> &SimpleTensor<T>::quantization_info(const QuantizationInfo &qinfo)
348{
349 _quantization_info = qinfo;
350 return *this;
351}
352
353template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100354size_t SimpleTensor<T>::size() const
355{
356 const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>());
357 return size * element_size();
358}
359
360template <typename T>
361Format SimpleTensor<T>::format() const
362{
363 return _format;
364}
365
366template <typename T>
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000367DataLayout SimpleTensor<T>::data_layout() const
368{
369 return _data_layout;
370}
371
372template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100373DataType SimpleTensor<T>::data_type() const
374{
375 if(_format != Format::UNKNOWN)
376 {
377 return data_type_from_format(_format);
378 }
379 else
380 {
381 return _data_type;
382 }
383}
384
385template <typename T>
386int SimpleTensor<T>::num_channels() const
387{
388 switch(_format)
389 {
390 case Format::U8:
Anthony Barbier1fbb8122018-02-19 19:36:02 +0000391 case Format::U16:
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100392 case Format::S16:
Anthony Barbier1fbb8122018-02-19 19:36:02 +0000393 case Format::U32:
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100394 case Format::S32:
395 case Format::F16:
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100396 case Format::F32:
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100397 return 1;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100398 // Because the U and V channels are subsampled
399 // these formats appear like having only 2 channels:
400 case Format::YUYV422:
401 case Format::UYVY422:
402 return 2;
403 case Format::UV88:
404 return 2;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100405 case Format::RGB888:
406 return 3;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100407 case Format::RGBA8888:
408 return 4;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100409 case Format::UNKNOWN:
410 return _num_channels;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100411 //Doesn't make sense for planar formats:
412 case Format::NV12:
413 case Format::NV21:
414 case Format::IYUV:
415 case Format::YUV444:
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100416 default:
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100417 return 0;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100418 }
419}
420
421template <typename T>
422int SimpleTensor<T>::num_elements() const
423{
424 return _shape.total_size();
425}
426
427template <typename T>
Giorgio Arenaa2611812017-07-21 10:08:48 +0100428PaddingSize SimpleTensor<T>::padding() const
429{
430 return PaddingSize(0);
431}
432
433template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100434const T *SimpleTensor<T>::data() const
435{
436 return _buffer.get();
437}
438
439template <typename T>
440T *SimpleTensor<T>::data()
441{
442 return _buffer.get();
443}
444
445template <typename T>
446const void *SimpleTensor<T>::operator()(const Coordinates &coord) const
447{
John Richardson25f23682017-11-27 14:35:09 +0000448 return _buffer.get() + coord2index(_shape, coord) * _num_channels;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100449}
450
451template <typename T>
452void *SimpleTensor<T>::operator()(const Coordinates &coord)
453{
John Richardson25f23682017-11-27 14:35:09 +0000454 return _buffer.get() + coord2index(_shape, coord) * _num_channels;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100455}
456
457template <typename U>
458void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2)
459{
460 // Use unqualified call to swap to enable ADL. But make std::swap available
461 // as backup.
462 using std::swap;
463 swap(tensor1._shape, tensor2._shape);
464 swap(tensor1._format, tensor2._format);
465 swap(tensor1._data_type, tensor2._data_type);
466 swap(tensor1._num_channels, tensor2._num_channels);
Giorgio Arenac0f54432018-03-16 14:02:34 +0000467 swap(tensor1._quantization_info, tensor2._quantization_info);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100468 swap(tensor1._buffer, tensor2._buffer);
469}
470} // namespace test
471} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000472#endif /* ARM_COMPUTE_TEST_SIMPLE_TENSOR_H */