xref: /aosp_15_r20/external/ComputeLibrary/tests/SimpleTensor.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2017-2020 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_TEST_SIMPLE_TENSOR_H
25*c217d954SCole Faust #define ARM_COMPUTE_TEST_SIMPLE_TENSOR_H
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/core/TensorShape.h"
28*c217d954SCole Faust #include "arm_compute/core/Types.h"
29*c217d954SCole Faust #include "arm_compute/core/Utils.h"
30*c217d954SCole Faust #include "tests/IAccessor.h"
31*c217d954SCole Faust #include "tests/Utils.h"
32*c217d954SCole Faust 
33*c217d954SCole Faust #include <algorithm>
34*c217d954SCole Faust #include <array>
35*c217d954SCole Faust #include <cstddef>
36*c217d954SCole Faust #include <cstdint>
37*c217d954SCole Faust #include <functional>
38*c217d954SCole Faust #include <memory>
39*c217d954SCole Faust #include <stdexcept>
40*c217d954SCole Faust #include <utility>
41*c217d954SCole Faust 
42*c217d954SCole Faust namespace arm_compute
43*c217d954SCole Faust {
44*c217d954SCole Faust namespace test
45*c217d954SCole Faust {
46*c217d954SCole Faust class RawTensor;
47*c217d954SCole Faust 
48*c217d954SCole Faust /** Simple tensor object that stores elements in a consecutive chunk of memory.
49*c217d954SCole Faust  *
50*c217d954SCole Faust  * It can be created by either loading an image from a file which also
51*c217d954SCole Faust  * initialises the content of the tensor or by explcitly specifying the size.
52*c217d954SCole Faust  * The latter leaves the content uninitialised.
53*c217d954SCole Faust  *
54*c217d954SCole Faust  * Furthermore, the class provides methods to convert the tensor's values into
55*c217d954SCole Faust  * different image format.
56*c217d954SCole Faust  */
57*c217d954SCole Faust template <typename T>
58*c217d954SCole Faust class SimpleTensor : public IAccessor
59*c217d954SCole Faust {
60*c217d954SCole Faust public:
61*c217d954SCole Faust     /** Create an uninitialised tensor. */
62*c217d954SCole Faust     SimpleTensor() = default;
63*c217d954SCole Faust 
64*c217d954SCole Faust     /** Create an uninitialised tensor of the given @p shape and @p format.
65*c217d954SCole Faust      *
66*c217d954SCole Faust      * @param[in] shape  Shape of the new raw tensor.
67*c217d954SCole Faust      * @param[in] format Format of the new raw tensor.
68*c217d954SCole Faust      */
69*c217d954SCole Faust     SimpleTensor(TensorShape shape, Format format);
70*c217d954SCole Faust 
71*c217d954SCole Faust     /** Create an uninitialised tensor of the given @p shape and @p data type.
72*c217d954SCole Faust      *
73*c217d954SCole Faust      * @param[in] shape             Shape of the new raw tensor.
74*c217d954SCole Faust      * @param[in] data_type         Data type of the new raw tensor.
75*c217d954SCole Faust      * @param[in] num_channels      (Optional) Number of channels (default = 1).
76*c217d954SCole Faust      * @param[in] quantization_info (Optional) Quantization info for asymmetric quantization (default = empty).
77*c217d954SCole Faust      * @param[in] data_layout       (Optional) Data layout of the tensor (default = NCHW).
78*c217d954SCole Faust      */
79*c217d954SCole Faust     SimpleTensor(TensorShape shape, DataType data_type,
80*c217d954SCole Faust                  int              num_channels      = 1,
81*c217d954SCole Faust                  QuantizationInfo quantization_info = QuantizationInfo(),
82*c217d954SCole Faust                  DataLayout       data_layout       = DataLayout::NCHW);
83*c217d954SCole Faust 
84*c217d954SCole Faust     /** Create a deep copy of the given @p tensor.
85*c217d954SCole Faust      *
86*c217d954SCole Faust      * @param[in] tensor To be copied tensor.
87*c217d954SCole Faust      */
88*c217d954SCole Faust     SimpleTensor(const SimpleTensor &tensor);
89*c217d954SCole Faust 
90*c217d954SCole Faust     /** Create a deep copy of the given @p tensor.
91*c217d954SCole Faust      *
92*c217d954SCole Faust      * @param[in] tensor To be copied tensor.
93*c217d954SCole Faust      *
94*c217d954SCole Faust      * @return a copy of the given tensor.
95*c217d954SCole Faust      */
96*c217d954SCole Faust     SimpleTensor &operator=(SimpleTensor tensor);
97*c217d954SCole Faust     /** Allow instances of this class to be move constructed */
98*c217d954SCole Faust     SimpleTensor(SimpleTensor &&) = default;
99*c217d954SCole Faust     /** Default destructor. */
100*c217d954SCole Faust     ~SimpleTensor() = default;
101*c217d954SCole Faust 
102*c217d954SCole Faust     /** Tensor value type */
103*c217d954SCole Faust     using value_type = T;
104*c217d954SCole Faust     /** Tensor buffer pointer type */
105*c217d954SCole Faust     using Buffer = std::unique_ptr<value_type[]>;
106*c217d954SCole Faust 
107*c217d954SCole Faust     friend class RawTensor;
108*c217d954SCole Faust 
109*c217d954SCole Faust     /** Return value at @p offset in the buffer.
110*c217d954SCole Faust      *
111*c217d954SCole Faust      * @param[in] offset Offset within the buffer.
112*c217d954SCole Faust      *
113*c217d954SCole Faust      * @return value in the buffer.
114*c217d954SCole Faust      */
115*c217d954SCole Faust     T &operator[](size_t offset);
116*c217d954SCole Faust 
117*c217d954SCole Faust     /** Return constant value at @p offset in the buffer.
118*c217d954SCole Faust      *
119*c217d954SCole Faust      * @param[in] offset Offset within the buffer.
120*c217d954SCole Faust      *
121*c217d954SCole Faust      * @return constant value in the buffer.
122*c217d954SCole Faust      */
123*c217d954SCole Faust     const T &operator[](size_t offset) const;
124*c217d954SCole Faust 
125*c217d954SCole Faust     /** Shape of the tensor.
126*c217d954SCole Faust      *
127*c217d954SCole Faust      * @return the shape of the tensor.
128*c217d954SCole Faust      */
129*c217d954SCole Faust     TensorShape shape() const override;
130*c217d954SCole Faust     /** Size of each element in the tensor in bytes.
131*c217d954SCole Faust      *
132*c217d954SCole Faust      * @return the size of each element in the tensor in bytes.
133*c217d954SCole Faust      */
134*c217d954SCole Faust     size_t element_size() const override;
135*c217d954SCole Faust     /** Total size of the tensor in bytes.
136*c217d954SCole Faust      *
137*c217d954SCole Faust      * @return the total size of the tensor in bytes.
138*c217d954SCole Faust      */
139*c217d954SCole Faust     size_t size() const override;
140*c217d954SCole Faust     /** Image format of the tensor.
141*c217d954SCole Faust      *
142*c217d954SCole Faust      * @return the format of the tensor.
143*c217d954SCole Faust      */
144*c217d954SCole Faust     Format format() const override;
145*c217d954SCole Faust     /** Data layout of the tensor.
146*c217d954SCole Faust      *
147*c217d954SCole Faust      * @return the data layout of the tensor.
148*c217d954SCole Faust      */
149*c217d954SCole Faust     DataLayout data_layout() const override;
150*c217d954SCole Faust     /** Data type of the tensor.
151*c217d954SCole Faust      *
152*c217d954SCole Faust      * @return the data type of the tensor.
153*c217d954SCole Faust      */
154*c217d954SCole Faust     DataType data_type() const override;
155*c217d954SCole Faust     /** Number of channels of the tensor.
156*c217d954SCole Faust      *
157*c217d954SCole Faust      * @return the number of channels of the tensor.
158*c217d954SCole Faust      */
159*c217d954SCole Faust     int num_channels() const override;
160*c217d954SCole Faust     /** Number of elements of the tensor.
161*c217d954SCole Faust      *
162*c217d954SCole Faust      * @return the number of elements of the tensor.
163*c217d954SCole Faust      */
164*c217d954SCole Faust     int num_elements() const override;
165*c217d954SCole Faust     /** Available padding around the tensor.
166*c217d954SCole Faust      *
167*c217d954SCole Faust      * @return the available padding around the tensor.
168*c217d954SCole Faust      */
169*c217d954SCole Faust     PaddingSize padding() const override;
170*c217d954SCole Faust     /** Quantization info in case of asymmetric quantized type
171*c217d954SCole Faust      *
172*c217d954SCole Faust      * @return
173*c217d954SCole Faust      */
174*c217d954SCole Faust     QuantizationInfo quantization_info() const override;
175*c217d954SCole Faust 
176*c217d954SCole Faust     /** Constant pointer to the underlying buffer.
177*c217d954SCole Faust      *
178*c217d954SCole Faust      * @return a constant pointer to the data.
179*c217d954SCole Faust      */
180*c217d954SCole Faust     const T *data() const;
181*c217d954SCole Faust 
182*c217d954SCole Faust     /** Pointer to the underlying buffer.
183*c217d954SCole Faust      *
184*c217d954SCole Faust      * @return a pointer to the data.
185*c217d954SCole Faust      */
186*c217d954SCole Faust     T *data();
187*c217d954SCole Faust 
188*c217d954SCole Faust     /** Read only access to the specified element.
189*c217d954SCole Faust      *
190*c217d954SCole Faust      * @param[in] coord Coordinates of the desired element.
191*c217d954SCole Faust      *
192*c217d954SCole Faust      * @return A pointer to the desired element.
193*c217d954SCole Faust      */
194*c217d954SCole Faust     const void *operator()(const Coordinates &coord) const override;
195*c217d954SCole Faust 
196*c217d954SCole Faust     /** Access to the specified element.
197*c217d954SCole Faust      *
198*c217d954SCole Faust      * @param[in] coord Coordinates of the desired element.
199*c217d954SCole Faust      *
200*c217d954SCole Faust      * @return A pointer to the desired element.
201*c217d954SCole Faust      */
202*c217d954SCole Faust     void *operator()(const Coordinates &coord) override;
203*c217d954SCole Faust 
204*c217d954SCole Faust     /** Swaps the content of the provided tensors.
205*c217d954SCole Faust      *
206*c217d954SCole Faust      * @param[in, out] tensor1 Tensor to be swapped.
207*c217d954SCole Faust      * @param[in, out] tensor2 Tensor to be swapped.
208*c217d954SCole Faust      */
209*c217d954SCole Faust     template <typename U>
210*c217d954SCole Faust     friend void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2);
211*c217d954SCole Faust 
212*c217d954SCole Faust protected:
213*c217d954SCole Faust     Buffer           _buffer{ nullptr };
214*c217d954SCole Faust     TensorShape      _shape{};
215*c217d954SCole Faust     Format           _format{ Format::UNKNOWN };
216*c217d954SCole Faust     DataType         _data_type{ DataType::UNKNOWN };
217*c217d954SCole Faust     int              _num_channels{ 0 };
218*c217d954SCole Faust     QuantizationInfo _quantization_info{};
219*c217d954SCole Faust     DataLayout       _data_layout{ DataLayout::UNKNOWN };
220*c217d954SCole Faust };
221*c217d954SCole Faust 
222*c217d954SCole Faust template <typename T1, typename T2>
copy_tensor(const SimpleTensor<T2> & tensor)223*c217d954SCole Faust SimpleTensor<T1> copy_tensor(const SimpleTensor<T2> &tensor)
224*c217d954SCole Faust {
225*c217d954SCole Faust     SimpleTensor<T1> st(tensor.shape(), tensor.data_type(),
226*c217d954SCole Faust                         tensor.num_channels(),
227*c217d954SCole Faust                         tensor.quantization_info(),
228*c217d954SCole Faust                         tensor.data_layout());
229*c217d954SCole Faust     for(size_t n = 0; n < size_t(st.num_elements()); n++)
230*c217d954SCole Faust     {
231*c217d954SCole Faust         st.data()[n] = static_cast<T1>(tensor.data()[n]);
232*c217d954SCole Faust     }
233*c217d954SCole Faust     return st;
234*c217d954SCole Faust }
235*c217d954SCole Faust 
236*c217d954SCole Faust 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*c217d954SCole Faust SimpleTensor<T1> copy_tensor(const SimpleTensor<half> &tensor)
238*c217d954SCole Faust {
239*c217d954SCole Faust     SimpleTensor<T1> st(tensor.shape(), tensor.data_type(),
240*c217d954SCole Faust                         tensor.num_channels(),
241*c217d954SCole Faust                         tensor.quantization_info(),
242*c217d954SCole Faust                         tensor.data_layout());
243*c217d954SCole Faust     memcpy((void *)st.data(), (const void *)tensor.data(), size_t(st.num_elements() * sizeof(T1)));
244*c217d954SCole Faust     return st;
245*c217d954SCole Faust }
246*c217d954SCole Faust 
247*c217d954SCole Faust 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*c217d954SCole Faust SimpleTensor<T1> copy_tensor(const SimpleTensor<half> &tensor)
249*c217d954SCole Faust {
250*c217d954SCole Faust     SimpleTensor<T1> st(tensor.shape(), tensor.data_type(),
251*c217d954SCole Faust                         tensor.num_channels(),
252*c217d954SCole Faust                         tensor.quantization_info(),
253*c217d954SCole Faust                         tensor.data_layout());
254*c217d954SCole Faust     for(size_t n = 0; n < size_t(st.num_elements()); n++)
255*c217d954SCole Faust     {
256*c217d954SCole Faust         st.data()[n] = half_float::detail::half_cast<T1, T2>(tensor.data()[n]);
257*c217d954SCole Faust     }
258*c217d954SCole Faust     return st;
259*c217d954SCole Faust }
260*c217d954SCole Faust 
261*c217d954SCole Faust template <typename T>
SimpleTensor(TensorShape shape,Format format)262*c217d954SCole Faust SimpleTensor<T>::SimpleTensor(TensorShape shape, Format format)
263*c217d954SCole Faust     : _buffer(nullptr),
264*c217d954SCole Faust       _shape(shape),
265*c217d954SCole Faust       _format(format),
266*c217d954SCole Faust       _quantization_info(),
267*c217d954SCole Faust       _data_layout(DataLayout::NCHW)
268*c217d954SCole Faust {
269*c217d954SCole Faust     _num_channels = num_channels();
270*c217d954SCole Faust     _buffer       = std::make_unique<T[]>(num_elements() * _num_channels);
271*c217d954SCole Faust }
272*c217d954SCole Faust 
273*c217d954SCole Faust template <typename T>
SimpleTensor(TensorShape shape,DataType data_type,int num_channels,QuantizationInfo quantization_info,DataLayout data_layout)274*c217d954SCole Faust SimpleTensor<T>::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, QuantizationInfo quantization_info, DataLayout data_layout)
275*c217d954SCole Faust     : _buffer(nullptr),
276*c217d954SCole Faust       _shape(shape),
277*c217d954SCole Faust       _data_type(data_type),
278*c217d954SCole Faust       _num_channels(num_channels),
279*c217d954SCole Faust       _quantization_info(quantization_info),
280*c217d954SCole Faust       _data_layout(data_layout)
281*c217d954SCole Faust {
282*c217d954SCole Faust     _buffer = std::make_unique<T[]>(this->_shape.total_size() * _num_channels);
283*c217d954SCole Faust }
284*c217d954SCole Faust 
285*c217d954SCole Faust template <typename T>
SimpleTensor(const SimpleTensor & tensor)286*c217d954SCole Faust SimpleTensor<T>::SimpleTensor(const SimpleTensor &tensor)
287*c217d954SCole Faust     : _buffer(nullptr),
288*c217d954SCole Faust       _shape(tensor.shape()),
289*c217d954SCole Faust       _format(tensor.format()),
290*c217d954SCole Faust       _data_type(tensor.data_type()),
291*c217d954SCole Faust       _num_channels(tensor.num_channels()),
292*c217d954SCole Faust       _quantization_info(tensor.quantization_info()),
293*c217d954SCole Faust       _data_layout(tensor.data_layout())
294*c217d954SCole Faust {
295*c217d954SCole Faust     _buffer = std::make_unique<T[]>(tensor.num_elements() * _num_channels);
296*c217d954SCole Faust     std::copy_n(tensor.data(), this->_shape.total_size() * _num_channels, _buffer.get());
297*c217d954SCole Faust }
298*c217d954SCole Faust 
299*c217d954SCole Faust template <typename T>
300*c217d954SCole Faust SimpleTensor<T> &SimpleTensor<T>::operator=(SimpleTensor tensor)
301*c217d954SCole Faust {
302*c217d954SCole Faust     swap(*this, tensor);
303*c217d954SCole Faust 
304*c217d954SCole Faust     return *this;
305*c217d954SCole Faust }
306*c217d954SCole Faust 
307*c217d954SCole Faust template <typename T>
308*c217d954SCole Faust T &SimpleTensor<T>::operator[](size_t offset)
309*c217d954SCole Faust {
310*c217d954SCole Faust     return _buffer[offset];
311*c217d954SCole Faust }
312*c217d954SCole Faust 
313*c217d954SCole Faust template <typename T>
314*c217d954SCole Faust const T &SimpleTensor<T>::operator[](size_t offset) const
315*c217d954SCole Faust {
316*c217d954SCole Faust     return _buffer[offset];
317*c217d954SCole Faust }
318*c217d954SCole Faust 
319*c217d954SCole Faust template <typename T>
shape()320*c217d954SCole Faust TensorShape SimpleTensor<T>::shape() const
321*c217d954SCole Faust {
322*c217d954SCole Faust     return _shape;
323*c217d954SCole Faust }
324*c217d954SCole Faust 
325*c217d954SCole Faust template <typename T>
element_size()326*c217d954SCole Faust size_t SimpleTensor<T>::element_size() const
327*c217d954SCole Faust {
328*c217d954SCole Faust     return num_channels() * element_size_from_data_type(data_type());
329*c217d954SCole Faust }
330*c217d954SCole Faust 
331*c217d954SCole Faust template <typename T>
quantization_info()332*c217d954SCole Faust QuantizationInfo SimpleTensor<T>::quantization_info() const
333*c217d954SCole Faust {
334*c217d954SCole Faust     return _quantization_info;
335*c217d954SCole Faust }
336*c217d954SCole Faust 
337*c217d954SCole Faust template <typename T>
size()338*c217d954SCole Faust size_t SimpleTensor<T>::size() const
339*c217d954SCole Faust {
340*c217d954SCole Faust     const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>());
341*c217d954SCole Faust     return size * element_size();
342*c217d954SCole Faust }
343*c217d954SCole Faust 
344*c217d954SCole Faust template <typename T>
format()345*c217d954SCole Faust Format SimpleTensor<T>::format() const
346*c217d954SCole Faust {
347*c217d954SCole Faust     return _format;
348*c217d954SCole Faust }
349*c217d954SCole Faust 
350*c217d954SCole Faust template <typename T>
data_layout()351*c217d954SCole Faust DataLayout SimpleTensor<T>::data_layout() const
352*c217d954SCole Faust {
353*c217d954SCole Faust     return _data_layout;
354*c217d954SCole Faust }
355*c217d954SCole Faust 
356*c217d954SCole Faust template <typename T>
data_type()357*c217d954SCole Faust DataType SimpleTensor<T>::data_type() const
358*c217d954SCole Faust {
359*c217d954SCole Faust     if(_format != Format::UNKNOWN)
360*c217d954SCole Faust     {
361*c217d954SCole Faust         return data_type_from_format(_format);
362*c217d954SCole Faust     }
363*c217d954SCole Faust     else
364*c217d954SCole Faust     {
365*c217d954SCole Faust         return _data_type;
366*c217d954SCole Faust     }
367*c217d954SCole Faust }
368*c217d954SCole Faust 
369*c217d954SCole Faust template <typename T>
num_channels()370*c217d954SCole Faust int SimpleTensor<T>::num_channels() const
371*c217d954SCole Faust {
372*c217d954SCole Faust     switch(_format)
373*c217d954SCole Faust     {
374*c217d954SCole Faust         case Format::U8:
375*c217d954SCole Faust         case Format::U16:
376*c217d954SCole Faust         case Format::S16:
377*c217d954SCole Faust         case Format::U32:
378*c217d954SCole Faust         case Format::S32:
379*c217d954SCole Faust         case Format::F16:
380*c217d954SCole Faust         case Format::F32:
381*c217d954SCole Faust             return 1;
382*c217d954SCole Faust         // Because the U and V channels are subsampled
383*c217d954SCole Faust         // these formats appear like having only 2 channels:
384*c217d954SCole Faust         case Format::YUYV422:
385*c217d954SCole Faust         case Format::UYVY422:
386*c217d954SCole Faust             return 2;
387*c217d954SCole Faust         case Format::UV88:
388*c217d954SCole Faust             return 2;
389*c217d954SCole Faust         case Format::RGB888:
390*c217d954SCole Faust             return 3;
391*c217d954SCole Faust         case Format::RGBA8888:
392*c217d954SCole Faust             return 4;
393*c217d954SCole Faust         case Format::UNKNOWN:
394*c217d954SCole Faust             return _num_channels;
395*c217d954SCole Faust         //Doesn't make sense for planar formats:
396*c217d954SCole Faust         case Format::NV12:
397*c217d954SCole Faust         case Format::NV21:
398*c217d954SCole Faust         case Format::IYUV:
399*c217d954SCole Faust         case Format::YUV444:
400*c217d954SCole Faust         default:
401*c217d954SCole Faust             return 0;
402*c217d954SCole Faust     }
403*c217d954SCole Faust }
404*c217d954SCole Faust 
405*c217d954SCole Faust template <typename T>
num_elements()406*c217d954SCole Faust int SimpleTensor<T>::num_elements() const
407*c217d954SCole Faust {
408*c217d954SCole Faust     return _shape.total_size();
409*c217d954SCole Faust }
410*c217d954SCole Faust 
411*c217d954SCole Faust template <typename T>
padding()412*c217d954SCole Faust PaddingSize SimpleTensor<T>::padding() const
413*c217d954SCole Faust {
414*c217d954SCole Faust     return PaddingSize(0);
415*c217d954SCole Faust }
416*c217d954SCole Faust 
417*c217d954SCole Faust template <typename T>
data()418*c217d954SCole Faust const T *SimpleTensor<T>::data() const
419*c217d954SCole Faust {
420*c217d954SCole Faust     return _buffer.get();
421*c217d954SCole Faust }
422*c217d954SCole Faust 
423*c217d954SCole Faust template <typename T>
data()424*c217d954SCole Faust T *SimpleTensor<T>::data()
425*c217d954SCole Faust {
426*c217d954SCole Faust     return _buffer.get();
427*c217d954SCole Faust }
428*c217d954SCole Faust 
429*c217d954SCole Faust template <typename T>
operator()430*c217d954SCole Faust const void *SimpleTensor<T>::operator()(const Coordinates &coord) const
431*c217d954SCole Faust {
432*c217d954SCole Faust     return _buffer.get() + coord2index(_shape, coord) * _num_channels;
433*c217d954SCole Faust }
434*c217d954SCole Faust 
435*c217d954SCole Faust template <typename T>
operator()436*c217d954SCole Faust void *SimpleTensor<T>::operator()(const Coordinates &coord)
437*c217d954SCole Faust {
438*c217d954SCole Faust     return _buffer.get() + coord2index(_shape, coord) * _num_channels;
439*c217d954SCole Faust }
440*c217d954SCole Faust 
441*c217d954SCole Faust template <typename U>
swap(SimpleTensor<U> & tensor1,SimpleTensor<U> & tensor2)442*c217d954SCole Faust void swap(SimpleTensor<U> &tensor1, SimpleTensor<U> &tensor2)
443*c217d954SCole Faust {
444*c217d954SCole Faust     // Use unqualified call to swap to enable ADL. But make std::swap available
445*c217d954SCole Faust     // as backup.
446*c217d954SCole Faust     using std::swap;
447*c217d954SCole Faust     swap(tensor1._shape, tensor2._shape);
448*c217d954SCole Faust     swap(tensor1._format, tensor2._format);
449*c217d954SCole Faust     swap(tensor1._data_type, tensor2._data_type);
450*c217d954SCole Faust     swap(tensor1._num_channels, tensor2._num_channels);
451*c217d954SCole Faust     swap(tensor1._quantization_info, tensor2._quantization_info);
452*c217d954SCole Faust     swap(tensor1._buffer, tensor2._buffer);
453*c217d954SCole Faust }
454*c217d954SCole Faust } // namespace test
455*c217d954SCole Faust } // namespace arm_compute
456*c217d954SCole Faust #endif /* ARM_COMPUTE_TEST_SIMPLE_TENSOR_H */
457