FFmpeg
dnn-layer-mathunary.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 #include <stdio.h>
22 #include <string.h>
23 #include <math.h>
25 #include "libavutil/avassert.h"
26 
27 #define EPS 0.00001
28 
29 static float get_expected(float f, DNNMathUnaryOperation op)
30 {
31  switch (op)
32  {
33  case DMUO_ABS:
34  return (f >= 0) ? f : -f;
35  case DMUO_SIN:
36  return sin(f);
37  case DMUO_COS:
38  return cos(f);
39  case DMUO_TAN:
40  return tan(f);
41  case DMUO_ASIN:
42  return asin(f);
43  case DMUO_ACOS:
44  return acos(f);
45  case DMUO_ATAN:
46  return atan(f);
47  case DMUO_SINH:
48  return sinh(f);
49  case DMUO_COSH:
50  return cosh(f);
51  case DMUO_TANH:
52  return tanh(f);
53  case DMUO_ASINH:
54  return asinh(f);
55  case DMUO_ACOSH:
56  return acosh(f);
57  case DMUO_ATANH:
58  return atanh(f);
59  case DMUO_CEIL:
60  return ceil(f);
61  case DMUO_FLOOR:
62  return floor(f);
63  case DMUO_ROUND:
64  return round(f);
65  case DMUO_EXP:
66  return exp(f);
67  default:
68  av_assert0(!"not supported yet");
69  return 0.f;
70  }
71 }
72 
74 {
76  DnnOperand operands[2];
77  int32_t input_indexes[1];
78  float input[1*1*3*3] = {
79  0.1, 0.5, 0.75, -3, 2.5, 2, -2.1, 7.8, 100};
80  float *output;
81 
82  params.un_op = op;
83 
84  operands[0].data = input;
85  operands[0].dims[0] = 1;
86  operands[0].dims[1] = 1;
87  operands[0].dims[2] = 3;
88  operands[0].dims[3] = 3;
89  operands[1].data = NULL;
90 
91  input_indexes[0] = 0;
92  ff_dnn_execute_layer_math_unary(operands, input_indexes, 1, &params, NULL);
93 
94  output = operands[1].data;
95  for (int i = 0; i < sizeof(input) / sizeof(float); ++i) {
96  float expected_output = get_expected(input[i], op);
97  int output_nan = isnan(output[i]);
98  int expected_nan = isnan(expected_output);
99  if ((!output_nan && !expected_nan && fabs(output[i] - expected_output) > EPS) ||
100  (output_nan && !expected_nan) || (!output_nan && expected_nan)) {
101  printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output);
102  av_freep(&output);
103  return 1;
104  }
105  }
106 
107  av_freep(&output);
108  return 0;
109 }
110 
111 int main(int agrc, char **argv)
112 {
113  if (test(DMUO_ABS))
114  return 1;
115  if (test(DMUO_SIN))
116  return 1;
117  if (test(DMUO_COS))
118  return 1;
119  if (test(DMUO_TAN))
120  return 1;
121  if (test(DMUO_ASIN))
122  return 1;
123  if (test(DMUO_ACOS))
124  return 1;
125  if (test(DMUO_ATAN))
126  return 1;
127  if (test(DMUO_SINH))
128  return 1;
129  if (test(DMUO_COSH))
130  return 1;
131  if (test(DMUO_TANH))
132  return 1;
133  if (test(DMUO_ASINH))
134  return 1;
135  if (test(DMUO_ACOSH))
136  return 1;
137  if (test(DMUO_ATANH))
138  return 1;
139  if (test(DMUO_CEIL))
140  return 1;
141  if (test(DMUO_FLOOR))
142  return 1;
143  if (test(DMUO_ROUND))
144  return 1;
145  if (test(DMUO_EXP))
146  return 1;
147  return 0;
148 }
dnn_backend_native_layer_mathunary.h
EPS
#define EPS
Definition: dnn-layer-mathunary.c:27
DMUO_TANH
@ DMUO_TANH
Definition: dnn_backend_native_layer_mathunary.h:42
DMUO_ACOS
@ DMUO_ACOS
Definition: dnn_backend_native_layer_mathunary.h:38
output
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
Definition: filter_design.txt:225
test
static int test(DNNMathUnaryOperation op)
Definition: dnn-layer-mathunary.c:73
DMUO_ACOSH
@ DMUO_ACOSH
Definition: dnn_backend_native_layer_mathunary.h:44
DnnLayerMathUnaryParams::un_op
DNNMathUnaryOperation un_op
Definition: dnn_backend_native_layer_mathunary.h:54
DMUO_ASINH
@ DMUO_ASINH
Definition: dnn_backend_native_layer_mathunary.h:43
DMUO_CEIL
@ DMUO_CEIL
Definition: dnn_backend_native_layer_mathunary.h:46
DMUO_COSH
@ DMUO_COSH
Definition: dnn_backend_native_layer_mathunary.h:41
avassert.h
ceil
static __device__ float ceil(float a)
Definition: cuda_runtime.h:176
DMUO_SIN
@ DMUO_SIN
Definition: dnn_backend_native_layer_mathunary.h:34
DMUO_TAN
@ DMUO_TAN
Definition: dnn_backend_native_layer_mathunary.h:36
DMUO_ROUND
@ DMUO_ROUND
Definition: dnn_backend_native_layer_mathunary.h:48
floor
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
op
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:75
DnnOperand::data
void * data
data pointer with data length in bytes.
Definition: dnn_backend_native.h:104
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
f
#define f(width, name)
Definition: cbs_vp9.c:255
DMUO_SINH
@ DMUO_SINH
Definition: dnn_backend_native_layer_mathunary.h:40
ff_dnn_execute_layer_math_unary
int ff_dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters, NativeContext *ctx)
Execute the Unary Math Layer.
Definition: dnn_backend_native_layer_mathunary.c:54
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
isnan
#define isnan(x)
Definition: libm.h:340
DnnOperand::dims
int32_t dims[4]
there are two memory layouts, NHWC or NCHW, so we use dims, dims[0] is Number.
Definition: dnn_backend_native.h:74
DMUO_EXP
@ DMUO_EXP
Definition: dnn_backend_native_layer_mathunary.h:49
exp
int8_t exp
Definition: eval.c:72
get_expected
static float get_expected(float f, DNNMathUnaryOperation op)
Definition: dnn-layer-mathunary.c:29
DMUO_ATANH
@ DMUO_ATANH
Definition: dnn_backend_native_layer_mathunary.h:45
DMUO_FLOOR
@ DMUO_FLOOR
Definition: dnn_backend_native_layer_mathunary.h:47
printf
printf("static const uint8_t my_array[100] = {\n")
input
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
Definition: filter_design.txt:172
DnnLayerMathUnaryParams
Definition: dnn_backend_native_layer_mathunary.h:53
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
round
static av_always_inline av_const double round(double x)
Definition: libm.h:444
DMUO_ATAN
@ DMUO_ATAN
Definition: dnn_backend_native_layer_mathunary.h:39
DNNMathUnaryOperation
DNNMathUnaryOperation
Definition: dnn_backend_native_layer_mathunary.h:32
DMUO_ASIN
@ DMUO_ASIN
Definition: dnn_backend_native_layer_mathunary.h:37
main
int main(int agrc, char **argv)
Definition: dnn-layer-mathunary.c:111
DnnOperand
Definition: dnn_backend_native.h:69
DMUO_ABS
@ DMUO_ABS
Definition: dnn_backend_native_layer_mathunary.h:33
DMUO_COS
@ DMUO_COS
Definition: dnn_backend_native_layer_mathunary.h:35
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
int32_t
int32_t
Definition: audioconvert.c:56