Unified · Split
| @@ -28,15 +28,26 @@ · 27 unmodified lines | |||
| 28 | #include "dnn_backend_native_layer_conv2d.h" | 28 | #include "dnn_backend_native_layer_conv2d.h" |
| 29 | #include "dnn_backend_native_layers.h" | 29 | #include "dnn_backend_native_layers.h" |
| 30 | 30 | ||
| 31 | static const AVClass dnn_native_class = { | ||
| 32 | .class_name = "dnn_native", | ||
| 33 | .item_name = av_default_item_name, | ||
| 34 | .option = NULL, | ||
| 35 | .version = LIBAVUTIL_VERSION_INT, | ||
| 36 | .category = AV_CLASS_CATEGORY_FILTER, | ||
| 37 | }; | ||
| 38 | |||
| 31 | static DNNReturnType get_input_native(void *model, DNNData *input, const char *input_name) | 39 | static DNNReturnType get_input_native(void *model, DNNData *input, const char *input_name) |
| 32 | { | 40 | { |
| 33 | NativeModel *native_model = (NativeModel *)model; | 41 | NativeModel *native_model = (NativeModel *)model; |
| 42 | NativeContext *ctx = &native_model->ctx; | ||
| 34 | 43 | ||
| 35 | for (int i = 0; i < native_model->operands_num; ++i) { | 44 | for (int i = 0; i < native_model->operands_num; ++i) { |
| 36 | DnnOperand *oprd = &native_model->operands[i]; | 45 | DnnOperand *oprd = &native_model->operands[i]; |
| 37 | if (strcmp(oprd->name, input_name) == 0) { | 46 | if (strcmp(oprd->name, input_name) == 0) { |
| 38 | if (oprd->type != DOT_INPUT) | 47 | if (oprd->type != DOT_INPUT) { |
| 48 | av_log(ctx, AV_LOG_ERROR, "Found \"%s\" in model, but it is not input node\n", input_name); | ||
| 39 | return DNN_ERROR; | 49 | return DNN_ERROR; |
| 50 | } | ||
| 40 | input->dt = oprd->data_type; | 51 | input->dt = oprd->data_type; |
| 41 | av_assert0(oprd->dims[0] == 1); | 52 | av_assert0(oprd->dims[0] == 1); |
| 42 | input->height = oprd->dims[1]; | 53 | input->height = oprd->dims[1]; |
| @@ -47,30 +58,37 @@ static DNNReturnType get_input_native(void *model, DNNData *input, const char *i · 4 unmodified lines | |||
| 47 | } | 58 | } |
| 48 | 59 | ||
| 49 | // do not find the input operand | 60 | // do not find the input operand |
| 61 | av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name); | ||
| 50 | return DNN_ERROR; | 62 | return DNN_ERROR; |
| 51 | } | 63 | } |
| 52 | 64 | ||
| 53 | static DNNReturnType set_input_native(void *model, DNNData *input, const char *input_name) | 65 | static DNNReturnType set_input_native(void *model, DNNData *input, const char *input_name) |
| 54 | { | 66 | { |
| 55 | NativeModel *native_model = (NativeModel *)model; | 67 | NativeModel *native_model = (NativeModel *)model; |
| 68 | NativeContext *ctx = &native_model->ctx; | ||
| 56 | DnnOperand *oprd = NULL; | 69 | DnnOperand *oprd = NULL; |
| 57 | 70 | ||
| 58 | if (native_model->layers_num <= 0 || native_model->operands_num <= 0) | 71 | if (native_model->layers_num <= 0 || native_model->operands_num <= 0) { |
| 72 | av_log(ctx, AV_LOG_ERROR, "No operands or layers in model\n"); | ||
| 59 | return DNN_ERROR; | 73 | return DNN_ERROR; |
| 74 | } | ||
| 60 | 75 | ||
| 61 | /* inputs */ | 76 | /* inputs */ |
| 62 | for (int i = 0; i < native_model->operands_num; ++i) { | 77 | for (int i = 0; i < native_model->operands_num; ++i) { |
| 63 | oprd = &native_model->operands[i]; | 78 | oprd = &native_model->operands[i]; |
| 64 | if (strcmp(oprd->name, input_name) == 0) { | 79 | if (strcmp(oprd->name, input_name) == 0) { |
| 65 | if (oprd->type != DOT_INPUT) | 80 | if (oprd->type != DOT_INPUT) { |
| 81 | av_log(ctx, AV_LOG_ERROR, "Found \"%s\" in model, but it is not input node\n", input_name); | ||
| 66 | return DNN_ERROR; | 82 | return DNN_ERROR; |
| 83 | } | ||
| 67 | break; | 84 | break; |
| 68 | } | 85 | } |
| 69 | oprd = NULL; | 86 | oprd = NULL; |
| 70 | } | 87 | } |
| 71 | 88 | if (!oprd) { | |
| 72 | if (!oprd) | 89 | av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name); |
| 73 | return DNN_ERROR; | 90 | return DNN_ERROR; |
| 91 | } | ||
| 74 | 92 | ||
| 75 | oprd->dims[0] = 1; | 93 | oprd->dims[0] = 1; |
| 76 | oprd->dims[1] = input->height; | 94 | oprd->dims[1] = input->height; |
| @@ -79,11 +97,15 @@ static DNNReturnType set_input_native(void *model, DNNData *input, const char *i · 2 unmodified lines | |||
| 79 | 97 | ||
| 80 | av_freep(&oprd->data); | 98 | av_freep(&oprd->data); |
| 81 | oprd->length = calculate_operand_data_length(oprd); | 99 | oprd->length = calculate_operand_data_length(oprd); |
| 82 | if (oprd->length <= 0) | 100 | if (oprd->length <= 0) { |
| 101 | av_log(ctx, AV_LOG_ERROR, "The input data length overflow\n"); | ||
| 83 | return DNN_ERROR; | 102 | return DNN_ERROR; |
| 103 | } | ||
| 84 | oprd->data = av_malloc(oprd->length); | 104 | oprd->data = av_malloc(oprd->length); |
| 85 | if (!oprd->data) | 105 | if (!oprd->data) { |
| 106 | av_log(ctx, AV_LOG_ERROR, "Failed to malloc memory for input data\n"); | ||
| 86 | return DNN_ERROR; | 107 | return DNN_ERROR; |
| 108 | } | ||
| 87 | 109 | ||
| 88 | input->data = oprd->data; | 110 | input->data = oprd->data; |
| 89 | 111 | ||
| @@ -150,6 +172,8 @@ DNNModel *ff_dnn_load_model_native(const char *model_filename, const char *optio · 60 unmodified lines | |||
| 150 | if (!native_model){ | 172 | if (!native_model){ |
| 151 | goto fail; | 173 | goto fail; |
| 152 | } | 174 | } |
| 175 | |||
| 176 | native_model->ctx.class = &dnn_native_class; | ||
| 153 | model->model = (void *)native_model; | 177 | model->model = (void *)native_model; |
| 154 | 178 | ||
| 155 | avio_seek(model_file_context, file_size - 8, SEEK_SET); | 179 | avio_seek(model_file_context, file_size - 8, SEEK_SET); |
| @@ -237,19 +261,26 @@ fail: · 81 unmodified lines | |||
| 237 | DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *outputs, const char **output_names, uint32_t nb_output) | 261 | DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *outputs, const char **output_names, uint32_t nb_output) |
| 238 | { | 262 | { |
| 239 | NativeModel *native_model = (NativeModel *)model->model; | 263 | NativeModel *native_model = (NativeModel *)model->model; |
| 264 | NativeContext *ctx = &native_model->ctx; | ||
| 240 | int32_t layer; | 265 | int32_t layer; |
| 241 | 266 | ||
| 242 | if (native_model->layers_num <= 0 || native_model->operands_num <= 0) | 267 | if (native_model->layers_num <= 0 || native_model->operands_num <= 0) { |
| 268 | av_log(ctx, AV_LOG_ERROR, "No operands or layers in model\n"); | ||
| 243 | return DNN_ERROR; | 269 | return DNN_ERROR; |
| 244 | if (!native_model->operands[0].data) | 270 | } |
| 271 | if (!native_model->operands[0].data) { | ||
| 272 | av_log(ctx, AV_LOG_ERROR, "Empty model input data\n"); | ||
| 245 | return DNN_ERROR; | 273 | return DNN_ERROR; |
| 274 | } | ||
| 246 | 275 | ||
| 247 | for (layer = 0; layer < native_model->layers_num; ++layer){ | 276 | for (layer = 0; layer < native_model->layers_num; ++layer){ |
| 248 | DNNLayerType layer_type = native_model->layers[layer].type; | 277 | DNNLayerType layer_type = native_model->layers[layer].type; |
| 249 | if (layer_funcs[layer_type].pf_exec(native_model->operands, | 278 | if (layer_funcs[layer_type].pf_exec(native_model->operands, |
| 250 | native_model->layers[layer].input_operand_indexes, | 279 | native_model->layers[layer].input_operand_indexes, |
| 251 | native_model->layers[layer].output_operand_index, | 280 | native_model->layers[layer].output_operand_index, |
| 252 | native_model->layers[layer].params) == DNN_ERROR) { | 281 | native_model->layers[layer].params, |
| 282 | &native_model->ctx) == DNN_ERROR) { | ||
| 283 | av_log(ctx, AV_LOG_ERROR, "Failed to execuet model\n"); | ||
| 253 | return DNN_ERROR; | 284 | return DNN_ERROR; |
| 254 | } | 285 | } |
| 255 | } | 286 | } |
| @@ -264,8 +295,10 @@ DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *output · 8 unmodified lines | |||
| 264 | } | 295 | } |
| 265 | } | 296 | } |
| 266 | 297 | ||
| 267 | if (oprd == NULL) | 298 | if (oprd == NULL) { |
| 299 | av_log(ctx, AV_LOG_ERROR, "Could not find output in model\n"); | ||
| 268 | return DNN_ERROR; | 300 | return DNN_ERROR; |
| 301 | } | ||
| 269 | 302 | ||
| 270 | outputs[i].data = oprd->data; | 303 | outputs[i].data = oprd->data; |
| 271 | outputs[i].height = oprd->dims[1]; | 304 | outputs[i].height = oprd->dims[1]; |
| @@ -106,8 +106,13 @@ typedef struct InputParams{ · 105 unmodified lines | |||
| 106 | int height, width, channels; | 106 | int height, width, channels; |
| 107 | } InputParams; | 107 | } InputParams; |
| 108 | 108 | ||
| 109 | typedef struct NativeContext { | ||
| 110 | const AVClass *class; | ||
| 111 | } NativeContext; | ||
| 112 | |||
| 109 | // Represents simple feed-forward convolutional network. | 113 | // Represents simple feed-forward convolutional network. |
| 110 | typedef struct NativeModel{ | 114 | typedef struct NativeModel{ |
| 115 | NativeContext ctx; | ||
| 111 | Layer *layers; | 116 | Layer *layers; |
| 112 | int32_t layers_num; | 117 | int32_t layers_num; |
| 113 | DnnOperand *operands; | 118 | DnnOperand *operands; |
| @@ -56,7 +56,7 @@ int dnn_load_layer_avg_pool(Layer *layer, AVIOContext *model_file_context, int f · 55 unmodified lines | |||
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | int dnn_execute_layer_avg_pool(DnnOperand *operands, const int32_t *input_operand_indexes, | 58 | int dnn_execute_layer_avg_pool(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 59 | int32_t output_operand_index, const void *parameters) | 59 | int32_t output_operand_index, const void *parameters, NativeContext *ctx) |
| 60 | { | 60 | { |
| 61 | float *output; | 61 | float *output; |
| 62 | int height_end, width_end, height_radius, width_radius, output_height, output_width, kernel_area; | 62 | int height_end, width_end, height_radius, width_radius, output_height, output_width, kernel_area; |
| @@ -107,9 +107,15 @@ int dnn_execute_layer_avg_pool(DnnOperand *operands, const int32_t *input_operan · 44 unmodified lines | |||
| 107 | output_operand->dims[3] = channel; | 107 | output_operand->dims[3] = channel; |
| 108 | output_operand->data_type = operands[input_operand_index].data_type; | 108 | output_operand->data_type = operands[input_operand_index].data_type; |
| 109 | output_operand->length = calculate_operand_data_length(output_operand); | 109 | output_operand->length = calculate_operand_data_length(output_operand); |
| 110 | if (output_operand->length <= 0) { | ||
| 111 | av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n"); | ||
| 112 | return DNN_ERROR; | ||
| 113 | } | ||
| 110 | output_operand->data = av_realloc(output_operand->data, output_operand->length); | 114 | output_operand->data = av_realloc(output_operand->data, output_operand->length); |
| 111 | if (!output_operand->data) | 115 | if (!output_operand->data) { |
| 116 | av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n"); | ||
| 112 | return DNN_ERROR; | 117 | return DNN_ERROR; |
| 118 | } | ||
| 113 | output = output_operand->data; | 119 | output = output_operand->data; |
| 114 | 120 | ||
| 115 | for (int y = 0; y < height_end; y += kernel_strides) { | 121 | for (int y = 0; y < height_end; y += kernel_strides) { |
| @@ -35,6 +35,6 @@ typedef struct AvgPoolParams{ · 34 unmodified lines | |||
| 35 | 35 | ||
| 36 | int dnn_load_layer_avg_pool(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); | 36 | int dnn_load_layer_avg_pool(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); |
| 37 | int dnn_execute_layer_avg_pool(DnnOperand *operands, const int32_t *input_operand_indexes, | 37 | int dnn_execute_layer_avg_pool(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 38 | int32_t output_operand_index, const void *parameters); | 38 | int32_t output_operand_index, const void *parameters, NativeContext *ctx); |
| 39 | 39 | ||
| 40 | #endif | 40 | #endif |
| @@ -89,7 +89,7 @@ int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int fil · 88 unmodified lines | |||
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes, | 91 | int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 92 | int32_t output_operand_index, const void *parameters) | 92 | int32_t output_operand_index, const void *parameters, NativeContext *ctx) |
| 93 | { | 93 | { |
| 94 | float *output; | 94 | float *output; |
| 95 | int32_t input_operand_index = input_operand_indexes[0]; | 95 | int32_t input_operand_index = input_operand_indexes[0]; |
| @@ -113,11 +113,15 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 17 unmodified lines | |||
| 113 | output_operand->dims[3] = conv_params->output_num; | 113 | output_operand->dims[3] = conv_params->output_num; |
| 114 | output_operand->data_type = operands[input_operand_index].data_type; | 114 | output_operand->data_type = operands[input_operand_index].data_type; |
| 115 | output_operand->length = calculate_operand_data_length(output_operand); | 115 | output_operand->length = calculate_operand_data_length(output_operand); |
| 116 | if (output_operand->length <= 0) | 116 | if (output_operand->length <= 0) { |
| 117 | av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n"); | ||
| 117 | return DNN_ERROR; | 118 | return DNN_ERROR; |
| 119 | } | ||
| 118 | output_operand->data = av_realloc(output_operand->data, output_operand->length); | 120 | output_operand->data = av_realloc(output_operand->data, output_operand->length); |
| 119 | if (!output_operand->data) | 121 | if (!output_operand->data) { |
| 122 | av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n"); | ||
| 120 | return DNN_ERROR; | 123 | return DNN_ERROR; |
| 124 | } | ||
| 121 | output = output_operand->data; | 125 | output = output_operand->data; |
| 122 | 126 | ||
| 123 | av_assert0(channel == conv_params->input_num); | 127 | av_assert0(channel == conv_params->input_num); |
| @@ -37,5 +37,5 @@ typedef struct ConvolutionalParams{ · 36 unmodified lines | |||
| 37 | 37 | ||
| 38 | int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); | 38 | int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); |
| 39 | int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes, | 39 | int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 40 | int32_t output_operand_index, const void *parameters); | 40 | int32_t output_operand_index, const void *parameters, NativeContext *ctx); |
| 41 | #endif | 41 | #endif |
| @@ -50,7 +50,7 @@ int dnn_load_layer_depth2space(Layer *layer, AVIOContext *model_file_context, in · 49 unmodified lines | |||
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_operand_indexes, | 52 | int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 53 | int32_t output_operand_index, const void *parameters) | 53 | int32_t output_operand_index, const void *parameters, NativeContext *ctx) |
| 54 | { | 54 | { |
| 55 | float *output; | 55 | float *output; |
| 56 | const DepthToSpaceParams *params = (const DepthToSpaceParams *)parameters; | 56 | const DepthToSpaceParams *params = (const DepthToSpaceParams *)parameters; |
| @@ -75,11 +75,15 @@ int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_ope · 18 unmodified lines | |||
| 75 | output_operand->dims[3] = new_channels; | 75 | output_operand->dims[3] = new_channels; |
| 76 | output_operand->data_type = operands[input_operand_index].data_type; | 76 | output_operand->data_type = operands[input_operand_index].data_type; |
| 77 | output_operand->length = calculate_operand_data_length(output_operand); | 77 | output_operand->length = calculate_operand_data_length(output_operand); |
| 78 | if (output_operand->length <= 0) | 78 | if (output_operand->length <= 0) { |
| 79 | av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n"); | ||
| 79 | return DNN_ERROR; | 80 | return DNN_ERROR; |
| 81 | } | ||
| 80 | output_operand->data = av_realloc(output_operand->data, output_operand->length); | 82 | output_operand->data = av_realloc(output_operand->data, output_operand->length); |
| 81 | if (!output_operand->data) | 83 | if (!output_operand->data) { |
| 84 | av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n"); | ||
| 82 | return DNN_ERROR; | 85 | return DNN_ERROR; |
| 86 | } | ||
| 83 | output = output_operand->data; | 87 | output = output_operand->data; |
| 84 | 88 | ||
| 85 | for (y = 0; y < height; ++y){ | 89 | for (y = 0; y < height; ++y){ |
| @@ -36,6 +36,6 @@ typedef struct DepthToSpaceParams{ · 35 unmodified lines | |||
| 36 | 36 | ||
| 37 | int dnn_load_layer_depth2space(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); | 37 | int dnn_load_layer_depth2space(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); |
| 38 | int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_operand_indexes, | 38 | int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 39 | int32_t output_operand_index, const void *parameters); | 39 | int32_t output_operand_index, const void *parameters, NativeContext *ctx); |
| 40 | 40 | ||
| 41 | #endif | 41 | #endif |
| @@ -149,7 +149,7 @@ int dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, in · 148 unmodified lines | |||
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes, | 151 | int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 152 | int32_t output_operand_index, const void *parameters) | 152 | int32_t output_operand_index, const void *parameters, NativeContext *ctx) |
| 153 | { | 153 | { |
| 154 | const DnnOperand *input = &operands[input_operand_indexes[0]]; | 154 | const DnnOperand *input = &operands[input_operand_indexes[0]]; |
| 155 | DnnOperand *output = &operands[output_operand_index]; | 155 | DnnOperand *output = &operands[output_operand_index]; |
| @@ -160,11 +160,15 @@ int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_ope · 4 unmodified lines | |||
| 160 | 160 | ||
| 161 | output->data_type = input->data_type; | 161 | output->data_type = input->data_type; |
| 162 | output->length = calculate_operand_data_length(output); | 162 | output->length = calculate_operand_data_length(output); |
| 163 | if (output->length <= 0) | 163 | if (output->length <= 0) { |
| 164 | av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n"); | ||
| 164 | return DNN_ERROR; | 165 | return DNN_ERROR; |
| 166 | } | ||
| 165 | output->data = av_realloc(output->data, output->length); | 167 | output->data = av_realloc(output->data, output->length); |
| 166 | if (!output->data) | 168 | if (!output->data) { |
| 169 | av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n"); | ||
| 167 | return DNN_ERROR; | 170 | return DNN_ERROR; |
| 171 | } | ||
| 168 | 172 | ||
| 169 | switch (params->bin_op) { | 173 | switch (params->bin_op) { |
| 170 | case DMBO_SUB: | 174 | case DMBO_SUB: |
| @@ -186,6 +190,7 @@ int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_ope · 15 unmodified lines | |||
| 186 | math_binary_not_commutative(floormod, params, input, output, operands, input_operand_indexes); | 190 | math_binary_not_commutative(floormod, params, input, output, operands, input_operand_indexes); |
| 187 | return 0; | 191 | return 0; |
| 188 | default: | 192 | default: |
| 193 | av_log(ctx, AV_LOG_ERROR, "Unmatch math binary operator\n"); | ||
| 189 | return DNN_ERROR; | 194 | return DNN_ERROR; |
| 190 | } | 195 | } |
| 191 | } | 196 | } |
| @@ -49,6 +49,6 @@ typedef struct DnnLayerMathBinaryParams{ · 48 unmodified lines | |||
| 49 | 49 | ||
| 50 | int dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); | 50 | int dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); |
| 51 | int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes, | 51 | int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 52 | int32_t output_operand_index, const void *parameters); | 52 | int32_t output_operand_index, const void *parameters, NativeContext *ctx); |
| 53 | 53 | ||
| 54 | #endif | 54 | #endif |
| @@ -53,7 +53,7 @@ int dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int · 52 unmodified lines | |||
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes, | 55 | int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 56 | int32_t output_operand_index, const void *parameters) | 56 | int32_t output_operand_index, const void *parameters, NativeContext *ctx) |
| 57 | { | 57 | { |
| 58 | const DnnOperand *input = &operands[input_operand_indexes[0]]; | 58 | const DnnOperand *input = &operands[input_operand_indexes[0]]; |
| 59 | DnnOperand *output = &operands[output_operand_index]; | 59 | DnnOperand *output = &operands[output_operand_index]; |
| @@ -67,11 +67,15 @@ int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_oper · 7 unmodified lines | |||
| 67 | 67 | ||
| 68 | output->data_type = input->data_type; | 68 | output->data_type = input->data_type; |
| 69 | output->length = calculate_operand_data_length(output); | 69 | output->length = calculate_operand_data_length(output); |
| 70 | if (output->length <= 0) | 70 | if (output->length <= 0) { |
| 71 | av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n"); | ||
| 71 | return DNN_ERROR; | 72 | return DNN_ERROR; |
| 73 | } | ||
| 72 | output->data = av_realloc(output->data, output->length); | 74 | output->data = av_realloc(output->data, output->length); |
| 73 | if (!output->data) | 75 | if (!output->data) { |
| 76 | av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n"); | ||
| 74 | return DNN_ERROR; | 77 | return DNN_ERROR; |
| 78 | } | ||
| 75 | 79 | ||
| 76 | dims_count = calculate_operand_dims_count(output); | 80 | dims_count = calculate_operand_dims_count(output); |
| 77 | src = input->data; | 81 | src = input->data; |
| @@ -143,6 +147,7 @@ int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_oper · 65 unmodified lines | |||
| 143 | dst[i] = round(src[i]); | 147 | dst[i] = round(src[i]); |
| 144 | return 0; | 148 | return 0; |
| 145 | default: | 149 | default: |
| 150 | av_log(ctx, AV_LOG_ERROR, "Unmatch math unary operator\n"); | ||
| 146 | return DNN_ERROR; | 151 | return DNN_ERROR; |
| 147 | } | 152 | } |
| 148 | } | 153 | } |
| @@ -55,6 +55,6 @@ typedef struct DnnLayerMathUnaryParams{ · 54 unmodified lines | |||
| 55 | 55 | ||
| 56 | int dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); | 56 | int dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); |
| 57 | int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes, | 57 | int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 58 | int32_t output_operand_index, const void *parameters); | 58 | int32_t output_operand_index, const void *parameters, NativeContext *ctx); |
| 59 | 59 | ||
| 60 | #endif | 60 | #endif |
| @@ -50,7 +50,7 @@ int dnn_load_layer_maximum(Layer *layer, AVIOContext *model_file_context, int fi · 49 unmodified lines | |||
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | int dnn_execute_layer_maximum(DnnOperand *operands, const int32_t *input_operand_indexes, | 52 | int dnn_execute_layer_maximum(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 53 | int32_t output_operand_index, const void *parameters) | 53 | int32_t output_operand_index, const void *parameters, NativeContext *ctx) |
| 54 | { | 54 | { |
| 55 | const DnnOperand *input = &operands[input_operand_indexes[0]]; | 55 | const DnnOperand *input = &operands[input_operand_indexes[0]]; |
| 56 | DnnOperand *output = &operands[output_operand_index]; | 56 | DnnOperand *output = &operands[output_operand_index]; |
| @@ -64,11 +64,15 @@ int dnn_execute_layer_maximum(DnnOperand *operands, const int32_t *input_operand · 7 unmodified lines | |||
| 64 | 64 | ||
| 65 | output->data_type = input->data_type; | 65 | output->data_type = input->data_type; |
| 66 | output->length = calculate_operand_data_length(output); | 66 | output->length = calculate_operand_data_length(output); |
| 67 | if (output->length <= 0) | 67 | if (output->length <= 0) { |
| 68 | av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n"); | ||
| 68 | return DNN_ERROR; | 69 | return DNN_ERROR; |
| 70 | } | ||
| 69 | output->data = av_realloc(output->data, output->length); | 71 | output->data = av_realloc(output->data, output->length); |
| 70 | if (!output->data) | 72 | if (!output->data) { |
| 73 | av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n"); | ||
| 71 | return DNN_ERROR; | 74 | return DNN_ERROR; |
| 75 | } | ||
| 72 | 76 | ||
| 73 | dims_count = calculate_operand_dims_count(output); | 77 | dims_count = calculate_operand_dims_count(output); |
| 74 | src = input->data; | 78 | src = input->data; |
| @@ -39,6 +39,6 @@ typedef struct DnnLayerMaximumParams{ · 38 unmodified lines | |||
| 39 | 39 | ||
| 40 | int dnn_load_layer_maximum(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); | 40 | int dnn_load_layer_maximum(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); |
| 41 | int dnn_execute_layer_maximum(DnnOperand *operands, const int32_t *input_operand_indexes, | 41 | int dnn_execute_layer_maximum(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 42 | int32_t output_operand_index, const void *parameters); | 42 | int32_t output_operand_index, const void *parameters, NativeContext *ctx); |
| 43 | 43 | ||
| 44 | #endif | 44 | #endif |
| @@ -76,7 +76,7 @@ static int after_get_buddy(int given, int border, LayerPadModeParam mode) · 75 unmodified lines | |||
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes, | 78 | int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 79 | int32_t output_operand_index, const void *parameters) | 79 | int32_t output_operand_index, const void *parameters, NativeContext *ctx) |
| 80 | { | 80 | { |
| 81 | int32_t before_paddings; | 81 | int32_t before_paddings; |
| 82 | int32_t after_paddings; | 82 | int32_t after_paddings; |
| @@ -111,11 +111,15 @@ int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_ind · 28 unmodified lines | |||
| 111 | output_operand->dims[3] = new_channel; | 111 | output_operand->dims[3] = new_channel; |
| 112 | output_operand->data_type = operands[input_operand_index].data_type; | 112 | output_operand->data_type = operands[input_operand_index].data_type; |
| 113 | output_operand->length = calculate_operand_data_length(output_operand); | 113 | output_operand->length = calculate_operand_data_length(output_operand); |
| 114 | if (output_operand->length <= 0) | 114 | if (output_operand->length <= 0) { |
| 115 | av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n"); | ||
| 115 | return DNN_ERROR; | 116 | return DNN_ERROR; |
| 117 | } | ||
| 116 | output_operand->data = av_realloc(output_operand->data, output_operand->length); | 118 | output_operand->data = av_realloc(output_operand->data, output_operand->length); |
| 117 | if (!output_operand->data) | 119 | if (!output_operand->data) { |
| 120 | av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n"); | ||
| 118 | return DNN_ERROR; | 121 | return DNN_ERROR; |
| 122 | } | ||
| 119 | output = output_operand->data; | 123 | output = output_operand->data; |
| 120 | 124 | ||
| 121 | // copy the original data | 125 | // copy the original data |
| @@ -38,6 +38,6 @@ typedef struct LayerPadParams{ · 37 unmodified lines | |||
| 38 | 38 | ||
| 39 | int dnn_load_layer_pad(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); | 39 | int dnn_load_layer_pad(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); |
| 40 | int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes, | 40 | int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 41 | int32_t output_operand_index, const void *parameters); | 41 | int32_t output_operand_index, const void *parameters, NativeContext *ctx); |
| 42 | 42 | ||
| 43 | #endif | 43 | #endif |
| @@ -25,7 +25,7 @@ · 24 unmodified lines | |||
| 25 | #include "dnn_backend_native.h" | 25 | #include "dnn_backend_native.h" |
| 26 | 26 | ||
| 27 | typedef int (*LAYER_EXEC_FUNC)(DnnOperand *operands, const int32_t *input_operand_indexes, | 27 | typedef int (*LAYER_EXEC_FUNC)(DnnOperand *operands, const int32_t *input_operand_indexes, |
| 28 | int32_t output_operand_index, const void *parameters); | 28 | int32_t output_operand_index, const void *parameters, NativeContext *ctx); |
| 29 | typedef int (*LAYER_LOAD_FUNC)(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); | 29 | typedef int (*LAYER_LOAD_FUNC)(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num); |
| 30 | 30 | ||
| 31 | typedef struct LayerFunc { | 31 | typedef struct LayerFunc { |
| @@ -91,7 +91,7 @@ static int test_with_same(void) · 90 unmodified lines | |||
| 91 | operands[1].data = NULL; | 91 | operands[1].data = NULL; |
| 92 | 92 | ||
| 93 | input_indexes[0] = 0; | 93 | input_indexes[0] = 0; |
| 94 | dnn_execute_layer_avg_pool(operands, input_indexes, 1, ¶ms); | 94 | dnn_execute_layer_avg_pool(operands, input_indexes, 1, ¶ms, NULL); |
| 95 | 95 | ||
| 96 | output = operands[1].data; | 96 | output = operands[1].data; |
| 97 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); ++i) { | 97 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); ++i) { |
| @@ -171,7 +171,7 @@ static int test_with_valid(void) · 73 unmodified lines | |||
| 171 | operands[1].data = NULL; | 171 | operands[1].data = NULL; |
| 172 | 172 | ||
| 173 | input_indexes[0] = 0; | 173 | input_indexes[0] = 0; |
| 174 | dnn_execute_layer_avg_pool(operands, input_indexes, 1, ¶ms); | 174 | dnn_execute_layer_avg_pool(operands, input_indexes, 1, ¶ms, NULL); |
| 175 | 175 | ||
| 176 | output = operands[1].data; | 176 | output = operands[1].data; |
| 177 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); ++i) { | 177 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); ++i) { |
| @@ -114,7 +114,7 @@ static int test_with_same_dilate(void) · 113 unmodified lines | |||
| 114 | operands[1].data = NULL; | 114 | operands[1].data = NULL; |
| 115 | 115 | ||
| 116 | input_indexes[0] = 0; | 116 | input_indexes[0] = 0; |
| 117 | dnn_execute_layer_conv2d(operands, input_indexes, 1, ¶ms); | 117 | dnn_execute_layer_conv2d(operands, input_indexes, 1, ¶ms, NULL); |
| 118 | 118 | ||
| 119 | output = operands[1].data; | 119 | output = operands[1].data; |
| 120 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { | 120 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { |
| @@ -214,7 +214,7 @@ static int test_with_valid(void) · 93 unmodified lines | |||
| 214 | operands[1].data = NULL; | 214 | operands[1].data = NULL; |
| 215 | 215 | ||
| 216 | input_indexes[0] = 0; | 216 | input_indexes[0] = 0; |
| 217 | dnn_execute_layer_conv2d(operands, input_indexes, 1, ¶ms); | 217 | dnn_execute_layer_conv2d(operands, input_indexes, 1, ¶ms, NULL); |
| 218 | 218 | ||
| 219 | output = operands[1].data; | 219 | output = operands[1].data; |
| 220 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { | 220 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { |
| @@ -81,7 +81,7 @@ static int test(void) · 80 unmodified lines | |||
| 81 | 81 | ||
| 82 | input_indexes[0] = 0; | 82 | input_indexes[0] = 0; |
| 83 | params.block_size = 2; | 83 | params.block_size = 2; |
| 84 | dnn_execute_layer_depth2space(operands, input_indexes, 1, ¶ms); | 84 | dnn_execute_layer_depth2space(operands, input_indexes, 1, ¶ms, NULL); |
| 85 | 85 | ||
| 86 | output = operands[1].data; | 86 | output = operands[1].data; |
| 87 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { | 87 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { |
| @@ -71,7 +71,7 @@ static int test_broadcast_input0(DNNMathBinaryOperation op) · 70 unmodified lines | |||
| 71 | operands[1].data = NULL; | 71 | operands[1].data = NULL; |
| 72 | 72 | ||
| 73 | input_indexes[0] = 0; | 73 | input_indexes[0] = 0; |
| 74 | dnn_execute_layer_math_binary(operands, input_indexes, 1, ¶ms); | 74 | dnn_execute_layer_math_binary(operands, input_indexes, 1, ¶ms, NULL); |
| 75 | 75 | ||
| 76 | output = operands[1].data; | 76 | output = operands[1].data; |
| 77 | for (int i = 0; i < sizeof(input) / sizeof(float); i++) { | 77 | for (int i = 0; i < sizeof(input) / sizeof(float); i++) { |
| @@ -111,7 +111,7 @@ static int test_broadcast_input1(DNNMathBinaryOperation op) · 33 unmodified lines | |||
| 111 | operands[1].data = NULL; | 111 | operands[1].data = NULL; |
| 112 | 112 | ||
| 113 | input_indexes[0] = 0; | 113 | input_indexes[0] = 0; |
| 114 | dnn_execute_layer_math_binary(operands, input_indexes, 1, ¶ms); | 114 | dnn_execute_layer_math_binary(operands, input_indexes, 1, ¶ms, NULL); |
| 115 | 115 | ||
| 116 | output = operands[1].data; | 116 | output = operands[1].data; |
| 117 | for (int i = 0; i < sizeof(input) / sizeof(float); i++) { | 117 | for (int i = 0; i < sizeof(input) / sizeof(float); i++) { |
| @@ -159,7 +159,7 @@ static int test_no_broadcast(DNNMathBinaryOperation op) · 41 unmodified lines | |||
| 159 | 159 | ||
| 160 | input_indexes[0] = 0; | 160 | input_indexes[0] = 0; |
| 161 | input_indexes[1] = 1; | 161 | input_indexes[1] = 1; |
| 162 | dnn_execute_layer_math_binary(operands, input_indexes, 2, ¶ms); | 162 | dnn_execute_layer_math_binary(operands, input_indexes, 2, ¶ms, NULL); |
| 163 | 163 | ||
| 164 | output = operands[2].data; | 164 | output = operands[2].data; |
| 165 | for (int i = 0; i < sizeof(input0) / sizeof(float); i++) { | 165 | for (int i = 0; i < sizeof(input0) / sizeof(float); i++) { |
| @@ -87,7 +87,7 @@ static int test(DNNMathUnaryOperation op) · 86 unmodified lines | |||
| 87 | operands[1].data = NULL; | 87 | operands[1].data = NULL; |
| 88 | 88 | ||
| 89 | input_indexes[0] = 0; | 89 | input_indexes[0] = 0; |
| 90 | dnn_execute_layer_math_unary(operands, input_indexes, 1, ¶ms); | 90 | dnn_execute_layer_math_unary(operands, input_indexes, 1, ¶ms, NULL); |
| 91 | 91 | ||
| 92 | output = operands[1].data; | 92 | output = operands[1].data; |
| 93 | for (int i = 0; i < sizeof(input) / sizeof(float); ++i) { | 93 | for (int i = 0; i < sizeof(input) / sizeof(float); ++i) { |
| @@ -45,7 +45,7 @@ static int test(void) · 44 unmodified lines | |||
| 45 | operands[1].data = NULL; | 45 | operands[1].data = NULL; |
| 46 | 46 | ||
| 47 | input_indexes[0] = 0; | 47 | input_indexes[0] = 0; |
| 48 | dnn_execute_layer_maximum(operands, input_indexes, 1, ¶ms); | 48 | dnn_execute_layer_maximum(operands, input_indexes, 1, ¶ms, NULL); |
| 49 | 49 | ||
| 50 | output = operands[1].data; | 50 | output = operands[1].data; |
| 51 | for (int i = 0; i < sizeof(input) / sizeof(float); i++) { | 51 | for (int i = 0; i < sizeof(input) / sizeof(float); i++) { |
| @@ -79,7 +79,7 @@ static int test_with_mode_symmetric(void) · 78 unmodified lines | |||
| 79 | operands[1].data = NULL; | 79 | operands[1].data = NULL; |
| 80 | 80 | ||
| 81 | input_indexes[0] = 0; | 81 | input_indexes[0] = 0; |
| 82 | dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms); | 82 | dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms, NULL); |
| 83 | 83 | ||
| 84 | output = operands[1].data; | 84 | output = operands[1].data; |
| 85 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { | 85 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { |
| @@ -144,7 +144,7 @@ static int test_with_mode_reflect(void) · 58 unmodified lines | |||
| 144 | operands[1].data = NULL; | 144 | operands[1].data = NULL; |
| 145 | 145 | ||
| 146 | input_indexes[0] = 0; | 146 | input_indexes[0] = 0; |
| 147 | dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms); | 147 | dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms, NULL); |
| 148 | 148 | ||
| 149 | output = operands[1].data; | 149 | output = operands[1].data; |
| 150 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { | 150 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { |
| @@ -210,7 +210,7 @@ static int test_with_mode_constant(void) · 59 unmodified lines | |||
| 210 | operands[1].data = NULL; | 210 | operands[1].data = NULL; |
| 211 | 211 | ||
| 212 | input_indexes[0] = 0; | 212 | input_indexes[0] = 0; |
| 213 | dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms); | 213 | dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms, NULL); |
| 214 | 214 | ||
| 215 | output = operands[1].data; | 215 | output = operands[1].data; |
| 216 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { | 216 | for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) { |