FFmpeg
dnn_backend_native_layer_mathbinary.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * DNN native backend implementation.
24  */
25 
26 #include "dnn_backend_native.h"
27 #include "libavutil/avassert.h"
29 
30 typedef float (*FunType)(float src0, float src1);
32 
33 static float sub(float src0, float src1)
34 {
35  return src0 - src1;
36 }
37 static float add(float src0, float src1)
38 {
39  return src0 + src1;
40 }
41 static float mul(float src0, float src1)
42 {
43  return src0 * src1;
44 }
45 static float realdiv(float src0, float src1)
46 {
47  return src0 / src1;
48 }
49 static float minimum(float src0, float src1)
50 {
51  return FFMIN(src0, src1);
52 }
53 static float floormod(float src0, float src1)
54 {
55  return (float)((int)(src0) % (int)(src1));
56 }
57 
58 static void math_binary_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
59 {
60  int dims_count;
61  const float *src;
62  float *dst;
63  dims_count = calculate_operand_dims_count(output);
64  src = input->data;
65  dst = output->data;
66  if (params->input0_broadcast || params->input1_broadcast) {
67  for (int i = 0; i < dims_count; ++i) {
68  dst[i] = pfun(params->v, src[i]);
69  }
70  } else {
71  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
72  const float *src1 = input1->data;
73  for (int i = 0; i < dims_count; ++i) {
74  dst[i] = pfun(src[i], src1[i]);
75  }
76  }
77 }
78 static void math_binary_not_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
79 {
80  int dims_count;
81  const float *src;
82  float *dst;
83  dims_count = calculate_operand_dims_count(output);
84  src = input->data;
85  dst = output->data;
86  if (params->input0_broadcast) {
87  for (int i = 0; i < dims_count; ++i) {
88  dst[i] = pfun(params->v, src[i]);
89  }
90  } else if (params->input1_broadcast) {
91  for (int i = 0; i < dims_count; ++i) {
92  dst[i] = pfun(src[i], params->v);
93  }
94  } else {
95  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
96  const float *src1 = input1->data;
97  for (int i = 0; i < dims_count; ++i) {
98  dst[i] = pfun(src[i], src1[i]);
99  }
100  }
101 }
102 int dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
103 {
105  int dnn_size = 0;
106  int input_index = 0;
107  params = av_malloc(sizeof(*params));
108  if (!params)
109  return 0;
110 
111  params->bin_op = (int32_t)avio_rl32(model_file_context);
112  dnn_size += 4;
113 
114  params->input0_broadcast = (int32_t)avio_rl32(model_file_context);
115  dnn_size += 4;
116  if (params->input0_broadcast) {
117  params->v = av_int2float(avio_rl32(model_file_context));
118  } else {
119  layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
120  if (layer->input_operand_indexes[input_index] >= operands_num) {
121  return 0;
122  }
123  input_index++;
124  }
125  dnn_size += 4;
126 
127  params->input1_broadcast = (int32_t)avio_rl32(model_file_context);
128  dnn_size += 4;
129  if (params->input1_broadcast) {
130  params->v = av_int2float(avio_rl32(model_file_context));
131  } else {
132  layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
133  if (layer->input_operand_indexes[input_index] >= operands_num) {
134  return 0;
135  }
136  input_index++;
137  }
138  dnn_size += 4;
139 
140  layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
141  dnn_size += 4;
142  layer->params = params;
143 
144  if (layer->output_operand_index >= operands_num) {
145  return 0;
146  }
147 
148  return dnn_size;
149 }
150 
151 int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes,
152  int32_t output_operand_index, const void *parameters, NativeContext *ctx)
153 {
154  const DnnOperand *input = &operands[input_operand_indexes[0]];
155  DnnOperand *output = &operands[output_operand_index];
156  const DnnLayerMathBinaryParams *params = (const DnnLayerMathBinaryParams *)parameters;
157 
158  for (int i = 0; i < 4; ++i)
159  output->dims[i] = input->dims[i];
160 
161  output->data_type = input->data_type;
162  output->length = calculate_operand_data_length(output);
163  if (output->length <= 0) {
164  av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
165  return DNN_ERROR;
166  }
167  output->data = av_realloc(output->data, output->length);
168  if (!output->data) {
169  av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
170  return DNN_ERROR;
171  }
172 
173  switch (params->bin_op) {
174  case DMBO_SUB:
175  math_binary_not_commutative(sub, params, input, output, operands, input_operand_indexes);
176  return 0;
177  case DMBO_ADD:
178  math_binary_commutative(add, params, input, output, operands, input_operand_indexes);
179  return 0;
180  case DMBO_MUL:
181  math_binary_commutative(mul, params, input, output, operands, input_operand_indexes);
182  return 0;
183  case DMBO_REALDIV:
184  math_binary_not_commutative(realdiv, params, input, output, operands, input_operand_indexes);
185  return 0;
186  case DMBO_MINIMUM:
187  math_binary_commutative(minimum, params, input, output, operands, input_operand_indexes);
188  return 0;
189  case DMBO_FLOORMOD:
190  math_binary_not_commutative(floormod, params, input, output, operands, input_operand_indexes);
191  return 0;
192  default:
193  av_log(ctx, AV_LOG_ERROR, "Unmatch math binary operator\n");
194  return DNN_ERROR;
195  }
196 }
Bytestream IO Context.
Definition: avio.h:161
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
static float floormod(float src0, float src1)
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
DNN inference functions interface for native backend.
int32_t calculate_operand_dims_count(const DnnOperand *oprd)
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
DNN inference functions interface for native backend.
#define av_malloc(s)
int dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
static float realdiv(float src0, float src1)
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
DNNDataType data_type
support different kinds of data type such as float, half float, int8 etc, first support float now...
#define av_log(a,...)
#define src
Definition: vp8dsp.c:254
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
void * data
data pointer with data length in bytes.
GLenum GLint * params
Definition: opengl_enc.c:113
simple assert() macros that are a bit more flexible than ISO C assert().
int32_t dims[4]
there are two memory layouts, NHWC or NCHW, so we use dims, dims[0] is Number.
static void math_binary_not_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
static float mul(float src0, float src1)
int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters, NativeContext *ctx)
#define FFMIN(a, b)
Definition: common.h:96
static float minimum(float src0, float src1)
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
#define src1
Definition: h264pred.c:139
#define src0
Definition: h264pred.c:138
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
int
int32_t calculate_operand_data_length(const DnnOperand *oprd)
static void math_binary_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
void * params
static float add(float src0, float src1)
static float sub(float src0, float src1)
float(* FunType)(float src0, float src1)
int i
Definition: input.c:407
int32_t output_operand_index