Compute Library
 23.05
arm_compute::helpers::tensor_transform Namespace Reference

Functions

int calculate_stride_on_index (int index, Coordinates strides)
 Computes stride of a given index. More...
 
int calculate_start_on_index (TensorShape input_shape, int index, Coordinates starts, Coordinates strides, int32_t begin_mask)
 Computes absolute start position of a given index for a strided slice operation. More...
 
int calculate_end_on_index (TensorShape input_shape, int index, int start_on_index, Coordinates ends, Coordinates strides, int32_t end_mask=0, int32_t shrink_axis_mask=0)
 Returns the absolute end position of a given index for a strided slice operation. More...
 
std::tuple< Coordinates, Coordinates, Coordinatescalculate_strided_slice_coords (TensorShape input_shape, Coordinates starts, Coordinates ends, Coordinates strides, int32_t begin_mask=0, int32_t end_mask=0, int32_t shrink_axis_mask=0)
 Calculate start, end and stride coordinates for a strided slice. More...
 
TensorShape compute_strided_slice_output_shape (TensorShape input_shape, Coordinates starts, Coordinates ends, Coordinates strides, int32_t begin_mask=0, int32_t end_mask=0, int32_t shrink_axis_mask=0, bool return_unshrinked=false)
 Computes output shape of strided slice. More...
 
int32_t construct_slice_end_mask (Coordinates ends)
 Constructs end mask in case we want to perform a slice operation using the strided slice interface. More...
 

Function Documentation

◆ calculate_end_on_index()

int calculate_end_on_index ( TensorShape  input_shape,
int  index,
int  start_on_index,
Coordinates  ends,
Coordinates  strides,
int32_t  end_mask = 0,
int32_t  shrink_axis_mask = 0 
)

Returns the absolute end position of a given index for a strided slice operation.

Parameters
[in]input_shapeInput tensor shape
[in]indexIndex of tensor to calculate absolute start position
[in]start_on_indexAbsolute start coordinate for given index
[in]endsEnd coordinates
[in]stridesSlice strides
[in]end_mask(Optional) If the ith bit of end_mask is set, end[i] is ignored and the fullest possible range in that dimension is used instead.
[in]shrink_axis_mask(Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1. A slice of size 1 starting from starts[i] in the dimension must be preserved.
Returns
Absolute end position of a given index

Definition at line 72 of file tensor_transform.cpp.

References calculate_stride_on_index(), arm_compute::utility::clamp(), arm_compute::helpers::bit_ops::is_bit_set(), arm_compute::support::cpp11::lowest(), and Dimensions< T >::num_dimensions().

Referenced by calculate_strided_slice_coords(), and compute_strided_slice_output_shape().

75 {
76  // Early exit
77  if(index >= static_cast<int>(ends.num_dimensions()))
78  {
79  return input_shape[index];
80  }
81 
82  const int stride = calculate_stride_on_index(index, strides);
83  const bool shrink_axis = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, index);
84 
85  // Calculate start
86  int stop = ends[index];
87 
88  // Shrink dimension
89  if(shrink_axis)
90  {
91  if(start_on_index == std::numeric_limits<int>::max())
92  {
93  stop = start_on_index;
94  }
95  else
96  {
97  stop = start_on_index + 1;
98  }
99  }
100 
101  // Reset in case of begin mask present
102  if(arm_compute::helpers::bit_ops::is_bit_set(end_mask, index) && !shrink_axis)
103  {
104  stop = (stride > 0) ? std::numeric_limits<int>::max() : std::numeric_limits<int>::lowest();
105  }
106 
107  // Account negative end points
108  const int dim_size = input_shape[index];
109  if(stop < 0)
110  {
111  stop += dim_size;
112  }
113 
114  // Final clamp
115  stop = (stride > 0) ? utility::clamp(stop, 0, dim_size) : utility::clamp(stop, -1, dim_size - 1);
116 
117  return stop;
118 }
int calculate_stride_on_index(int index, Coordinates strides)
Computes stride of a given index.
bool is_bit_set(T v, unsigned int idx)
Checks if the idx-th bit is set in an integral type.
Definition: bit_ops.h:45
DataType clamp(const DataType &n, const DataType &lower=std::numeric_limits< RangeType >::lowest(), const DataType &upper=std::numeric_limits< RangeType >::max())
Performs clamping among a lower and upper value.
Definition: Utility.h:102
const auto input_shape
Validate test suite is to test ARM_COMPUTE_RETURN_ON_* macros we use to check the validity of given a...

◆ calculate_start_on_index()

int calculate_start_on_index ( TensorShape  input_shape,
int  index,
Coordinates  starts,
Coordinates  strides,
int32_t  begin_mask 
)

Computes absolute start position of a given index for a strided slice operation.

