22#include "Validation.h"
43inline uint32_t compute_out_size(uint32_t image_size, uint32_t whole_pad, uint32_t filter_size,
46 assert((image_size + whole_pad - filter_size) % stride == 0);
47 return (image_size + whole_pad - filter_size) / stride + 1;
59 const Buffer<T> *ifm_buf)
61 auto ifm_shape = ifm_buf->
shape();
63 const uint32_t batches = ifm_shape.
dim(0);
64 const uint32_t depth = ifm_shape.dim(3);
66 const uint32_t ifm_height = ifm_shape.dim(1);
67 const uint32_t ifm_width = ifm_shape.dim(2);
75 const uint32_t pad_top = maxpool2d->
pad()->
top();
76 const uint32_t pad_bottom = maxpool2d->
pad()->
bottom();
78 const uint32_t pad_left = maxpool2d->
pad()->
left();
79 const uint32_t pad_right = maxpool2d->
pad()->
right();
81 const uint32_t output_height =
82 compute_out_size(ifm_height, pad_top + pad_bottom, window_height, stride_height);
83 const uint32_t output_width =
84 compute_out_size(ifm_width, pad_left + pad_right, window_width, stride_width);
88 auto output_buf = make_buffer<T, LexicalLayout>(
output_shape);
90 for (uint32_t batch = 0; batch < batches; ++batch)
92 for (uint32_t out_y = 0; out_y < output_height; ++out_y)
94 for (uint32_t out_x = 0; out_x < output_width; ++out_x)
96 for (uint32_t channel = 0; channel < depth; ++channel)
98 const int in_x_origin = (out_x * stride_width) - pad_left;
99 const int in_y_origin = (out_y * stride_height) - pad_top;
103 const uint32_t filter_x_start = std::max(0, -in_x_origin);
104 const uint32_t filter_x_end = std::min(window_width, ifm_width - in_x_origin);
106 const uint32_t filter_y_start = std::max(0, -in_y_origin);
107 const uint32_t filter_y_end = std::min(window_height, ifm_height - in_y_origin);
109 T max = std::numeric_limits<T>::lowest();
111 for (uint32_t filter_y = filter_y_start; filter_y < filter_y_end; ++filter_y)
113 for (uint32_t filter_x = filter_x_start; filter_x < filter_x_end; ++filter_x)
115 const uint32_t in_x = in_x_origin + filter_x;
116 const uint32_t in_y = in_y_origin + filter_y;
117 max = std::max(max, ifm_buf->at(
Index({batch, in_y, in_x, channel})));
121 output_buf.at(
Index({batch, out_y, out_x, channel})) = max;
139 auto ifm_data = annot_data(maxpool2d->
ifm());
141 validate(ifm_data,
"Can't find input data of MaxPool2D");
142 validate(ifm_data->shape()->rank() == 4,
"IFM rank should be 4");
144 "ifm of MaxPool2D is not Feature");
146 std::unique_ptr<NodeData> maxpool2d_data =
nullptr;
148 switch (ifm_data->dtype())
150 case loco::DataType::FLOAT32:
152 auto ifm_buf = ifm_data->as_f32_bufptr();
154 auto maxpool2d_buf = maxPool2D<float>(maxpool2d, ifm_buf);
156 maxpool2d_data =
make_data(maxpool2d_buf);
160 throw std::runtime_error(
"NYI for this DataType");
163 assert(maxpool2d_data !=
nullptr);
165 annot_data(maxpool2d, std::move(maxpool2d_data));
174void NodeExecution::execute(
loco::MaxPool2D *maxpool2d) { execute_node(maxpool2d); }
const Padding2D * pad(void) const
const Stride< 2 > * stride(void) const
const Window< 2 > * window(void) const
uint32_t left(void) const
uint32_t bottom(void) const
uint32_t right(void) const
uint32_t horizontal(void) const
uint32_t vertical(void) const
uint32_t horizontal(void) const
uint32_t vertical(void) const
uint32_t & dim(uint32_t axis)
const Shape & shape(void) const
const luci_interpreter::RuntimeShape output_shape
bool validate(Code *code)
void annot_domain(loco::Node *node, const loco::Domain &domain)
Wrapper to annotate domain to node. Cannot annotate unknown domain.
std::unique_ptr< NodeData > make_data(const NodeData::Buffer< DT > &buffer)
Copy buffer to make NodeData.
Buffer< T > make_buffer(const Shape &shape)