Compute Library
 21.05
SimpleTensor< T > Class Template Reference

Simple tensor object that stores elements in a consecutive chunk of memory. More...

#include <SimpleTensor.h>

Collaboration diagram for SimpleTensor< T >:
[legend]

Public Types

using value_type = T
 Tensor value type. More...
 
using Buffer = std::unique_ptr< value_type[]>
 Tensor buffer pointer type. More...
 

Public Member Functions

 SimpleTensor ()=default
 Create an uninitialised tensor. More...
 
 SimpleTensor (TensorShape shape, Format format)
 Create an uninitialised tensor of the given shape and format. More...
 
 SimpleTensor (TensorShape shape, DataType data_type, int num_channels=1, QuantizationInfo quantization_info=QuantizationInfo(), DataLayout data_layout=DataLayout::NCHW)
 Create an uninitialised tensor of the given shape and data type. More...
 
 SimpleTensor (const SimpleTensor &tensor)
 Create a deep copy of the given tensor. More...
 
SimpleTensoroperator= (SimpleTensor tensor)
 Create a deep copy of the given tensor. More...
 
 SimpleTensor (SimpleTensor &&)=default
 Allow instances of this class to be move constructed. More...
 
 ~SimpleTensor ()=default
 Default destructor. More...
 
T & operator[] (size_t offset)
 Return value at offset in the buffer. More...
 
const T & operator[] (size_t offset) const
 Return constant value at offset in the buffer. More...
 
TensorShape shape () const override
 Shape of the tensor. More...
 
size_t element_size () const override
 Size of each element in the tensor in bytes. More...
 
size_t size () const override
 Total size of the tensor in bytes. More...
 
Format format () const override
 Image format of the tensor. More...
 
DataLayout data_layout () const override
 Data layout of the tensor. More...
 
DataType data_type () const override
 Data type of the tensor. More...
 
int num_channels () const override
 Number of channels of the tensor. More...
 
int num_elements () const override
 Number of elements of the tensor. More...
 
PaddingSize padding () const override
 Available padding around the tensor. More...
 
QuantizationInfo quantization_info () const override
 Quantization info in case of asymmetric quantized type. More...
 
const T * data () const
 Constant pointer to the underlying buffer. More...
 
T * data ()
 Pointer to the underlying buffer. More...
 
const void * operator() (const Coordinates &coord) const override
 Read only access to the specified element. More...
 
void * operator() (const Coordinates &coord) override
 Access to the specified element. More...
 
- Public Member Functions inherited from IAccessor
virtual ~IAccessor ()=default
 Virtual destructor. More...
 

Friends

class RawTensor
 
template<typename U >
void swap (SimpleTensor< U > &tensor1, SimpleTensor< U > &tensor2)
 Swaps the content of the provided tensors. More...
 

Detailed Description

template<typename T>
class arm_compute::test::SimpleTensor< T >

Simple tensor object that stores elements in a consecutive chunk of memory.

It can be created by either loading an image from a file which also initialises the content of the tensor or by explcitly specifying the size. The latter leaves the content uninitialised.

Furthermore, the class provides methods to convert the tensor's values into different image format.

Definition at line 58 of file SimpleTensor.h.

Member Typedef Documentation

◆ Buffer

using Buffer = std::unique_ptr<value_type[]>

Tensor buffer pointer type.

Definition at line 105 of file SimpleTensor.h.

◆ value_type

using value_type = T

Tensor value type.

Definition at line 103 of file SimpleTensor.h.

Constructor & Destructor Documentation

◆ SimpleTensor() [1/5]

SimpleTensor ( )
default

Create an uninitialised tensor.

◆ SimpleTensor() [2/5]

SimpleTensor ( TensorShape  shape,
Format  format 
)

Create an uninitialised tensor of the given shape and format.

Parameters
[in]shapeShape of the new raw tensor.
[in]formatFormat of the new raw tensor.

Definition at line 262 of file SimpleTensor.h.

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 }
Format format() const override
Image format of the tensor.
Definition: SimpleTensor.h:345
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
Num samples, channels, height, width.
int num_channels() const override
Number of channels of the tensor.
Definition: SimpleTensor.h:370
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406

◆ SimpleTensor() [3/5]

SimpleTensor ( TensorShape  shape,
DataType  data_type,
int  num_channels = 1,
QuantizationInfo  quantization_info = QuantizationInfo(),
DataLayout  data_layout = DataLayout::NCHW 
)

Create an uninitialised tensor of the given shape and data type.

