From patchwork Sun Aug 8 10:55:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shubhanshu Saxena X-Patchwork-Id: 29343 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6c0f:0:0:0:0:0 with SMTP id a15csp1981082ioh; Sun, 8 Aug 2021 03:56:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyaUBB4p/sYo3pxTqClo4qbcQb+HmCsFYOP8xVZTdinktsU0iw0UWWAW6CgRFsIaO2g+LGb X-Received: by 2002:aa7:db94:: with SMTP id u20mr23238845edt.381.1628420217677; Sun, 08 Aug 2021 03:56:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628420217; cv=none; d=google.com; s=arc-20160816; b=qXgLbQ7/YNHgwmQFYYdmCNohv8fkfkAjA13SdYR0krqOiROeVtZ9gsryTi69Ukz1XW 4bSAOtW9Qro11orfbFlB5yfWiPlvsNJQagtG2e4QJfsggLjNKetCx/YxsrY6e/KnSqNK OHbOU/3sHyzTJAIZETx2g5Qvk1Noz4+rULVsjBB8focGpo0p+4V4AyDTCkKhGulGsBqC EOYf18NdfDMdqMPUVgIX7NajvHlW7HytQO8yxCKGf4aIkC+LFBGglUdL+yBoANyzOT2V pkJL9efZ59WEoUD5RYnEkUSrMDkCCA0rtAZ5xve3QKmaS/GPRZbHsC3p3GNN5p6U8DnV ZmrA== 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=3ZPNhoSmik2i9s6Hg1XxwOOl9Ef1DuxnNNC+h/FzzP8=; b=OzkvdcRN4Md/LRhpR7ewbxFmt/usBdOYP36Q6wBBjRoLdHkdHySaDw3UYpu1ksK2aC v5TGbjDuVO8+sKoLW8wTofB1IfVR6H0bQ9KfEocfdfQynflouPMr0rB9QGhdysBByhY8 5bwuKkb1LrOKosKCT5cfEiOQOvsZtFfPoLBzJDRkEPz+CgCjog72ETzFNT5xphEyHfYX JEyvN7jJgedMC5soDZYLx+3TLleEOA2dG0f6Fbd/g9UHtZff3d8GoZGQeH0ZHV7Y+bWQ mM0HHNsi71cQ+VBcWiOsRpuckVTiRF8AQVnO0Lq7eLnX1/koMs0oe9DlITOsfMRUYrAI vGDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=aUvdyWcc; 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 s13si14660170edd.363.2021.08.08.03.56.57; Sun, 08 Aug 2021 03:56:57 -0700 (PDT) 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=20161025 header.b=aUvdyWcc; 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 EFB9868A4A8; Sun, 8 Aug 2021 13:56:22 +0300 (EEST) 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 93F0668A49E for ; Sun, 8 Aug 2021 13:56:16 +0300 (EEST) Received: by mail-pl1-f177.google.com with SMTP id c16so13338311plh.7 for ; Sun, 08 Aug 2021 03:56:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OLxfPF/rWh6p6YqLopR9KSquSYZfnYtXySIFUvPcXqA=; b=aUvdyWcc4m6zvJTN4GA+AOkGcS52PdmaQ0snuU2aKPB3oYXurG5ztavFFTwbhPbTz+ CRUsgOzkWaCoVBnaits8OrtxLlLtneVlZqtkLF+BDAS2umohbAOkxb4O1TNogXi9y8eO kfNbBRBeTeOy0pEGtcj4oh575yCedFMeCdxLBuuSchtYGIR7bxIrr7O9Ks7fICYlPn/s +LkrkVkJUfPtF7unl4dXXNHuADoogUb56bfhMocG3GBS6NThzeY66bC3HLeIgllQWH1G bJadZYPyW7fijtN+hS40+pZ6rzZHCjlP/aYQ+QwHodbEg8+hkuK1wQwqIJMYb7sxCwEi EY6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OLxfPF/rWh6p6YqLopR9KSquSYZfnYtXySIFUvPcXqA=; b=C/lfl0ttbQFAEQAcU7/B/PdybMdkTl8FW7Zfrrq7tw21fd0yMcwVumoMWLYclFlfUr vjTuztqwEChlIYbBzUmQkQpCzmUMQ2AYN2Fbzz46jUaq3qiLfeABx9kFvv1nKpRvdVni Njdp7CcQMNHVZUJenCt+jPORj2geJFCELf+cvUtee84t1bBwLMIbEXIn+6YQPswNax4g 9W+ex0OLLioXM9E6JfVaWaAs35UCcaNXMCpszaW1Hc0PN+w+v2dYkUwDofsDeEjVZ8IM 24VqC0qTuAW7NrZjN2LRnHGGwxRo0ny9Qh2lgVVlvO68NQUTwQR6l6hsz+sWXW8NkV+K EA7w== X-Gm-Message-State: AOAM532f+XpGsKrUrcRvvzR12iWz1ZoE9O1a6mFoOZOI2w+FNTpIsNdO VtzubYpRkC4mR0hHGxtnSASVtTjLaYYAGw== X-Received: by 2002:a65:4008:: with SMTP id f8mr173551pgp.310.1628420174567; Sun, 08 Aug 2021 03:56:14 -0700 (PDT) Received: from localhost.localdomain ([103.133.123.69]) by smtp.googlemail.com with ESMTPSA id r18sm20917405pgk.54.2021.08.08.03.56.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Aug 2021 03:56:14 -0700 (PDT) From: Shubhanshu Saxena To: ffmpeg-devel@ffmpeg.org Date: Sun, 8 Aug 2021 16:25:35 +0530 Message-Id: <20210808105539.18822-5-shubhanshu.e01@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210808105539.18822-1-shubhanshu.e01@gmail.com> References: <20210808105539.18822-1-shubhanshu.e01@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 5/9] [GSoC] lavfi/dnn_backend_tf: Error Handling for execute_model_tf 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: 2apaGwo7BcZj This patch adds error handling for cases where the execute_model_tf fails, clears the used memory in the TFRequestItem and finally pushes it back to the request queue. Signed-off-by: Shubhanshu Saxena --- libavfilter/dnn/dnn_backend_tf.c | 52 ++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_tf.c b/libavfilter/dnn/dnn_backend_tf.c index 3089ce8194..ad5f1e633e 100644 --- a/libavfilter/dnn/dnn_backend_tf.c +++ b/libavfilter/dnn/dnn_backend_tf.c @@ -173,6 +173,24 @@ static DNNReturnType tf_start_inference(void *args) return DNN_SUCCESS; } +/** + * Free the TFRequestItem completely. + * + * @param arg Address of the TFInferRequest instance. + */ +static inline void destroy_request_item(TFRequestItem **arg) { + TFRequestItem *request; + if (!arg) { + return; + } + request = *arg; + tf_free_request(request->infer_request); + av_freep(&request->infer_request); + av_freep(&request->inference); + ff_dnn_async_module_cleanup(&request->exec_module); + av_freep(arg); +} + static DNNReturnType extract_inference_from_task(TaskItem *task, Queue *inference_queue) { TFModel *tf_model = task->model; @@ -881,6 +899,7 @@ DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_ if (!item) { goto err; } + item->inference = NULL; item->infer_request = tf_create_inference_request(); if (!item->infer_request) { av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for TensorFlow inference request\n"); @@ -892,8 +911,7 @@ DNNModel *ff_dnn_load_model_tf(const char *model_filename, DNNFunctionType func_ item->exec_module.args = item; if (ff_safe_queue_push_back(tf_model->request_queue, item) < 0) { - av_freep(&item->infer_request); - av_freep(&item); + destroy_request_item(&item); goto err; } } @@ -1060,8 +1078,7 @@ err: av_freep(&outputs); if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) { - av_freep(&request->infer_request); - av_freep(&request); + destroy_request_item(&request); av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n"); } } @@ -1073,28 +1090,35 @@ static DNNReturnType execute_model_tf(TFRequestItem *request, Queue *inference_q InferenceItem *inference; TaskItem *task; - inference = ff_queue_peek_front(inference_queue); - if (!inference) { - av_log(NULL, AV_LOG_ERROR, "Failed to get inference item\n"); - return DNN_ERROR; + if (ff_queue_size(inference_queue) == 0) { + destroy_request_item(&request); + return DNN_SUCCESS; } + + inference = ff_queue_peek_front(inference_queue); task = inference->task; tf_model = task->model; ctx = &tf_model->ctx; if (fill_model_input_tf(tf_model, request) != DNN_SUCCESS) { - return DNN_ERROR; + goto err; } if (task->async) { return ff_dnn_start_inference_async(ctx, &request->exec_module); } else { if (tf_start_inference(request) != DNN_SUCCESS) { - return DNN_ERROR; + goto err; } infer_completion_callback(request); return (task->inference_done == task->inference_todo) ? DNN_SUCCESS : DNN_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; } DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, DNNExecBaseParams *exec_params) @@ -1194,8 +1218,7 @@ DNNReturnType ff_dnn_flush_tf(const DNNModel *model) if (ret != DNN_SUCCESS) { av_log(ctx, AV_LOG_ERROR, "Failed to fill model input.\n"); if (ff_safe_queue_push_back(tf_model->request_queue, request) < 0) { - av_freep(&request->infer_request); - av_freep(&request); + destroy_request_item(&request); } return ret; } @@ -1211,10 +1234,7 @@ void ff_dnn_free_model_tf(DNNModel **model) tf_model = (*model)->model; while (ff_safe_queue_size(tf_model->request_queue) != 0) { TFRequestItem *item = ff_safe_queue_pop_front(tf_model->request_queue); - ff_dnn_async_module_cleanup(&item->exec_module); - tf_free_request(item->infer_request); - av_freep(&item->infer_request); - av_freep(&item); + destroy_request_item(&item); } ff_safe_queue_destroy(tf_model->request_queue);