Compute Library
 19.08
AccessWindowRectangle Class Reference

Implementation of a rectangular access pattern. More...

#include <IAccessWindow.h>

Collaboration diagram for AccessWindowRectangle:
[legend]

Public Member Functions

 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...
 
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...
 
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...
 
- Public Member Functions inherited from IAccessWindow
virtual ~IAccessWindow ()=default
 Default virtual destructor. More...
 

Detailed Description

Implementation of a rectangular access pattern.

Definition at line 107 of file IAccessWindow.h.

Constructor & Destructor Documentation

◆ AccessWindowRectangle() [1/4]

AccessWindowRectangle ( ITensorInfo info,
int  x,
int  y,
int  width,
int  height 
)
inline

Constructor for a rectangular access pattern.

Note
Width and height have to be non-negative.
Parameters
[in,out]infoTensor info of the accessed kernel.
[in]xOffset of the access in X direction.
[in]yOffset of the access in Y direction.
[in]widthNumber of elements that are accessed in X direction.
[in]heightNumber of elements that are accessed in Y direction.

Definition at line 120 of file IAccessWindow.h.

121  : AccessWindowRectangle(info, x, y, width, height, 1.f, 1.f)
122  {
123  }
AccessWindowRectangle(ITensorInfo *info, int x, int y, int width, int height)
Constructor for a rectangular access pattern.

◆ AccessWindowRectangle() [2/4]

AccessWindowRectangle ( ITensorInfo info,
int  x,
int  y,
int  width,
int  height,
float  scale_x,
float  scale_y 
)
inline

Constructor for a rectangular access pattern.

Note
Width, height and scale have to be non-negative.
Parameters
[in,out]infoTensor info of the accessed kernel.
[in]xOffset of the access in X direction.
[in]yOffset of the access in Y direction.
[in]widthNumber of elements that are accessed in X direction.
[in]heightNumber of elements that are accessed in Y direction.
[in]scale_xRatio along the X direction between the window used by the execute_window_loop and the rectangular access pattern defined
[in]scale_yRatio along the Y direction between the window used by the execute_window_loop and the rectangular access pattern defined

Definition at line 137 of file IAccessWindow.h.

138  : _info(info), _x(x), _y(y), _width(width), _height(height), _scale_x(scale_x), _scale_y(scale_y)
139  {
140  ARM_COMPUTE_ERROR_ON(width < 0);
141  ARM_COMPUTE_ERROR_ON(height < 0);
142  ARM_COMPUTE_ERROR_ON(scale_x < 0);
143  ARM_COMPUTE_ERROR_ON(scale_y < 0);
144  }
#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

References ARM_COMPUTE_ERROR_ON.

◆ AccessWindowRectangle() [3/4]

Prevent instances of this class from being copied (As this class contains pointers)

◆ AccessWindowRectangle() [4/4]

Allow instances of this class to be move constructed.

◆ ~AccessWindowRectangle()

~AccessWindowRectangle ( )
default

Default destructor.

Member Function Documentation

◆ compute_valid_region() [1/2]

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.

Note
This method assumes that there is no border.
Parameters
[in]windowExecution window of the kernel.
[in]input_valid_regionCombined valid region of all inputs.
Returns
a valid region.

Definition at line 32 of file IAccessWindow.cpp.

33 {
34  return compute_valid_region(window, input_valid_region, false, BorderSize(0));
35 }
Container for 2D border size.
Definition: Types.h:259
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.

Referenced by AccessWindowRectangle::set_valid_region().

◆ compute_valid_region() [2/2]

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.

Implements IAccessWindow.

Definition at line 37 of file IAccessWindow.cpp.

