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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "layer/unaryop.h"
  15. #include "testutil.h"
  16. #define OP_TYPE_MAX 17
  17. static int get_op_type()
  18. {
  19. static int op_type = 0;
  20. if (op_type == OP_TYPE_MAX)
  21. op_type = 0;
  22. return op_type++;
  23. }
  24. static int test_unaryop(const ncnn::Mat& _a)
  25. {
  26. ncnn::Mat a = _a;
  27. int op_type = get_op_type();
  28. if (op_type == 5 || op_type == 6 || op_type == 8)
  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. ncnn::ParamDict pd;
  39. pd.set(0, op_type);
  40. std::vector<ncnn::Mat> weights(0);
  41. ncnn::Option opt;
  42. opt.num_threads = 1;
  43. opt.use_vulkan_compute = true;
  44. opt.use_int8_inference = false;
  45. int ret = test_layer<ncnn::UnaryOp>("UnaryOp", pd, weights, opt, a);
  46. if (ret != 0)
  47. {
  48. fprintf(stderr, "test_unaryop failed a.dims=%d a=(%d %d %d) op_type=%d\n", a.dims, a.w, a.h, a.c, op_type);
  49. }
  50. return ret;
  51. }
  52. static int test_unaryop_0()
  53. {
  54. return 0
  55. || test_unaryop(RandomMat(6, 7, 16))
  56. || test_unaryop(RandomMat(5, 4, 12))
  57. || test_unaryop(RandomMat(3, 5, 13));
  58. }
  59. static int test_unaryop_1()
  60. {
  61. return 0
  62. || test_unaryop(RandomMat(6, 16))
  63. || test_unaryop(RandomMat(5, 12))
  64. || test_unaryop(RandomMat(7, 15));
  65. }
  66. static int test_unaryop_2()
  67. {
  68. return 0
  69. || test_unaryop(RandomMat(128))
  70. || test_unaryop(RandomMat(12))
  71. || test_unaryop(RandomMat(15));
  72. }
  73. int main()
  74. {
  75. SRAND(7767517);
  76. return 0
  77. || test_unaryop_0()
  78. || test_unaryop_1()
  79. || test_unaryop_2()
  80. // iterate full OP_TYPE_MAX
  81. || test_unaryop_0()
  82. || test_unaryop_1()
  83. || test_unaryop_2();
  84. }