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 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "testutil.h"
  15. #define OP_TYPE_MAX 20
  16. static int op_type = 0;
  17. static int test_unaryop(const ncnn::Mat& _a)
  18. {
  19. ncnn::Mat a = _a;
  20. if (op_type == 2 || op_type == 3)
  21. {
  22. // large dynamic range for floor ceil
  23. for (int i = 0; i < a.total(); i++)
  24. {
  25. a[i] *= 1000;
  26. }
  27. }
  28. if (op_type == 5 || op_type == 6 || op_type == 8 || op_type == 17)
  29. {
  30. // value must be positive for sqrt rsqrt log
  31. Randomize(a, 0.001f, 2.f);
  32. }
  33. if (op_type == 11 || op_type == 12 || op_type == 13)
  34. {
  35. // smaller range for tan asin acos
  36. Randomize(a, -1.f, 1.f);
  37. }
  38. #if __powerpc__
  39. // nearbyintf produces wrong result in halfway cases, why ?
  40. // too troublesome to resolve the compiler or qemu problem
  41. // so just skip them --- nihui
  42. if (op_type == 18)
  43. {
  44. // drop 0.4 ~ 0.6
  45. for (int i = 0; i < a.total(); i++)
  46. {
  47. float v = a[i];
  48. float vv = fabs(v - (int)v);
  49. while (vv > 0.4f && vv < 0.6f)
  50. {
  51. v = RandomFloat(-15, 15);
  52. vv = fabs(v - (int)v);
  53. }
  54. a[i] = v;
  55. }
  56. }
  57. #endif // __powerpc__
  58. ncnn::ParamDict pd;
  59. pd.set(0, op_type);
  60. std::vector<ncnn::Mat> weights(0);
  61. int ret = test_layer("UnaryOp", pd, weights, a);
  62. if (ret != 0)
  63. {
  64. 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);
  65. }
  66. return ret;
  67. }
  68. static int test_unaryop_0()
  69. {
  70. return 0
  71. || test_unaryop(RandomMat(11, 3, 2, 16))
  72. || test_unaryop(RandomMat(10, 2, 2, 12))
  73. || test_unaryop(RandomMat(6, 1, 5, 13));
  74. }
  75. static int test_unaryop_1()
  76. {
  77. return 0
  78. || test_unaryop(RandomMat(11, 7, 16))
  79. || test_unaryop(RandomMat(10, 4, 12))
  80. || test_unaryop(RandomMat(6, 5, 13));
  81. }
  82. static int test_unaryop_2()
  83. {
  84. return 0
  85. || test_unaryop(RandomMat(12, 16))
  86. || test_unaryop(RandomMat(10, 12))
  87. || test_unaryop(RandomMat(14, 15));
  88. }
  89. static int test_unaryop_3()
  90. {
  91. return 0
  92. || test_unaryop(RandomMat(128))
  93. || test_unaryop(RandomMat(12))
  94. || test_unaryop(RandomMat(15));
  95. }
  96. int main()
  97. {
  98. SRAND(7767517);
  99. for (op_type = 0; op_type < OP_TYPE_MAX; op_type++)
  100. {
  101. int ret = 0
  102. || test_unaryop_0()
  103. || test_unaryop_1()
  104. || test_unaryop_2()
  105. || test_unaryop_3();
  106. if (ret != 0)
  107. return ret;
  108. }
  109. return 0;
  110. }