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_scale.cpp 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "layer/scale.h"
  16. static int test_scale(const ncnn::Mat& a, int scale_data_size, int bias)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, scale_data_size);
  20. pd.set(1, bias);
  21. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  22. weights[0] = RandomMat(scale_data_size);
  23. if (bias)
  24. weights[1] = RandomMat(scale_data_size);
  25. ncnn::Option opt;
  26. opt.num_threads = 1;
  27. opt.use_int8_inference = false;
  28. int ret = test_layer<ncnn::Scale>("Scale", pd, weights, opt, a);
  29. if (ret != 0)
  30. {
  31. fprintf(stderr, "test_scale failed a.dims=%d a=(%d %d %d) scale_data_size=%d bias=%d\n", a.dims, a.w, a.h, a.c, scale_data_size, bias);
  32. }
  33. return ret;
  34. }
  35. static int test_scale_attention(const ncnn::Mat& a, int scale_data_size)
  36. {
  37. ncnn::ParamDict pd;
  38. pd.set(0, -233);
  39. std::vector<ncnn::Mat> weights(0);
  40. ncnn::Option opt;
  41. opt.num_threads = 1;
  42. opt.use_int8_inference = false;
  43. std::vector<ncnn::Mat> ab(2);
  44. ab[0] = a;
  45. ab[1] = RandomMat(scale_data_size);
  46. int ret = test_layer<ncnn::Scale>("Scale", pd, weights, opt, ab, 2);
  47. if (ret != 0)
  48. {
  49. fprintf(stderr, "test_scale_attention failed a.dims=%d a=(%d %d %d) scale_data_size=%d\n", a.dims, a.w, a.h, a.c, scale_data_size);
  50. }
  51. return ret;
  52. }
  53. static int test_scale_0()
  54. {
  55. return 0
  56. || test_scale(RandomMat(6, 7, 16), 16, 0)
  57. || test_scale(RandomMat(6, 7, 16), 16, 1)
  58. || test_scale(RandomMat(3, 5, 13), 13, 0)
  59. || test_scale(RandomMat(3, 5, 13), 13, 1)
  60. ;
  61. }
  62. static int test_scale_1()
  63. {
  64. return 0
  65. || test_scale(RandomMat(6, 16), 16, 0)
  66. || test_scale(RandomMat(6, 16), 16, 1)
  67. || test_scale(RandomMat(7, 15), 15, 0)
  68. || test_scale(RandomMat(7, 15), 15, 1)
  69. ;
  70. }
  71. static int test_scale_2()
  72. {
  73. return 0
  74. || test_scale(RandomMat(128), 128, 0)
  75. || test_scale(RandomMat(128), 128, 1)
  76. || test_scale(RandomMat(127), 127, 0)
  77. || test_scale(RandomMat(127), 127, 1)
  78. ;
  79. }
  80. static int test_scale_3()
  81. {
  82. return 0
  83. || test_scale_attention(RandomMat(6, 7, 16), 16)
  84. || test_scale_attention(RandomMat(3, 5, 13), 13)
  85. ;
  86. }
  87. static int test_scale_4()
  88. {
  89. return 0
  90. || test_scale_attention(RandomMat(6, 16), 16)
  91. || test_scale_attention(RandomMat(7, 15), 15)
  92. ;
  93. }
  94. static int test_scale_5()
  95. {
  96. return 0
  97. || test_scale_attention(RandomMat(128), 128)
  98. || test_scale_attention(RandomMat(127), 127)
  99. ;
  100. }
  101. int main()
  102. {
  103. SRAND(7767517);
  104. return 0
  105. || test_scale_0()
  106. || test_scale_1()
  107. || test_scale_2()
  108. || test_scale_3()
  109. || test_scale_4()
  110. || test_scale_5()
  111. ;
  112. }