blob: c1bd7f87b59452da00870753bf66f5a83dcf280e [file] [log] [blame]
Moritz Pflanzerc7d15032017-07-18 16:21:16 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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
Alex Gildayc357c472018-03-21 13:54:09 +0000176 /** Constant pointer to the underlying buffer.
177 *
178 * @return a constant pointer to the data.
179 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100180 const T *data() const;
181
Alex Gildayc357c472018-03-21 13:54:09 +0000182 /** Pointer to the underlying buffer.
183 *
184 * @return a pointer to the data.
185 */
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100186 T *data();
187
188 /** Read only access to the specified element.
189 *
190 * @param[in] coord Coordinates of the desired element.
191 *
192 * @return A pointer to the desired element.
193 */
194 const void *operator()(const Coordinates &coord) const override;
195
196 /** Access to the specified element.
197 *
198 * @param[in] coord Coordinates of the desired element.
199 *
200 * @return A pointer to the desired element.
201 */
202 void *operator()(const Coordinates &coord) override;
203
204 /** Swaps the content of the provided tensors.
205 *
206 * @param[in, out] tensor1 Tensor to be swapped.
207 * @param[in, out] tensor2 Tensor to be swapped.
208 */
209 template <typename U>
210 friend void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2);
211
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100212protected:
Chunosovd621bca2017-11-03 17:33:15 +0700213 Buffer _buffer{ nullptr };
214 TensorShape _shape{};
215 Format _format{ Format::UNKNOWN };
216 DataType _data_type{ DataType::UNKNOWN };
217 int _num_channels{ 0 };
Chunosovd621bca2017-11-03 17:33:15 +0700218 QuantizationInfo _quantization_info{};
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000219 DataLayout _data_layout{ DataLayout::UNKNOWN };
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100220};
221
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000222template <typename T1, typename T2>
223SimpleTensor<T1> copy_tensor(const SimpleTensor<T2> &tensor)
224{
225 SimpleTensor<T1> st(tensor.shape(), tensor.data_type(),
226 tensor.num_channels(),
227 tensor.quantization_info(),
228 tensor.data_layout());
229 for(size_t n = 0; n < size_t(st.num_elements()); n++)
230 {
231 st.data()[n] = static_cast<T1>(tensor.data()[n]);
232 }
233 return st;
234}
235
236template <typename T1, typename T2, typename std::enable_if<std::is_same<T1, T2>::value, int>::type = 0>
237SimpleTensor<T1> copy_tensor(const SimpleTensor<half> &tensor)
238{
239 SimpleTensor<T1> st(tensor.shape(), tensor.data_type(),
240 tensor.num_channels(),
241 tensor.quantization_info(),
242 tensor.data_layout());
243 memcpy((void *)st.data(), (const void *)tensor.data(), size_t(st.num_elements() * sizeof(T1)));
244 return st;
245}
246
247template < typename T1, typename T2, typename std::enable_if < (std::is_same<T1, half>::value || std::is_same<T2, half>::value), int >::type = 0 >
248SimpleTensor<T1> copy_tensor(const SimpleTensor<half> &tensor)
249{
250 SimpleTensor<T1> st(tensor.shape(), tensor.data_type(),
251 tensor.num_channels(),
252 tensor.quantization_info(),
253 tensor.data_layout());
254 for(size_t n = 0; n < size_t(st.num_elements()); n++)
255 {
256 st.data()[n] = half_float::detail::half_cast<T1, T2>(tensor.data()[n]);
257 }
258 return st;
259}
260
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100261template <typename T>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100262SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100263 : _buffer(nullptr),
264 _shape(shape),
265 _format(format),
Giorgio Arena563494c2018-04-30 17:29:41 +0100266 _quantization_info(),
267 _data_layout(DataLayout::NCHW)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100268{
John Richardson25f23682017-11-27 14:35:09 +0000269 _num_channels = num_channels();
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000270 _buffer = std::make_unique<T[]>(num_elements() * _num_channels);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100271}
272
273template <typename T>
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100274SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, QuantizationInfo quantization_info, DataLayout data_layout)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100275 : _buffer(nullptr),
276 _shape(shape),
277 _data_type(data_type),
278 _num_channels(num_channels),
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000279 _quantization_info(quantization_info),
280 _data_layout(data_layout)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100281{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000282 _buffer = std::make_unique<T[]>(this->_shape.total_size() * _num_channels);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100283}
284
285template <typename T>
286SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor)
287 : _buffer(nullptr),
288 _shape(tensor.shape()),
289 _format(tensor.format()),
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100290 _data_type(tensor.data_type()),
291 _num_channels(tensor.num_channels()),
Giorgio Arena563494c2018-04-30 17:29:41 +0100292 _quantization_info(tensor.quantization_info()),
293 _data_layout(tensor.data_layout())
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100294{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000295 _buffer = std::make_unique<T[]>(tensor.num_elements() * _num_channels);
Manuel Bottinia788c2f2019-04-08 13:18:00 +0100296 std::copy_n(tensor.data(), this->_shape.total_size() * _num_channels, _buffer.get());
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100297}
298
299template <typename T>
300SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor)
301{
302 swap(*this, tensor);
303
304 return *this;
305}
306
307template <typename T>
308T &SimpleTensor<T>::operator[](size_t offset)
309{
310 return _buffer[offset];
311}
312
313template <typename T>
314const T &SimpleTensor<T>::operator[](size_t offset) const
315{
316 return _buffer[offset];
317}
318
319template <typename T>
320TensorShape SimpleTensor<T>::shape() const
321{
322 return _shape;
323}
324
325template <typename T>
326size_t SimpleTensor<T>::element_size() const
327{
328 return num_channels() * element_size_from_data_type(data_type());
329}
330
331template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700332QuantizationInfo SimpleTensor<T>::quantization_info() const
333{
334 return _quantization_info;
335}
336
337template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100338size_t SimpleTensor<T>::size() const
339{
340 const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>());
341 return size * element_size();
342}
343
344template <typename T>
345Format SimpleTensor<T>::format() const
346{
347 return _format;
348}
349
350template <typename T>
Michele Di Giorgio4a65b982018-03-02 11:21:38 +0000351DataLayout SimpleTensor<T>::data_layout() const
352{
353 return _data_layout;
354}
355
356template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100357DataType SimpleTensor<T>::data_type() const
358{
359 if(_format != Format::UNKNOWN)
360 {
361 return data_type_from_format(_format);
362 }
363 else
364 {
365 return _data_type;
366 }
367}
368
369template <typename T>
370int SimpleTensor<T>::num_channels() const
371{
372 switch(_format)
373 {
374 case Format::U8:
Anthony Barbier1fbb8122018-02-19 19:36:02 +0000375 case Format::U16:
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100376 case Format::S16:
Anthony Barbier1fbb8122018-02-19 19:36:02 +0000377 case Format::U32:
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100378 case Format::S32:
379 case Format::F16:
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100380 case Format::F32:
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100381 return 1;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100382 // Because the U and V channels are subsampled
383 // these formats appear like having only 2 channels:
384 case Format::YUYV422:
385 case Format::UYVY422:
386 return 2;
387 case Format::UV88:
388 return 2;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100389 case Format::RGB888:
390 return 3;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100391 case Format::RGBA8888:
392 return 4;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100393 case Format::UNKNOWN:
394 return _num_channels;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100395 //Doesn't make sense for planar formats:
396 case Format::NV12:
397 case Format::NV21:
398 case Format::IYUV:
399 case Format::YUV444:
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100400 default:
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100401 return 0;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100402 }
403}
404
405template <typename T>
406int SimpleTensor<T>::num_elements() const
407{
408 return _shape.total_size();
409}
410
411template <typename T>
Giorgio Arenaa2611812017-07-21 10:08:48 +0100412PaddingSize SimpleTensor<T>::padding() const
413{
414 return PaddingSize(0);
415}
416
417template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100418const T *SimpleTensor<T>::data() const
419{
420 return _buffer.get();
421}
422
423template <typename T>
424T *SimpleTensor<T>::data()
425{
426 return _buffer.get();
427}
428
429template <typename T>
430const void *SimpleTensor<T>::operator()(const Coordinates &coord) const
431{
John Richardson25f23682017-11-27 14:35:09 +0000432 return _buffer.get() + coord2index(_shape, coord) * _num_channels;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100433}
434
435template <typename T>
436void *SimpleTensor<T>::operator()(const Coordinates &coord)
437{
John Richardson25f23682017-11-27 14:35:09 +0000438 return _buffer.get() + coord2index(_shape, coord) * _num_channels;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100439}
440
441template <typename U>
442void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2)
443{
444 // Use unqualified call to swap to enable ADL. But make std::swap available
445 // as backup.
446 using std::swap;
447 swap(tensor1._shape, tensor2._shape);
448 swap(tensor1._format, tensor2._format);
449 swap(tensor1._data_type, tensor2._data_type);
450 swap(tensor1._num_channels, tensor2._num_channels);
Giorgio Arenac0f54432018-03-16 14:02:34 +0000451 swap(tensor1._quantization_info, tensor2._quantization_info);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100452 swap(tensor1._buffer, tensor2._buffer);
453}
454} // namespace test
455} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000456#endif /* ARM_COMPUTE_TEST_SIMPLE_TENSOR_H */