From patchwork Fri Aug 21 13:21:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mingyu Yin X-Patchwork-Id: 21798 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 6A64644B623 for ; Fri, 21 Aug 2020 16:19:22 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 51DB368B5FC; Fri, 21 Aug 2020 16:19:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D1222687F97 for ; Fri, 21 Aug 2020 16:19:15 +0300 (EEST) IronPort-SDR: f1Qk0U4zc6PJI5d6LqmGhwNNIMNwfzx2X/2LUQCp0347QQBNkfkpY3qSRVvH+WTqyRjxuqFYNc LWc88DaSKzCg== X-IronPort-AV: E=McAfee;i="6000,8403,9719"; a="173575992" X-IronPort-AV: E=Sophos;i="5.76,335,1592895600"; d="scan'208";a="173575992" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Aug 2020 06:19:13 -0700 IronPort-SDR: 5hztVAhE5YhlCENXAPZzVYSYidq4fcY2SS23YTYcUoGF0/GGZ+ZrpxbTrghpfZQdnkWA05ZbKz g90WHHC5m4Vg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.76,335,1592895600"; d="scan'208";a="327758657" Received: from cyj-pro.sh.intel.com ([10.239.98.50]) by orsmga008.jf.intel.com with ESMTP; 21 Aug 2020 06:19:12 -0700 From: Mingyu Yin To: ffmpeg-devel@ffmpeg.org Date: Fri, 21 Aug 2020 21:21:35 +0800 Message-Id: <20200821132136.6636-1-mingyu.yin@intel.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH V4 1/2] dnn_backend_native_layer_mathbinary: change to function pointer X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Mingyu Yin --- .../dnn/dnn_backend_native_layer_mathbinary.c | 140 +++++++++--------- 1 file changed, 73 insertions(+), 67 deletions(-) diff --git a/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c b/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c index dd42c329a9..4373f82543 100644 --- a/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c +++ b/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c @@ -27,6 +27,74 @@ #include "libavutil/avassert.h" #include "dnn_backend_native_layer_mathbinary.h" +typedef float (*FunType)(const float src0, float src1); +FunType pfun; + +static float sub(float src0, float src1) +{ + return src0 - src1; +} +static float add(float src0, float src1) +{ + return src0 + src1; +} +static float mul(float src0, float src1) +{ + return src0 * src1; +} +static float realdiv(float src0, float src1) +{ + return src0 / src1; +} +static float minimum(float src0, float src1) +{ + return FFMIN(src0, src1); +} + +static void math_binary_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes) +{ + int dims_count; + const float *src; + float *dst; + dims_count = calculate_operand_dims_count(output); + src = input->data; + dst = output->data; + if (params->input0_broadcast || params->input1_broadcast) { + for (int i = 0; i < dims_count; ++i) { + dst[i] = pfun(params->v, src[i]); + } + } else { + const DnnOperand *input1 = &operands[input_operand_indexes[1]]; + const float *src1 = input1->data; + for (int i = 0; i < dims_count; ++i) { + dst[i] = pfun(src[i], src1[i]); + } + } +} +static void math_binary_not_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes) +{ + int dims_count; + const float *src; + float *dst; + dims_count = calculate_operand_dims_count(output); + src = input->data; + dst = output->data; + if (params->input0_broadcast) { + for (int i = 0; i < dims_count; ++i) { + dst[i] = pfun(params->v, src[i]); + } + } else if (params->input1_broadcast) { + for (int i = 0; i < dims_count; ++i) { + dst[i] = pfun(src[i], params->v); + } + } else { + const DnnOperand *input1 = &operands[input_operand_indexes[1]]; + const float *src1 = input1->data; + for (int i = 0; i < dims_count; ++i) { + dst[i] = pfun(src[i], src1[i]); + } + } +} int dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num) { DnnLayerMathBinaryParams *params; @@ -97,83 +165,21 @@ int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_ope if (!output->data) return DNN_ERROR; - dims_count = calculate_operand_dims_count(output); - src = input->data; - dst = output->data; - switch (params->bin_op) { case DMBO_SUB: - if (params->input0_broadcast) { - for (int i = 0; i < dims_count; ++i) { - dst[i] = params->v - src[i]; - } - } else if (params->input1_broadcast) { - for (int i = 0; i < dims_count; ++i) { - dst[i] = src[i] - params->v; - } - } else { - const DnnOperand *input1 = &operands[input_operand_indexes[1]]; - const float *src1 = input1->data; - for (int i = 0; i < dims_count; ++i) { - dst[i] = src[i] - src1[i]; - } - } + math_binary_not_commutative(sub, params, input, output, operands, input_operand_indexes); return 0; case DMBO_ADD: - if (params->input0_broadcast || params->input1_broadcast) { - for (int i = 0; i < dims_count; ++i) { - dst[i] = params->v + src[i]; - } - } else { - const DnnOperand *input1 = &operands[input_operand_indexes[1]]; - const float *src1 = input1->data; - for (int i = 0; i < dims_count; ++i) { - dst[i] = src[i] + src1[i]; - } - } + math_binary_commutative(add, params, input, output, operands, input_operand_indexes); return 0; case DMBO_MUL: - if (params->input0_broadcast || params->input1_broadcast) { - for (int i = 0; i < dims_count; ++i) { - dst[i] = params->v * src[i]; - } - } else { - const DnnOperand *input1 = &operands[input_operand_indexes[1]]; - const float *src1 = input1->data; - for (int i = 0; i < dims_count; ++i) { - dst[i] = src[i] * src1[i]; - } - } + math_binary_commutative(mul, params, input, output, operands, input_operand_indexes); return 0; case DMBO_REALDIV: - if (params->input0_broadcast) { - for (int i = 0; i < dims_count; ++i) { - dst[i] = params->v / src[i]; - } - } else if (params->input1_broadcast) { - for (int i = 0; i < dims_count; ++i) { - dst[i] = src[i] / params->v; - } - } else { - const DnnOperand *input1 = &operands[input_operand_indexes[1]]; - const float *src1 = input1->data; - for (int i = 0; i < dims_count; ++i) { - dst[i] = src[i] / src1[i]; - } - } + math_binary_not_commutative(realdiv, params, input, output, operands, input_operand_indexes); return 0; case DMBO_MINIMUM: - if (params->input0_broadcast || params->input1_broadcast) { - for (int i = 0; i < dims_count; ++i) { - dst[i] = FFMIN(params->v, src[i]); - } - } else { - const DnnOperand *input1 = &operands[input_operand_indexes[1]]; - const float *src1 = input1->data; - for (int i = 0; i < dims_count; ++i) { - dst[i] = FFMIN(src[i], src1[i]); - } - } + math_binary_commutative(minimum, params, input, output, operands, input_operand_indexes); return 0; default: return -1;