22#include "Validation.h"
46inline uint32_t compute_out_size(uint32_t image_size, uint32_t whole_pad, uint32_t filter_size,
49 assert((image_size + whole_pad - filter_size) % stride == 0);
50 return (image_size + whole_pad - filter_size) / stride + 1;
55 const Buffer<T> *ifm_buf)
60 auto ifm_shape = ifm_buf->shape();
62 const uint32_t batches = ifm_shape.dim(0);
63 const uint32_t depth = ifm_shape.dim(3);
65 const uint32_t ifm_height = ifm_shape.dim(1);
66 const uint32_t ifm_width = ifm_shape.dim(2);
74 const uint32_t pad_top = avgpool2d->
pad()->
top();
75 const uint32_t pad_bottom = avgpool2d->
pad()->
bottom();
77 const uint32_t pad_left = avgpool2d->
pad()->
left();
78 const uint32_t pad_right = avgpool2d->
pad()->
right();
80 const uint32_t output_height =
81 compute_out_size(ifm_height, pad_top + pad_bottom, window_height, stride_height);
82 const uint32_t output_width =
83 compute_out_size(ifm_width, pad_left + pad_right, window_width, stride_width);
87 auto output_buf = make_buffer<T, LexicalLayout>(
output_shape);
89 for (uint32_t batch = 0; batch < batches; ++batch)
91 for (uint32_t out_y = 0; out_y < output_height; ++out_y)
93 for (uint32_t out_x = 0; out_x < output_width; ++out_x)
95 for (uint32_t channel = 0; channel < depth; ++channel)
97 const int in_x_origin = (out_x * stride_width) - pad_left;
98 const int in_y_origin = (out_y * stride_height) - pad_top;
100 uint32_t f_x0, f_x1, f_y0, f_y1;
103 f_x0 = std::max(0, -in_x_origin);
104 f_x1 = std::min(window_width, ifm_width - in_x_origin);
105 f_y0 = std::max(0, -in_y_origin);
106 f_y1 = std::min(window_height, ifm_height - in_y_origin);
110 throw std::runtime_error(
"TODO support AvgPool2D::Convention::Full");
112 const uint32_t filter_x_start = f_x0;
113 const uint32_t filter_x_end = f_x1;
115 const uint32_t filter_y_start = f_y0;
116 const uint32_t filter_y_end = f_y1;
119 uint32_t filter_ele_count = 0;
121 for (uint32_t filter_y = filter_y_start; filter_y < filter_y_end; ++filter_y)
123 for (uint32_t filter_x = filter_x_start; filter_x < filter_x_end; ++filter_x)
125 const uint32_t in_x = in_x_origin + filter_x;
126 const uint32_t in_y = in_y_origin + filter_y;
127 total += ifm_buf->at(
Index({batch, in_y, in_x, channel}));
132 if (filter_ele_count <= 0)
133 throw std::runtime_error(
"The number of filter element must be greater than zero.");
134 output_buf.at(
Index({batch, out_y, out_x, channel})) = total / filter_ele_count;
152 auto ifm_data = annot_data(avgpool2d->
ifm());
154 validate(ifm_data,
"Can't find input data of AvgPool2D");
155 validate(ifm_data->shape()->rank() == 4,
"IFM rank should be 4");
157 "ifm of AvgPool2D is not Feature");
159 std::unique_ptr<NodeData> avgpool2d_data =
nullptr;
161 switch (ifm_data->dtype())
163 case loco::DataType::FLOAT32:
165 auto ifm_buf = ifm_data->as_f32_bufptr();
167 auto avgpool2d_buf = avgPool2D<float>(avgpool2d, ifm_buf);
169 avgpool2d_data =
make_data(avgpool2d_buf);
173 throw std::runtime_error(
"NYI for this DataType");
176 assert(avgpool2d_data !=
nullptr);
178 annot_data(avgpool2d, std::move(avgpool2d_data));
187void NodeExecution::execute(
loco::AvgPool2D *avgpool2d) { exectute_node(avgpool2d); }
Convention convention(void) const
const Stride< 2 > * stride(void) const
const Padding2D * pad(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
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)