Parameters
[in]shapeShape of the new raw tensor.
[in]data_typeData type of the new raw tensor.
[in]num_channels(Optional) Number of channels (default = 1).
[in]quantization_info(Optional) Quantization info for asymmetric quantization (default = empty).
[in]data_layout(Optional) Data layout of the tensor (default = NCHW).

Definition at line 274 of file SimpleTensor.h.

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 }
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:172
DataLayout data_layout() const override
Data layout of the tensor.
Definition: SimpleTensor.h:351
int num_channels() const override
Number of channels of the tensor.
Definition: SimpleTensor.h:370
QuantizationInfo quantization_info() const override
Quantization info in case of asymmetric quantized type.
Definition: SimpleTensor.h:332

◆ SimpleTensor() [4/5]

SimpleTensor ( const SimpleTensor< T > &  tensor)

Create a deep copy of the given tensor.

Parameters
[in]tensorTo be copied tensor.

Definition at line 286 of file SimpleTensor.h.

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 }
size_t total_size() const
Collapses all dimensions to a single linear total size.
Definition: TensorShape.h:172

◆ SimpleTensor() [5/5]

SimpleTensor ( SimpleTensor< T > &&  )
default

Allow instances of this class to be move constructed.

◆ ~SimpleTensor()

~SimpleTensor ( )
default

Default destructor.

Member Function Documentation

◆ data() [1/2]

◆ data() [2/2]

T * data ( )

Pointer to the underlying buffer.

Returns
a pointer to the data.

Definition at line 424 of file SimpleTensor.h.

425 {
426  return _buffer.get();
427 }

◆ data_layout()

DataLayout data_layout ( ) const
overridevirtual

Data layout of the tensor.

Returns
the data layout of the tensor.

Implements IAccessor.

Definition at line 351 of file SimpleTensor.h.

352 {
353  return _data_layout;
354 }

Referenced by arm_compute::test::copy_tensor(), arm_compute::test::validation::reference::deconvolution_layer(), arm_compute::test::validation::reference::winograd_filter_transform(), and arm_compute::test::validation::reference::winograd_input_transform().

◆ data_type()

DataType data_type ( ) const
overridevirtual

Data type of the tensor.

Returns
the data type of the tensor.

Implements IAccessor.

Definition at line 357 of file SimpleTensor.h.

358 {
359  if(_format != Format::UNKNOWN)
360  {
361  return data_type_from_format(_format);
362  }
363  else
364  {
365  return _data_type;
366  }
367 }
DataType data_type_from_format(Format format)
Return the data type used by a given single-planar pixel format.
Definition: Utils.h:219

Referenced by arm_compute::test::validation::reference::bitwise_and(), arm_compute::test::validation::reference::bitwise_or(), arm_compute::test::validation::reference::bitwise_xor(), arm_compute::test::validation::reference::bounding_box_transform(), arm_compute::test::validation::reference::compare(), arm_compute::test::validation::reference::compute_all_anchors(), arm_compute::test::copy_tensor(), arm_compute::test::validation::reference::deconvolution_layer(), AssetsLibrary::fill(), VerifyAccessor< D >::fill_tensor(), arm_compute::test::validation::reference::gemm(), arm_compute::test::validation::reference::gemm_mixed_precision(), arm_compute::test::validation::reference::gemm_reshape_lhs_matrix(), arm_compute::test::validation::reference::gemm_reshape_rhs_matrix(), arm_compute::test::validation::reference::gemm_transpose_1xW(), arm_compute::test::validation::reference::logical_and(), arm_compute::test::validation::reference::logical_or(), arm_compute::test::validation::reference::pixel_wise_multiplication(), arm_compute::test::validation::reference::prior_box_layer(), arm_compute::test::validation::reference::remap(), arm_compute::test::validation::reference::scale_core(), arm_compute::test::validation::reference::select(), arm_compute::test::validation::validate(), arm_compute::test::validation::validate_wrap(), arm_compute::test::validation::reference::winograd_filter_transform(), arm_compute::test::validation::reference::winograd_input_transform(), and arm_compute::test::validation::reference::winograd_output_transform().

◆ element_size()

size_t element_size ( ) const
overridevirtual

Size of each element in the tensor in bytes.

Returns
the size of each element in the tensor in bytes.

Implements IAccessor.

Definition at line 326 of file SimpleTensor.h.

327 {
329 }
size_t element_size_from_data_type(DataType dt)
The size in bytes of the data type.
Definition: Utils.h:185
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
int num_channels() const override
Number of channels of the tensor.
Definition: SimpleTensor.h:370