38 {
39  if(_info == nullptr)
40  {
41  return input_valid_region;
42  }
43 
44  Coordinates &anchor = input_valid_region.anchor;
45  Coordinates old_anchor(anchor);
46  TensorShape &shape = input_valid_region.shape;
47 
48  if(!border_undefined)
49  {
50  border_size = BorderSize(0);
51  }
52 
53  // Start of the valid region is equal to the start of the window. But it
54  // cannot be less than the start of the input's valid region plus the border
55  // size required by this kernel (if undefined).
56  // Additionally the valid region is shifted by the offset that is used by
57  // the kernel to write back output values.
58  anchor.set(0, std::max<int>(window.x().start() * _scale_x, anchor[0] + border_size.left) + _x);
59  if(_info->num_dimensions() > 1)
60  {
61  anchor.set(1, std::max<int>(window.y().start() * _scale_y, anchor[1] + border_size.top) + _y);
62  }
63 
64  // End of the valid region is equal to the start of the last write of the
65  // kernel plus the number of written elements. (This assumes that all
66  // written elements are valid). Nevertheless the end cannot be larger than
67  // the end of the input's valid region minus the border size.
68  // Note: not the end points of the region are stored but its size. Thus the
69  // old size is first converted into end points to compared against the
70  // execution window. Afterwards the new end points are converted back into
71  // a size of the region.
72  shape.set(0, std::min<int>(old_anchor[0] + shape[0] - border_size.right, (window.x().end() - window.x().step()) * _scale_x + _width) - anchor[0]);
73  if(_info->num_dimensions() > 1)
74  {
75  shape.set(1, std::min<int>(old_anchor[1] + shape[1] - border_size.bottom, (window.y().end() - window.y().step()) * _scale_y + _height) - anchor[1]);
76  }
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 }
unsigned int top
top of the border
Definition: Types.h:339
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Shape of a tensor.
Definition: TensorShape.h:39
TensorShape shape
Shape of the valid region.
Definition: Types.h:247
Container for 2D border size.
Definition: Types.h:259
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:102
unsigned int bottom
bottom of the border
Definition: Types.h:341
void set(size_t dimension, T value)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:74
Coordinates of an item.
Definition: Coordinates.h:37
unsigned int left
left of the border
Definition: Types.h:342
unsigned int right
right of the border
Definition: Types.h:340
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:152
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:97
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:92
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:246
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:143

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

◆ operator=() [1/2]

AccessWindowRectangle& operator= ( const AccessWindowRectangle )
delete

Prevent instances of this class from being copied (As this class contains pointers)

◆ operator=() [2/2]

AccessWindowRectangle& operator= ( AccessWindowRectangle &&  )
default

Allow instances of this class to be moved.

◆ set_valid_region()

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.

Parameters
[in]windowExecution window of the kernel.
[in]input_valid_regionCombined valid region of all inputs.
[in]border_undefined(Optional) Undefined borders are excluded from the valid region.
[in]border_size(Optional) Size of the border around the XY-plane of the tensor.

Definition at line 89 of file IAccessWindow.cpp.

90 {
91  if(_info != nullptr)
92  {
93  _info->set_valid_region(compute_valid_region(window, input_valid_region, border_undefined, border_size));
94  }
95 }
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.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.

References AccessWindowRectangle::compute_valid_region(), and ITensorInfo::set_valid_region().

Referenced by CLDeconvolutionLayerUpsampleKernel::configure(), and arm_compute::validate_and_configure_window().

◆ 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.

Implements IAccessWindow.

Definition at line 212 of file IAccessWindow.cpp.

213 {
214  // Only update the padding if the tensor allows it
215  if(_info == nullptr || !_info->is_resizable())
216  {
217  return false;
218  }
219  // Update strides in tensor info
220  return _info->extend_padding( get_needed_padding(window));
221 }
virtual bool is_resizable() const =0
Flag indicating whether the size of the tensor can be changed.
virtual bool extend_padding(const PaddingSize &padding)=0
Update the offset to the first element, the strides and the total size.

References ITensorInfo::extend_padding(), and ITensorInfo::is_resizable().

◆ 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.

Implements IAccessWindow.

Definition at line 97 of file IAccessWindow.cpp.

