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

test_unaryop.cpp 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "testutil.h"
  4. #define OP_TYPE_MAX 20
  5. static int op_type = 0;
  6. static int test_unaryop(const ncnn::Mat& _a)
  7. {
  8. ncnn::Mat a = _a;
  9. if (op_type == 2 || op_type == 3)
  10. {
  11. // large dynamic range for floor ceil
  12. for (int i = 0; i < a.total(); i++)
  13. {
  14. a[i] *= 1000;
  15. }
  16. }
  17. if (op_type == 5 || op_type == 6 || op_type == 8 || op_type == 17)
  18. {
  19. // value must be positive for sqrt rsqrt log
  20. Randomize(a, 0.001f, 2.f);
  21. }
  22. if (op_type == 11 || op_type == 12 || op_type == 13)
  23. {
  24. // smaller range for tan asin acos
  25. Randomize(a, -1.f, 1.f);
  26. }
  27. #if __powerpc__
  28. // nearbyintf produces wrong result in halfway cases, why ?
  29. // too troublesome to resolve the compiler or qemu problem
  30. // so just skip them --- nihui
  31. if (op_type == 18)
  32. {
  33. // drop 0.4 ~ 0.6
  34. for (int i = 0; i < a.total(); i++)
  35. {
  36. float v = a[i];
  37. float vv = fabs(v - (int)v);
  38. while (vv > 0.4f && vv < 0.6f)
  39. {
  40. v = RandomFloat(-15, 15);
  41. vv = fabs(v - (int)v);
  42. }
  43. a[i] = v;
  44. }
  45. }
  46. #endif // __powerpc__
  47. ncnn::ParamDict pd;
  48. pd.set(0, op_type);
  49. std::vector<ncnn::Mat> weights(0);
  50. int ret = test_layer("UnaryOp", pd, weights, a);
  51. if (ret != 0)
  52. {
  53. fprintf(stderr, "test_unaryop failed a.dims=%d a=(%d %d %d %d) op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, op_type);
  54. }
  55. return ret;
  56. }
  57. static int test_unaryop_0()
  58. {
  59. return 0
  60. || test_unaryop(RandomMat(11, 3, 2, 16))
  61. || test_unaryop(RandomMat(10, 2, 2, 12))
  62. || test_unaryop(RandomMat(6, 1, 5, 13));
  63. }
  64. static int test_unaryop_1()
  65. {
  66. return 0
  67. || test_unaryop(RandomMat(11, 7, 16))
  68. || test_unaryop(RandomMat(10, 4, 12))
  69. || test_unaryop(RandomMat(6, 5, 13));
  70. }
  71. static int test_unaryop_2()
  72. {
  73. return 0
  74. || test_unaryop(RandomMat(12, 16))
  75. || test_unaryop(RandomMat(10, 12))
  76. || test_unaryop(RandomMat(14, 15));
  77. }
  78. static int test_unaryop_3()
  79. {
  80. return 0
  81. || test_unaryop(RandomMat(128))
  82. || test_unaryop(RandomMat(12))
  83. || test_unaryop(RandomMat(15));
  84. }
  85. int main()
  86. {
  87. SRAND(7767517);
  88. for (op_type = 0; op_type < OP_TYPE_MAX; op_type++)
  89. {
  90. int ret = 0
  91. || test_unaryop_0()
  92. || test_unaryop_1()
  93. || test_unaryop_2()
  94. || test_unaryop_3();
  95. if (ret != 0)
  96. return ret;
  97. }
  98. return 0;
  99. }