From patchwork Thu Feb 24 08:22:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shubhanshu Saxena X-Patchwork-Id: 34508 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6838:d078:0:0:0:0 with SMTP id x24csp1616433nkx; Thu, 24 Feb 2022 00:23:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJzRXqufHwYjJ8Ud0g/LosxfxRES7UO8kMt+wP0in9UXYdcjr+TFJv5/H7Ec+HlkF1zWrNle X-Received: by 2002:a17:906:3ec9:b0:6bb:2eb9:84f with SMTP id d9-20020a1709063ec900b006bb2eb9084fmr1364133ejj.86.1645691035006; Thu, 24 Feb 2022 00:23:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645691035; cv=none; d=google.com; s=arc-20160816; b=NrXOVqSrMu8H7V4vLj/tgHStSQlq5bVFpQqUkHm9JByEHTCeJB4BMecKWaoE8LkZki LsUsSrrvlnSlkynyraBXkdEg3x4DdOW5DzuHgNtoNwfrpcxw6YfQ6dog3147EdNlh7Us u+5HqR+csqqp1tG6gQGBxQVH+U9HDzEY1JKTubyEbocuGQmoDHmAkO8lC8ThqIFJGr9K To4Fx+yQMHUBgAZaKsVfx8ZmCsqzGICRpj7pE2o0BoWvp4FBTzmnX8zBePsJyMYURxzd qcN8OIWgZ6ZefUPS2NA5FCTqSQUrhuQ6ljccL36Lf6OzJ8uLlucdjieHA/t6fXUKpkmx CjpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=10BPo0gzwAwk3JOVXcfttf/w7KPEQs8Gxr55ovy7ItQ=; b=Xsfs65yXjkz99nRfKx0q3OA1zm9Z3gWZ/c84N9wQcEs2nQfKU7Peys7YTNe8a50uQv rzpTQAVEmtLZQtyLcO+UOir+DFuozjPor0CWwYVshTNStXBfjie7PJXcEByYr9m392XI sOLxNEb4Gikchqi3yxk2gLtSO8jISu7hAXWMfusCB5k7BG8Eh7SCWZQqcWBY3yX3ArrW X1TUfCx52gLEdOvYCGXsn10a4qSCUk+E/6nIfJlSryYd0RgB6Ry3YmK4/jqZkejr8OLx 9axsL6uoaa58LSaQcuymQ5G5V3o/kzd2XnjOjeArn/+cMCWZZvoL2Ea+l2VKNE5RF8ur Z2sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=lRt5tCH2; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bm11si1156678edb.456.2022.02.24.00.23.54; Thu, 24 Feb 2022 00:23:54 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=lRt5tCH2; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 73E4268B2E3; Thu, 24 Feb 2022 10:23:27 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BDA8268B2C6 for ; Thu, 24 Feb 2022 10:23:26 +0200 (EET) Received: by mail-pl1-f177.google.com with SMTP id q11so1038955pln.11 for ; Thu, 24 Feb 2022 00:23:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FWFKrdJHKElBFw/MDeD7hDEP0zXEPzN6efCXB4L49So=; b=lRt5tCH2fuO8XQDV3cEoEAweRReeD0gTkbteGNQt7xbpfKgneqG8Wc5MNuZ2vzvlL2 aA/6vPSr1M+P8/+jypBAJaFvXO8tEBiigjP7UJTXgLBorqWk2IWL4yXcLTnMoyt2ilCs gJdWwFPPOQ5rCE6WD9d66I98mIxJ+ak0/0CArysC14mAqONYvfAjwjZcdieT7dkcu+Kp nfo//TgDN0KmpeWMCnZ3yYEYhQ4hZjFOxviiAtOyEWIeVOr69jcL88TPBS2ujrmQNdsD WDrrKRibos58zK2Rn4664DrMhVgs5EfQKsu6Qo4AtR3Qc6mu5FYS3Zi8o5xvNY8DeTmJ aPlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FWFKrdJHKElBFw/MDeD7hDEP0zXEPzN6efCXB4L49So=; b=cWSgdUQ/OV6Ysd6/7lBxVGlY3TvtwpJeGNJFi1NstKSTeE5B/6uBgO/ZmwI2TnM3M6 ks4oCmU30if3byYmklpYl7tYonH1Z83P3G7E9cvAcaPymGXOWu75sM8ZZT6pjKUZcc6d 9wLKsR43FiLTyehrAr6pip7Jdl0eZo7U2jbnUq2J3erh4SPFo4+pTtZRj902tS2CTnYa 3Ddx3jnXexkyBoTcDUeMIzmcUYaJ9zH4WjEG8FrBhdhbHOtgHa4dECHDaMyLFFFgqe1+ xaqgnRnaknWk8xti5lP6GBiQYmdarXfACEA83RT7AgEsAeRf5QkO4/Q6EHeV3QR8RqZw 9y0w== X-Gm-Message-State: AOAM530aT2CKZQRT4yW9DFHPNrS1qp2Q/TXK411SC7aSYyG69b6Edgtj 3602hicc4VyBn1gI+zsm1S8OocQ8hW7jlA== X-Received: by 2002:a17:90a:6c05:b0:1bc:94af:13d4 with SMTP id x5-20020a17090a6c0500b001bc94af13d4mr1679345pjj.170.1645691004263; Thu, 24 Feb 2022 00:23:24 -0800 (PST) Received: from Pavilion-x360.bbrouter ([103.133.123.155]) by smtp.googlemail.com with ESMTPSA id k11sm2174503pff.169.2022.02.24.00.23.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 00:23:24 -0800 (PST) From: Shubhanshu Saxena To: ffmpeg-devel@ffmpeg.org Date: Thu, 24 Feb 2022 13:52:40 +0530 Message-Id: <20220224082243.11208-5-shubhanshu.e01@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220224082243.11208-1-shubhanshu.e01@gmail.com> References: <20220224082243.11208-1-shubhanshu.e01@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/8] lavfi/dnn_backend_tf: Return Specific Error Codes X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Shubhanshu Saxena Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: FHwOHCLwALPM Switch to returning specific error codes or DNN_GENERIC_ERROR when an error is encountered. For TensorFlow C API errors, currently DNN_GENERIC_ERROR is returned. Signed-off-by: Shubhanshu Saxena --- libavfilter/dnn/dnn_backend_tf.c | 147 +++++++++++++++++-------------- libavfilter/dnn/dnn_backend_tf.h | 4 +- 2 files changed, 84 insertions(+), 67 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c index 7dd48fb612..d021e3c3bb 100644 --- a/libavfilter/dnn/dnn_backend_tf.c +++ b/libavfilter/dnn/dnn_backend_tf.c @@ -90,7 +90,7 @@ static const AVOption dnn_tensorflow_options[] = { AVFILTER_DEFINE_CLASS(dnn_tensorflow); -static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *lltask_queue); +static int execute_model_tf(TFRequestItem *request, Queue *lltask_queue); static void infer_completion_callback(void *args); static inline void destroy_request_item(TFRequestItem **arg); @@ -152,9 +152,10 @@ static TFInferRequest *tf_create_inference_request(void) * * @param request pointer to the TFRequestItem for inference * @retval DNN_SUCCESS if execution is successful - * @retval DNN_ERROR if execution fails + * @retval AVERROR(EINVAL) if request is NULL + * @retval DNN_GENERIC_ERROR if execution fails */ -static DNNReturnType tf_start_inference(void *args) +static int tf_start_inference(void *args) { TFRequestItem *request = args; TFInferRequest *infer_request = request->infer_request; @@ -164,7 +165,7 @@ static DNNReturnType tf_start_inference(void *args) if (!request) { av_log(&tf_model->ctx, AV_LOG_ERROR, "TFRequestItem is NULL\n"); - return DNN_ERROR; + return AVERROR(EINVAL); } TF_SessionRun(tf_model->session, NULL, @@ -178,7 +179,7 @@ static DNNReturnType tf_start_inference(void *args) if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) { destroy_request_item(&request); } - return DNN_ERROR; + return DNN_GENERIC_ERROR; } return DNN_SUCCESS; } @@ -202,14 +203,14 @@ static inline void destroy_request_item(TFRequestItem **arg) { av_freep(arg); } -static DNNReturnType extract_lltask_from_task(TaskItem *task, Queue *lltask_queue) +static int extract_lltask_from_task(TaskItem *task, Queue *lltask_queue) { TFModel *tf_model = task->model; TFContext *ctx = &tf_model->ctx; LastLevelTaskItem *lltask = av_malloc(sizeof(*lltask)); if (!lltask) { av_log(ctx, AV_LOG_ERROR, "Unable to allocate space for LastLevelTaskItem\n"); - return DNN_ERROR; + return AVERROR(ENOMEM); } task->inference_todo = 1; task->inference_done = 0; @@ -217,7 +218,7 @@ static DNNReturnType extract_lltask_from_task(TaskItem *task, Queue *lltask_queu if (ff_queue_push_back(lltask_queue, lltask) < 0) { av_log(ctx, AV_LOG_ERROR, "Failed to push back lltask_queue.\n"); av_freep(&lltask); - return DNN_ERROR; + return AVERROR(ENOMEM); } return DNN_SUCCESS; } @@ -277,7 +278,7 @@ static TF_Tensor *allocate_input_tensor(const DNNData *input) input_dims[1] * input_dims[2] * input_dims[3] * size); } -static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input_name) +static int get_input_tf(void *model, DNNData *input, const char *input_name) { TFModel *tf_model = model; TFContext *ctx = &tf_model->ctx; @@ -288,7 +289,7 @@ static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input tf_output.oper = TF_GraphOperationByName(tf_model->graph, input_name); if (!tf_output.oper) { av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name); - return DNN_ERROR; + return AVERROR(EINVAL); } tf_output.index = 0; @@ -300,7 +301,7 @@ static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input if (TF_GetCode(status) != TF_OK){ TF_DeleteStatus(status); av_log(ctx, AV_LOG_ERROR, "Failed to get input tensor shape: number of dimension incorrect\n"); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } TF_DeleteStatus(status); @@ -313,10 +314,10 @@ static DNNReturnType get_input_tf(void *model, DNNData *input, const char *input return DNN_SUCCESS; } -static DNNReturnType get_output_tf(void *model, const char *input_name, int input_width, int input_height, +static int get_output_tf(void *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height) { - DNNReturnType ret; + int ret; TFModel *tf_model = model; TFContext *ctx = &tf_model->ctx; TaskItem task; @@ -329,20 +330,21 @@ static DNNReturnType get_output_tf(void *model, const char *input_name, int inpu .out_frame = NULL, }; - if (ff_dnn_fill_gettingoutput_task(&task, &exec_params, tf_model, input_height, input_width, ctx) != DNN_SUCCESS) { + ret = ff_dnn_fill_gettingoutput_task(&task, &exec_params, tf_model, input_height, input_width, ctx); + if (ret != DNN_SUCCESS) { goto err; } - if (extract_lltask_from_task(&task, tf_model->lltask_queue) != DNN_SUCCESS) { + ret = extract_lltask_from_task(&task, tf_model->lltask_queue); + if (ret != DNN_SUCCESS) { av_log(ctx, AV_LOG_ERROR, "unable to extract inference from task.\n"); - ret = DNN_ERROR; goto err; } request = ff_safe_queue_pop_front(tf_model->request_queue); if (!request) { av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n"); - ret = DNN_ERROR; + ret = AVERROR(EINVAL); goto err; } @@ -386,7 +388,7 @@ static int hex_to_data(uint8_t *data, const char *p) return len; } -static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename) +static int load_tf_model(TFModel *tf_model, const char *model_filename) { TFContext *ctx = &tf_model->ctx; TF_Buffer *graph_def; @@ -407,7 +409,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename */ if (strncmp(tf_model->ctx.options.sess_config, "0x", 2) != 0) { av_log(ctx, AV_LOG_ERROR, "sess_config should start with '0x'\n"); - return DNN_ERROR; + return AVERROR(EINVAL); } config = tf_model->ctx.options.sess_config + 2; sess_config_length = hex_to_data(NULL, config); @@ -415,11 +417,11 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename sess_config = av_mallocz(sess_config_length + AV_INPUT_BUFFER_PADDING_SIZE); if (!sess_config) { av_log(ctx, AV_LOG_ERROR, "failed to allocate memory\n"); - return DNN_ERROR; + return AVERROR(ENOMEM); } if (hex_to_data(sess_config, config) < 0) { av_log(ctx, AV_LOG_ERROR, "failed to convert hex to data\n"); - return DNN_ERROR; + return AVERROR(EINVAL); } } @@ -427,7 +429,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename if (!graph_def){ av_log(ctx, AV_LOG_ERROR, "Failed to read model \"%s\" graph\n", model_filename); av_freep(&sess_config); - return DNN_ERROR; + return AVERROR(EINVAL); } tf_model->graph = TF_NewGraph(); tf_model->status = TF_NewStatus(); @@ -440,7 +442,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename TF_DeleteStatus(tf_model->status); av_log(ctx, AV_LOG_ERROR, "Failed to import serialized graph to model graph\n"); av_freep(&sess_config); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } init_op = TF_GraphOperationByName(tf_model->graph, "init"); @@ -455,7 +457,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename TF_DeleteSessionOptions(sess_opts); av_log(ctx, AV_LOG_ERROR, "Failed to set config for sess options with %s\n", tf_model->ctx.options.sess_config); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } } @@ -466,7 +468,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename TF_DeleteGraph(tf_model->graph); TF_DeleteStatus(tf_model->status); av_log(ctx, AV_LOG_ERROR, "Failed to create new session with model graph\n"); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } // Run initialization operation with name "init" if it is present in graph @@ -481,7 +483,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename TF_DeleteGraph(tf_model->graph); TF_DeleteStatus(tf_model->status); av_log(ctx, AV_LOG_ERROR, "Failed to run session when initializing\n"); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } } @@ -490,7 +492,7 @@ static DNNReturnType load_tf_model(TFModel *tf_model, const char *model_filename #define NAME_BUFFER_SIZE 256 -static DNNReturnType add_conv_layer(TFModel *tf_model, TF_Operation *transpose_op, TF_Operation **cur_op, +static int add_conv_layer(TFModel *tf_model, TF_Operation *transpose_op, TF_Operation **cur_op, ConvolutionalParams* params, const int layer) { TFContext *ctx = &tf_model->ctx; @@ -594,7 +596,7 @@ static DNNReturnType add_conv_layer(TFModel *tf_model, TF_Operation *transpose_o break; default: avpriv_report_missing_feature(ctx, "convolutional activation function %d", params->activation); - return DNN_ERROR; + return AVERROR(ENOSYS); } input.oper = *cur_op; TF_AddInput(op_desc, input); @@ -609,10 +611,10 @@ err: TF_DeleteTensor(kernel_tensor); TF_DeleteTensor(biases_tensor); av_log(ctx, AV_LOG_ERROR, "Failed to add conv layer %d\n", layer); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } -static DNNReturnType add_depth_to_space_layer(TFModel *tf_model, TF_Operation **cur_op, +static int add_depth_to_space_layer(TFModel *tf_model, TF_Operation **cur_op, DepthToSpaceParams *params, const int layer) { TFContext *ctx = &tf_model->ctx; @@ -630,13 +632,13 @@ static DNNReturnType add_depth_to_space_layer(TFModel *tf_model, TF_Operation ** *cur_op = TF_FinishOperation(op_desc, tf_model->status); if (TF_GetCode(tf_model->status) != TF_OK){ av_log(ctx, AV_LOG_ERROR, "Failed to add depth_to_space to layer %d\n", layer); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } return DNN_SUCCESS; } -static DNNReturnType add_pad_layer(TFModel *tf_model, TF_Operation **cur_op, +static int add_pad_layer(TFModel *tf_model, TF_Operation **cur_op, LayerPadParams *params, const int layer) { TFContext *ctx = &tf_model->ctx; @@ -666,13 +668,13 @@ static DNNReturnType add_pad_layer(TFModel *tf_model, TF_Operation **cur_op, if (TF_GetCode(tf_model->status) != TF_OK){ TF_DeleteTensor(tensor); av_log(ctx, AV_LOG_ERROR, "Failed to set value for pad of layer %d\n", layer); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } op = TF_FinishOperation(op_desc, tf_model->status); if (TF_GetCode(tf_model->status) != TF_OK){ TF_DeleteTensor(tensor); av_log(ctx, AV_LOG_ERROR, "Failed to add pad to layer %d\n", layer); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } op_desc = TF_NewOperation(tf_model->graph, "MirrorPad", "mirror_pad"); @@ -688,13 +690,13 @@ static DNNReturnType add_pad_layer(TFModel *tf_model, TF_Operation **cur_op, if (TF_GetCode(tf_model->status) != TF_OK){ TF_DeleteTensor(tensor); av_log(ctx, AV_LOG_ERROR, "Failed to add mirror_pad to layer %d\n", layer); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } return DNN_SUCCESS; } -static DNNReturnType add_maximum_layer(TFModel *tf_model, TF_Operation **cur_op, +static int add_maximum_layer(TFModel *tf_model, TF_Operation **cur_op, DnnLayerMaximumParams *params, const int layer) { TFContext *ctx = &tf_model->ctx; @@ -716,13 +718,13 @@ static DNNReturnType add_maximum_layer(TFModel *tf_model, TF_Operation **cur_op, if (TF_GetCode(tf_model->status) != TF_OK){ TF_DeleteTensor(tensor); av_log(ctx, AV_LOG_ERROR, "Failed to set value for maximum/y of layer %d", layer); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } op = TF_FinishOperation(op_desc, tf_model->status); if (TF_GetCode(tf_model->status) != TF_OK){ TF_DeleteTensor(tensor); av_log(ctx, AV_LOG_ERROR, "Failed to add maximum/y to layer %d\n", layer); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } snprintf(name_buffer, NAME_BUFFER_SIZE, "maximum%d", layer); @@ -737,13 +739,13 @@ static DNNReturnType add_maximum_layer(TFModel *tf_model, TF_Operation **cur_op, if (TF_GetCode(tf_model->status) != TF_OK){ TF_DeleteTensor(tensor); av_log(ctx, AV_LOG_ERROR, "Failed to add maximum to layer %d\n", layer); - return DNN_ERROR; + return DNN_GENERIC_ERROR; } return DNN_SUCCESS; } -static DNNReturnType load_native_model(TFModel *tf_model, const char *model_filename) +static int load_native_model(TFModel *tf_model, const char *model_filename) { TFContext *ctx = &tf_model->ctx; int32_t layer; @@ -755,14 +757,14 @@ static DNNReturnType load_native_model(TFModel *tf_model, const char *model_file int32_t *transpose_perm; int64_t transpose_perm_shape[] = {4}; int64_t input_shape[] = {1, -1, -1, -1}; - DNNReturnType layer_add_res; + int layer_add_res; DNNModel *model = NULL; NativeModel *native_model; model = ff_dnn_load_model_native(model_filename, DFT_PROCESS_FRAME, NULL, NULL); if (!model){ av_log(ctx, AV_LOG_ERROR, "Failed to load native model\n"); - return DNN_ERROR; + return AVERROR(EINVAL); } native_model = model->model; @@ -775,7 +777,7 @@ static DNNReturnType load_native_model(TFModel *tf_model, const char *model_file TF_DeleteGraph(tf_model->graph); \ TF_DeleteStatus(tf_model->status); \ av_log(ctx, AV_LOG_ERROR, "Failed to set value or add operator to layer\n"); \ - return DNN_ERROR; \ + return DNN_GENERIC_ERROR; \ } op_desc = TF_NewOperation(tf_model->graph, "Placeholder", "x"); @@ -942,7 +944,7 @@ err: return NULL; } -static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *request) { +static int fill_model_input_tf(TFModel *tf_model, TFRequestItem *request) { DNNData input; LastLevelTaskItem *lltask; TaskItem *task; @@ -954,7 +956,8 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque task = lltask->task; request->lltask = lltask; - if (get_input_tf(tf_model, &input, task->input_name) != DNN_SUCCESS) { + ret = get_input_tf(tf_model, &input, task->input_name); + if (ret != DNN_SUCCESS) { goto err; } @@ -965,12 +968,14 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque infer_request->tf_input = av_malloc(sizeof(TF_Output)); if (!infer_request->tf_input) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input tensor\n"); + ret = AVERROR(ENOMEM); goto err; } infer_request->tf_input->oper = TF_GraphOperationByName(tf_model->graph, task->input_name); if (!infer_request->tf_input->oper){ av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", task->input_name); + ret = DNN_GENERIC_ERROR; goto err; } infer_request->tf_input->index = 0; @@ -978,6 +983,7 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque infer_request->input_tensor = allocate_input_tensor(&input); if (!infer_request->input_tensor){ av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input tensor\n"); + ret = AVERROR(ENOMEM); goto err; } input.data = (float *)TF_TensorData(infer_request->input_tensor); @@ -1003,12 +1009,14 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque infer_request->tf_outputs = av_malloc_array(task->nb_output, sizeof(TF_Output)); if (infer_request->tf_outputs == NULL) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for *tf_outputs\n"); + ret = AVERROR(ENOMEM); goto err; } infer_request->output_tensors = av_calloc(task->nb_output, sizeof(*infer_request->output_tensors)); if (!infer_request->output_tensors) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for output tensor\n"); + ret = AVERROR(ENOMEM); goto err; } @@ -1017,6 +1025,7 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque infer_request->tf_outputs[i].oper = TF_GraphOperationByName(tf_model->graph, task->output_names[i]); if (!infer_request->tf_outputs[i].oper) { av_log(ctx, AV_LOG_ERROR, "Could not find output \"%s\" in model\n", task->output_names[i]); + ret = DNN_GENERIC_ERROR; goto err; } infer_request->tf_outputs[i].index = 0; @@ -1025,7 +1034,7 @@ static DNNReturnType fill_model_input_tf(TFModel *tf_model, TFRequestItem *reque return DNN_SUCCESS; err: tf_free_request(infer_request); - return DNN_ERROR; + return ret; } static void infer_completion_callback(void *args) { @@ -1086,12 +1095,13 @@ err: } } -static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *lltask_queue) +static int execute_model_tf(TFRequestItem *request, Queue *lltask_queue) { TFModel *tf_model; TFContext *ctx; LastLevelTaskItem *lltask; TaskItem *task; + int ret = 0; if (ff_queue_size(lltask_queue) == 0) { destroy_request_item(&request); @@ -1103,7 +1113,8 @@ static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *lltask_queu tf_model = task->model; ctx = &tf_model->ctx; - if (fill_model_input_tf(tf_model, request) != DNN_SUCCESS) { + ret = fill_model_input_tf(tf_model, request); + if (ret != DNN_SUCCESS) { goto err; } @@ -1112,58 +1123,64 @@ static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *lltask_queu goto err; } return DNN_SUCCESS; - } else { - if (tf_start_inference(request) != DNN_SUCCESS) { + } + else { + ret = tf_start_inference(request); + if (ret != DNN_SUCCESS) { goto err; } infer_completion_callback(request); - return (task->inference_done == task->inference_todo) ? DNN_SUCCESS : DNN_ERROR; + return (task->inference_done == task->inference_todo) ? DNN_SUCCESS : DNN_GENERIC_ERROR; } err: tf_free_request(request->infer_request); if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) { destroy_request_item(&request); } - return DNN_ERROR; + return ret; } -DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params) +int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params) { TFModel *tf_model = model->model; TFContext *ctx = &tf_model->ctx; TaskItem *task; TFRequestItem *request; + int ret = 0; - if (ff_check_exec_params(ctx, DNN_TF, model->func_type, exec_params) != 0) { - return DNN_ERROR; + ret = ff_check_exec_params(ctx, DNN_TF, model->func_type, exec_params); + if (ret != 0) { + return ret; } task = av_malloc(sizeof(*task)); if (!task) { av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n"); - return DNN_ERROR; + return AVERROR(ENOMEM); } - if (ff_dnn_fill_task(task, exec_params, tf_model, ctx->options.async, 1) != DNN_SUCCESS) { + ret = ff_dnn_fill_task(task, exec_params, tf_model, ctx->options.async, 1); + if (ret != DNN_SUCCESS) { av_freep(&task); - return DNN_ERROR; + return ret; } if (ff_queue_push_back(tf_model->task_queue, task) < 0) { av_freep(&task); av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n"); - return DNN_ERROR; + return AVERROR(ENOMEM); } - if (extract_lltask_from_task(task, tf_model->lltask_queue) != DNN_SUCCESS) { + ret = extract_lltask_from_task(task, tf_model->lltask_queue); + if (ret != DNN_SUCCESS) { av_log(ctx, AV_LOG_ERROR, "unable to extract last level task from task.\n"); - return DNN_ERROR; + return ret; } request = ff_safe_queue_pop_front(tf_model->request_queue); if (!request) { av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n"); - return DNN_ERROR; + return AVERROR(EINVAL); } return execute_model_tf(request, tf_model->lltask_queue); } @@ -1174,12 +1191,12 @@ DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVF return ff_dnn_get_result_common(tf_model->task_queue, in, out); } -DNNReturnType ff_dnn_flush_tf(const DNNModel *model) +int ff_dnn_flush_tf(const DNNModel *model) { TFModel *tf_model = model->model; TFContext *ctx = &tf_model->ctx; TFRequestItem *request; - DNNReturnType ret; + int ret; if (ff_queue_size(tf_model->lltask_queue) == 0) { // no pending task need to flush @@ -1189,7 +1206,7 @@ DNNReturnType ff_dnn_flush_tf(const DNNModel *model) request = ff_safe_queue_pop_front(tf_model->request_queue); if (!request) { av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n"); - return DNN_ERROR; + return AVERROR(EINVAL); } ret = fill_model_input_tf(tf_model, request); diff --git a/libavfilter/dnn/dnn_backend_tf.h b/libavfilter/dnn/dnn_backend_tf.h index f14ea8c47a..0b63a4b6d2 100644 --- a/libavfilter/dnn/dnn_backend_tf.h +++ b/libavfilter/dnn/dnn_backend_tf.h @@ -31,9 +31,9 @@ DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_type, const char *options, AVFilterContext *filter_ctx); -DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params); +int ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params); DNNAsyncStatusType ff_dnn_get_result_tf(const DNNModel *model, AVFrame **in, AVFrame **out); -DNNReturnType ff_dnn_flush_tf(const DNNModel *model); +int ff_dnn_flush_tf(const DNNModel *model); void ff_dnn_free_model_tf(DNNModel **model);