blob: 6091991e66f63998dfb3db0c4c2c10a37303958b [file] [log] [blame]
Moritz Pflanzerc7d15032017-07-18 16:21:16 +01001/*
2 * Copyright (c) 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_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).
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010080 */
Chunosovd621bca2017-11-03 17:33:15 +070081 SimpleTensor(TensorShape shape, DataType data_type,
82 int num_channels = 1,
83 int fixed_point_position = 0, QuantizationInfo quantization_info = QuantizationInfo());
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010084
85 /** Create a deep copy of the given @p tensor.
86 *
87 * @param[in] tensor To be copied tensor.
88 */
89 SimpleTensor(const SimpleTensor &tensor);
90
91 /** Create a deep copy of the given @p tensor.
92 *
93 * @param[in] tensor To be copied tensor.
94 */
95 SimpleTensor &operator =(SimpleTensor tensor);
96 SimpleTensor(SimpleTensor &&) = default;
97 ~SimpleTensor() = default;
98
99 using value_type = T;
100 using Buffer = std::unique_ptr<value_type[]>;
101
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100102 friend class RawTensor;
103
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100104 /** Return value at @p offset in the buffer.
105 *
106 * @param[in] offset Offset within the buffer.
107 */
108 T &operator[](size_t offset);
109
110 /** Return constant value at @p offset in the buffer.
111 *
112 * @param[in] offset Offset within the buffer.
113 */
114 const T &operator[](size_t offset) const;
115
116 /** Shape of the tensor. */
117 TensorShape shape() const override;
118
119 /** Size of each element in the tensor in bytes. */
120 size_t element_size() const override;
121
122 /** Total size of the tensor in bytes. */
123 size_t size() const override;
124
125 /** Image format of the tensor. */
126 Format format() const override;
127
128 /** Data type of the tensor. */
129 DataType data_type() const override;
130
131 /** Number of channels of the tensor. */
132 int num_channels() const override;
133
134 /** Number of elements of the tensor. */
135 int num_elements() const override;
136
Giorgio Arenaa2611812017-07-21 10:08:48 +0100137 /** Available padding around the tensor. */
138 PaddingSize padding() const override;
139
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100140 /** The number of bits for the fractional part of the fixed point numbers. */
141 int fixed_point_position() const override;
142
Chunosovd621bca2017-11-03 17:33:15 +0700143 /** Quantization info in case of asymmetric quantized type */
144 QuantizationInfo quantization_info() const override;
145
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100146 /** Constant pointer to the underlying buffer. */
147 const T *data() const;
148
149 /** Pointer to the underlying buffer. */
150 T *data();
151
152 /** Read only access to the specified element.
153 *
154 * @param[in] coord Coordinates of the desired element.
155 *
156 * @return A pointer to the desired element.
157 */
158 const void *operator()(const Coordinates &coord) const override;
159
160 /** Access to the specified element.
161 *
162 * @param[in] coord Coordinates of the desired element.
163 *
164 * @return A pointer to the desired element.
165 */
166 void *operator()(const Coordinates &coord) override;
167
168 /** Swaps the content of the provided tensors.
169 *
170 * @param[in, out] tensor1 Tensor to be swapped.
171 * @param[in, out] tensor2 Tensor to be swapped.
172 */
173 template <typename U>
174 friend void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2);
175
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100176protected:
Chunosovd621bca2017-11-03 17:33:15 +0700177 Buffer _buffer{ nullptr };
178 TensorShape _shape{};
179 Format _format{ Format::UNKNOWN };
180 DataType _data_type{ DataType::UNKNOWN };
181 int _num_channels{ 0 };
182 int _fixed_point_position{ 0 };
183 QuantizationInfo _quantization_info{};
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100184};
185
186template <typename T>
187SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format, int fixed_point_position)
188 : _buffer(nullptr),
189 _shape(shape),
190 _format(format),
Chunosovd621bca2017-11-03 17:33:15 +0700191 _fixed_point_position(fixed_point_position),
192 _quantization_info()
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100193{
194 _buffer = support::cpp14::make_unique<T[]>(num_elements() * num_channels());
195}
196
197template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700198SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, int fixed_point_position, QuantizationInfo quantization_info)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100199 : _buffer(nullptr),
200 _shape(shape),
201 _data_type(data_type),
202 _num_channels(num_channels),
Chunosovd621bca2017-11-03 17:33:15 +0700203 _fixed_point_position(fixed_point_position),
204 _quantization_info(quantization_info)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100205{
206 _buffer = support::cpp14::make_unique<T[]>(num_elements() * this->num_channels());
207}
208
209template <typename T>
210SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor)
211 : _buffer(nullptr),
212 _shape(tensor.shape()),
213 _format(tensor.format()),
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100214 _data_type(tensor.data_type()),
215 _num_channels(tensor.num_channels()),
Chunosovd621bca2017-11-03 17:33:15 +0700216 _fixed_point_position(tensor.fixed_point_position()),
217 _quantization_info(tensor.quantization_info())
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100218{
219 _buffer = support::cpp14::make_unique<T[]>(tensor.num_elements() * num_channels());
220 std::copy_n(tensor.data(), num_elements() * num_channels(), _buffer.get());
221}
222
223template <typename T>
224SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor)
225{
226 swap(*this, tensor);
227
228 return *this;
229}
230
231template <typename T>
232T &SimpleTensor<T>::operator[](size_t offset)
233{
234 return _buffer[offset];
235}
236
237template <typename T>
238const T &SimpleTensor<T>::operator[](size_t offset) const
239{
240 return _buffer[offset];
241}
242
243template <typename T>
244TensorShape SimpleTensor<T>::shape() const
245{
246 return _shape;
247}
248
249template <typename T>
250size_t SimpleTensor<T>::element_size() const
251{
252 return num_channels() * element_size_from_data_type(data_type());
253}
254
255template <typename T>
256int SimpleTensor<T>::fixed_point_position() const
257{
258 return _fixed_point_position;
259}
260
261template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700262QuantizationInfo SimpleTensor<T>::quantization_info() const
263{
264 return _quantization_info;
265}
266
267template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100268size_t SimpleTensor<T>::size() const
269{
270 const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>());
271 return size * element_size();
272}
273
274template <typename T>
275Format SimpleTensor<T>::format() const
276{
277 return _format;
278}
279
280template <typename T>
281DataType SimpleTensor<T>::data_type() const
282{
283 if(_format != Format::UNKNOWN)
284 {
285 return data_type_from_format(_format);
286 }
287 else
288 {
289 return _data_type;
290 }
291}
292
293template <typename T>
294int SimpleTensor<T>::num_channels() const
295{
296 switch(_format)
297 {
298 case Format::U8:
299 case Format::S16:
300 case Format::U16:
301 case Format::S32:
302 case Format::U32:
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100303 case Format::F32:
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100304 return 1;
305 case Format::RGB888:
306 return 3;
307 case Format::UNKNOWN:
308 return _num_channels;
309 default:
310 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
311 }
312}
313
314template <typename T>
315int SimpleTensor<T>::num_elements() const
316{
317 return _shape.total_size();
318}
319
320template <typename T>
Giorgio Arenaa2611812017-07-21 10:08:48 +0100321PaddingSize SimpleTensor<T>::padding() const
322{
323 return PaddingSize(0);
324}
325
326template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100327const T *SimpleTensor<T>::data() const
328{
329 return _buffer.get();
330}
331
332template <typename T>
333T *SimpleTensor<T>::data()
334{
335 return _buffer.get();
336}
337
338template <typename T>
339const void *SimpleTensor<T>::operator()(const Coordinates &coord) const
340{
341 return _buffer.get() + coord2index(_shape, coord);
342}
343
344template <typename T>
345void *SimpleTensor<T>::operator()(const Coordinates &coord)
346{
347 return _buffer.get() + coord2index(_shape, coord);
348}
349
350template <typename U>
351void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2)
352{
353 // Use unqualified call to swap to enable ADL. But make std::swap available
354 // as backup.
355 using std::swap;
356 swap(tensor1._shape, tensor2._shape);
357 swap(tensor1._format, tensor2._format);
358 swap(tensor1._data_type, tensor2._data_type);
359 swap(tensor1._num_channels, tensor2._num_channels);
360 swap(tensor1._buffer, tensor2._buffer);
361}
362} // namespace test
363} // namespace arm_compute
364#endif /* __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ */