Compute Library
 19.08
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...
 
void shift_right (size_t step)
 Shifts right the tensor shape increasing its 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_t value)
 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...
 
void remove (size_t idx)
 Remove dimension of a given index. 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() [1/3]

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.

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  }
library fill(src, distribution, 0)
Dimensions(Ts... dims)
Constructor to initialize the tensor shape.
Definition: Dimensions.h:52

◆ TensorShape() [2/3]

TensorShape ( const TensorShape )
default

Allow instances of this class to be copy constructed.

◆ TensorShape() [3/3]

TensorShape ( TensorShape &&  )
default

Allow instances of this class to be move constructed.

◆ ~TensorShape()

~TensorShape ( )
default

Default destructor.

Member Function Documentation

◆ broadcast_shape()

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 210 of file TensorShape.h.

211  {
212  TensorShape bc_shape;
213 
214  auto broadcast = [&bc_shape](const TensorShape & other)
215  {
216  if(bc_shape.num_dimensions() == 0)
217  {
218  bc_shape = other;
219  }
220  else if(other.num_dimensions() != 0)
221  {
222  for(size_t d = 0; d < TensorShape::num_max_dimensions; ++d)
223  {
224  const size_t dim_min = std::min(bc_shape[d], other[d]);
225  const size_t dim_max = std::max(bc_shape[d], other[d]);
226 
227  if((dim_min != 1) && (dim_min != dim_max))
228  {
229  bc_shape = TensorShape{ 0U };
230  break;
231  }
232 
233  bc_shape.set(d, dim_max);
234  }
235  }
236  };
237 
238  utility::for_each(broadcast, shapes...);
239 
240  return bc_shape;
241  }
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:93
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45

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

Referenced by arm_compute::test::validation::reference::arithmetic_division(), arm_compute::test::validation::reference::arithmetic_operation(), ITensorInfo::broadcast_shape_and_valid_region(), arm_compute::test::validation::reference::compare(), and arm_compute::test::validation::reference::pixel_wise_multiplication().

◆ collapse()

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.

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
library fill(src, distribution, 0)

References Dimensions< T >::collapse(), and arm_compute::test::validation::fill().

Referenced by arm_compute::misc::shape_calculator::compute_softmax_shape(), NEGEMMLowpMatrixMultiplyKernel::configure(), and FlattenLayerNode::configure_output().

◆ collapsed_from()

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 160 of file TensorShape.h.

161  {
162  TensorShape copy(*this);
163  copy.collapse(num_dimensions() - start, start);
164  return copy;
165  }
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

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

Referenced by CLElementwiseOperationKernel::run(), CLComparisonKernel::run(), CLPixelWiseMultiplicationKernel::run(), NEPixelWiseMultiplicationKernel::run(), and CLComplexPixelWiseMultiplicationKernel::run().

◆ operator=() [1/2]

TensorShape& operator= ( const TensorShape )
default

Allow instances of this class to be copied.

◆ operator=() [2/2]

TensorShape& operator= ( TensorShape &&  )
default

Allow instances of this class to be moved.

◆ remove_dimension()

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.

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:337
SimpleTensor< T > copy(const SimpleTensor< T > &src, const TensorShape &output_shape)
Definition: Copy.cpp:37
library fill(src, distribution, 0)

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

Referenced by arm_compute::misc::shape_calculator::compute_lhs_reshaped_shape(), arm_compute::misc::shape_calculator::compute_reductionB_shape(), CLReduceMean::configure(), NEReduceMean::configure(), CLFFTConvolutionLayer::configure(), and NEFFTConvolutionLayer::configure().

◆ set()

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.

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
library fill(src, distribution, 0)

Referenced by GraphBuilder::add_convolution_node(), GraphBuilder::add_deconvolution_node(), GraphBuilder::add_depthwise_convolution_node(), GraphBuilder::add_scale_layer(), TensorShape::broadcast_shape(), ITensorInfo::broadcast_shape_and_valid_region(), arm_compute::misc::shape_calculator::calculate_concatenate_shape(), arm_compute::calculate_valid_region_scale(), arm_compute::misc::shape_calculator::compute_deconvolution_upsampled_shape(), arm_compute::misc::shape_calculator::compute_interleaved_shape(), arm_compute::misc::shape_calculator::compute_lhs_reshaped_shape(), PoolingLayerNode::compute_output_descriptor(), PriorBoxLayerNode::compute_output_descriptor(), ReorgLayerNode::compute_output_descriptor(), SplitLayerNode::compute_output_descriptor(), DetectionOutputLayerNode::compute_output_descriptor(), DeconvolutionLayerNode::compute_output_descriptor(), UpsampleLayerNode::compute_output_descriptor(), FusedConvolutionBatchNormalizationNode::compute_output_descriptor(), DepthwiseConvolutionLayerNode::compute_output_descriptor(), FusedDepthwiseConvolutionBatchNormalizationNode::compute_output_descriptor(), ConvolutionLayerNode::compute_output_descriptor(), arm_compute::misc::shape_calculator::compute_padded_shape(), arm_compute::misc::shape_calculator::compute_reductionB_shape(), arm_compute::misc::shape_calculator::compute_rhs_reshaped_shape(), arm_compute::misc::shape_calculator::compute_split_shape(), arm_compute::misc::shape_calculator::compute_stack_shape(), arm_compute::misc::shape_calculator::compute_tiled_shape(), arm_compute::misc::shape_calculator::compute_transposed_shape(), CLReduceMean::configure(), NEReduceMean::configure(), NEGEMMLowpAssemblyMatrixMultiplyCore::configure(), NEHOGDescriptor::configure(), CLHOGDescriptor::configure(), CLSoftmaxLayer::configure(), NEGEMMMatrixMultiplyKernel::configure(), CLCannyEdge::configure(), NESoftmaxLayer::configure(), NEColorConvertKernel::configure(), NEGEMM::configure(), NEHOGMultiDetection::configure(), CLHOGMultiDetection::configure(), CLGaussianPyramidHalf::configure(), NEGaussianPyramidHalf::configure(), GCConvolutionLayer::configure(), NEGEMMConvolutionLayer::configure(), CLGEMMConvolutionLayer::configure(), CLDepthwiseConvolutionLayer::configure(), NEDepthwiseConvolutionLayer::configure(), PadLayerNode::configure_output(), ResizeLayerNode::configure_output(), ROIAlignLayerNode::configure_output(), arm_compute::test::validation::DATA_TEST_CASE(), arm_compute::test::validation::reference::deconvolution_layer(), NPYLoader::fill_tensor(), arm_compute::intersect_valid_regions(), NEDepthwiseConvolutionAssemblyDispatch::is_optimized_supported(), arm_compute::test::validation::reference::scale_core(), ValidRegion::set(), Window::shape(), arm_compute::test::shape_to_valid_region_laplacian_pyramid(), arm_compute::test::validation::transpose(), CLSoftmaxLayer::validate(), NESoftmaxLayer::validate(), NEGEMMLowpMatrixMultiplyCore::validate(), NEGEMMConvolutionLayer::validate(), CLGEMMConvolutionLayer::validate(), CLDepthwiseConvolutionLayer::validate(), and NEDepthwiseConvolutionLayer::validate().

◆ shift_right()

void shift_right ( size_t  step)
inline

Shifts right the tensor shape increasing its dimensions.

Parameters
[in]stepRotation step

Definition at line 143 of file TensorShape.h.

144  {
146 
147  std::rotate(begin(), begin() + TensorShape::num_max_dimensions - step, end());
148  _num_dimensions += step;
149 
150  // Correct number dimensions to ignore trailing dimensions of size 1
151  apply_dimension_correction();
152  }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
std::array< size_t, num_max_dimensions >::iterator begin()
Returns a read/write iterator that points to the first element in the dimension array.
Definition: Dimensions.h:194
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.
Definition: Dimensions.h:218
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:122
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45

References ARM_COMPUTE_ERROR_ON, Dimensions< size_t >::begin(), Dimensions< size_t >::end(), Dimensions< size_t >::num_dimensions(), Dimensions< size_t >::num_max_dimensions, and arm_compute::test::validation::step.

Referenced by arm_compute::misc::shape_calculator::compute_col2im_shape(), and arm_compute::misc::shape_calculator::compute_softmax_shape().

◆ total_size()

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 171 of file TensorShape.h.

172  {
173  return std::accumulate(_id.begin(), _id.end(), 1, std::multiplies<size_t>());
174  }
__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

References accumulate().

Referenced by arm_compute::auto_init_if_empty(), arm_compute::test::validation::reference::col2im(), CLSelectKernel::configure(), NESpaceToBatchLayer::configure(), CLSpaceToBatchLayer::configure(), GCAccessor::num_elements(), Accessor::num_elements(), CLAccessor::num_elements(), CLElementwiseOperationKernel::run(), CLComparisonKernel::run(), CLPixelWiseMultiplicationKernel::run(), NEPixelWiseMultiplicationKernel::run(), CLComplexPixelWiseMultiplicationKernel::run(), SubTensorInfo::set_tensor_shape(), SubTensorInfo::set_valid_region(), arm_compute::test::validation::TEST_CASE(), NEGatherKernel::validate(), and NEDeconvolutionLayer::validate().

◆ total_size_lower()

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 193 of file TensorShape.h.

194  {
196  return std::accumulate(_id.begin(), _id.begin() + dimension, 1, std::multiplies<size_t>());
197  }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
__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

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

Referenced by CLConvertFullyConnectedWeightsKernel::validate(), and NEConvertFullyConnectedWeightsKernel::validate().

◆ total_size_upper()

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 181 of file TensorShape.h.

182  {
184  return std::accumulate(_id.begin() + dimension, _id.end(), 1, std::multiplies<size_t>());
185  }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
__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

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

Referenced by CLPoolingLayerKernel::configure(), CLWinogradInputTransformKernel::configure(), CLWinogradOutputTransformKernel::configure(), arm_compute::test::validation::reference::fully_connected_layer(), CLPoolingLayerKernel::run(), and CLWinogradInputTransformKernel::run().


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