]> git.sesse.net Git - ffmpeg/blob - libavfilter/dnn/dnn_backend_native_layer_conv2d.c
5c313454f73868389f532d45b01572d9260472a3
[ffmpeg] / libavfilter / dnn / dnn_backend_native_layer_conv2d.c
1 /*
2  * Copyright (c) 2018 Sergey Lavrushkin
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/thread.h"
23 #include "libavutil/cpu.h"
24 #include "dnn_backend_native_layer_conv2d.h"
25
26 #define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))
27
28 //struct to pass parameters
29 typedef struct thread_common_param{
30     DnnOperand *operands;
31     const int32_t *input_operand_indexes;
32     int32_t output_operand_index;
33     const void *parameters;
34     NativeContext *ctx;
35     float *output_data;
36     int thread_num;
37 } thread_common_param;
38
39 typedef struct thread_param{
40     thread_common_param *thread_common_param;
41     int thread_index;
42 } thread_param;
43
44 int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
45 {
46     ConvolutionalParams *conv_params;
47     int kernel_size;
48     int dnn_size = 0;
49     conv_params = av_malloc(sizeof(*conv_params));
50     if (!conv_params)
51         return 0;
52
53     conv_params->dilation = (int32_t)avio_rl32(model_file_context);
54     conv_params->padding_method = (int32_t)avio_rl32(model_file_context);
55     conv_params->activation = (int32_t)avio_rl32(model_file_context);
56     conv_params->input_num = (int32_t)avio_rl32(model_file_context);
57     conv_params->output_num = (int32_t)avio_rl32(model_file_context);
58     conv_params->kernel_size = (int32_t)avio_rl32(model_file_context);
59     conv_params->has_bias = (int32_t)avio_rl32(model_file_context);
60     dnn_size += 28;
61
62     kernel_size = conv_params->input_num * conv_params->output_num *
63                       conv_params->kernel_size * conv_params->kernel_size;
64     dnn_size += kernel_size * 4;
65     if (conv_params->has_bias)
66         dnn_size += conv_params->output_num * 4;
67
68     if (dnn_size > file_size || conv_params->input_num <= 0 ||
69         conv_params->output_num <= 0 || conv_params->kernel_size <= 0){
70         av_freep(&conv_params);
71         return 0;
72     }
73
74     conv_params->kernel = av_malloc(kernel_size * sizeof(float));
75     if (!conv_params->kernel) {
76         av_freep(&conv_params);
77         return 0;
78     }
79     for (int i = 0; i < kernel_size; ++i) {
80         conv_params->kernel[i] = av_int2float(avio_rl32(model_file_context));
81     }
82
83     conv_params->biases = NULL;
84     if (conv_params->has_bias) {
85         conv_params->biases = av_malloc(conv_params->output_num * sizeof(float));
86         if (!conv_params->biases){
87             av_freep(&conv_params->kernel);
88             av_freep(&conv_params);
89             return 0;
90         }
91         for (int i = 0; i < conv_params->output_num; ++i){
92             conv_params->biases[i] = av_int2float(avio_rl32(model_file_context));
93         }
94     }
95
96     layer->params = conv_params;
97
98     layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
99     layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
100     dnn_size += 8;
101
102     if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) {
103         return 0;
104     }
105
106     return dnn_size;
107 }
108
109 static void * dnn_execute_layer_conv2d_thread(void *threadarg)
110 {
111     //pass parameters
112     thread_param *thread_param = (struct thread_param *)threadarg;
113     thread_common_param *thread_common_param = thread_param->thread_common_param;
114     DnnOperand *operands = thread_common_param->operands;
115     int32_t input_operand_index = thread_common_param->input_operand_indexes[0];
116     int height = operands[input_operand_index].dims[1];
117     int width = operands[input_operand_index].dims[2];
118     int channel = operands[input_operand_index].dims[3];
119     const float *input = operands[input_operand_index].data;
120     const ConvolutionalParams *conv_params = (const ConvolutionalParams *)(thread_common_param->parameters);
121
122     int radius = conv_params->kernel_size >> 1;
123     int src_linesize = width * conv_params->input_num;
124     int filter_linesize = conv_params->kernel_size * conv_params->input_num;
125     int filter_size = conv_params->kernel_size * filter_linesize;
126     int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
127
128     int thread_stride = (height - pad_size * 2) / thread_common_param->thread_num;
129     int thread_start = thread_stride * thread_param->thread_index + pad_size;
130     int thread_end = (thread_param->thread_index == thread_common_param->thread_num - 1) ? (height - pad_size) : (thread_start + thread_stride);
131
132     float *output = thread_common_param->output_data;
133     output += (conv_params->output_num) * (width - 2 * pad_size) * (thread_start - pad_size);
134
135     av_assert0(channel == conv_params->input_num);
136
137     for (int y = thread_start; y < thread_end; ++y) {
138         for (int x = pad_size; x < width - pad_size; ++x) {
139             for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) {
140                 if (conv_params->has_bias)
141                     output[n_filter] = conv_params->biases[n_filter];
142                 else
143                     output[n_filter] = 0.f;
144
145                 for (int ch = 0; ch < conv_params->input_num; ++ch) {
146                     for (int kernel_y = 0; kernel_y < conv_params->kernel_size; ++kernel_y) {
147                         for (int kernel_x = 0; kernel_x < conv_params->kernel_size; ++kernel_x) {
148                             float input_pel;
149                             if (conv_params->padding_method == SAME_CLAMP_TO_EDGE) {
150                                 int y_pos = CLAMP_TO_EDGE(y + (kernel_y - radius) * conv_params->dilation, height);
151                                 int x_pos = CLAMP_TO_EDGE(x + (kernel_x - radius) * conv_params->dilation, width);
152                                 input_pel = input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
153                             } else {
154                                 int y_pos = y + (kernel_y - radius) * conv_params->dilation;
155                                 int x_pos = x + (kernel_x - radius) * conv_params->dilation;
156                                 input_pel = (x_pos < 0 || x_pos >= width || y_pos < 0 || y_pos >= height) ? 0.0 :
157                                                    input[y_pos * src_linesize + x_pos * conv_params->input_num + ch];
158                             }
159
160
161                             output[n_filter] += input_pel * conv_params->kernel[n_filter * filter_size + kernel_y * filter_linesize +
162                                                                                 kernel_x * conv_params->input_num + ch];
163                         }
164                     }
165                 }
166                 switch (conv_params->activation){
167                 case RELU:
168                     output[n_filter] = FFMAX(output[n_filter], 0.0);
169                     break;
170                 case TANH:
171                     output[n_filter] = 2.0f  / (1.0f + exp(-2.0f * output[n_filter])) - 1.0f;
172                     break;
173                 case SIGMOID:
174                     output[n_filter] = 1.0f / (1.0f + exp(-output[n_filter]));
175                     break;
176                 case NONE:
177                     break;
178                 case LEAKY_RELU:
179                     output[n_filter] = FFMAX(output[n_filter], 0.0) + 0.2 * FFMIN(output[n_filter], 0.0);
180                 }
181             }
182             output += conv_params->output_num;
183         }
184     }
185     return (void *)DNN_SUCCESS;
186 }
187
188
189 int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes,
190                              int32_t output_operand_index, const void *parameters, NativeContext *ctx)
191 {
192     int thread_num = (ctx->options.conv2d_threads <= 0 || ctx->options.conv2d_threads > av_cpu_count())
193         ? (av_cpu_count() + 1) : (ctx->options.conv2d_threads);
194 #if HAVE_PTHREAD_CANCEL
195     pthread_t *thread_id = av_malloc(thread_num * sizeof(pthread_t));
196 #endif
197     thread_param **thread_param = av_malloc(thread_num * sizeof(*thread_param));
198     thread_common_param thread_common_param;
199     const ConvolutionalParams *conv_params = (const ConvolutionalParams *)(parameters);
200     int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0;
201     DnnOperand *output_operand = &operands[output_operand_index];
202
203     output_operand->dims[0] = operands[input_operand_indexes[0]].dims[0];
204     output_operand->dims[1] = operands[input_operand_indexes[0]].dims[1] - pad_size * 2;
205     output_operand->dims[2] = operands[input_operand_indexes[0]].dims[2] - pad_size * 2;
206     output_operand->dims[3] = conv_params->output_num;
207     output_operand->data_type = operands[input_operand_indexes[0]].data_type;
208     output_operand->length = calculate_operand_data_length(output_operand);
209     if (output_operand->length <= 0) {
210         av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
211         return DNN_ERROR;
212     }
213     output_operand->data = av_realloc(output_operand->data, output_operand->length);
214     if (!output_operand->data) {
215         av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
216         return DNN_ERROR;
217     }
218     thread_common_param.output_data = output_operand->data;
219     thread_common_param.operands = operands;
220     thread_common_param.input_operand_indexes = input_operand_indexes;
221     thread_common_param.output_operand_index = output_operand_index;
222     thread_common_param.parameters = parameters;
223     thread_common_param.ctx = ctx;
224
225 #if HAVE_PTHREAD_CANCEL
226     thread_common_param.thread_num = thread_num;
227
228     //create threads
229     for (int i = 0; i < thread_num; i++){
230         thread_param[i] = av_malloc(sizeof(**thread_param));
231         thread_param[i]->thread_common_param = &thread_common_param;
232         thread_param[i]->thread_index = i;
233         pthread_create(&thread_id[i], NULL, dnn_execute_layer_conv2d_thread, (void *)thread_param[i]);
234     }
235
236     //join threads, res gets function return
237     for (int i = 0; i < thread_num; i++){
238         pthread_join(thread_id[i], NULL);
239     }
240
241     //release memory
242     av_free(thread_id);
243
244     for (int i = 0; i < thread_num; i++){
245         av_free(thread_param[i]);
246     }
247 #else
248     thread_common_param.thread_num = 1;
249     thread_param[0] = av_malloc(sizeof(thread_param));
250     thread_param[0]->thread_common_param = &thread_common_param;
251     thread_param[0]->thread_index = 0;
252     dnn_execute_layer_conv2d_thread((void *)thread_param[0]);
253     av_free(thread_param[0]);
254 #endif
255
256     av_free(thread_param);
257     return DNN_SUCCESS;
258 }