@@ -331,10 +331,6 @@ DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *output
{
ConvolutionalNetwork *network = (ConvolutionalNetwork *)model->model;
int32_t layer;
- ConvolutionalParams *conv_params;
- DepthToSpaceParams *depth_to_space_params;
- LayerPadParams *pad_params;
- DnnLayerMaximumParams *maximum_params;
uint32_t nb = FFMIN(nb_output, network->nb_output);
if (network->layers_num <= 0 || network->operands_num <= 0)
@@ -344,28 +340,22 @@ DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *output
for (layer = 0; layer < network->layers_num; ++layer){
switch (network->layers[layer].type){
- case DLT_CONV2D:
- conv_params = (ConvolutionalParams *)network->layers[layer].params;
- convolve(network->operands, network->layers[layer].input_operand_indexes,
- network->layers[layer].output_operand_index, conv_params);
- break;
- case DLT_DEPTH_TO_SPACE:
- depth_to_space_params = (DepthToSpaceParams *)network->layers[layer].params;
- depth_to_space(network->operands, network->layers[layer].input_operand_indexes,
- network->layers[layer].output_operand_index, depth_to_space_params->block_size);
- break;
- case DLT_MIRROR_PAD:
- pad_params = (LayerPadParams *)network->layers[layer].params;
- dnn_execute_layer_pad(network->operands, network->layers[layer].input_operand_indexes,
- network->layers[layer].output_operand_index, pad_params);
- break;
- case DLT_MAXIMUM:
- maximum_params = (DnnLayerMaximumParams *)network->layers[layer].params;
- dnn_execute_layer_maximum(network->operands, network->layers[layer].input_operand_indexes,
- network->layers[layer].output_operand_index, maximum_params);
- break;
+
+#define SETUP_LAYER(layer) \
+ case layer: \
+ dnn_execute_layer_##layer(network->operands, \
+ network->layers[layer].input_operand_indexes, \
+ network->layers[layer].output_operand_index, \
+ network->layers[layer].params); \
+ break;
+#include "dnn_backend_native_layers.hxx"
+#undef SETUP_LAYER
+
case DLT_INPUT:
return DNN_ERROR;
+ default:
+ av_assert0(!"should not reach here");
+ break;
}
}
@@ -23,7 +23,8 @@
#define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))
-int convolve(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const ConvolutionalParams *conv_params)
+int dnn_execute_layer_DLT_CONV2D(DnnOperand *operands, const int32_t *input_operand_indexes,
+ int32_t output_operand_index, const void *parameters)
{
float *output;
int32_t input_operand_index = input_operand_indexes[0];
@@ -32,6 +33,7 @@ int convolve(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t
int width = operands[input_operand_index].dims[2];
int channel = operands[input_operand_index].dims[3];
const float *input = operands[input_operand_index].data;
+ const ConvolutionalParams *conv_params = (const ConvolutionalParams *)parameters;
int radius = conv_params->kernel_size >> 1;
int src_linesize = width * conv_params->input_num;
@@ -35,5 +35,6 @@ typedef struct ConvolutionalParams{
float *biases;
} ConvolutionalParams;
-int convolve(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const ConvolutionalParams *conv_params);
+int dnn_execute_layer_DLT_CONV2D(DnnOperand *operands, const int32_t *input_operand_indexes,
+ int32_t output_operand_index, const void *parameters);
#endif
@@ -27,9 +27,12 @@
#include "libavutil/avassert.h"
#include "dnn_backend_native_layer_depth2space.h"
-int depth_to_space(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, int block_size)
+int dnn_execute_layer_DLT_DEPTH_TO_SPACE(DnnOperand *operands, const int32_t *input_operand_indexes,
+ int32_t output_operand_index, const void *parameters)
{
float *output;
+ const DepthToSpaceParams *params = (const DepthToSpaceParams *)parameters;
+ int block_size = params->block_size;
int32_t input_operand_index = input_operand_indexes[0];
int number = operands[input_operand_index].dims[0];
int height = operands[input_operand_index].dims[1];
@@ -34,6 +34,7 @@ typedef struct DepthToSpaceParams{
int block_size;
} DepthToSpaceParams;
-int depth_to_space(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, int block_size);
+int dnn_execute_layer_DLT_DEPTH_TO_SPACE(DnnOperand *operands, const int32_t *input_operand_indexes,
+ int32_t output_operand_index, const void *parameters);
#endif
@@ -27,10 +27,12 @@
#include "libavutil/avassert.h"
#include "dnn_backend_native_layer_maximum.h"
-int dnn_execute_layer_maximum(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const DnnLayerMaximumParams *params)
+int dnn_execute_layer_DLT_MAXIMUM(DnnOperand *operands, const int32_t *input_operand_indexes,
+ int32_t output_operand_index, const void *parameters)
{
const DnnOperand *input = &operands[input_operand_indexes[0]];
DnnOperand *output = &operands[output_operand_index];
+ const DnnLayerMaximumParams *params = (const DnnLayerMaximumParams *)parameters;
int dims_count;
const float *src;
float *dst;
@@ -37,6 +37,7 @@ typedef struct DnnLayerMaximumParams{
}val;
} DnnLayerMaximumParams;
-int dnn_execute_layer_maximum(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const DnnLayerMaximumParams *params);
+int dnn_execute_layer_DLT_MAXIMUM(DnnOperand *operands, const int32_t *input_operand_indexes,
+ int32_t output_operand_index, const void *parameters);
#endif
@@ -48,12 +48,13 @@ static int after_get_buddy(int given, int border, LayerPadModeParam mode)
}
}
-int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index,
- const LayerPadParams *params)
+int dnn_execute_layer_DLT_MIRROR_PAD(DnnOperand *operands, const int32_t *input_operand_indexes,
+ int32_t output_operand_index, const void *parameters)
{
int32_t before_paddings;
int32_t after_paddings;
float* output;
+ const LayerPadParams *params = (const LayerPadParams *)parameters;
// suppose format is <N, H, W, C>
int32_t input_operand_index = input_operand_indexes[0];
@@ -36,7 +36,7 @@ typedef struct LayerPadParams{
float constant_values;
} LayerPadParams;
-int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index,
- const LayerPadParams *params);
+int dnn_execute_layer_DLT_MIRROR_PAD(DnnOperand *operands, const int32_t *input_operand_indexes,
+ int32_t output_operand_index, const void *parameters);
#endif
new file mode 100644
@@ -0,0 +1,4 @@
+SETUP_LAYER(DLT_CONV2D)
+SETUP_LAYER(DLT_DEPTH_TO_SPACE)
+SETUP_LAYER(DLT_MAXIMUM)
+SETUP_LAYER(DLT_MIRROR_PAD)
@@ -113,7 +113,7 @@ static int test_with_same_dilate(void)
operands[1].data = NULL;
input_indexes[0] = 0;
- convolve(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_DLT_CONV2D(operands, input_indexes, 1, ¶ms);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
@@ -212,7 +212,7 @@ static int test_with_valid(void)
operands[1].data = NULL;
input_indexes[0] = 0;
- convolve(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_DLT_CONV2D(operands, input_indexes, 1, ¶ms);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
@@ -48,6 +48,7 @@ static int test(void)
print(list(output.flatten()))
*/
+ DepthToSpaceParams params;
DnnOperand operands[2];
int32_t input_indexes[1];
float input[1*5*3*4] = {
@@ -79,7 +80,8 @@ static int test(void)
operands[1].data = NULL;
input_indexes[0] = 0;
- depth_to_space(operands, input_indexes, 1, 2);
+ params.block_size = 2;
+ dnn_execute_layer_DLT_DEPTH_TO_SPACE(operands, input_indexes, 1, ¶ms);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
@@ -45,7 +45,7 @@ static int test(void)
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_maximum(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_DLT_MAXIMUM(operands, input_indexes, 1, ¶ms);
output = operands[1].data;
for (int i = 0; i < sizeof(input) / sizeof(float); i++) {
@@ -79,7 +79,7 @@ static int test_with_mode_symmetric(void)
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_DLT_MIRROR_PAD(operands, input_indexes, 1, ¶ms);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
@@ -144,7 +144,7 @@ static int test_with_mode_reflect(void)
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_DLT_MIRROR_PAD(operands, input_indexes, 1, ¶ms);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
@@ -210,7 +210,7 @@ static int test_with_mode_constant(void)
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_DLT_MIRROR_PAD(operands, input_indexes, 1, ¶ms);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
with this change, the code will be simpler when more layers supported. Signed-off-by: Guo, Yejun <yejun.guo@intel.com> --- libavfilter/dnn/dnn_backend_native.c | 38 ++++++++-------------- libavfilter/dnn/dnn_backend_native_layer_conv2d.c | 4 ++- libavfilter/dnn/dnn_backend_native_layer_conv2d.h | 3 +- .../dnn/dnn_backend_native_layer_depth2space.c | 5 ++- .../dnn/dnn_backend_native_layer_depth2space.h | 3 +- libavfilter/dnn/dnn_backend_native_layer_maximum.c | 4 ++- libavfilter/dnn/dnn_backend_native_layer_maximum.h | 3 +- libavfilter/dnn/dnn_backend_native_layer_pad.c | 5 +-- libavfilter/dnn/dnn_backend_native_layer_pad.h | 4 +-- libavfilter/dnn/dnn_backend_native_layers.hxx | 4 +++ tests/dnn/dnn-layer-conv2d-test.c | 4 +-- tests/dnn/dnn-layer-depth2space-test.c | 4 ++- tests/dnn/dnn-layer-maximum-test.c | 2 +- tests/dnn/dnn-layer-pad-test.c | 6 ++-- 14 files changed, 48 insertions(+), 41 deletions(-) create mode 100644 libavfilter/dnn/dnn_backend_native_layers.hxx