diff mbox series

[FFmpeg-devel,V3,2/2] dnn_backend_native_layer_mathbinary: change to function pointer

Message ID 20200821085211.31210-2-mingyu.yin@intel.com
State Accepted
Headers show
Series [FFmpeg-devel,V3,1/2] dnn_backend_native_layer_mathbinary: add floormod support | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Mingyu Yin Aug. 21, 2020, 8:52 a.m. UTC
Signed-off-by: Mingyu Yin <mingyu.yin@intel.com>
---
 .../dnn/dnn_backend_native_layer_mathbinary.c | 166 +++++++++---------
 1 file changed, 80 insertions(+), 86 deletions(-)

Comments

Guo, Yejun Aug. 21, 2020, 10:45 a.m. UTC | #1
> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of Mingyu
> Yin
> Sent: 2020年8月21日 16:52
> To: ffmpeg-devel@ffmpeg.org
> Subject: [FFmpeg-devel] [PATCH V3 2/2] dnn_backend_native_layer_mathbinary:
> change to function pointer
> 
> Signed-off-by: Mingyu Yin <mingyu.yin@intel.com>
> ---
>  .../dnn/dnn_backend_native_layer_mathbinary.c | 166 +++++++++---------
>  1 file changed, 80 insertions(+), 86 deletions(-)
> 
> diff --git a/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c
> b/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c
> index 6876aaf2c6..6738acc6ce 100644
> --- a/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c
> +++ b/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c
> @@ -27,6 +27,79 @@
>  #include "libavutil/avassert.h"
>  #include "dnn_backend_native_layer_mathbinary.h"
> 
> +typedef float (*FunType)(const float src, float dst);
> +FunType pfun;
> +
> +static float sub(float src, float dst)
> +{
> +    return src - dst;
> +}
> +static float add(float src, float dst)
> +{
> +    return src + dst;
> +}
> +static float mul(float src, float dst)
> +{
> +    return src * dst;
> +}
> +static float realdiv(float src, float dst)
> +{
> +    return src / dst;
> +}
> +static float minimum(float src, float dst)
> +{
> +    return FFMIN(src, dst);
> +}
> +static float floormod(float src, float dst)
> +{
> +    return (float)((int)(src) % (int)(dst));
> +}

the function parameter is not src/dst, we can use src0 and 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;
> @@ -82,9 +155,6 @@ int dnn_execute_layer_math_binary(DnnOperand
> *operands, const int32_t *input_ope
>      const DnnOperand *input = &operands[input_operand_indexes[0]];
>      DnnOperand *output = &operands[output_operand_index];
>      const DnnLayerMathBinaryParams *params = (const
> DnnLayerMathBinaryParams *)parameters;
> -    int dims_count;
> -    const float *src;
> -    float *dst;
> 
>      for (int i = 0; i < 4; ++i)
>          output->dims[i] = input->dims[i];
> @@ -97,100 +167,24 @@ 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) {
> +    switch (params->bin_op){

you've removed a space between ) and {, and added a new space at the end of the line.
Do not change this line.
diff mbox series

Patch

diff --git a/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c b/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c
index 6876aaf2c6..6738acc6ce 100644
--- a/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c
+++ b/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c
@@ -27,6 +27,79 @@ 
 #include "libavutil/avassert.h"
 #include "dnn_backend_native_layer_mathbinary.h"
 
+typedef float (*FunType)(const float src, float dst);
+FunType pfun;
+
+static float sub(float src, float dst)
+{
+    return src - dst;
+}
+static float add(float src, float dst)
+{
+    return src + dst;
+}
+static float mul(float src, float dst)
+{
+    return src * dst;
+}
+static float realdiv(float src, float dst)
+{
+    return src / dst;
+}
+static float minimum(float src, float dst)
+{
+    return FFMIN(src, dst);
+}
+static float floormod(float src, float dst)
+{
+    return (float)((int)(src) % (int)(dst));
+}
+
+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;
@@ -82,9 +155,6 @@  int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_ope
     const DnnOperand *input = &operands[input_operand_indexes[0]];
     DnnOperand *output = &operands[output_operand_index];
     const DnnLayerMathBinaryParams *params = (const DnnLayerMathBinaryParams *)parameters;
-    int dims_count;
-    const float *src;
-    float *dst;
 
     for (int i = 0; i < 4; ++i)
         output->dims[i] = input->dims[i];
@@ -97,100 +167,24 @@  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) {
+    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;
     case DMBO_FLOORMOD:
-        if (params->input0_broadcast) {
-            for (int i = 0; i < dims_count; ++i) {
-                dst[i] = (int)(params->v) % (int)(src[i]);
-            }
-        } else if (params->input1_broadcast) {
-            for (int i = 0; i < dims_count; ++i) {
-                dst[i] = (int)(src[i]) % (int)(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] = (int)(src[i]) % (int)(src1[i]);
-            }
-        }
+        math_binary_not_commutative(floormod, params, input, output, operands, input_operand_indexes);
         return 0;
     default:
         return -1;