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 ThreadCommonParam{ |
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 |
|
|
} ThreadCommonParam; |
37 |
|
|
|
38 |
|
|
typedef struct ThreadParam{ |
39 |
|
|
ThreadCommonParam *thread_common_param; |
40 |
|
|
int thread_start, thread_end; |
41 |
|
|
#if HAVE_PTHREAD_CANCEL |
42 |
|
|
pthread_t thread; |
43 |
|
|
#endif |
44 |
|
|
} ThreadParam; |
45 |
|
|
|
46 |
|
|
int ff_dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num) |
47 |
|
|
{ |
48 |
|
|
ConvolutionalParams *conv_params; |
49 |
|
|
int kernel_size; |
50 |
|
|
int dnn_size = 0; |
51 |
|
|
conv_params = av_malloc(sizeof(*conv_params)); |
52 |
|
|
if (!conv_params) |
53 |
|
|
return 0; |
54 |
|
|
|
55 |
|
|
conv_params->dilation = (int32_t)avio_rl32(model_file_context); |
56 |
|
|
conv_params->padding_method = (int32_t)avio_rl32(model_file_context); |
57 |
|
|
conv_params->activation = (int32_t)avio_rl32(model_file_context); |
58 |
|
|
conv_params->input_num = (int32_t)avio_rl32(model_file_context); |
59 |
|
|
conv_params->output_num = (int32_t)avio_rl32(model_file_context); |
60 |
|
|
conv_params->kernel_size = (int32_t)avio_rl32(model_file_context); |
61 |
|
|
conv_params->has_bias = (int32_t)avio_rl32(model_file_context); |
62 |
|
|
dnn_size += 28; |
63 |
|
|
|
64 |
|
|
kernel_size = conv_params->input_num * conv_params->output_num * |
65 |
|
|
conv_params->kernel_size * conv_params->kernel_size; |
66 |
|
|
dnn_size += kernel_size * 4; |
67 |
|
|
if (conv_params->has_bias) |
68 |
|
|
dnn_size += conv_params->output_num * 4; |
69 |
|
|
|
70 |
|
|
if (dnn_size > file_size || conv_params->input_num <= 0 || |
71 |
|
|
conv_params->output_num <= 0 || conv_params->kernel_size <= 0){ |
72 |
|
|
av_freep(&conv_params); |
73 |
|
|
return 0; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
conv_params->kernel = av_malloc_array(kernel_size, sizeof(*conv_params->kernel)); |
77 |
|
|
if (!conv_params->kernel) { |
78 |
|
|
av_freep(&conv_params); |
79 |
|
|
return 0; |
80 |
|
|
} |
81 |
|
|
for (int i = 0; i < kernel_size; ++i) { |
82 |
|
|
conv_params->kernel[i] = av_int2float(avio_rl32(model_file_context)); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
conv_params->biases = NULL; |
86 |
|
|
if (conv_params->has_bias) { |
87 |
|
|
conv_params->biases = av_malloc_array(conv_params->output_num, sizeof(*conv_params->biases)); |
88 |
|
|
if (!conv_params->biases){ |
89 |
|
|
av_freep(&conv_params->kernel); |
90 |
|
|
av_freep(&conv_params); |
91 |
|
|
return 0; |
92 |
|
|
} |
93 |
|
|
for (int i = 0; i < conv_params->output_num; ++i){ |
94 |
|
|
conv_params->biases[i] = av_int2float(avio_rl32(model_file_context)); |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
layer->params = conv_params; |
99 |
|
|
|
100 |
|
|
layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context); |
101 |
|
|
layer->output_operand_index = (int32_t)avio_rl32(model_file_context); |
102 |
|
|
dnn_size += 8; |
103 |
|
|
|
104 |
|
|
if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) { |
105 |
|
|
return 0; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
return dnn_size; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
2 |
static void * dnn_execute_layer_conv2d_thread(void *threadarg) |
112 |
|
|
{ |
113 |
|
|
//pass parameters |
114 |
|
2 |
ThreadParam *thread_param = threadarg; |
115 |
|
2 |
ThreadCommonParam *thread_common_param = thread_param->thread_common_param; |
116 |
|
2 |
DnnOperand *operands = thread_common_param->operands; |
117 |
|
2 |
int32_t input_operand_index = thread_common_param->input_operand_indexes[0]; |
118 |
|
2 |
int height = operands[input_operand_index].dims[1]; |
119 |
|
2 |
int width = operands[input_operand_index].dims[2]; |
120 |
|
2 |
int channel = operands[input_operand_index].dims[3]; |
121 |
|
2 |
const float *input = operands[input_operand_index].data; |
122 |
|
2 |
const ConvolutionalParams *conv_params = thread_common_param->parameters; |
123 |
|
|
|
124 |
|
2 |
int radius = conv_params->kernel_size >> 1; |
125 |
|
2 |
int src_linesize = width * conv_params->input_num; |
126 |
|
2 |
int filter_linesize = conv_params->kernel_size * conv_params->input_num; |
127 |
|
2 |
int filter_size = conv_params->kernel_size * filter_linesize; |
128 |
✓✓ |
2 |
int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0; |
129 |
|
|
|
130 |
|
2 |
float *output = thread_common_param->output_data; |
131 |
|
2 |
output += (conv_params->output_num) * (width - 2 * pad_size) * (thread_param->thread_start - pad_size); |
132 |
|
|
|
133 |
✗✓ |
2 |
av_assert0(channel == conv_params->input_num); |
134 |
|
|
|
135 |
✓✓ |
10 |
for (int y = thread_param->thread_start; y < thread_param->thread_end; ++y) { |
136 |
✓✓ |
50 |
for (int x = pad_size; x < width - pad_size; ++x) { |
137 |
✓✓ |
126 |
for (int n_filter = 0; n_filter < conv_params->output_num; ++n_filter) { |
138 |
✓✗ |
84 |
if (conv_params->has_bias) |
139 |
|
84 |
output[n_filter] = conv_params->biases[n_filter]; |
140 |
|
|
else |
141 |
|
|
output[n_filter] = 0.f; |
142 |
|
|
|
143 |
✓✓ |
336 |
for (int ch = 0; ch < conv_params->input_num; ++ch) { |
144 |
✓✓ |
1008 |
for (int kernel_y = 0; kernel_y < conv_params->kernel_size; ++kernel_y) { |
145 |
✓✓ |
3024 |
for (int kernel_x = 0; kernel_x < conv_params->kernel_size; ++kernel_x) { |
146 |
|
|
float input_pel; |
147 |
✗✓ |
2268 |
if (conv_params->padding_method == SAME_CLAMP_TO_EDGE) { |
148 |
|
|
int y_pos = CLAMP_TO_EDGE(y + (kernel_y - radius) * conv_params->dilation, height); |
149 |
|
|
int x_pos = CLAMP_TO_EDGE(x + (kernel_x - radius) * conv_params->dilation, width); |
150 |
|
|
input_pel = input[y_pos * src_linesize + x_pos * conv_params->input_num + ch]; |
151 |
|
|
} else { |
152 |
|
2268 |
int y_pos = y + (kernel_y - radius) * conv_params->dilation; |
153 |
|
2268 |
int x_pos = x + (kernel_x - radius) * conv_params->dilation; |
154 |
✓✓✓✓ ✓✓✓✓
|
3840 |
input_pel = (x_pos < 0 || x_pos >= width || y_pos < 0 || y_pos >= height) ? 0.0 : |
155 |
|
1572 |
input[y_pos * src_linesize + x_pos * conv_params->input_num + ch]; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
|
159 |
|
2268 |
output[n_filter] += input_pel * conv_params->kernel[n_filter * filter_size + kernel_y * filter_linesize + |
160 |
|
2268 |
kernel_x * conv_params->input_num + ch]; |
161 |
|
|
} |
162 |
|
|
} |
163 |
|
|
} |
164 |
✗✓✗✗ ✗✗ |
84 |
switch (conv_params->activation){ |
165 |
|
|
case RELU: |
166 |
|
|
output[n_filter] = FFMAX(output[n_filter], 0.0); |
167 |
|
|
break; |
168 |
|
84 |
case TANH: |
169 |
|
84 |
output[n_filter] = 2.0f / (1.0f + exp(-2.0f * output[n_filter])) - 1.0f; |
170 |
|
84 |
break; |
171 |
|
|
case SIGMOID: |
172 |
|
|
output[n_filter] = 1.0f / (1.0f + exp(-output[n_filter])); |
173 |
|
|
break; |
174 |
|
|
case NONE: |
175 |
|
|
break; |
176 |
|
|
case LEAKY_RELU: |
177 |
|
|
output[n_filter] = FFMAX(output[n_filter], 0.0) + 0.2 * FFMIN(output[n_filter], 0.0); |
178 |
|
|
} |
179 |
|
84 |
} |
180 |
|
42 |
output += conv_params->output_num; |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
2 |
return NULL; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
|
187 |
|
2 |
int ff_dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes, |
188 |
|
|
int32_t output_operand_index, const void *parameters, NativeContext *ctx) |
189 |
|
|
{ |
190 |
|
|
#if HAVE_PTHREAD_CANCEL |
191 |
✗✓ |
2 |
int thread_num = (ctx->options.conv2d_threads <= 0 || ctx->options.conv2d_threads > av_cpu_count()) |
192 |
✓✗ |
4 |
? (av_cpu_count() + 1) : (ctx->options.conv2d_threads); |
193 |
|
2 |
int ret = DNN_SUCCESS, thread_stride; |
194 |
|
|
ThreadParam *thread_param; |
195 |
|
|
#else |
196 |
|
|
ThreadParam thread_param = { 0 }; |
197 |
|
|
#endif |
198 |
|
|
ThreadCommonParam thread_common_param; |
199 |
|
2 |
const ConvolutionalParams *conv_params = parameters; |
200 |
|
2 |
int height = operands[input_operand_indexes[0]].dims[1]; |
201 |
|
2 |
int width = operands[input_operand_indexes[0]].dims[2]; |
202 |
✓✓ |
2 |
int pad_size = (conv_params->padding_method == VALID) ? (conv_params->kernel_size - 1) / 2 * conv_params->dilation : 0; |
203 |
|
2 |
DnnOperand *output_operand = &operands[output_operand_index]; |
204 |
|
|
void *tmp; |
205 |
|
|
|
206 |
|
2 |
output_operand->dims[0] = operands[input_operand_indexes[0]].dims[0]; |
207 |
|
2 |
output_operand->dims[1] = height - pad_size * 2; |
208 |
|
2 |
output_operand->dims[2] = width - pad_size * 2; |
209 |
|
2 |
output_operand->dims[3] = conv_params->output_num; |
210 |
|
2 |
output_operand->data_type = operands[input_operand_indexes[0]].data_type; |
211 |
|
2 |
output_operand->length = ff_calculate_operand_data_length(output_operand); |
212 |
✗✓ |
2 |
if (output_operand->length <= 0) { |
213 |
|
|
av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n"); |
214 |
|
|
return DNN_ERROR; |
215 |
|
|
} |
216 |
|
2 |
tmp = av_realloc(output_operand->data, output_operand->length); |
217 |
✗✓ |
2 |
if (!tmp) { |
218 |
|
|
av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n"); |
219 |
|
|
return DNN_ERROR; |
220 |
|
|
} |
221 |
|
2 |
output_operand->data = tmp; |
222 |
|
2 |
thread_common_param.output_data = output_operand->data; |
223 |
|
2 |
thread_common_param.operands = operands; |
224 |
|
2 |
thread_common_param.input_operand_indexes = input_operand_indexes; |
225 |
|
2 |
thread_common_param.output_operand_index = output_operand_index; |
226 |
|
2 |
thread_common_param.parameters = parameters; |
227 |
|
2 |
thread_common_param.ctx = ctx; |
228 |
|
|
|
229 |
|
|
#if HAVE_PTHREAD_CANCEL |
230 |
|
2 |
thread_param = av_malloc_array(thread_num, sizeof(*thread_param)); |
231 |
✗✓ |
2 |
if (!thread_param) |
232 |
|
|
return DNN_ERROR; |
233 |
|
2 |
thread_stride = (height - pad_size * 2) / thread_num; |
234 |
|
|
//create threads |
235 |
✓✓ |
4 |
for (int i = 0; i < thread_num; i++){ |
236 |
|
2 |
thread_param[i].thread_common_param = &thread_common_param; |
237 |
|
2 |
thread_param[i].thread_start = thread_stride * i + pad_size; |
238 |
✓✗ |
2 |
thread_param[i].thread_end = (i == thread_num - 1) ? (height - pad_size) : (thread_param[i].thread_start + thread_stride); |
239 |
✗✓ |
2 |
if (pthread_create(&thread_param[i].thread, NULL, |
240 |
|
2 |
dnn_execute_layer_conv2d_thread, &thread_param[i])) { |
241 |
|
|
thread_num = i; |
242 |
|
|
ret = DNN_ERROR; |
243 |
|
|
break; |
244 |
|
|
} |
245 |
|
|
} |
246 |
|
|
|
247 |
✓✓ |
4 |
for (int i = 0; i < thread_num; i++){ |
248 |
|
2 |
pthread_join(thread_param[i].thread, NULL); |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
//release memory |
252 |
|
2 |
av_freep(&thread_param); |
253 |
|
|
|
254 |
|
2 |
return ret; |
255 |
|
|
#else |
256 |
|
|
thread_param.thread_common_param = &thread_common_param; |
257 |
|
|
thread_param.thread_start = pad_size; |
258 |
|
|
thread_param.thread_end = height - pad_size; |
259 |
|
|
dnn_execute_layer_conv2d_thread(&thread_param); |
260 |
|
|
|
261 |
|
|
return DNN_SUCCESS; |
262 |
|
|
#endif |
263 |
|
|
} |