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.

174 lines
4.5KB

  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. #define EPSON 0.00001
  25. static int test_sub_broadcast_input0(void)
  26. {
  27. DnnLayerMathBinaryParams params;
  28. DnnOperand operands[2];
  29. int32_t input_indexes[1];
  30. float input[1*1*2*3] = {
  31. -3, 2.5, 2, -2.1, 7.8, 100
  32. };
  33. float *output;
  34. params.bin_op = DMBO_SUB;
  35. params.input0_broadcast = 1;
  36. params.input1_broadcast = 0;
  37. params.v = 7.28;
  38. operands[0].data = input;
  39. operands[0].dims[0] = 1;
  40. operands[0].dims[1] = 1;
  41. operands[0].dims[2] = 2;
  42. operands[0].dims[3] = 3;
  43. operands[1].data = NULL;
  44. input_indexes[0] = 0;
  45. dnn_execute_layer_math_binary(operands, input_indexes, 1, &params);
  46. output = operands[1].data;
  47. for (int i = 0; i < sizeof(input) / sizeof(float); i++) {
  48. float expected_output = params.v - input[i];
  49. if (fabs(output[i] - expected_output) > EPSON) {
  50. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output);
  51. av_freep(&output);
  52. return 1;
  53. }
  54. }
  55. av_freep(&output);
  56. return 0;
  57. }
  58. static int test_sub_broadcast_input1(void)
  59. {
  60. DnnLayerMathBinaryParams params;
  61. DnnOperand operands[2];
  62. int32_t input_indexes[1];
  63. float input[1*1*2*3] = {
  64. -3, 2.5, 2, -2.1, 7.8, 100
  65. };
  66. float *output;
  67. params.bin_op = DMBO_SUB;
  68. params.input0_broadcast = 0;
  69. params.input1_broadcast = 1;
  70. params.v = 7.28;
  71. operands[0].data = input;
  72. operands[0].dims[0] = 1;
  73. operands[0].dims[1] = 1;
  74. operands[0].dims[2] = 2;
  75. operands[0].dims[3] = 3;
  76. operands[1].data = NULL;
  77. input_indexes[0] = 0;
  78. dnn_execute_layer_math_binary(operands, input_indexes, 1, &params);
  79. output = operands[1].data;
  80. for (int i = 0; i < sizeof(input) / sizeof(float); i++) {
  81. float expected_output = input[i] - params.v;
  82. if (fabs(output[i] - expected_output) > EPSON) {
  83. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output);
  84. av_freep(&output);
  85. return 1;
  86. }
  87. }
  88. av_freep(&output);
  89. return 0;
  90. }
  91. static int test_sub_no_broadcast(void)
  92. {
  93. DnnLayerMathBinaryParams params;
  94. DnnOperand operands[3];
  95. int32_t input_indexes[2];
  96. float input0[1*1*2*3] = {
  97. -3, 2.5, 2, -2.1, 7.8, 100
  98. };
  99. float input1[1*1*2*3] = {
  100. -1, 2, 3, -21, 8, 10.0
  101. };
  102. float *output;
  103. params.bin_op = DMBO_SUB;
  104. params.input0_broadcast = 0;
  105. params.input1_broadcast = 0;
  106. operands[0].data = input0;
  107. operands[0].dims[0] = 1;
  108. operands[0].dims[1] = 1;
  109. operands[0].dims[2] = 2;
  110. operands[0].dims[3] = 3;
  111. operands[1].data = input1;
  112. operands[1].dims[0] = 1;
  113. operands[1].dims[1] = 1;
  114. operands[1].dims[2] = 2;
  115. operands[1].dims[3] = 3;
  116. operands[2].data = NULL;
  117. input_indexes[0] = 0;
  118. input_indexes[1] = 1;
  119. dnn_execute_layer_math_binary(operands, input_indexes, 2, &params);
  120. output = operands[2].data;
  121. for (int i = 0; i < sizeof(input0) / sizeof(float); i++) {
  122. float expected_output = input0[i] - input1[i];
  123. if (fabs(output[i] - expected_output) > EPSON) {
  124. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output);
  125. av_freep(&output);
  126. return 1;
  127. }
  128. }
  129. av_freep(&output);
  130. return 0;
  131. }
  132. static int test_sub(void)
  133. {
  134. if (test_sub_broadcast_input0())
  135. return 1;
  136. if (test_sub_broadcast_input1())
  137. return 1;
  138. if (test_sub_no_broadcast())
  139. return 1;
  140. return 0;
  141. }
  142. int main(int argc, char **argv)
  143. {
  144. if (test_sub())
  145. return 1;
  146. return 0;
  147. }