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

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 "layer/scale.h"
  15. #include "testutil.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_vulkan_compute = true;
  28. opt.use_int8_inference = false;
  29. int ret = test_layer<ncnn::Scale>("Scale", pd, weights, opt, a);
  30. if (ret != 0)
  31. {
  32. 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);
  33. }
  34. return ret;
  35. }
  36. static int test_scale_attention(const ncnn::Mat& a, int scale_data_size)
  37. {
  38. ncnn::ParamDict pd;
  39. pd.set(0, -233);
  40. std::vector<ncnn::Mat> weights(0);
  41. ncnn::Option opt;
  42. opt.num_threads = 1;
  43. opt.use_int8_inference = false;
  44. std::vector<ncnn::Mat> ab(2);
  45. ab[0] = a;
  46. ab[1] = RandomMat(scale_data_size);
  47. int ret = test_layer<ncnn::Scale>("Scale", pd, weights, opt, ab, 2);
  48. if (ret != 0)
  49. {
  50. 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);
  51. }
  52. return ret;
  53. }
  54. static int test_scale_0()
  55. {
  56. return 0
  57. || test_scale(RandomMat(6, 7, 16), 16, 0)
  58. || test_scale(RandomMat(6, 7, 16), 16, 1)
  59. || test_scale(RandomMat(3, 5, 13), 13, 0)
  60. || test_scale(RandomMat(3, 5, 13), 13, 1);
  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. static int test_scale_2()
  71. {
  72. return 0
  73. || test_scale(RandomMat(128), 128, 0)
  74. || test_scale(RandomMat(128), 128, 1)
  75. || test_scale(RandomMat(127), 127, 0)
  76. || test_scale(RandomMat(127), 127, 1);
  77. }
  78. static int test_scale_3()
  79. {
  80. return 0
  81. || test_scale_attention(RandomMat(6, 7, 16), 16)
  82. || test_scale_attention(RandomMat(3, 5, 13), 13);
  83. }
  84. static int test_scale_4()
  85. {
  86. return 0
  87. || test_scale_attention(RandomMat(6, 16), 16)
  88. || test_scale_attention(RandomMat(7, 15), 15);
  89. }
  90. static int test_scale_5()
  91. {
  92. return 0
  93. || test_scale_attention(RandomMat(128), 128)
  94. || test_scale_attention(RandomMat(127), 127);
  95. }
  96. int main()
  97. {
  98. SRAND(7767517);
  99. return 0
  100. || test_scale_0()
  101. || test_scale_1()
  102. || test_scale_2()
  103. || test_scale_3()
  104. || test_scale_4()
  105. || test_scale_5();
  106. }