You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.2KB

  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. #include <stdio.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include "libavfilter/dnn/dnn_backend_native_layer_mathbinary.h"
  24. #include "libavutil/avassert.h"
  25. #define EPSON 0.00005
  26. static float get_expected(float f1, float f2, DNNMathBinaryOperation op)
  27. {
  28. switch (op)
  29. {
  30. case DMBO_SUB:
  31. return f1 - f2;
  32. case DMBO_ADD:
  33. return f1 + f2;
  34. case DMBO_MUL:
  35. return f1 * f2;
  36. case DMBO_REALDIV:
  37. return f1 / f2;
  38. default:
  39. av_assert0(!"not supported yet");
  40. return 0.f;
  41. }
  42. }
  43. static int test_broadcast_input0(DNNMathBinaryOperation op)
  44. {
  45. DnnLayerMathBinaryParams params;
  46. DnnOperand operands[2];
  47. int32_t input_indexes[1];
  48. float input[1*1*2*3] = {
  49. -3, 2.5, 2, -2.1, 7.8, 100
  50. };
  51. float *output;
  52. params.bin_op = op;
  53. params.input0_broadcast = 1;
  54. params.input1_broadcast = 0;
  55. params.v = 7.28;
  56. operands[0].data = input;
  57. operands[0].dims[0] = 1;
  58. operands[0].dims[1] = 1;
  59. operands[0].dims[2] = 2;
  60. operands[0].dims[3] = 3;
  61. operands[1].data = NULL;
  62. input_indexes[0] = 0;
  63. dnn_execute_layer_math_binary(operands, input_indexes, 1, &params);
  64. output = operands[1].data;
  65. for (int i = 0; i < sizeof(input) / sizeof(float); i++) {
  66. float expected_output = get_expected(params.v, input[i], op);
  67. if (fabs(output[i] - expected_output) > EPSON) {
  68. printf("op %d, at index %d, output: %f, expected_output: %f (%s:%d)\n",
  69. op, i, output[i], expected_output, __FILE__, __LINE__);
  70. av_freep(&output);
  71. return 1;
  72. }
  73. }
  74. av_freep(&output);
  75. return 0;
  76. }
  77. static int test_broadcast_input1(DNNMathBinaryOperation op)
  78. {
  79. DnnLayerMathBinaryParams params;
  80. DnnOperand operands[2];
  81. int32_t input_indexes[1];
  82. float input[1*1*2*3] = {
  83. -3, 2.5, 2, -2.1, 7.8, 100
  84. };
  85. float *output;
  86. params.bin_op = op;
  87. params.input0_broadcast = 0;
  88. params.input1_broadcast = 1;
  89. params.v = 7.28;
  90. operands[0].data = input;
  91. operands[0].dims[0] = 1;
  92. operands[0].dims[1] = 1;
  93. operands[0].dims[2] = 2;
  94. operands[0].dims[3] = 3;
  95. operands[1].data = NULL;
  96. input_indexes[0] = 0;
  97. dnn_execute_layer_math_binary(operands, input_indexes, 1, &params);
  98. output = operands[1].data;
  99. for (int i = 0; i < sizeof(input) / sizeof(float); i++) {
  100. float expected_output = get_expected(input[i], params.v, op);
  101. if (fabs(output[i] - expected_output) > EPSON) {
  102. printf("op %d, at index %d, output: %f, expected_output: %f (%s:%d)\n",
  103. op, i, output[i], expected_output, __FILE__, __LINE__);
  104. av_freep(&output);
  105. return 1;
  106. }
  107. }
  108. av_freep(&output);
  109. return 0;
  110. }
  111. static int test_no_broadcast(DNNMathBinaryOperation op)
  112. {
  113. DnnLayerMathBinaryParams params;
  114. DnnOperand operands[3];
  115. int32_t input_indexes[2];
  116. float input0[1*1*2*3] = {
  117. -3, 2.5, 2, -2.1, 7.8, 100
  118. };
  119. float input1[1*1*2*3] = {
  120. -1, 2, 3, -21, 8, 10.0
  121. };
  122. float *output;
  123. params.bin_op = op;
  124. params.input0_broadcast = 0;
  125. params.input1_broadcast = 0;
  126. operands[0].data = input0;
  127. operands[0].dims[0] = 1;
  128. operands[0].dims[1] = 1;
  129. operands[0].dims[2] = 2;
  130. operands[0].dims[3] = 3;
  131. operands[1].data = input1;
  132. operands[1].dims[0] = 1;
  133. operands[1].dims[1] = 1;
  134. operands[1].dims[2] = 2;
  135. operands[1].dims[3] = 3;
  136. operands[2].data = NULL;
  137. input_indexes[0] = 0;
  138. input_indexes[1] = 1;
  139. dnn_execute_layer_math_binary(operands, input_indexes, 2, &params);
  140. output = operands[2].data;
  141. for (int i = 0; i < sizeof(input0) / sizeof(float); i++) {
  142. float expected_output = get_expected(input0[i], input1[i], op);
  143. if (fabs(output[i] - expected_output) > EPSON) {
  144. printf("op %d, at index %d, output: %f, expected_output: %f (%s:%d)\n",
  145. op, i, output[i], expected_output, __FILE__, __LINE__);
  146. av_freep(&output);
  147. return 1;
  148. }
  149. }
  150. av_freep(&output);
  151. return 0;
  152. }
  153. static int test(DNNMathBinaryOperation op)
  154. {
  155. if (test_broadcast_input0(op))
  156. return 1;
  157. if (test_broadcast_input1(op))
  158. return 1;
  159. if (test_no_broadcast(op))
  160. return 1;
  161. return 0;
  162. }
  163. int main(int argc, char **argv)
  164. {
  165. if (test(DMBO_SUB))
  166. return 1;
  167. if (test(DMBO_ADD))
  168. return 1;
  169. if (test(DMBO_MUL))
  170. return 1;
  171. if (test(DMBO_REALDIV))
  172. return 1;
  173. return 0;
  174. }