Compute Library
 22.05
AccessWindowTranspose Class Reference

Implementation of a XY-transpose access pattern. More...

#include <AccessWindowTranspose.h>

Collaboration diagram for AccessWindowTranspose:
[legend]

Public Member Functions

bool update_window_if_needed (Window &window) const override
 Shrink the window if padding is not large enough. More...
 
bool update_padding_if_needed (const Window &window) override
 Increase the padding to be large enough for the window. More...
 
ValidRegion compute_valid_region (const Window &window, ValidRegion input_valid_region, bool border_undefined, BorderSize border_size) const override
 Compute the valid region based on access pattern and valid region of the inputs. More...
 
- Public Member Functions inherited from AccessWindowRectangle
 AccessWindowRectangle (ITensorInfo *info, int x, int y, int width, int height)
 Constructor for a rectangular access pattern. More...
 
 AccessWindowRectangle (ITensorInfo *info, int x, int y, int width, int height, float scale_x, float scale_y)
 Constructor for a rectangular access pattern. More...
 
 AccessWindowRectangle (const AccessWindowRectangle &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 AccessWindowRectangle (AccessWindowRectangle &&)=default
 Allow instances of this class to be move constructed. More...
 
AccessWindowRectangleoperator= (const AccessWindowRectangle &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
AccessWindowRectangleoperator= (AccessWindowRectangle &&)=default
 Allow instances of this class to be moved. More...
 
 ~AccessWindowRectangle ()=default
 Default destructor. More...
 
void set_valid_region (const Window &window, const ValidRegion &input_valid_region, bool border_undefined=false, const BorderSize &border_size=BorderSize(0))
 Set the valid region based on access pattern, valid region of the inputs and border mode. More...
 
ValidRegion compute_valid_region (const Window &window, const ValidRegion &input_valid_region) const
 Compute the valid region based on access pattern, valid region of the inputs and border mode. More...
 
- Public Member Functions inherited from IAccessWindow
virtual ~IAccessWindow ()=default
 Default virtual destructor. More...
 

Detailed Description

Implementation of a XY-transpose access pattern.

Definition at line 38 of file AccessWindowTranspose.h.

Member Function Documentation

◆ compute_valid_region()

ValidRegion compute_valid_region ( const Window window,
ValidRegion  input_valid_region,
bool  border_undefined,
BorderSize  border_size 
) const
overridevirtual

Compute the valid region based on access pattern and valid region of the inputs.

Note
This method assumes that all elements written by the kernel are valid.
Parameters
[in]windowExecution window of the kernel.
[in]input_valid_regionCombined valid region of all inputs.
[in]border_undefinedUndefined borders are excluded from the valid region.
[in]border_sizeSize of the border around the XY-plane of the tensor.
Returns
a valid region.

Reimplemented from AccessWindowRectangle.

Definition at line 32 of file AccessWindowTranspose.cpp.

References ValidRegion::anchor, BorderSize::bottom, Window::Dimension::end(), arm_compute::mlgo::parser::end(), BorderSize::left, BorderSize::right, Dimensions< T >::set(), TensorShape::set(), arm_compute::test::validation::shape, ValidRegion::shape, Window::Dimension::start(), Window::Dimension::step(), BorderSize::top, Window::x(), and Window::y().

33 {
34  if(_info == nullptr)
35  {
36  return input_valid_region;
37  }
38 
39  Coordinates &anchor = input_valid_region.anchor;
40  TensorShape &shape = input_valid_region.shape;
41  Coordinates old_anchor(anchor);
42  TensorShape old_shape(shape);
43 
44  if(!border_undefined)
45  {
46  border_size = BorderSize(0);
47  }
48 
49  // Start of the valid region is equal to the start of the window. But it
50  // cannot be less than the start of the input's valid region plus the border
51  // size required by this kernel (if undefined).
52  // Additionally the valid region is shifted by the offset that is used by
53  // the kernel to write back output values.
54  // As the relation between input and output is transposed window.y() is
55  // used for x anchor and window.x() for y anchor.
56  if(_info->dimension(0) > 1)
57  {
58  anchor.set(0, std::max<int>(window.y().start() * _scale_x, anchor[1] + border_size.top) + _x);
59  }
60  anchor.set(1, std::max<int>(window.x().start() * _scale_y, anchor[0] + border_size.left) + _y);
61 
62  // End of the valid region is equal to the start of the last write of the
63  // kernel plus the number of written elements. (This assumes that all
64  // written elements are valid). Nevertheless the end cannot be larger than
65  // the end of the input's valid region minus the border size.
66  // Note: not the end points of the region are stored but its size. Thus the
67  // old size is first converted into end points to compared against the
68  // execution window. Afterwards the new end points are converted back into
69  // a size of the region.
70  // As the relation between input and output is transposed window.y() is
71  // used for x shape and window.x() for y shape.
72  if(_info->dimension(0) > 1)
73  {
74  shape.set(0, std::min<int>((old_anchor[1] + old_shape[0]) * _scale_x - border_size.right, (window.y().end() - window.y().step()) * _scale_x + _width) - anchor[0]);
75  }
76  shape.set(1, std::min<int>((old_anchor[0] + old_shape[1]) * _scale_y - border_size.bottom, (window.x().end() - window.x().step()) * _scale_y + _height) - anchor[1]);
77 
78  // For higher dimensions use the intersection of the window size and the
79  // valid region of the input
80  for(size_t d = 2; d < _info->num_dimensions(); ++d)
81  {
82  anchor.set(d, std::max(window[d].start(), input_valid_region.anchor[d]));
83  shape.set(d, std::min<int>(window[d].end(), input_valid_region.shape[d]) - anchor[d]);
84  }
85 
86  return input_valid_region;
87 }
void set(size_t dimension, T value, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:76
unsigned int top
top of the border
Definition: Types.h:390
Shape of a tensor.
Definition: TensorShape.h:39
TensorShape shape
Shape of the valid region.
Definition: Types.h:266
Container for 2D border size.
Definition: Types.h:282
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:106
unsigned int bottom
bottom of the border
Definition: Types.h:392
Coordinates of an item.
Definition: Coordinates.h:37
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
unsigned int left
left of the border
Definition: Types.h:393
unsigned int right
right of the border
Definition: Types.h:391
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:167
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:101
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:96
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:79
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:265
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:158

◆ update_padding_if_needed()

bool update_padding_if_needed ( const Window window)
overridevirtual

Increase the padding to be large enough for the window.

Parameters
[in]windowWindow used by the kernel.
Returns
True if the padding has been changed.

Reimplemented from AccessWindowRectangle.

Definition at line 189 of file AccessWindowTranspose.cpp.

References ARM_COMPUTE_ERROR_ON, BorderSize::bottom, Window::Dimension::end(), BorderSize::left, BorderSize::right, arm_compute::test::validation::shape, Window::Dimension::start(), Window::Dimension::step(), BorderSize::top, Window::x(), and Window::y().

190 {
191  // Only update the padding if the tensor allows it
192  if(_info == nullptr || !_info->is_resizable())
193  {
194  return false;
195  }
196 
197  ARM_COMPUTE_ERROR_ON(window.y().step() == 0);
198  ARM_COMPUTE_ERROR_ON(window.x().step() == 0);
199 
200  const int min_x = window.y().start() * _scale_x + _x;
201  const int max_x = (window.y().end() - window.y().step()) * _scale_x + _x + _width;
202  const int min_y = window.x().start() * _scale_y + _y;
203  const int max_y = (window.x().end() - window.x().step()) * _scale_y + _y + _height;
204 
205  const TensorShape &shape = _info->tensor_shape();
206 
207  PaddingSize padding;
208  padding.left = std::max(0, -min_x);
209  padding.right = std::max<int>(0, max_x - shape[0]);
210  padding.top = std::max(0, -min_y);
211  padding.bottom = std::max<int>(0, max_y - shape[1]);
212 
213  // Update strides in tensor info
214  return _info->extend_padding(padding);
215 }
unsigned int top
top of the border
Definition: Types.h:390
Shape of a tensor.
Definition: TensorShape.h:39
Container for 2D border size.
Definition: Types.h:282
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:106
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
unsigned int bottom
bottom of the border
Definition: Types.h:392
unsigned int left
left of the border
Definition: Types.h:393
unsigned int right
right of the border
Definition: Types.h:391
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:167
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:101
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:96
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:158

◆ update_window_if_needed()

bool update_window_if_needed ( Window window) const
overridevirtual

Shrink the window if padding is not large enough.

Parameters
[in]windowWindow used by the kernel.
Returns
True if the window has been changed.

Reimplemented from AccessWindowRectangle.

Definition at line 89 of file AccessWindowTranspose.cpp.

References arm_compute::adjust_down(), arm_compute::adjust_up(), Window::Dimension::end(), arm_compute::mlgo::parser::end(), Window::set(), arm_compute::test::validation::shape, Window::Dimension::start(), Window::Dimension::step(), Window::validate(), Window::x(), and Window::y().

90 {
91  // Only update the window size if we can't use padding
92  if(_info == nullptr || _info->is_resizable())
93  {
94  return false;
95  }
96 
97  const TensorShape &shape = _info->tensor_shape();
98  const Strides &strides = _info->strides_in_bytes();
99  const size_t offset_first_element = _info->offset_first_element_in_bytes();
100 
101  bool window_modified = false;
102 
103  int front_pad_y = 0;
104 
105  // Transpose and scale
106  const int min_y = window.x().start() * _scale_y + _y;
107  const int max_y = window.x().end() * _scale_y + _y;
108 
109  // Adjust window start for output's Y dimension (so X in (input) window)
110  if(min_y < 0)
111  {
112  // Calculate rows available above the tensor
113  const int front_pad_y_available = -offset_first_element / strides[1];
114 
115  if(min_y < front_pad_y_available)
116  {
117  // Not enough padding available, need to shrink the window
118  const int start = adjust_up(min_y, front_pad_y_available, window.x().step() * _scale_y) - _y;
119 
120  window.set(0, Window::Dimension(start / _scale_y, window.x().end(), window.x().step()));
121  window_modified = true;
122  }
123 
124  // Update front padding with reconstructed value
125  front_pad_y = std::max(0, static_cast<int>(std::floor(-window.x().start() * _scale_y)) - _y);
126  }
127 
128  // Adjust window end for Y dimension
129  if(max_y > static_cast<int>(shape[1]))
130  {
131  const int stride_z = _info->num_dimensions() > 2 ? strides[2] : _info->total_size();
132 
133  // Calculate rows available below the tensor
134  const int tail_pad_y_available = (stride_z / strides[1]) - shape[1] - front_pad_y;
135 
136  if(static_cast<int>(shape[1]) + tail_pad_y_available < max_y)
137  {
138  // Not enough padding available, need to shrink the window
139  const int end = adjust_down(max_y, shape[1] + tail_pad_y_available, window.x().step() * _scale_y) + window.x().step() * _scale_y - _y - _height;
140  window.set(0, Window::Dimension(window.x().start(), end / _scale_y, window.x().step()));
141  window_modified = true;
142  }
143  }
144 
145  int front_pad_x = 0;
146 
147  // Transpose and scale
148  const int min_x = window.y().start() * _scale_x + _x;
149  const int max_x = window.y().end() * _scale_x + _x;
150 
151  const int stride_y = _info->num_dimensions() > 1 ? strides[1] : _info->total_size();
152 
153  // Adjust window start for X dimension
154  if(min_x < 0)
155  {
156  const int front_pad_x_available = -std::min<int>(static_cast<int>(offset_first_element) - front_pad_y * strides[1], stride_y - shape[0] * strides[0]) / static_cast<int>(strides[0]);
157 
158  if(min_x < front_pad_x_available)
159  {
160  // Not enough padding available, need to shrink the window
161  const int start = adjust_up(min_x, front_pad_x_available, window.y().step() * _scale_x) - _x;
162  window.set(1, Window::Dimension(start / _scale_x, window.y().end(), window.y().step()));
163  window_modified = true;
164  }
165 
166  // Update front padding with reconstructed value
167  front_pad_x = std::max(0, static_cast<int>(std::floor(-window.y().start() * _scale_x)) - _x);
168  }
169 
170  // Adjust window end for X dimension
171  if(max_x > static_cast<int>(shape[0]))
172  {
173  const int tail_pad_x_available = (stride_y / strides[0]) - shape[0] - front_pad_x;
174 
175  if(static_cast<int>(shape[0]) + tail_pad_x_available < max_x)
176  {
177  // Not enough padding available, need to shrink the window
178  const int end = adjust_down(max_x, shape[0] + tail_pad_x_available, window.y().step() * _scale_x) + window.y().step() * _scale_x - _x - _width;
179  window.set(1, Window::Dimension(window.y().start(), end / _scale_x, window.y().step()));
180  window_modified = true;
181  }
182  }
183 
184  window.validate();
185 
186  return window_modified;
187 }
Shape of a tensor.
Definition: TensorShape.h:39
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:106
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
void validate() const
Will validate all the window&#39;s dimensions&#39; values when asserts are enabled.
Definition: Window.inl:173
int adjust_up(int required, int available, int step)
Increase required in steps of step until it&#39;s greater than available.
Definition: IAccessWindow.h:63
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
Strides of an item in bytes.
Definition: Strides.h:37
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:167
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:101
int adjust_down(int required, int available, int step)
Decrease required in steps of step until it&#39;s less than available.
Definition: IAccessWindow.h:47
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:96
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:158

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