Referenced by AssetsLibrary::fill(), RawTensor::RawTensor(), arm_compute::test::validation::validate(), and arm_compute::test::validation::validate_wrap().

◆ format()

Format format ( ) const
overridevirtual

Image format of the tensor.

Returns
the format of the tensor.

Implements IAccessor.

Definition at line 345 of file SimpleTensor.h.

346 {
347  return _format;
348 }

Referenced by RawTensor::RawTensor(), arm_compute::test::validation::validate(), and arm_compute::test::validation::validate_wrap().

◆ num_channels()

int num_channels ( ) const
overridevirtual

Number of channels of the tensor.

Returns
the number of channels of the tensor.

Implements IAccessor.

Definition at line 370 of file SimpleTensor.h.

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 }
A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte.
1 channel, 1 U8 per channel
1 channel, 1 F32 per channel
A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling.
1 channel, 1 U16 per channel
A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling.
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
3 channels, 1 U8 per channel
1 channel, 1 U32 per channel
A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes.
1 channel, 1 S16 per channel
A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes.
4 channels, 1 U8 per channel
2 channel, 1 U8 per channel
A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes.

Referenced by arm_compute::test::copy_tensor(), RawTensor::RawTensor(), arm_compute::test::validation::validate(), and arm_compute::test::validation::validate_wrap().

◆ num_elements()

◆ operator()() [1/2]

const void * operator() ( const Coordinates coord) const
overridevirtual

Read only access to the specified element.

Parameters
[in]coordCoordinates of the desired element.
Returns
A pointer to the desired element.

Implements IAccessor.

Definition at line 430 of file SimpleTensor.h.

431 {
432  return _buffer.get() + coord2index(_shape, coord) * _num_channels;
433 }
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:387

◆ operator()() [2/2]

void * operator() ( const Coordinates coord)
overridevirtual

Access to the specified element.

Parameters
[in]coordCoordinates of the desired element.
Returns
A pointer to the desired element.

Implements IAccessor.

Definition at line 436 of file SimpleTensor.h.

437 {
438  return _buffer.get() + coord2index(_shape, coord) * _num_channels;
439 }
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:387

◆ operator=()

SimpleTensor< T > & operator= ( SimpleTensor< T >  tensor)

Create a deep copy of the given tensor.

Parameters
[in]tensorTo be copied tensor.
Returns
a copy of the given tensor.

Definition at line 300 of file SimpleTensor.h.

301 {
302  swap(*this, tensor);
303 
304  return *this;
305 }
friend void swap(SimpleTensor< U > &tensor1, SimpleTensor< U > &tensor2)
Swaps the content of the provided tensors.
Definition: SimpleTensor.h:442

◆ operator[]() [1/2]

T & operator[] ( size_t  offset)

Return value at offset in the buffer.

Parameters
[in]offsetOffset within the buffer.
Returns
value in the buffer.

Definition at line 308 of file SimpleTensor.h.

309 {
310  return _buffer[offset];
311 }
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:861

◆ operator[]() [2/2]

const T & operator[] ( size_t  offset) const

Return constant value at offset in the buffer.

Parameters
[in]offsetOffset within the buffer.
Returns
constant value in the buffer.

Definition at line 314 of file SimpleTensor.h.

315 {
316  return _buffer[offset];
317 }
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Definition: helpers.h:861

◆ padding()

PaddingSize padding ( ) const
overridevirtual

Available padding around the tensor.

Returns
the available padding around the tensor.

Implements IAccessor.

Definition at line 412 of file SimpleTensor.h.

413 {
414  return PaddingSize(0);
415 }
BorderSize PaddingSize
Container for 2D padding size.
Definition: Types.h:382

◆ quantization_info()

◆ shape()

TensorShape shape ( ) const
overridevirtual

Shape of the tensor.

Returns
the shape of the tensor.

Implements IAccessor.

Definition at line 320 of file SimpleTensor.h.

321 {
322  return _shape;
323 }

