Compute Library  18.05
TensorShape Class Reference

Shape of a tensor. More...

#include <TensorShape.h>

Collaboration diagram for TensorShape:
[legend]

Public Member Functions

template<typename... Ts>
 TensorShape (Ts...dims)
 Constructor to initialize the tensor shape. More...
 
 TensorShape (const TensorShape &)=default
 Allow instances of this class to be copy constructed. More...
 
TensorShapeoperator= (const TensorShape &)=default
 Allow instances of this class to be copied. More...
 
 TensorShape (TensorShape &&)=default
 Allow instances of this class to be move constructed. More...
 
TensorShapeoperator= (TensorShape &&)=default
 Allow instances of this class to be moved. More...
 
 ~TensorShape ()=default
 Default destructor. More...
 
TensorShapeset (size_t dimension, size_t value, bool apply_dim_correction=true)
 Accessor to set the value of one of the dimensions. More...
 
void remove_dimension (size_t n)
 Accessor to remove the dimension n from the tensor shape. More...
 
void collapse (size_t n, size_t first=0)
 Collapse the first n dimensions. More...
 
TensorShape collapsed_from (size_t start) const
 Return a copy with collapsed dimensions starting from a given point. More...
 
size_t total_size () const
 Collapses all dimensions to a single linear total size. More...
 
size_t total_size_upper (size_t dimension) const
 Collapses given dimension and above. More...
 
size_t total_size_lower (size_t dimension) const
 Compute size of dimensions lower than the given one. More...
 
- Public Member Functions inherited from Dimensions< size_t >
 Dimensions (Ts...dims)
 Constructor to initialize the tensor shape. More...
 
 Dimensions (const Dimensions &)=default
 Allow instances of this class to be copy constructed. More...
 
 Dimensions (Dimensions &&)=default
 Allow instances of this class to be move constructed. More...
 
Dimensionsoperator= (const Dimensions &)=default
 Allow instances of this class to be copied. More...
 
Dimensionsoperator= (Dimensions &&)=default
 Allow instances of this class to be moved. More...
 
void set (size_t dimension, size_tvalue)
 Accessor to set the value of one of the dimensions. More...
 
size_t x () const
 Alias to access the size of the first dimension. More...
 
size_t y () const
 Alias to access the size of the second dimension. More...
 
size_t z () const
 Alias to access the size of the third dimension. More...
 
const size_t & operator[] (size_t dimension) const
 Generic accessor to get the size of any dimension. More...
 
size_t & operator[] (size_t dimension)
 Generic accessor to get the size of any dimension. More...
 
unsigned int num_dimensions () const
 Returns the effective dimensionality of the tensor. More...
 
void set_num_dimensions (size_t num_dimensions)
 Set number of dimensions. More...
 
void collapse (const size_t n, const size_t first=0)
 Collapse dimensions. More...
 
void collapse_from (size_t start)
 Collapse dimensions starting from a given point. More...
 
std::array< size_t, num_max_dimensions >::iterator begin ()
 Returns a read/write iterator that points to the first element in the dimension array. More...
 
std::array< size_t, num_max_dimensions >::const_iterator begin () const
 Returns a read-only (constant) iterator that points to the first element in the dimension array. More...
 
std::array< size_t, num_max_dimensions >::const_iterator cbegin () const
 Returns a read-only (constant) iterator that points to the first element in the dimension array. More...
 
std::array< size_t, num_max_dimensions >::iterator end ()
 Returns a read/write iterator that points one past the last element in the dimension array. More...
 
std::array< size_t, num_max_dimensions >::const_iterator end () const
 Returns a read-only (constant) iterator that points one past the last element in the dimension array. More...
 
std::array< size_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. More...
 

Static Public Member Functions

template<typename... Shapes>
static TensorShape broadcast_shape (const Shapes &...shapes)
 If shapes are broadcast compatible, return the broadcasted shape. More...
 

Additional Inherited Members

- Static Public Attributes inherited from Dimensions< size_t >
static constexpr size_t num_max_dimensions
 Number of dimensions the tensor has. More...
 

Detailed Description

Shape of a tensor.

Definition at line 39 of file TensorShape.h.

Constructor & Destructor Documentation

TensorShape ( Ts...  dims)
inline

Constructor to initialize the tensor shape.

Parameters
[in]dimsValues to initialize the dimensions.

Definition at line 47 of file TensorShape.h.

References TensorShape::operator=(), and TensorShape::~TensorShape().

48  : Dimensions{ dims... }
49  {
50  // Initialize unspecified dimensions to 1
51  if(_num_dimensions > 0)
52  {
53  std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
54  }
55 
56  // Correct number dimensions to ignore trailing dimensions of size 1
57  apply_dimension_correction();
58  }
Dimensions(Ts...dims)
Constructor to initialize the tensor shape.
Definition: Dimensions.h:52
TensorShape ( const TensorShape )
default

Allow instances of this class to be copy constructed.

TensorShape ( TensorShape &&  )
default

Allow instances of this class to be move constructed.

~TensorShape ( )
default

Default destructor.

Referenced by TensorShape::TensorShape().

Member Function Documentation

static TensorShape broadcast_shape ( const Shapes &...  shapes)
inlinestatic

If shapes are broadcast compatible, return the broadcasted shape.

Two tensor shapes are broadcast compatible if for each dimension, they're equal or one of them is 1.

If two shapes are compatible, each dimension in the broadcasted shape is the max of the original dimensions.

Parameters
[in]shapesTensor shapes.
Returns
The broadcasted shape or an empty shape if the shapes are not broadcast compatible.

Definition at line 196 of file TensorShape.h.

References arm_compute::utility::for_each(), arm_compute::test::fixed_point_arithmetic::detail::max(), arm_compute::test::fixed_point_arithmetic::detail::min(), Dimensions< T >::num_dimensions(), Dimensions< size_t >::num_max_dimensions, TensorShape::set(), and arm_compute::U.

Referenced by arm_compute::test::validation::reference::arithmetic_addition(), ITensorInfo::broadcast_shape_and_valid_region(), and arm_compute::test::validation::reference::pixel_wise_multiplication().

197  {
198  TensorShape bc_shape;
199 
200  auto broadcast = [&bc_shape](const TensorShape & other)
201  {
202  if(bc_shape.num_dimensions() == 0)
203  {
204  bc_shape = other;
205  }
206  else if(other.num_dimensions() != 0)
207  {
208  for(size_t d = 0; d < TensorShape::num_max_dimensions; ++d)
209  {
210  const size_t dim_min = std::min(bc_shape[d], other[d]);
211  const size_t dim_max = std::max(bc_shape[d], other[d]);
212 
213  if((dim_min != 1) && (dim_min != dim_max))
214  {
215  bc_shape = TensorShape{ 0U };
216  break;
217  }
218 
219  bc_shape.set(d, dim_max);
220  }
221  }
222  };
223 
224  utility::for_each(broadcast, shapes...);
225 
226  return bc_shape;
227  }
fixed_point< T > min(fixed_point< T > x, fixed_point< T > y)
Definition: FixedPoint.h:897
TensorShape(Ts...dims)
Constructor to initialize the tensor shape.
Definition: TensorShape.h:47
void for_each(F &&)
Base case of for_each.
Definition: Utility.h:91
fixed_point< T > max(fixed_point< T > x, fixed_point< T > y)
Definition: FixedPoint.h:902
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45
void collapse ( size_t  n,
size_t  first = 0 
)
inline

Collapse the first n dimensions.

Parameters
[in]nNumber of dimensions to collapse into first
[in]firstDimensions into which the following n are collapsed.

Definition at line 132 of file TensorShape.h.

References Dimensions< T >::collapse().

Referenced by TensorShape::collapsed_from(), and arm_compute::misc::shape_calculator::compute_weights_reshaped_shape().

133  {
134  Dimensions::collapse(n, first);
135 
136  // Make sure all empty dimensions are filled with 1
137  std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
138  }
void collapse(const size_t n, const size_t first=0)
Collapse dimensions.
Definition: Dimensions.h:138
TensorShape collapsed_from ( size_t  start) const
inline

Return a copy with collapsed dimensions starting from a given point.

Parameters
[in]startStarting point of collapsing dimensions.
Returns
A copy with collapse dimensions starting from start.

Definition at line 146 of file TensorShape.h.

References TensorShape::collapse(), arm_compute::test::validation::reference::copy(), and Dimensions< size_t >::num_dimensions().

147  {
148  TensorShape copy(*this);
149  copy.collapse(num_dimensions(), start);
150  return copy;
151  }
SimpleTensor< T > copy(const SimpleTensor< T > &src, const TensorShape &output_shape)
Definition: Copy.cpp:37
TensorShape(Ts...dims)
Constructor to initialize the tensor shape.
Definition: TensorShape.h:47
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:122
TensorShape& operator= ( const TensorShape )
default

Allow instances of this class to be copied.

Referenced by TensorShape::TensorShape().

TensorShape& operator= ( TensorShape &&  )
default

Allow instances of this class to be moved.

void remove_dimension ( size_t  n)
inline

Accessor to remove the dimension n from the tensor shape.

Note
The upper dimensions of the tensor shape will be shifted down by 1
Parameters
[in]nDimension to remove

Definition at line 110 of file TensorShape.h.

References ARM_COMPUTE_ERROR_ON, and arm_compute::test::validation::reference::copy().

Referenced by arm_compute::misc::shape_calculator::compute_reductionA_shape(), arm_compute::misc::shape_calculator::compute_reductionB_shape(), and arm_compute::test::validation::DATA_TEST_CASE().

111  {
112  ARM_COMPUTE_ERROR_ON(_num_dimensions < 1);
113  ARM_COMPUTE_ERROR_ON(n >= _num_dimensions);
114 
115  std::copy(_id.begin() + n + 1, _id.end(), _id.begin() + n);
116 
117  // Reduce number of dimensions
118  _num_dimensions--;
119 
120  // Make sure all empty dimensions are filled with 1
121  std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
122 
123  // Correct number dimensions to ignore trailing dimensions of size 1
124  apply_dimension_correction();
125  }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:328
SimpleTensor< T > copy(const SimpleTensor< T > &src, const TensorShape &output_shape)
Definition: Copy.cpp:37
TensorShape& set ( size_t  dimension,
size_t  value,
bool  apply_dim_correction = true 
)
inline

Accessor to set the value of one of the dimensions.

Parameters
[in]dimensionDimension for which the value is set.
[in]valueValue to be set for the dimension.
[in]apply_dim_correctionFlag to state whether apply dimension correction after setting one dimension. E.g. when permuting NCHW -> NHWC, 1x1x2 would become 2x1x1, but _num_dimensions should be 3 rather than 1.
Returns
*this.

Definition at line 78 of file TensorShape.h.

References Dimensions< T >::set().

Referenced by arm_compute::adjust_odd_shape(), TensorShape::broadcast_shape(), arm_compute::calculate_depth_concatenate_shape(), arm_compute::calculate_subsampled_shape(), arm_compute::misc::shape_calculator::calculate_width_concatenate_shape(), arm_compute::misc::shape_calculator::compute_col2im_shape(), arm_compute::misc::shape_calculator::compute_interleaved_shape(), arm_compute::misc::shape_calculator::compute_mm_shape(), arm_compute::misc::shape_calculator::compute_reductionB_shape(), arm_compute::misc::shape_calculator::compute_transpose1xW_shape(), arm_compute::misc::shape_calculator::compute_transpose1xW_with_element_size_shape(), arm_compute::misc::shape_calculator::compute_transposed_shape(), arm_compute::test::validation::DATA_TEST_CASE(), arm_compute::test::validation::reference::deconvolution_layer(), arm_compute::test::index2coord(), arm_compute::index2coords(), NPYLoader::init_tensor(), arm_compute::intersect_valid_regions(), arm_compute::permute(), arm_compute::test::validation::reference::scale(), arm_compute::test::shape_to_valid_region(), arm_compute::test::shape_to_valid_region_gaussian_pyramid_half(), and arm_compute::test::validation::transpose().

79  {
80  // Clear entire shape if one dimension is zero
81  if(value == 0)
82  {
83  _num_dimensions = 0;
84  std::fill(_id.begin(), _id.end(), 0);
85  }
86  else
87  {
88  // Make sure all empty dimensions are filled with 1
89  std::fill(_id.begin() + _num_dimensions, _id.end(), 1);
90 
91  // Set the specified dimension and increase the number of dimensions if
92  // necessary
93  Dimensions::set(dimension, value);
94 
95  // Correct number dimensions to ignore trailing dimensions of size 1
96  if(apply_dim_correction)
97  {
98  apply_dimension_correction();
99  }
100  }
101  return *this;
102  }
void set(size_t dimension, T value)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:74
size_t total_size ( ) const
inline

Collapses all dimensions to a single linear total size.

Returns
The total tensor size in terms of elements.

Definition at line 157 of file TensorShape.h.

References accumulate().

Referenced by arm_compute::auto_init_if_empty(), arm_compute::test::coord2index(), arm_compute::coords2index(), arm_compute::test::validation::DATA_TEST_CASE(), arm_compute::test::validation::reference::depthwise_convolution(), arm_compute::test::index2coord(), arm_compute::index2coords(), GCAccessor::num_elements(), Accessor::num_elements(), CLAccessor::num_elements(), SimpleTensor< T >::num_elements(), arm_compute::test::validation::reference::reshape_layer(), arm_compute::set_shape_if_empty(), and SubTensorInfo::set_valid_region().

158  {
159  return std::accumulate(_id.begin(), _id.end(), 1, std::multiplies<size_t>());
160  }
__kernel void accumulate(__global uchar *input_ptr, uint input_stride_x, uint input_step_x, uint input_stride_y, uint input_step_y, uint input_offset_first_element_in_bytes, __global uchar *accu_ptr, uint accu_stride_x, uint accu_step_x, uint accu_stride_y, uint accu_step_y, uint accu_offset_first_element_in_bytes)
This function accumulates an input image into output image.
Definition: accumulate.cl:41
size_t total_size_lower ( size_t  dimension) const
inline

Compute size of dimensions lower than the given one.

Parameters
[in]dimensionUpper boundary.
Returns
The linear size of the collapsed dimensions.

Definition at line 179 of file TensorShape.h.

References accumulate(), ARM_COMPUTE_ERROR_ON, and Dimensions< size_t >::num_max_dimensions.

180  {
182  return std::accumulate(_id.begin(), _id.begin() + dimension, 1, std::multiplies<size_t>());
183  }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:328
__kernel void accumulate(__global uchar *input_ptr, uint input_stride_x, uint input_step_x, uint input_stride_y, uint input_step_y, uint input_offset_first_element_in_bytes, __global uchar *accu_ptr, uint accu_stride_x, uint accu_step_x, uint accu_stride_y, uint accu_step_y, uint accu_offset_first_element_in_bytes)
This function accumulates an input image into output image.
Definition: accumulate.cl:41
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45
size_t total_size_upper ( size_t  dimension) const
inline

Collapses given dimension and above.

Parameters
[in]dimensionSize of the wanted dimension
Returns
The linear size of the collapsed dimensions

Definition at line 167 of file TensorShape.h.

References accumulate(), ARM_COMPUTE_ERROR_ON, and Dimensions< size_t >::num_max_dimensions.

Referenced by arm_compute::test::validation::reference::fully_connected_layer(), and arm_compute::setup_assembly_kernel().

168  {
170  return std::accumulate(_id.begin() + dimension, _id.end(), 1, std::multiplies<size_t>());
171  }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:328
__kernel void accumulate(__global uchar *input_ptr, uint input_stride_x, uint input_step_x, uint input_stride_y, uint input_step_y, uint input_offset_first_element_in_bytes, __global uchar *accu_ptr, uint accu_stride_x, uint accu_step_x, uint accu_stride_y, uint accu_step_y, uint accu_offset_first_element_in_bytes)
This function accumulates an input image into output image.
Definition: accumulate.cl:41
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45

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