Parameters
[in]input_shapeInput tensor shape
[in]indexIndex of tensor to calculate absolute start position
[in]startsStart coordinates
[in]stridesSlice strides
[in]begin_mask(Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
Returns
Absolute start position of a given index

Definition at line 39 of file tensor_transform.cpp.

References calculate_stride_on_index(), arm_compute::utility::clamp(), arm_compute::helpers::bit_ops::is_bit_set(), arm_compute::support::cpp11::lowest(), and Dimensions< T >::num_dimensions().

Referenced by calculate_strided_slice_coords(), and compute_strided_slice_output_shape().

40 {
41  // Early exit
42  if(index >= static_cast<int>(starts.num_dimensions()))
43  {
44  return 0;
45  }
46 
47  // Get stride
48  const int stride = calculate_stride_on_index(index, strides);
49 
50  // Calculate start
51  int start = starts[index];
52 
53  // Reset in case of begin mask present
54  if(arm_compute::helpers::bit_ops::is_bit_set(begin_mask, index))
55  {
56  start = stride > 0 ? std::numeric_limits<int>::lowest() : std::numeric_limits<int>::max();
57  }
58 
59  // Account negative start points
60  const int dim_size = input_shape[index];
61  if(start < 0)
62  {
63  start += dim_size;
64  }
65 
66  // Final clamp
67  start = utility::clamp(start, 0, dim_size - 1);
68 
69  return start;
70 }
int calculate_stride_on_index(int index, Coordinates strides)
Computes stride of a given index.
bool is_bit_set(T v, unsigned int idx)
Checks if the idx-th bit is set in an integral type.
Definition: bit_ops.h:45
DataType clamp(const DataType &n, const DataType &lower=std::numeric_limits< RangeType >::lowest(), const DataType &upper=std::numeric_limits< RangeType >::max())
Performs clamping among a lower and upper value.
Definition: Utility.h:102
const auto input_shape
Validate test suite is to test ARM_COMPUTE_RETURN_ON_* macros we use to check the validity of given a...

◆ calculate_stride_on_index()

int calculate_stride_on_index ( int  index,
Coordinates  strides 
)

Computes stride of a given index.

Parameters
[in]indexIndex of tensor to calculate absolute start position
[in]stridesSlice strides
Returns
Stride at a given index

Definition at line 34 of file tensor_transform.cpp.

References Dimensions< T >::num_dimensions().

Referenced by calculate_end_on_index(), calculate_start_on_index(), calculate_strided_slice_coords(), and compute_strided_slice_output_shape().

35 {
36  return index >= static_cast<int>(strides.num_dimensions()) ? 1 : strides[index];
37 }

◆ calculate_strided_slice_coords()

std::tuple< Coordinates, Coordinates, Coordinates > calculate_strided_slice_coords ( TensorShape  input_shape,
Coordinates  starts,
Coordinates  ends,
Coordinates  strides,
int32_t  begin_mask = 0,
int32_t  end_mask = 0,
int32_t  shrink_axis_mask = 0 
)

Calculate start, end and stride coordinates for a strided slice.

Parameters
[in]input_shapeInput tensor shape
[in]startsStart coordinates
[in]endsEnd coordinates
[in]stridesSlice strides
[in]begin_mask(Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
[in]end_mask(Optional) If the ith bit of end_mask is set, end[i] is ignored and the fullest possible range in that dimension is used instead.
[in]shrink_axis_mask(Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1. A slice of size 1 starting from starts[i] in the dimension must be preserved.
Returns
A tuple with <Start,End,Strides>

Definition at line 120 of file tensor_transform.cpp.

References calculate_end_on_index(), calculate_start_on_index(), calculate_stride_on_index(), and Dimensions< T >::num_dimensions().

Referenced by CLStridedSliceKernel::configure(), NEStridedSliceKernel::configure(), and arm_compute::test::validation::reference::strided_slice().

123 {
124  Coordinates starts_abs{};
125  Coordinates ends_abs{};
126  Coordinates final_strides{};
127 
128  for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
129  {
130  const int start_i = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
131  starts_abs.set(i, start_i);
132  ends_abs.set(i, calculate_end_on_index(input_shape, i, start_i, ends, strides, end_mask, shrink_axis_mask));
133  final_strides.set(i, calculate_stride_on_index(i, strides));
134  }
135 
136  return std::make_tuple(starts_abs, ends_abs, final_strides);
137 }
int calculate_stride_on_index(int index, Coordinates strides)
Computes stride of a given index.
const auto input_shape
Validate test suite is to test ARM_COMPUTE_RETURN_ON_* macros we use to check the validity of given a...
int calculate_end_on_index(TensorShape input_shape, int index, int start_on_index, Coordinates ends, Coordinates strides, int32_t end_mask=0, int32_t shrink_axis_mask=0)
Returns the absolute end position of a given index for a strided slice operation. ...
int calculate_start_on_index(TensorShape input_shape, int index, Coordinates starts, Coordinates strides, int32_t begin_mask)
Computes absolute start position of a given index for a strided slice operation.

◆ compute_strided_slice_output_shape()

TensorShape compute_strided_slice_output_shape ( TensorShape  input_shape,
Coordinates  starts,
Coordinates  ends,
Coordinates  strides,
int32_t  begin_mask = 0,
int32_t  end_mask = 0,
int32_t  shrink_axis_mask = 0,
bool  return_unshrinked = false 
)

Computes output shape of strided slice.

Warning
Starts and ends must be non-negative
Starts, ends and final strides should have the same dimensions as the input shape
Parameters
[in]input_shapeInput tensor shape
[in]startsAbsolute start coordinates
[in]endsAbsolute end coordinates
[in]stridesSlice strides
[in]begin_mask(Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
[in]end_mask(Optional) If the ith bit of end_mask is set, end[i] is ignored and the fullest possible range in that dimension is used instead.
[in]shrink_axis_mask(Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1. A slice of size 1 starting from starts[i] in the dimension must be preserved.
[in]return_unshrinked(Optional) Returns un-shrinked shape
Returns
The output tensor shape

Definition at line 139 of file tensor_transform.cpp.

References calculate_end_on_index(), calculate_start_on_index(), calculate_stride_on_index(), arm_compute::mlgo::parser::end(), arm_compute::helpers::bit_ops::is_bit_set(), Dimensions< T >::num_dimensions(), arm_compute::test::validation::output_shape, arm_compute::test::validation::reference::range(), and TensorShape::set().

Referenced by StridedSliceLayerNode::compute_output_descriptor(), arm_compute::misc::shape_calculator::compute_slice_shape(), arm_compute::misc::shape_calculator::compute_strided_slice_shape(), and arm_compute::test::validation::reference::strided_slice().

141 {
142  unsigned int index = 0;
143 
144  TensorShape output_shape;
145  for(unsigned int i = 0; i < input_shape.num_dimensions(); ++i)
146  {
147  const int stride = calculate_stride_on_index(index, strides);
148  const int start = calculate_start_on_index(input_shape, i, starts, strides, begin_mask);
149  const int end = calculate_end_on_index(input_shape, i, start, ends, strides, end_mask, shrink_axis_mask);
150  const int range = end - start;
151 
152  const bool is_shrink = arm_compute::helpers::bit_ops::is_bit_set(shrink_axis_mask, i);
153  if(return_unshrinked || !is_shrink)
154  {
155  if((range == 0) || // Zero range
156  (range < 0 && stride >= 0) || // Negative range with positive stride
157  (range > 0 && stride <= 0)) // Positive range with negative stride
158  {
159  output_shape.set(index, 0);
160  return output_shape;
161  }
162  else
163  {
164  int dim = range / stride + (range % stride != 0 ? 1 : 0);
165  output_shape.set(index++, dim);
166  }
167  }
168  }
169  return output_shape;
170 }
int calculate_stride_on_index(int index, Coordinates strides)
Computes stride of a given index.
bool is_bit_set(T v, unsigned int idx)
Checks if the idx-th bit is set in an integral type.
Definition: bit_ops.h:45
const auto input_shape
Validate test suite is to test ARM_COMPUTE_RETURN_ON_* macros we use to check the validity of given a...
SimpleTensor< T > range(SimpleTensor< T > &dst, float start, const size_t num_of_elements, float step)
Definition: Range.cpp:50
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
int calculate_end_on_index(TensorShape input_shape, int index, int start_on_index, Coordinates ends, Coordinates strides, int32_t end_mask=0, int32_t shrink_axis_mask=0)
Returns the absolute end position of a given index for a strided slice operation. ...
int calculate_start_on_index(TensorShape input_shape, int index, Coordinates starts, Coordinates strides, int32_t begin_mask)
Computes absolute start position of a given index for a strided slice operation.

◆ construct_slice_end_mask()

int32_t construct_slice_end_mask ( Coordinates  ends)

Constructs end mask in case we want to perform a slice operation using the strided slice interface.

Note
Ends are inclusive in slice operations that is why construction an end mask is needed
Parameters
[in]endsEnd coordinates
Returns
End mask

Definition at line 172 of file tensor_transform.cpp.

References Dimensions< T >::num_dimensions().

Referenced by arm_compute::misc::shape_calculator::compute_slice_shape(), NESlice::configure(), CLSlice::configure(), NESlice::validate(), and CLSlice::validate().

173 {
174  // Create end mask
175  int32_t end_mask = 0;
176  for(unsigned int i = 0; i < ends.num_dimensions(); ++i)
177  {
178  if(ends[i] < 0)
179  {
180  end_mask |= 1 << i;
181  }
182  }
183 
184  return end_mask;
185 }