ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
26{
27
29 : cpu::ops::DepthwiseConvolutionLayer(), _grad_weights{nullptr}, _grad_bias{nullptr},
30 _back_prop_input{nullptr}, _back_prop_output{nullptr}, _act_back_prop_output{nullptr},
31 _filter_dim_buffers{nullptr}
32{
33 // DO NOTHING
34}
35
37 IPortableTensor *grad_weights,
38 IPortableTensor *grad_bias,
39 const IPortableTensor *back_prop_output,
40 const ir::Activation activation)
41{
42 _back_prop_input = back_prop_input;
43 _back_prop_output = back_prop_output;
44 _grad_weights = grad_weights;
45 _grad_bias = grad_bias;
46
47 if (_dilationWidth != 1 || _dilationHeight != 1)
48 throw std::runtime_error("train DepthwiseConvolutionLayer: Unsupported dilation yet");
49
50 if (activation != ir::Activation::NONE)
51 {
52 _act_back_prop_output = std::make_unique<BackPropTensor>(_back_prop_output->get_info());
53 _act_back_prop_output->setBuffer(
54 std::make_shared<basic::Allocator>(_act_back_prop_output->total_size()));
55 }
56
57 const int64_t k_packet_size = [&]() {
58 const auto data_type = _back_prop_output->data_type();
59 switch (data_type)
60 {
61 case OperandType::FLOAT32:
62 {
63 return nnfw::cker::eigen_support::kPacketSize<float>();
64 }
65 default:
66 throw std::runtime_error("train DepthwiseConvolutionLayer: unsupported data type");
67 }
68 }();
69
70 const auto incoming_shape = getShape(_back_prop_output);
71 const int out_depth = incoming_shape.Dims(3);
72
73 const int padded_filter_inner_dim_size =
74 ((out_depth + k_packet_size - 1) / k_packet_size) * k_packet_size;
75
76 // prepare out_bprop and in_bprop buffer for cker
77 // NOTE The Eigen library uses both main thread as well as a thread pool.
78 // Therefore, it needs to add an additional memory buffer for main thread.
79 const int thread_count = nnfw::cker::eigen_support::getThreadCount() + 1;
80
81 auto filter_dim_buffers_info = ir::OperandInfo(_back_prop_input->get_info());
82 filter_dim_buffers_info.shape({thread_count, padded_filter_inner_dim_size});
83 _filter_dim_buffers = std::make_unique<Tensor>(filter_dim_buffers_info);
84 _filter_dim_buffers->setBuffer(
85 std::make_shared<basic::Allocator>(_filter_dim_buffers->total_size()));
86}
87
89
91{
92 const auto data_type = _back_prop_output->data_type();
93 assert(data_type == _input->data_type());
94 switch (data_type)
95 {
96 case OperandType::FLOAT32:
97 {
98 assert(data_type == _grad_bias->data_type());
99 backwardFloat32();
100 break;
101 }
102 default:
103 throw std::runtime_error{"train DepthwiseConvolutionLayer: unsupported data type"};
104 }
105}
106
107void DepthwiseConvolutionLayer::backwardFloat32()
108{
109 // Calculate gradient for activation
110 const IPortableTensor *backprop_act;
111 try
112 {
113 backprop_act =
114 backpropActivation(_activation, _output, _back_prop_output, _act_back_prop_output.get());
115 }
116 catch (const std::exception &e)
117 {
118 throw std::runtime_error{"train DepthwiseConvolutionLayer: " + std::string(e.what())};
119 }
120 assert(backprop_act != nullptr);
121
123 dconv_params.stride_width = _strideWidth;
124 dconv_params.stride_height = _strideHeight;
125 dconv_params.padding_values.width = _paddingLeft;
126 dconv_params.padding_values.height = _paddingTop;
127 dconv_params.depth_multiplier = _multiplier;
130
131 // Calculate gradient for input
133 dconv_params, getShape(backprop_act), getBuffer<float>(backprop_act), getShape(_kernel),
134 getBuffer<float>(_kernel), getBuffer<float>(_padded_filter.get()), getShape(_back_prop_input),
135 getBuffer<float>(_back_prop_input), _use_padded_filter, getBuffer<float>(_filter_buffers.get()),
136 getBuffer<float>(_filter_dim_buffers.get()));
137
138 // Calculate gradient for weights
140 dconv_params, getShape(backprop_act), getBuffer<float>(backprop_act), getShape(_input),
141 getBuffer<float>(_input), getShape(_grad_weights), getBuffer<float>(_grad_weights),
142 getBuffer<float>(_padded_filter.get()), getBuffer<float>(_filter_buffers.get()));
143
144 // Calculate gradient for bias
145 if (_bias)
146 {
147 assert(_grad_bias);
148 biasGrad(backprop_act, _grad_bias);
149 }
150}
151
152} // namespace onert::backend::train::ops
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:54
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