Referenced by arm_compute::test::validation::reference::absolute_difference(), arm_compute::test::validation::reference::arithmetic_division(), arm_compute::test::validation::reference::arithmetic_operation(), arm_compute::test::validation::reference::bitwise_and(), arm_compute::test::validation::reference::bitwise_or(), arm_compute::test::validation::reference::bitwise_xor(), arm_compute::test::validation::reference::bounding_box_transform(), arm_compute::test::colorconvert_helper::detail::colorconvert_rgb_to_iyuv(), arm_compute::test::colorconvert_helper::detail::colorconvert_rgb_to_nv12(), arm_compute::test::validation::reference::compare(), arm_compute::test::validation::reference::compute_all_anchors(), arm_compute::test::validation::reference::convolution_layer_nchw(), arm_compute::test::copy_tensor(), arm_compute::test::validation::reference::crop_and_resize(), arm_compute::test::validation::reference::deconvolution_layer(), AssetsLibrary::fill(), arm_compute::test::validation::reference::fully_connected_layer(), arm_compute::test::validation::reference::gather(), arm_compute::test::validation::reference::gemm(), arm_compute::test::validation::reference::gemm_interleave_4x4(), arm_compute::test::validation::reference::gemm_interleave_blocked(), arm_compute::test::validation::reference::gemm_mixed_precision(), arm_compute::test::validation::reference::gemm_reshape_lhs_matrix(), arm_compute::test::validation::reference::gemm_reshape_rhs_matrix(), arm_compute::test::validation::reference::gemm_transpose_1xW(), arm_compute::test::validation::reference::gemmlowp_matrix_multiply_core(), arm_compute::test::validation::reference::gemmlowp_quantize_down_scale(), arm_compute::test::validation::reference::gemmlowp_quantize_down_scale_by_fixedpoint(), arm_compute::test::validation::reference::gemmlowp_quantize_down_scale_by_float(), AssetsLibrary::get(), AssetsLibrary::get_image_shape(), arm_compute::test::validation::get_tile(), arm_compute::test::validation::reference::l2_normalize(), arm_compute::test::validation::reference::logical_and(), arm_compute::test::validation::reference::logical_or(), arm_compute::test::validation::matrix_multiply(), arm_compute::test::validation::reference::max_unpooling_layer_internal(), arm_compute::test::validation::reference::non_max_suppression(), arm_compute::test::validation::reference::pixel_wise_multiplication(), arm_compute::test::validation::reference::prior_box_layer(), arm_compute::test::validation::reference::remap(), arm_compute::test::validation::reference::reverse(), arm_compute::test::colorconvert_helper::detail::rgb_to_yuv_calculation(), arm_compute::test::validation::reference::roi_align_layer(), arm_compute::test::validation::reference::roi_pool_layer(), arm_compute::test::validation::reference::scale_core(), arm_compute::test::validation::reference::select(), arm_compute::test::validation::transpose_matrix(), arm_compute::test::validation::reference::unstack(), arm_compute::test::validation::validate(), arm_compute::test::validation::validate_wrap(), arm_compute::test::validation::reference::winograd_filter_transform(), arm_compute::test::validation::reference::winograd_input_transform(), arm_compute::test::validation::reference::winograd_output_transform(), and arm_compute::test::validation::zeros().

◆ size()

size_t size ( ) const
overridevirtual

Total size of the tensor in bytes.

Returns
the total size of the tensor in bytes.

Implements IAccessor.

Definition at line 338 of file SimpleTensor.h.

339 {
340  const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<size_t>());
341  return size * element_size();
342 }
size_t element_size() const override
Size of each element in the tensor in bytes.
Definition: SimpleTensor.h:326
size_t size() const override
Total size of the tensor in bytes.
Definition: SimpleTensor.h:338
SimpleTensor< T2 > accumulate(const SimpleTensor< T1 > &src, DataType output_data_type)
Definition: Accumulate.cpp:38
std::array< T, num_max_dimensions >::const_iterator cend() const
Returns a read-only (constant) iterator that points one past the last element in the dimension array.
Definition: Dimensions.h:255
std::array< T, num_max_dimensions >::const_iterator cbegin() const
Returns a read-only (constant) iterator that points to the first element in the dimension array.
Definition: Dimensions.h:231

Referenced by AssetsLibrary::fill(), and arm_compute::test::validation::reference::weights_reshape().

Friends And Related Function Documentation

◆ RawTensor

friend class RawTensor
friend

Definition at line 107 of file SimpleTensor.h.

◆ swap

void swap ( SimpleTensor< U > &  tensor1,
SimpleTensor< U > &  tensor2 
)
friend

Swaps the content of the provided tensors.

Parameters
[in,out]tensor1Tensor to be swapped.
[in,out]tensor2Tensor to be swapped.

Definition at line 442 of file SimpleTensor.h.

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 }
friend void swap(SimpleTensor< U > &tensor1, SimpleTensor< U > &tensor2)
Swaps the content of the provided tensors.
Definition: SimpleTensor.h:442
void swap(SimpleTensor< U > &tensor1, SimpleTensor< U > &tensor2)
Definition: SimpleTensor.h:442

The documentation for this class was generated from the following file: