ONE - On-device Neural Engine
Loading...
Searching...
No Matches
DepthwiseConvolutionLayer.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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
18
19#include "OperationUtils.h"
20
24
25namespace onert
26{
27namespace backend
28{
29namespace train
30{
31namespace ops
32{
33
35 : cpu::ops::DepthwiseConvolutionLayer(), _grad_weights{nullptr}, _grad_bias{nullptr},
36 _back_prop_input{nullptr}, _back_prop_output{nullptr}, _act_back_prop_output{nullptr},
37 _filter_dim_buffers{nullptr}
38{
39 // DO NOTHING
40}
41
43 IPortableTensor *grad_weights,
44 IPortableTensor *grad_bias,
45 const IPortableTensor *back_prop_output,
46 const ir::Activation activation)
47{
48 _back_prop_input = back_prop_input;
49 _back_prop_output = back_prop_output;
50 _grad_weights = grad_weights;
51 _grad_bias = grad_bias;
52
53 if (_dilationWidth != 1 || _dilationHeight != 1)
54 throw std::runtime_error("train DepthwiseConvolutionLayer: Unsupported dilation yet");
55
56 if (activation != ir::Activation::NONE)
57 {
58 _act_back_prop_output = std::make_unique<BackPropTensor>(_back_prop_output->get_info());
59 _act_back_prop_output->setBuffer(
60 std::make_shared<basic::Allocator>(_act_back_prop_output->total_size()));
61 }
62
63 const int64_t k_packet_size = [&]() {
64 const auto data_type = _back_prop_output->data_type();
65 switch (data_type)
66 {
67 case OperandType::FLOAT32:
68 {
69 return nnfw::cker::eigen_support::kPacketSize<float>();
70 }
71 default:
72 throw std::runtime_error("train DepthwiseConvolutionLayer: unsupported data type");
73 }
74 }();
75
76 const auto incoming_shape = getShape(_back_prop_output);
77 const int out_depth = incoming_shape.Dims(3);
78
79 const int padded_filter_inner_dim_size =
80 ((out_depth + k_packet_size - 1) / k_packet_size) * k_packet_size;
81
82 // prepare out_bprop and in_bprop buffer for cker
83 // NOTE The Eigen library uses both main thread as well as a thread pool.
84 // Therefore, it needs to add an additional memory buffer for main thread.
85 const int thread_count = nnfw::cker::eigen_support::getThreadCount() + 1;
86
87 auto filter_dim_buffers_info = ir::OperandInfo(_back_prop_input->get_info());
88 filter_dim_buffers_info.shape({thread_count, padded_filter_inner_dim_size});
89 _filter_dim_buffers = std::make_unique<Tensor>(filter_dim_buffers_info);
90 _filter_dim_buffers->setBuffer(
91 std::make_shared<basic::Allocator>(_filter_dim_buffers->total_size()));
92}
93
95
97{
98 const auto data_type = _back_prop_output->data_type();
99 assert(data_type == _input->data_type());
100 switch (data_type)
101 {
102 case OperandType::FLOAT32:
103 {
104 assert(data_type == _grad_bias->data_type());
105 backwardFloat32();
106 break;
107 }
108 default:
109 throw std::runtime_error{"train DepthwiseConvolutionLayer: unsupported data type"};
110 }
111}
112
113void DepthwiseConvolutionLayer::backwardFloat32()
114{
115 // Calculate gradient for activation
116 const IPortableTensor *backprop_act;
117 try
118 {
119 backprop_act =
120 backpropActivation(_activation, _output, _back_prop_output, _act_back_prop_output.get());
121 }
122 catch (const std::exception &e)
123 {
124 throw std::runtime_error{"train DepthwiseConvolutionLayer: " + std::string(e.what())};
125 }
126 assert(backprop_act != nullptr);
127
129 dconv_params.stride_width = _strideWidth;
130 dconv_params.stride_height = _strideHeight;
131 dconv_params.padding_values.width = _paddingLeft;
132 dconv_params.padding_values.height = _paddingTop;
133 dconv_params.depth_multiplier = _multiplier;
136
137 // Calculate gradient for input
139 dconv_params, getShape(backprop_act), getBuffer<float>(backprop_act), getShape(_kernel),
140 getBuffer<float>(_kernel), getBuffer<float>(_padded_filter.get()), getShape(_back_prop_input),
141 getBuffer<float>(_back_prop_input), _use_padded_filter, getBuffer<float>(_filter_buffers.get()),
142 getBuffer<float>(_filter_dim_buffers.get()));
143
144 // Calculate gradient for weights
146 dconv_params, getShape(backprop_act), getBuffer<float>(backprop_act), getShape(_input),
147 getBuffer<float>(_input), getShape(_grad_weights), getBuffer<float>(_grad_weights),
148 getBuffer<float>(_padded_filter.get()), getBuffer<float>(_filter_buffers.get()));
149
150 // Calculate gradient for bias
151 if (_bias)
152 {
153 assert(_grad_bias);
154 biasGrad(backprop_act, _grad_bias);
155 }
156}
157
158} // namespace ops
159} // namespace train
160} // namespace backend
161} // namespace onert
A tensor class that is portable for other backends.
const ir::OperandInfo & get_info() const
ir::DataType data_type() const override final
void configureBackward(IPortableTensor *back_prop_input, IPortableTensor *grad_weights, IPortableTensor *grad_bias, const IPortableTensor *back_prop_output, const ir::Activation activation)
Class to save tensor's shape and type.
Definition OperandInfo.h:56
void backpropFilter(const DepthwiseConvParams &params, const Shape &incoming_shape, const T *incoming_data, const Shape &input_shape, const T *input_data, const Shape &filter_grad_shape, T *filter_grad_data, T *padded_filter_data, T *filter_buffers_data)
void backpropInput(const DepthwiseConvParams &params, const Shape &incoming_shape, const T *incoming_data, const Shape &filter_shape, const T *filter_data, T *padded_filter_data, const Shape &grad_shape, T *grad_data, bool pad_filter, T *filter_buffers_data, T *filter_dim_buffers_data)
void biasGrad(const IPortableTensor *input_backprop, IPortableTensor *bias_grad)
backpropagate bias
const IPortableTensor * backpropActivation(const ir::Activation &activation, const IPortableTensor *output, const IPortableTensor *input_backprop, IPortableTensor *output_backprop)
backpropagate acitvation
nnfw::cker::Shape getShape(const IPortableTensor *tensor)
Get shape of tensor.
PaddingValues padding_values
Definition Types.h:234