ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ConvolutionLayer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ConvolutionLayer.h"
18
19#include "ir/Padding.h"
20
21namespace onert
22{
23namespace backend
24{
25namespace xnnpack
26{
27namespace ops
28{
29ConvolutionLayer::ConvolutionLayer(const std::shared_ptr<ExternalContext> external_context)
30 : Layer(external_context), _input(nullptr), _kernel(nullptr), _bias(nullptr), _output(nullptr),
31 _padding_type(ir::PaddingType::EXPLICIT), _padding_left(0), _padding_top(0), _padding_right(0),
32 _padding_bottom(0), _stride_width(0), _stride_height(0), _dilation_width_factor(1),
33 _dilation_height_factor(1), _activation(ir::Activation::NONE)
34{
35 // DO NOTHING
36}
37
39 const IPortableTensor *bias, ir::PaddingType padding_type,
40 const uint32_t padding_left, const uint32_t padding_right,
41 const uint32_t padding_top, const uint32_t padding_bottom,
42 const uint32_t stride_width, const uint32_t stride_height,
43 const uint32_t dilation_width_factor,
44 const uint32_t dilation_height_factor,
45 const ir::Activation activation, IPortableTensor *output)
46{
47 _input = input;
48 _kernel = kernel;
49 _bias = bias;
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 _dilation_width_factor = dilation_width_factor;
58 _dilation_height_factor = dilation_height_factor;
59 _activation = activation;
60 _output = output;
61
62 assert(_activation == ir::Activation::NONE || _activation == ir::Activation::RELU ||
63 _activation == ir::Activation::RELU1 || _activation == ir::Activation::RELU6);
64}
65
67{
68 assert(_external_context && _external_context->getThreadPool());
69 if (!_setup)
70 {
71 _setup = setup();
72 assert(_setup);
73 }
74
75 if (_input->data_type() == OperandType::FLOAT32)
76 {
77 enum xnn_status status = xnn_run_operator(_kernel_op, _external_context->getThreadPool());
78 if (status != xnn_status_success)
79 {
80 throw std::runtime_error{"failed to run FP32 Convolution operator"};
81 }
82 }
83 else
84 {
85 throw std::runtime_error{"XNNPACK Conv: unsupported data type"};
86 }
87}
88
90{
91 float output_activation_min = 0.f, output_activation_max = 0.f;
92 CalculateActivationRange<float>(_activation, &output_activation_min, &output_activation_max);
93
94 // NHWC
95 // Kernel format is [depth_out, kernel_height, kernel_width, depth_in].
96 const auto &kernel_shape = _kernel->getShape();
97 uint32_t kernel_height = kernel_shape.dim(1);
98 uint32_t kernel_width = kernel_shape.dim(2);
99 uint32_t output_channels = kernel_shape.dim(0);
100 uint32_t input_channels = kernel_shape.dim(3);
101 assert(static_cast<uint32_t>(_input->getShape().dim(3)) == input_channels);
102 assert(static_cast<uint32_t>(_output->getShape().dim(3)) == output_channels);
103
104 enum xnn_status status = xnn_create_convolution2d_nhwc_f32(
105 _padding_top, _padding_right, _padding_bottom, _padding_left, kernel_height, kernel_width,
106 _stride_height, _stride_width, _dilation_height_factor, _dilation_width_factor, 1 /* groups */,
107 input_channels /* group_input_channels */, output_channels /* group_output_channels */,
108 input_channels /* input_channel_stride */, output_channels /* output_channel_stride */,
109 reinterpret_cast<const float *>(_kernel->buffer()),
110 reinterpret_cast<const float *>(_bias->buffer()), output_activation_min, output_activation_max,
111 0, nullptr, nullptr, &_kernel_op);
112 if (status != xnn_status_success)
113 {
114 throw std::runtime_error{"failed to create FP32 Convolution operator"};
115 }
116 assert(_kernel_op != nullptr);
117 return true;
118}
119
121{
122 if (_input->buffer() == nullptr || _output->buffer() == nullptr)
123 {
124 // it could be models's input or output
125 return false;
126 }
127
128 uint32_t input_width = _input->getShape().dim(2);
129 uint32_t input_height = _input->getShape().dim(1);
130 uint32_t batch_size = _input->getShape().dim(0);
131 size_t workspace_size = 0;
132 size_t workspace_alignment = 0;
133 enum xnn_status status = xnn_reshape_convolution2d_nhwc_f32(
134 _kernel_op, batch_size, input_height, input_width, &workspace_size, &workspace_alignment,
135 nullptr, nullptr, _external_context->getThreadPool());
136 if (status != xnn_status_success)
137 {
138 throw std::runtime_error{"failed to create FP32 DepthwiseConvolution operator"};
139 }
140
141 std::vector<uint8_t> workspace(workspace_size);
142 status = xnn_setup_convolution2d_nhwc_f32(_kernel_op, workspace.data(),
143 reinterpret_cast<const float *>(_input->buffer()),
144 reinterpret_cast<float *>(_output->buffer()));
145 if (status != xnn_status_success)
146 {
147 throw std::runtime_error{"failed to create FP32 Convolution operator"};
148 }
149 return true;
150}
151
152} // namespace ops
153} // namespace xnnpack
154} // namespace backend
155} // namespace onert
A tensor class that is portable for other backends.
ir::DataType data_type() const override final
ir::Shape getShape() const override final
Get ir::Shape of tensor.
virtual uint8_t * buffer() const =0
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 dilation_width_factor, const uint32_t dilation_height_factor, const ir::Activation activation, IPortableTensor *output)
ConvolutionLayer(const std::shared_ptr< ExternalContext > external_context)
const std::shared_ptr< ExternalContext > _external_context
Definition Layer.h:73