22#include "Validation.h"
36inline uint32_t compute_out_size(uint32_t image_size, uint32_t filter_size, uint32_t stride)
38 assert((image_size + stride - filter_size) % stride == 0);
39 return (image_size + stride - filter_size) / stride;
53template <
typename RET_T,
typename IFM_T,
typename FIL_T>
54Buffer<RET_T> calc_conv2D(
const loco::Conv2D *conv2d,
const Buffer<IFM_T> *input_buf,
55 const Buffer<FIL_T> *filter_buf)
57 auto input_shape = input_buf->shape();
58 auto filter_shape = filter_buf->shape();
63 "channel value mismatch");
65 const uint32_t input_height = input_shape.dim(1);
66 const uint32_t input_width = input_shape.dim(2);
68 const uint32_t filter_height = filter_shape.dim(1);
69 const uint32_t filter_width = filter_shape.dim(2);
75 const uint32_t dilation_width_factor = 1;
76 const uint32_t dilation_height_factor = 1;
78 const uint32_t pad_top = conv2d->
pad()->
top();
79 const uint32_t pad_bottom = conv2d->
pad()->
bottom();
81 const uint32_t pad_left = conv2d->
pad()->
left();
82 const uint32_t pad_right = conv2d->
pad()->
right();
84 const uint32_t output_height =
85 compute_out_size(input_height + pad_top + pad_bottom, filter_height, stride_height);
86 const uint32_t output_width =
87 compute_out_size(input_width + pad_left + pad_right, filter_width, stride_width);
89 const uint32_t batches = input_shape.dim(0);
90 const uint32_t input_depth = input_shape.dim(3);
91 const uint32_t output_depth = filter_shape.dim(0);
94 auto output_buf = make_buffer<RET_T, LexicalLayout>(
output_shape);
96 for (uint32_t batch = 0; batch < batches; ++batch)
98 for (uint32_t out_y = 0; out_y < output_height; ++out_y)
100 for (uint32_t out_x = 0; out_x < output_width; ++out_x)
102 for (uint32_t out_channel = 0; out_channel < output_depth; ++out_channel)
104 const int in_x_origin = (out_x * stride_width) - pad_left;
105 const int in_y_origin = (out_y * stride_height) - pad_top;
107 RET_T total =
static_cast<RET_T
>(0);
109 for (uint32_t filter_y = 0; filter_y < filter_height; ++filter_y)
111 for (uint32_t filter_x = 0; filter_x < filter_width; ++filter_x)
113 for (uint32_t in_channel = 0; in_channel < input_depth; ++in_channel)
115 const int32_t in_x = in_x_origin + dilation_width_factor * filter_x;
116 const int32_t in_y = in_y_origin + dilation_height_factor * filter_y;
120 if ((in_x >= 0) && ((unsigned)in_x < input_width) && (in_y >= 0) &&
121 ((
unsigned)in_y < input_height))
124 input_buf->at(
Index({batch, (unsigned)in_y, (
unsigned)in_x, in_channel}));
126 filter_buf->at(
Index({out_channel, filter_y, filter_x, in_channel}));
127 total += (input_value * filter_value);
132 output_buf.at(
Index({batch, out_y, out_x, out_channel})) = total;
149 auto ifm_data = annot_data(conv2d->
ifm());
150 auto ker_data = annot_data(conv2d->
ker());
152 validate(ifm_data,
"Can't find input data of Conv2D");
153 validate(ifm_data->shape()->rank() == 4,
"ifm rank must be 4");
155 validate(ker_data,
"Can't find kernel data of Conv2D");
156 validate(ker_data->shape()->rank() == 4,
"Kernel rank must be 4");
161 std::unique_ptr<NodeData> conv2d_result =
nullptr;
163 if (ifm_data->dtype() == loco::DataType::FLOAT32 && ker_data->dtype() == loco::DataType::FLOAT32)
165 auto ifm_buf = ifm_data->as_f32_bufptr();
166 auto ker_buf = ker_data->as_f32_bufptr();
168 auto conv2d_buf = calc_conv2D<float, float, float>(conv2d, ifm_buf, ker_buf);
173 throw std::runtime_error(
"NYI for these DataTypes");
175 assert(conv2d_result !=
nullptr);
177 annot_data(conv2d, std::move(conv2d_result));
186void NodeExecution::execute(
loco::Conv2D *conv2d) { execute_node(conv2d); }
const Stride< 2 > * stride(void) const
const Padding2D * pad(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
const luci_interpreter::RuntimeShape output_shape
bool validate(Code *code)
void validate(bool true_cond, const std::string &&exception_msg)
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)