• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2020 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 "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 
42 namespace arm_compute
43 {
44 namespace test
45 {
46 class RawTensor;
47 
48 /** 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  */
57 template <typename T>
58 class SimpleTensor : public IAccessor
59 {
60 public:
61     /** Create an uninitialised tensor. */
62     SimpleTensor() = default;
63 
64     /** Create an uninitialised tensor of the given @p shape and @p format.
65      *
66      * @param[in] shape  Shape of the new raw tensor.
67      * @param[in] format Format of the new raw tensor.
68      */
69     SimpleTensor(TensorShape shape, Format format);
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] quantization_info (Optional) Quantization info for asymmetric quantization (default = empty).
77      * @param[in] data_layout       (Optional) Data layout of the tensor (default = NCHW).
78      */
79     SimpleTensor(TensorShape shape, DataType data_type,
80                  int              num_channels      = 1,
81                  QuantizationInfo quantization_info = QuantizationInfo(),
82                  DataLayout       data_layout       = DataLayout::NCHW);
83 
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.
93      *
94      * @return a copy of the given tensor.
95      */
96     SimpleTensor &operator=(SimpleTensor tensor);
97     /** Allow instances of this class to be move constructed */
98     SimpleTensor(SimpleTensor &&) = default;
99     /** Default destructor. */
100     ~SimpleTensor() = default;
101 
102     /** Tensor value type */
103     using value_type = T;
104     /** Tensor buffer pointer type */
105     using Buffer = std::unique_ptr<value_type[]>;
106 
107     friend class RawTensor;
108 
109     /** Return value at @p offset in the buffer.
110      *
111      * @param[in] offset Offset within the buffer.
112      *
113      * @return value in the buffer.
114      */
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.
120      *
121      * @return constant value in the buffer.
122      */
123     const T &operator[](size_t offset) const;
124 
125     /** Shape of the tensor.
126      *
127      * @return the shape of the tensor.
128      */
129     TensorShape shape() const override;
130     /** Size of each element in the tensor in bytes.
131      *
132      * @return the size of each element in the tensor in bytes.
133      */
134     size_t element_size() const override;
135     /** Total size of the tensor in bytes.
136      *
137      * @return the total size of the tensor in bytes.
138      */
139     size_t size() const override;
140     /** Image format of the tensor.
141      *
142      * @return the format of the tensor.
143      */
144     Format format() const override;
145     /** 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      */
154     DataType data_type() const override;
155     /** Number of channels of the tensor.
156      *
157      * @return the number of channels of the tensor.
158      */
159     int num_channels() const override;
160     /** Number of elements of the tensor.
161      *
162      * @return the number of elements of the tensor.
163      */
164     int num_elements() const override;
165     /** Available padding around the tensor.
166      *
167      * @return the available padding around the tensor.
168      */
169     PaddingSize padding() const override;
170     /** Quantization info in case of asymmetric quantized type
171      *
172      * @return
173      */
174     QuantizationInfo quantization_info() const override;
175 
176     /** Constant pointer to the underlying buffer.
177      *
178      * @return a constant pointer to the data.
179      */
180     const T *data() const;
181 
182     /** Pointer to the underlying buffer.
183      *
184      * @return a pointer to the data.
185      */
186     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 
212 protected:
213     Buffer           _buffer{ nullptr };
214     TensorShape      _shape{};
215     Format           _format{ Format::UNKNOWN };
216     DataType         _data_type{ DataType::UNKNOWN };
217     int              _num_channels{ 0 };
218     QuantizationInfo _quantization_info{};
219     DataLayout       _data_layout{ DataLayout::UNKNOWN };
220 };
221 
222 template <typename T1, typename T2>
copy_tensor(const SimpleTensor<T2> & tensor)223 SimpleTensor<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 
236 template <typename T1, typename T2, typename std::enable_if<std::is_same<T1, T2>::value, int>::type = 0>
copy_tensor(const SimpleTensor<half> & tensor)237 SimpleTensor<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 
247 template < typename T1, typename T2, typename std::enable_if < (std::is_same<T1, half>::value || std::is_same<T2, half>::value), int >::type = 0 >
copy_tensor(const SimpleTensor<half> & tensor)248 SimpleTensor<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 
261 template <typename T>
SimpleTensor(TensorShape shape,Format format)262 SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format)
263     : _buffer(nullptr),
264       _shape(shape),
265       _format(format),
266       _quantization_info(),
267       _data_layout(DataLayout::NCHW)
268 {
269     _num_channels = num_channels();
270     _buffer       = std::make_unique<T[]>(num_elements() * _num_channels);
271 }
272 
273 template <typename T>
SimpleTensor(TensorShape shape,DataType data_type,int num_channels,QuantizationInfo quantization_info,DataLayout data_layout)274 SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, QuantizationInfo quantization_info, DataLayout data_layout)
275     : _buffer(nullptr),
276       _shape(shape),
277       _data_type(data_type),
278       _num_channels(num_channels),
279       _quantization_info(quantization_info),
280       _data_layout(data_layout)
281 {
282     _buffer = std::make_unique<T[]>(this->_shape.total_size() * _num_channels);
283 }
284 
285 template <typename T>
SimpleTensor(const SimpleTensor & tensor)286 SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor)
287     : _buffer(nullptr),
288       _shape(tensor.shape()),
289       _format(tensor.format()),
290       _data_type(tensor.data_type()),
291       _num_channels(tensor.num_channels()),
292       _quantization_info(tensor.quantization_info()),
293       _data_layout(tensor.data_layout())
294 {
295     _buffer = std::make_unique<T[]>(tensor.num_elements() * _num_channels);
296     std::copy_n(tensor.data(), this->_shape.total_size() * _num_channels, _buffer.get());
297 }
298 
299 template <typename T>
300 SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor)
301 {
302     swap(*this, tensor);
303 
304     return *this;
305 }
306 
307 template <typename T>
308 T &SimpleTensor<T>::operator[](size_t offset)
309 {
310     return _buffer[offset];
311 }
312 
313 template <typename T>
314 const T &SimpleTensor<T>::operator[](size_t offset) const
315 {
316     return _buffer[offset];
317 }
318 
319 template <typename T>
shape()320 TensorShape SimpleTensor<T>::shape() const
321 {
322     return _shape;
323 }
324 
325 template <typename T>
element_size()326 size_t SimpleTensor<T>::element_size() const
327 {
328     return num_channels() * element_size_from_data_type(data_type());
329 }
330 
331 template <typename T>
quantization_info()332 QuantizationInfo SimpleTensor<T>::quantization_info() const
333 {
334     return _quantization_info;
335 }
336 
337 template <typename T>
size()338 size_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 
344 template <typename T>
format()345 Format SimpleTensor<T>::format() const
346 {
347     return _format;
348 }
349 
350 template <typename T>
data_layout()351 DataLayout SimpleTensor<T>::data_layout() const
352 {
353     return _data_layout;
354 }
355 
356 template <typename T>
data_type()357 DataType 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 
369 template <typename T>
num_channels()370 int SimpleTensor<T>::num_channels() const
371 {
372     switch(_format)
373     {
374         case Format::U8:
375         case Format::U16:
376         case Format::S16:
377         case Format::U32:
378         case Format::S32:
379         case Format::F16:
380         case Format::F32:
381             return 1;
382         // 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;
389         case Format::RGB888:
390             return 3;
391         case Format::RGBA8888:
392             return 4;
393         case Format::UNKNOWN:
394             return _num_channels;
395         //Doesn't make sense for planar formats:
396         case Format::NV12:
397         case Format::NV21:
398         case Format::IYUV:
399         case Format::YUV444:
400         default:
401             return 0;
402     }
403 }
404 
405 template <typename T>
num_elements()406 int SimpleTensor<T>::num_elements() const
407 {
408     return _shape.total_size();
409 }
410 
411 template <typename T>
padding()412 PaddingSize SimpleTensor<T>::padding() const
413 {
414     return PaddingSize(0);
415 }
416 
417 template <typename T>
data()418 const T *SimpleTensor<T>::data() const
419 {
420     return _buffer.get();
421 }
422 
423 template <typename T>
data()424 T *SimpleTensor<T>::data()
425 {
426     return _buffer.get();
427 }
428 
429 template <typename T>
operator()430 const void *SimpleTensor<T>::operator()(const Coordinates &coord) const
431 {
432     return _buffer.get() + coord2index(_shape, coord) * _num_channels;
433 }
434 
435 template <typename T>
operator()436 void *SimpleTensor<T>::operator()(const Coordinates &coord)
437 {
438     return _buffer.get() + coord2index(_shape, coord) * _num_channels;
439 }
440 
441 template <typename U>
swap(SimpleTensor<U> & tensor1,SimpleTensor<U> & tensor2)442 void 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);
451     swap(tensor1._quantization_info, tensor2._quantization_info);
452     swap(tensor1._buffer, tensor2._buffer);
453 }
454 } // namespace test
455 } // namespace arm_compute
456 #endif /* ARM_COMPUTE_TEST_SIMPLE_TENSOR_H */
457