31 const std::shared_ptr<ExternalContext> external_context)
32 :
Layer(external_context), _input(nullptr), _kernel(nullptr), _bias(nullptr), _output(nullptr),
33 _padding_type(ir::PaddingType::EXPLICIT), _padding_left(0), _padding_top(0), _padding_right(0),
34 _padding_bottom(0), _stride_width(0), _stride_height(0), _multiplier(1),
35 _dilation_width_factor(1), _dilation_height_factor(1), _activation(ir::Activation::
NONE)
42 ir::PaddingType padding_type,
const uint32_t padding_left,
const uint32_t padding_right,
43 const uint32_t padding_top,
const uint32_t padding_bottom,
const uint32_t stride_width,
44 const uint32_t stride_height,
const uint32_t multiplier,
const uint32_t dilation_width_factor,
50 _padding_type = padding_type;
51 _padding_left = padding_left;
52 _padding_right = padding_right;
53 _padding_top = padding_top;
54 _padding_bottom = padding_bottom;
55 _stride_width = stride_width;
56 _stride_height = stride_height;
57 _multiplier = multiplier;
58 _dilation_width_factor = dilation_width_factor;
59 _dilation_height_factor = dilation_height_factor;
60 _activation = activation;
92 float output_activation_min = 0.f, output_activation_max = 0.f;
93 CalculateActivationRange<float>(_activation, &output_activation_min, &output_activation_max);
97 const auto &kernel_shape = _kernel->
getShape();
98 uint32_t kernel_height = kernel_shape.dim(1);
99 uint32_t kernel_width = kernel_shape.dim(2);
100 uint32_t output_channels = kernel_shape.dim(3);
101 uint32_t input_channels = _input->
getShape().dim(3);
102 assert(
static_cast<uint32_t
>(_output->
getShape().dim(3)) == output_channels);
103 assert(output_channels == input_channels * _multiplier);
105 enum xnn_status status = xnn_create_convolution2d_nhwc_f32(
106 _padding_top, _padding_right, _padding_bottom, _padding_left, kernel_height, kernel_width,
107 _stride_height, _stride_width, _dilation_height_factor, _dilation_width_factor,
109 _multiplier , input_channels ,
110 output_channels ,
reinterpret_cast<const float *
>(_kernel->
buffer()),
111 reinterpret_cast<const float *
>(_bias->
buffer()), output_activation_min, output_activation_max,
112 XNN_FLAG_DEPTHWISE_CONVOLUTION,
nullptr,
nullptr, &
_kernel_op);
113 if (status != xnn_status_success)
115 throw std::runtime_error{
"failed to create FP32 DepthwiseConvolution operator"};
123 if (_input->
buffer() ==
nullptr || _output->
buffer() ==
nullptr)
129 uint32_t input_width = _input->
getShape().dim(2);
130 uint32_t input_height = _input->
getShape().dim(1);
131 uint32_t batch_size = _input->
getShape().dim(0);
132 size_t workspace_size = 0;
133 size_t workspace_alignment = 0;
134 enum xnn_status status = xnn_reshape_convolution2d_nhwc_f32(
135 _kernel_op, batch_size, input_height, input_width, &workspace_size, &workspace_alignment,
137 if (status != xnn_status_success)
139 throw std::runtime_error{
"failed to create FP32 DepthwiseConvolution operator"};
142 std::vector<uint8_t> workspace(workspace_size);
143 status = xnn_setup_convolution2d_nhwc_f32(
_kernel_op, workspace.data(),
144 reinterpret_cast<const float *
>(_input->
buffer()),
145 reinterpret_cast<float *
>(_output->
buffer()));
146 if (status != xnn_status_success)
148 throw std::runtime_error{
"failed to create FP32 DepthwiseConvolution operator"};
void configure(const IPortableTensor *input, const IPortableTensor *kernel, const IPortableTensor *bias, ir::PaddingType padding_type, const uint32_t padding_left, const uint32_t padding_right, const uint32_t padding_top, const uint32_t padding_bottom, const uint32_t stride_width, const uint32_t stride_height, const uint32_t multiplier, const uint32_t dilation_width_factor, const uint32_t dilation_height_factor, const ir::Activation activation, IPortableTensor *output)