98 {
99  // Only update the window size if we can't use padding
100  if(_info == nullptr || _info->is_resizable())
101  {
102  return false;
103  }
104 
105  PaddingSize needed = get_needed_padding(window);
106  PaddingSize available = _info->padding();
107 
108  if(needed.top <= available.top && needed.right <= available.right
109  && needed.bottom <= available.bottom
110  && needed.left <= available.left)
111  {
112  return false;
113  }
114 
115  const TensorShape &shape = _info->tensor_shape();
116  const Strides &strides = _info->strides_in_bytes();
117  const size_t offset_first_element = _info->offset_first_element_in_bytes();
118 
119  bool window_modified = false;
120 
121  int front_pad_y = 0;
122 
123  const int min_y = window.y().start() * _scale_y + _y;
124  const int max_y = (window.y().end() - window.y().step()) * _scale_y + _y + _height;
125 
126  // Adjust window start for Y dimension
127  if(min_y < 0)
128  {
129  // Calculate rows available above the tensor
130  const int front_pad_y_available = -static_cast<int>(offset_first_element / strides[1]);
131 
132  if(min_y < front_pad_y_available)
133  {
134  // Not enough padding available, need to shrink the window
135  int start = adjust_up(min_y, front_pad_y_available, window.y().step() * _scale_y) - _y;
136  start = std::min<int>(start / _scale_y, window.y().end());
137 
138  window.set(1, Window::Dimension(start, window.y().end(), window.y().step()));
139  window_modified = true;
140  }
141 
142  // Update front padding with reconstructed value
143  front_pad_y = std::max(0, static_cast<int>(std::floor(-window.y().start() * _scale_y)) - _y);
144  }
145 
146  // Adjust window end for Y dimension
147  if(max_y > static_cast<int>(shape[1]))
148  {
149  const int stride_z = _info->num_dimensions() > 2 ? strides[2] : _info->total_size();
150 
151  // Calculate rows available below the tensor
152  const int tail_pad_y_available = (stride_z / strides[1]) - shape[1] - front_pad_y;
153 
154  if(static_cast<int>(shape[1]) + tail_pad_y_available < max_y)
155  {
156  // Not enough padding available, need to shrink the window
157  int end = adjust_down(max_y, shape[1] + tail_pad_y_available, window.y().step() * _scale_y) + window.y().step() * _scale_y - _y - _height;
158  end = std::max<int>(window.y().start(), end / _scale_y);
159 
160  window.set(1, Window::Dimension(window.y().start(), end, window.y().step()));
161  window_modified = true;
162  }
163  }
164 
165  int front_pad_x = 0;
166 
167  const int min_x = window.x().start() * _scale_x + _x;
168  const int max_x = (window.x().end() - window.x().step()) * _scale_x + _x + _width;
169 
170  const int stride_y = _info->num_dimensions() > 1 ? strides[1] : _info->total_size();
171 
172  // Adjust window start for X dimension
173  if(min_x < 0)
174  {
175  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]);
176 
177  if(min_x < front_pad_x_available)
178  {
179  // Not enough padding available, need to shrink the window
180  int start = adjust_up(min_x, front_pad_x_available, window.x().step() * _scale_x) - _x;
181  start = std::min<int>(start / _scale_x, window.x().end());
182 
183  window.set(0, Window::Dimension(start, window.x().end(), window.x().step()));
184  window_modified = true;
185  }
186 
187  // Update front padding with reconstructed value
188  front_pad_x = std::max(0, static_cast<int>(std::floor(-window.x().start() * _scale_x)) - _x);
189  }
190 
191  // Adjust window end for X dimension
192  if(max_x > static_cast<int>(shape[0]))
193  {
194  const int tail_pad_x_available = (stride_y / strides[0]) - shape[0] - front_pad_x;
195 
196  if(static_cast<int>(shape[0]) + tail_pad_x_available < max_x)
197  {
198  // Not enough padding available, need to shrink the window
199  int end = adjust_down(max_x, shape[0] + tail_pad_x_available, window.x().step() * _scale_x) + window.x().step() * _scale_x - _x - _width;
200  end = std::max<int>(window.x().start(), end / _scale_x);
201 
202  window.set(0, Window::Dimension(window.x().start(), end, window.x().step()));
203  window_modified = true;
204  }
205  }
206 
207  window.validate();
208 
209  return window_modified;
210 }
unsigned int top
top of the border
Definition: Types.h:339
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Shape of a tensor.
Definition: TensorShape.h:39
Container for 2D border size.
Definition: Types.h:259
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:102
Describe one of the image's dimensions with a start, end and step.
Definition: Window.h:75
unsigned int bottom
bottom of the border
Definition: Types.h:341
void validate() const
Will validate all the window's dimensions' values when asserts are enabled.
Definition: Window.inl:159
virtual bool is_resizable() const =0
Flag indicating whether the size of the tensor can be changed.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
int adjust_up(int required, int available, int step)
Increase required in steps of step until it's greater than available.
Definition: IAccessWindow.h:63
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:48
virtual PaddingSize padding() const =0
Padding of tensor.
unsigned int left
left of the border
Definition: Types.h:342
unsigned int right
right of the border
Definition: Types.h:340
Strides of an item in bytes.
Definition: Strides.h:37
virtual size_t offset_first_element_in_bytes() const =0
The offset from the beginning of the memory allocation to the first element of the tensor.
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:152
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:97
int adjust_down(int required, int available, int step)
Decrease required in steps of step until it's less than available.
Definition: IAccessWindow.h:47
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:92
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:143

References arm_compute::adjust_down(), arm_compute::adjust_up(), BorderSize::bottom, Window::Dimension::end(), ITensorInfo::is_resizable(), BorderSize::left, ITensorInfo::num_dimensions(), ITensorInfo::offset_first_element_in_bytes(), ITensorInfo::padding(), BorderSize::right, Window::set(), arm_compute::test::validation::shape, Window::Dimension::start(), Window::Dimension::step(), ITensorInfo::strides_in_bytes(), ITensorInfo::tensor_shape(), BorderSize::top, ITensorInfo::total_size(), Window::validate(), Window::x(), and Window::y().


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