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_convolution1d.cpp 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2021 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. static int test_convolution1d(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias)
  16. {
  17. ncnn::Mat a = RandomMat(w, h);
  18. ncnn::ParamDict pd;
  19. pd.set(0, outh); // num_output
  20. pd.set(1, kernel); // kernel_w
  21. pd.set(2, dilation); // dilation_w
  22. pd.set(3, stride); // stride_w
  23. pd.set(4, pad); // pad_w
  24. pd.set(5, bias); // bias_term
  25. pd.set(6, outh * h * kernel);
  26. int activation_type = RAND() % 6; // 0 1 2 3 4 5
  27. ncnn::Mat activation_params(2);
  28. activation_params[0] = RandomFloat(-1, 0); // alpha
  29. activation_params[1] = RandomFloat(0, 1); // beta
  30. pd.set(9, activation_type);
  31. pd.set(10, activation_params);
  32. std::vector<ncnn::Mat> weights(bias ? 2 : 1);
  33. weights[0] = RandomMat(outh * h * kernel);
  34. if (bias)
  35. weights[1] = RandomMat(outh);
  36. int ret = test_layer("Convolution1D", pd, weights, a);
  37. if (ret != 0)
  38. {
  39. fprintf(stderr, "test_convolution1d failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
  40. }
  41. return ret;
  42. }
  43. static int test_convolution1d_0()
  44. {
  45. static const int kdsp[16][4] = {
  46. {1, 1, 1, 0},
  47. {1, 1, 2, 0},
  48. {2, 1, 1, 1},
  49. {2, 1, 2, -233},
  50. {3, 1, 1, 1},
  51. {3, 1, 2, 1},
  52. {3, 2, 1, 1},
  53. {4, 1, 1, 2},
  54. {4, 1, 2, -233},
  55. {4, 2, 1, -234},
  56. {5, 1, 1, -234},
  57. {5, 1, 2, 2},
  58. {5, 2, 2, 2},
  59. {7, 1, 1, 3},
  60. {7, 1, 2, 3},
  61. {7, 2, 1, -233},
  62. };
  63. for (int i = 0; i < 16; i++)
  64. {
  65. const int k = kdsp[i][0];
  66. const int d = kdsp[i][1];
  67. const int s = kdsp[i][2];
  68. const int p = kdsp[i][3];
  69. const int b0 = i % 2;
  70. const int b1 = 1 - b0;
  71. int ret = 0
  72. || test_convolution1d(9, 1, 1, k, d, s, p, b0)
  73. || test_convolution1d(9, 1, 3, k, d, s, p, b1)
  74. || test_convolution1d(9, 1, 7, k, d, s, p, b0)
  75. || test_convolution1d(9, 1, 15, k, d, s, p, b1)
  76. || test_convolution1d(9, 1, 31, k, d, s, p, b0)
  77. || test_convolution1d(9, 3, 1, k, d, s, p, b1)
  78. || test_convolution1d(9, 3, 3, k, d, s, p, b0)
  79. || test_convolution1d(9, 3, 7, k, d, s, p, b1)
  80. || test_convolution1d(9, 3, 15, k, d, s, p, b0)
  81. || test_convolution1d(9, 3, 31, k, d, s, p, b1)
  82. || test_convolution1d(9, 7, 1, k, d, s, p, b0)
  83. || test_convolution1d(9, 7, 3, k, d, s, p, b1)
  84. || test_convolution1d(9, 7, 7, k, d, s, p, b0)
  85. || test_convolution1d(9, 7, 15, k, d, s, p, b1)
  86. || test_convolution1d(9, 7, 31, k, d, s, p, b0)
  87. || test_convolution1d(9, 15, 1, k, d, s, p, b1)
  88. || test_convolution1d(9, 15, 3, k, d, s, p, b0)
  89. || test_convolution1d(9, 15, 7, k, d, s, p, b1)
  90. || test_convolution1d(9, 15, 15, k, d, s, p, b0)
  91. || test_convolution1d(9, 15, 31, k, d, s, p, b1)
  92. || test_convolution1d(9, 31, 1, k, d, s, p, b0)
  93. || test_convolution1d(9, 31, 3, k, d, s, p, b1)
  94. || test_convolution1d(9, 31, 7, k, d, s, p, b0)
  95. || test_convolution1d(9, 31, 15, k, d, s, p, b1)
  96. || test_convolution1d(25, 28, 31, k, d, s, p, b0)
  97. || test_convolution1d(25, 31, 28, k, d, s, p, b1)
  98. || test_convolution1d(25, 28, 28, k, d, s, p, b0)
  99. || test_convolution1d(25, 24, 28, k, d, s, p, b1)
  100. || test_convolution1d(25, 24, 31, k, d, s, p, b0)
  101. || test_convolution1d(25, 28, 24, k, d, s, p, b1)
  102. || test_convolution1d(25, 31, 24, k, d, s, p, b0)
  103. || test_convolution1d(25, 24, 24, k, d, s, p, b1)
  104. || test_convolution1d(25, 28, 48, k, d, s, p, b0)
  105. || test_convolution1d(25, 31, 48, k, d, s, p, b1)
  106. || test_convolution1d(25, 24, 48, k, d, s, p, b0)
  107. || test_convolution1d(25, 48, 28, k, d, s, p, b1)
  108. || test_convolution1d(25, 48, 31, k, d, s, p, b0)
  109. || test_convolution1d(25, 48, 24, k, d, s, p, b1)
  110. || test_convolution1d(25, 31, 31, k, d, s, p, b0)
  111. || test_convolution1d(25, 48, 48, k, d, s, p, b1);
  112. if (ret != 0)
  113. return -1;
  114. }
  115. return 0
  116. || test_convolution1d(7, 1, 4, 3, 1, 1, 1, 1)
  117. || test_convolution1d(14, 1, 4, 3, 1, 2, 1, 1)
  118. || test_convolution1d(15, 4, 4, 3, 1, 1, 1, 1)
  119. || test_convolution1d(15, 8, 8, 3, 1, 1, 1, 1)
  120. || test_convolution1d(11, 8, 16, 3, 1, 1, 1, 1)
  121. || test_convolution1d(13, 16, 24, 3, 1, 1, 1, 1)
  122. || test_convolution1d(8, 16, 24, 3, 1, 1, 1, 0)
  123. || test_convolution1d(4, 16, 24, 3, 1, 1, 1, 1)
  124. || test_convolution1d(4, 16, 24, 3, 1, 1, 1, 0)
  125. || test_convolution1d(6, 64, 64, 3, 1, 2, 0, 1);
  126. }
  127. static int test_convolution1d_dynamic(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias)
  128. {
  129. ncnn::Mat a = RandomMat(w, h);
  130. ncnn::ParamDict pd;
  131. pd.set(0, 0);
  132. pd.set(1, 0);
  133. pd.set(2, dilation);
  134. pd.set(3, stride);
  135. pd.set(4, pad);
  136. pd.set(5, bias);
  137. pd.set(6, 0);
  138. pd.set(19, 1); // dynamic weight
  139. int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
  140. ncnn::Mat activation_params(2);
  141. activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
  142. activation_params[1] = RandomFloat(0, 1); // beta
  143. pd.set(9, activation_type);
  144. pd.set(10, activation_params);
  145. std::vector<ncnn::Mat> as(bias ? 3 : 2);
  146. as[0] = a;
  147. as[1] = RandomMat(kernel, h, outh);
  148. if (bias)
  149. as[2] = RandomMat(outh);
  150. std::vector<ncnn::Mat> weights(0);
  151. int ret = test_layer("Convolution1D", pd, weights, as);
  152. if (ret != 0)
  153. {
  154. fprintf(stderr, "test_convolution1d_dynamic failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
  155. }
  156. return ret;
  157. }
  158. static int test_convolution1d_1()
  159. {
  160. static const int kdsp[7][4] = {
  161. {1, 1, 1, 0},
  162. {1, 1, 2, 0},
  163. {2, 1, 1, 1},
  164. {2, 1, 2, -233},
  165. {3, 1, 1, 1},
  166. {3, 1, 2, 1},
  167. {3, 2, 1, -234},
  168. };
  169. for (int i = 0; i < 7; i++)
  170. {
  171. const int k = kdsp[i][0];
  172. const int d = kdsp[i][1];
  173. const int s = kdsp[i][2];
  174. const int p = kdsp[i][3];
  175. int ret = 0
  176. || test_convolution1d_dynamic(11, 1, 1, k, d, s, p, 1)
  177. || test_convolution1d_dynamic(11, 4, 13, k, d, s, p, 0)
  178. || test_convolution1d_dynamic(11, 13, 4, k, d, s, p, 1)
  179. || test_convolution1d_dynamic(11, 12, 12, k, d, s, p, 0)
  180. || test_convolution1d_dynamic(11, 8, 12, k, d, s, p, 1)
  181. || test_convolution1d_dynamic(11, 8, 13, k, d, s, p, 0)
  182. || test_convolution1d_dynamic(11, 13, 8, k, d, s, p, 1)
  183. || test_convolution1d_dynamic(11, 12, 16, k, d, s, p, 0)
  184. || test_convolution1d_dynamic(11, 15, 15, k, d, s, p, 0)
  185. || test_convolution1d_dynamic(11, 16, 16, k, d, s, p, 0);
  186. if (ret != 0)
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. int main()
  192. {
  193. SRAND(7767517);
  194. return test_convolution1d_0() || test_convolution1d_1();
  195. }