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.

210 lines
5.3KB

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