22#include "Validation.h"
41inline uint32_t compute_out_size(uint32_t image_size, uint32_t whole_pad, uint32_t filter_size,
44 assert((image_size + whole_pad - filter_size) % stride == 0);
45 return (image_size + whole_pad - filter_size) / stride + 1;
60template <
typename RET_T,
typename IFM_T,
typename KER_T>
62 const Buffer<KER_T> *ker_buf)
64 auto ifm_shape = ifm_buf->shape();
65 auto ker_shape = ker_buf->shape();
70 "channel value mismatch");
72 const uint32_t ifm_height = ifm_shape.dim(1);
73 const uint32_t ifm_width = ifm_shape.dim(2);
75 const uint32_t ker_height = ker_shape.dim(0);
76 const uint32_t ker_width = ker_shape.dim(1);
82 const uint32_t dilation_width_factor = 1;
83 const uint32_t dilation_height_factor = 1;
85 const uint32_t pad_top = dw_conv2d->
pad()->
top();
86 const uint32_t pad_bottom = dw_conv2d->
pad()->
bottom();
88 const uint32_t pad_left = dw_conv2d->
pad()->
left();
89 const uint32_t pad_right = dw_conv2d->
pad()->
right();
91 const uint32_t ofm_height =
92 compute_out_size(ifm_height, pad_top + pad_bottom, ker_height, stride_height);
93 const uint32_t ofm_width =
94 compute_out_size(ifm_width, pad_left + pad_right, ker_width, stride_width);
96 const uint32_t batches = ifm_shape.dim(0);
97 const uint32_t ifm_depth = ifm_shape.dim(3);
98 const uint32_t multiplier = ker_shape.dim(3);
99 const uint32_t ofm_depth = ifm_depth * multiplier;
101 Shape ofm_shape{batches, ofm_height, ofm_width, ofm_depth};
102 auto ofm_buf = make_buffer<RET_T, LexicalLayout>(ofm_shape);
104 for (uint32_t batch = 0; batch < batches; ++batch)
106 for (uint32_t ofm_y = 0; ofm_y < ofm_height; ++ofm_y)
108 for (uint32_t ofm_x = 0; ofm_x < ofm_width; ++ofm_x)
110 for (uint32_t ch = 0; ch < ifm_depth; ++ch)
112 for (uint32_t nth = 0; nth < multiplier; nth++)
114 const int in_x_origin = (ofm_x * stride_width) - pad_left;
115 const int in_y_origin = (ofm_y * stride_height) - pad_top;
117 for (uint32_t ker_y = 0; ker_y < ker_height; ++ker_y)
119 for (uint32_t ker_x = 0; ker_x < ker_width; ++ker_x)
121 const int in_x = in_x_origin + dilation_width_factor * ker_x;
122 const int in_y = in_y_origin + dilation_height_factor * ker_y;
125 if ((in_x >= 0) && ((unsigned)in_x < ifm_width) && (in_y >= 0) &&
126 ((
unsigned)in_y < ifm_height))
128 auto ifm_value = ifm_buf->at(
Index({batch, (unsigned)in_y, (
unsigned)in_x, ch}));
129 auto ker_value = ker_buf->at(
Index({ker_y, ker_x, ch, nth}));
130 total += (ifm_value * ker_value);
134 uint32_t ofm_channel = ch * multiplier + nth;
135 ofm_buf.at(
Index({batch, ofm_y, ofm_x, ofm_channel})) = total;
153 auto ifm_data = annot_data(dw_conv2d->
ifm());
154 auto ker_data = annot_data(dw_conv2d->
ker());
156 validate(ifm_data,
"Can't find input data of DepthwiseConv2D");
157 validate(ifm_data->shape()->rank() == 4,
"ifm rank must be 4");
159 validate(ker_data,
"Can't find kernel data of DepthwiseConv2D");
160 validate(ker_data->shape()->rank() == 4,
"Kernel rank must be 4");
163 "IFM of DepthwiseConv2D is not feature");
165 "Kernel of DepthwiseConv2D is not depthwise filter");
167 std::unique_ptr<NodeData> dw_conv2d_result =
nullptr;
169 if (ifm_data->dtype() == loco::DataType::FLOAT32 && ker_data->dtype() == loco::DataType::FLOAT32)
171 auto ifm_buf = ifm_data->as_f32_bufptr();
172 auto ker_buf = ker_data->as_f32_bufptr();
174 auto dw_conv2d_buf = calc_dw_conv2d<float, float, float>(dw_conv2d, ifm_buf, ker_buf);
176 dw_conv2d_result =
make_data(dw_conv2d_buf);
179 throw std::runtime_error(
"NYI for these DataTypes");
181 assert(dw_conv2d_result !=
nullptr);
183 annot_data(dw_conv2d, std::move(dw_conv2d_result));
Depthwise 2D Convolution.
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
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)