blob: ea955fa73017fcfb6b48936184f775899b6dc193 [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{
47/** Simple tensor object that stores elements in a consecutive chunk of memory.
48 *
49 * It can be created by either loading an image from a file which also
50 * initialises the content of the tensor or by explcitly specifying the size.
51 * The latter leaves the content uninitialised.
52 *
53 * Furthermore, the class provides methods to convert the tensor's values into
54 * different image format.
55 */
56template <typename T>
Moritz Pflanzer82e70a12017-08-08 16:20:45 +010057class SimpleTensor : public IAccessor
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010058{
59public:
60 /** Create an uninitialised tensor. */
61 SimpleTensor() = default;
62
63 /** Create an uninitialised tensor of the given @p shape and @p format.
64 *
65 * @param[in] shape Shape of the new raw tensor.
66 * @param[in] format Format of the new raw tensor.
67 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
68 */
69 SimpleTensor(TensorShape shape, Format format, int fixed_point_position = 0);
70
71 /** Create an uninitialised tensor of the given @p shape and @p data type.
72 *
73 * @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] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers (default = 0).
77 */
78 SimpleTensor(TensorShape shape, DataType data_type, int num_channels = 1, int fixed_point_position = 0);
79
80 /** Create a deep copy of the given @p tensor.
81 *
82 * @param[in] tensor To be copied tensor.
83 */
84 SimpleTensor(const SimpleTensor &tensor);
85
86 /** Create a deep copy of the given @p tensor.
87 *
88 * @param[in] tensor To be copied tensor.
89 */
90 SimpleTensor &operator =(SimpleTensor tensor);
91 SimpleTensor(SimpleTensor &&) = default;
92 ~SimpleTensor() = default;
93
94 using value_type = T;
95 using Buffer = std::unique_ptr<value_type[]>;
96
97 /** Return value at @p offset in the buffer.
98 *
99 * @param[in] offset Offset within the buffer.
100 */
101 T &operator[](size_t offset);
102
103 /** Return constant value at @p offset in the buffer.
104 *
105 * @param[in] offset Offset within the buffer.
106 */
107 const T &operator[](size_t offset) const;
108
109 /** Shape of the tensor. */
110 TensorShape shape() const override;
111
112 /** Size of each element in the tensor in bytes. */
113 size_t element_size() const override;
114
115 /** Total size of the tensor in bytes. */
116 size_t size() const override;
117
118 /** Image format of the tensor. */
119 Format format() const override;
120
121 /** Data type of the tensor. */
122 DataType data_type() const override;
123
124 /** Number of channels of the tensor. */
125 int num_channels() const override;
126
127 /** Number of elements of the tensor. */
128 int num_elements() const override;
129
Giorgio Arenaa2611812017-07-21 10:08:48 +0100130 /** Available padding around the tensor. */
131 PaddingSize padding() const override;
132
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100133 /** The number of bits for the fractional part of the fixed point numbers. */
134 int fixed_point_position() const override;
135
136 /** Constant pointer to the underlying buffer. */
137 const T *data() const;
138
139 /** Pointer to the underlying buffer. */
140 T *data();
141
142 /** Read only access to the specified element.
143 *
144 * @param[in] coord Coordinates of the desired element.
145 *
146 * @return A pointer to the desired element.
147 */
148 const void *operator()(const Coordinates &coord) const override;
149
150 /** Access to the specified element.
151 *
152 * @param[in] coord Coordinates of the desired element.
153 *
154 * @return A pointer to the desired element.
155 */
156 void *operator()(const Coordinates &coord) override;
157
158 /** Swaps the content of the provided tensors.
159 *
160 * @param[in, out] tensor1 Tensor to be swapped.
161 * @param[in, out] tensor2 Tensor to be swapped.
162 */
163 template <typename U>
164 friend void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2);
165
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100166protected:
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100167 Buffer _buffer{ nullptr };
168 TensorShape _shape{};
169 Format _format{ Format::UNKNOWN };
170 DataType _data_type{ DataType::UNKNOWN };
171 int _num_channels{ 0 };
172 int _fixed_point_position{ 0 };
173};
174
175template <typename T>
176SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format, int fixed_point_position)
177 : _buffer(nullptr),
178 _shape(shape),
179 _format(format),
180 _fixed_point_position(fixed_point_position)
181{
182 _buffer = support::cpp14::make_unique<T[]>(num_elements() * num_channels());
183}
184
185template <typename T>
186SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, int fixed_point_position)
187 : _buffer(nullptr),
188 _shape(shape),
189 _data_type(data_type),
190 _num_channels(num_channels),
191 _fixed_point_position(fixed_point_position)
192{
193 _buffer = support::cpp14::make_unique<T[]>(num_elements() * this->num_channels());
194}
195
196template <typename T>
197SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor)
198 : _buffer(nullptr),
199 _shape(tensor.shape()),
200 _format(tensor.format()),
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100201 _data_type(tensor.data_type()),
202 _num_channels(tensor.num_channels()),
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100203 _fixed_point_position(tensor.fixed_point_position())
204{
205 _buffer = support::cpp14::make_unique<T[]>(tensor.num_elements() * num_channels());
206 std::copy_n(tensor.data(), num_elements() * num_channels(), _buffer.get());
207}
208
209template <typename T>
210SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor)
211{
212 swap(*this, tensor);
213
214 return *this;
215}
216
217template <typename T>
218T &SimpleTensor<T>::operator[](size_t offset)
219{
220 return _buffer[offset];
221}
222
223template <typename T>
224const T &SimpleTensor<T>::operator[](size_t offset) const
225{
226 return _buffer[offset];
227}
228
229template <typename T>
230TensorShape SimpleTensor<T>::shape() const
231{
232 return _shape;
233}
234
235template <typename T>
236size_t SimpleTensor<T>::element_size() const
237{
238 return num_channels() * element_size_from_data_type(data_type());
239}
240
241template <typename T>
242int SimpleTensor<T>::fixed_point_position() const
243{
244 return _fixed_point_position;
245}
246
247template <typename T>
248size_t SimpleTensor<T>::size() const
249{
250 const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>());
251 return size * element_size();
252}
253
254template <typename T>
255Format SimpleTensor<T>::format() const
256{
257 return _format;
258}
259
260template <typename T>
261DataType SimpleTensor<T>::data_type() const
262{
263 if(_format != Format::UNKNOWN)
264 {
265 return data_type_from_format(_format);
266 }
267 else
268 {
269 return _data_type;
270 }
271}
272
273template <typename T>
274int SimpleTensor<T>::num_channels() const
275{
276 switch(_format)
277 {
278 case Format::U8:
279 case Format::S16:
280 case Format::U16:
281 case Format::S32:
282 case Format::U32:
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100283 case Format::F32:
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100284 return 1;
285 case Format::RGB888:
286 return 3;
287 case Format::UNKNOWN:
288 return _num_channels;
289 default:
290 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
291 }
292}
293
294template <typename T>
295int SimpleTensor<T>::num_elements() const
296{
297 return _shape.total_size();
298}
299
300template <typename T>
Giorgio Arenaa2611812017-07-21 10:08:48 +0100301PaddingSize SimpleTensor<T>::padding() const
302{
303 return PaddingSize(0);
304}
305
306template <typename T>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100307const T *SimpleTensor<T>::data() const
308{
309 return _buffer.get();
310}
311
312template <typename T>
313T *SimpleTensor<T>::data()
314{
315 return _buffer.get();
316}
317
318template <typename T>
319const void *SimpleTensor<T>::operator()(const Coordinates &coord) const
320{
321 return _buffer.get() + coord2index(_shape, coord);
322}
323
324template <typename T>
325void *SimpleTensor<T>::operator()(const Coordinates &coord)
326{
327 return _buffer.get() + coord2index(_shape, coord);
328}
329
330template <typename U>
331void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2)
332{
333 // Use unqualified call to swap to enable ADL. But make std::swap available
334 // as backup.
335 using std::swap;
336 swap(tensor1._shape, tensor2._shape);
337 swap(tensor1._format, tensor2._format);
338 swap(tensor1._data_type, tensor2._data_type);
339 swap(tensor1._num_channels, tensor2._num_channels);
340 swap(tensor1._buffer, tensor2._buffer);
341}
342} // namespace test
343} // namespace arm_compute
344#endif /* __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ */