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_rnn.cpp 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "layer/rnn.h"
  15. #include "testutil.h"
  16. static int test_rnn(const ncnn::Mat& a, int outch, int direction)
  17. {
  18. int input_size = a.w;
  19. int num_directions = direction == 2 ? 2 : 1;
  20. ncnn::ParamDict pd;
  21. pd.set(0, outch);
  22. pd.set(1, outch * input_size * num_directions);
  23. pd.set(2, direction);
  24. std::vector<ncnn::Mat> weights(3);
  25. weights[0] = RandomMat(outch * input_size * num_directions);
  26. weights[1] = RandomMat(outch * num_directions);
  27. weights[2] = RandomMat(outch * outch * num_directions);
  28. int ret = test_layer<ncnn::RNN>("RNN", pd, weights, a);
  29. if (ret != 0)
  30. {
  31. fprintf(stderr, "test_rnn failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
  32. }
  33. return ret;
  34. }
  35. int test_rnn_layer_with_hidden(const ncnn::Mat& a, int outch, int direction)
  36. {
  37. int input_size = a.w;
  38. int num_directions = direction == 2 ? 2 : 1;
  39. ncnn::ParamDict pd;
  40. pd.set(0, outch);
  41. pd.set(1, outch * input_size * num_directions);
  42. pd.set(2, direction);
  43. std::vector<ncnn::Mat> weights(3);
  44. weights[0] = RandomMat(outch * input_size * num_directions);
  45. weights[1] = RandomMat(outch * num_directions);
  46. weights[2] = RandomMat(outch * outch * num_directions);
  47. // initial hidden state
  48. ncnn::Mat hidden = RandomMat(outch, num_directions);
  49. std::vector<ncnn::Mat> as(2);
  50. as[0] = a;
  51. as[1] = hidden;
  52. int ret = test_layer<ncnn::RNN>("RNN", pd, weights, as, 2);
  53. if (ret != 0)
  54. {
  55. fprintf(stderr, "test_rnn_layer_with_hidden failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
  56. }
  57. return ret;
  58. }
  59. int test_rnn_layer_with_hidden_input(const ncnn::Mat& a, int outch, int direction)
  60. {
  61. int input_size = a.w;
  62. int num_directions = direction == 2 ? 2 : 1;
  63. ncnn::ParamDict pd;
  64. pd.set(0, outch);
  65. pd.set(1, outch * input_size * num_directions);
  66. pd.set(2, direction);
  67. std::vector<ncnn::Mat> weights(3);
  68. weights[0] = RandomMat(outch * input_size * num_directions);
  69. weights[1] = RandomMat(outch * num_directions);
  70. weights[2] = RandomMat(outch * outch * num_directions);
  71. // initial hidden state
  72. ncnn::Mat hidden = RandomMat(outch, num_directions);
  73. std::vector<ncnn::Mat> as(2);
  74. as[0] = a;
  75. as[1] = hidden;
  76. int ret = test_layer<ncnn::RNN>("RNN", pd, weights, as, 1);
  77. if (ret != 0)
  78. {
  79. fprintf(stderr, "test_rnn_layer_with_hidden_input failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
  80. }
  81. return ret;
  82. }
  83. int test_rnn_layer_with_hidden_output(const ncnn::Mat& a, int outch, int direction)
  84. {
  85. int input_size = a.w;
  86. int num_directions = direction == 2 ? 2 : 1;
  87. ncnn::ParamDict pd;
  88. pd.set(0, outch);
  89. pd.set(1, outch * input_size * num_directions);
  90. pd.set(2, direction);
  91. std::vector<ncnn::Mat> weights(3);
  92. weights[0] = RandomMat(outch * input_size * num_directions);
  93. weights[1] = RandomMat(outch * num_directions);
  94. weights[2] = RandomMat(outch * outch * num_directions);
  95. std::vector<ncnn::Mat> as(1);
  96. as[0] = a;
  97. int ret = test_layer<ncnn::RNN>("RNN", pd, weights, as, 2);
  98. if (ret != 0)
  99. {
  100. fprintf(stderr, "test_rnn_layer_with_hidden_output failed a.dims=%d a=(%d %d %d) outch=%d, direction = %d \n", a.dims, a.w, a.h, a.c, outch, direction);
  101. }
  102. return ret;
  103. }
  104. static int test_rnn_0()
  105. {
  106. return 0
  107. || test_rnn(RandomMat(4, 1), 2, 2)
  108. || test_rnn(RandomMat(8, 2), 2, 2)
  109. || test_rnn(RandomMat(16, 8), 7, 2)
  110. || test_rnn(RandomMat(17, 8), 8, 2)
  111. || test_rnn(RandomMat(19, 15), 8, 2)
  112. || test_rnn(RandomMat(5, 16), 16, 2)
  113. || test_rnn(RandomMat(3, 16), 8, 2)
  114. || test_rnn(RandomMat(8, 16), 16, 2)
  115. || test_rnn(RandomMat(2, 5), 17, 2);
  116. }
  117. static int test_rnn_1()
  118. {
  119. return 0
  120. || test_rnn_layer_with_hidden(RandomMat(4, 4), 1, 2)
  121. || test_rnn_layer_with_hidden(RandomMat(8, 2), 2, 2)
  122. || test_rnn_layer_with_hidden(RandomMat(16, 8), 7, 2)
  123. || test_rnn_layer_with_hidden(RandomMat(17, 8), 8, 2)
  124. || test_rnn_layer_with_hidden(RandomMat(19, 15), 8, 2)
  125. || test_rnn_layer_with_hidden(RandomMat(5, 16), 16, 2)
  126. || test_rnn_layer_with_hidden(RandomMat(3, 16), 8, 2)
  127. || test_rnn_layer_with_hidden(RandomMat(2, 5), 99, 2)
  128. || test_rnn_layer_with_hidden(RandomMat(4, 4), 1, 1)
  129. || test_rnn_layer_with_hidden(RandomMat(8, 2), 2, 1)
  130. || test_rnn_layer_with_hidden(RandomMat(16, 8), 7, 1)
  131. || test_rnn_layer_with_hidden(RandomMat(17, 8), 8, 1)
  132. || test_rnn_layer_with_hidden(RandomMat(19, 15), 8, 1)
  133. || test_rnn_layer_with_hidden(RandomMat(5, 16), 16, 1)
  134. || test_rnn_layer_with_hidden(RandomMat(3, 16), 8, 1)
  135. || test_rnn_layer_with_hidden(RandomMat(2, 5), 99, 1)
  136. || test_rnn_layer_with_hidden(RandomMat(4, 2), 1, 0)
  137. || test_rnn_layer_with_hidden(RandomMat(8, 2), 2, 0)
  138. || test_rnn_layer_with_hidden(RandomMat(16, 8), 7, 0)
  139. || test_rnn_layer_with_hidden(RandomMat(17, 8), 8, 0)
  140. || test_rnn_layer_with_hidden(RandomMat(19, 15), 8, 0)
  141. || test_rnn_layer_with_hidden(RandomMat(5, 16), 16, 0)
  142. || test_rnn_layer_with_hidden(RandomMat(3, 16), 8, 0)
  143. || test_rnn_layer_with_hidden(RandomMat(2, 5), 17, 0)
  144. || test_rnn_layer_with_hidden_input(RandomMat(4, 4), 1, 2)
  145. || test_rnn_layer_with_hidden_input(RandomMat(8, 2), 2, 2)
  146. || test_rnn_layer_with_hidden_input(RandomMat(16, 8), 7, 2)
  147. || test_rnn_layer_with_hidden_input(RandomMat(17, 8), 8, 2)
  148. || test_rnn_layer_with_hidden_input(RandomMat(19, 15), 8, 2)
  149. || test_rnn_layer_with_hidden_input(RandomMat(5, 16), 16, 2)
  150. || test_rnn_layer_with_hidden_input(RandomMat(3, 16), 8, 2)
  151. || test_rnn_layer_with_hidden_input(RandomMat(2, 5), 99, 2)
  152. || test_rnn_layer_with_hidden_input(RandomMat(4, 4), 1, 1)
  153. || test_rnn_layer_with_hidden_input(RandomMat(8, 2), 2, 1)
  154. || test_rnn_layer_with_hidden_input(RandomMat(16, 8), 7, 1)
  155. || test_rnn_layer_with_hidden_input(RandomMat(17, 8), 8, 1)
  156. || test_rnn_layer_with_hidden_input(RandomMat(19, 15), 8, 1)
  157. || test_rnn_layer_with_hidden_input(RandomMat(5, 16), 16, 1)
  158. || test_rnn_layer_with_hidden_input(RandomMat(3, 16), 8, 1)
  159. || test_rnn_layer_with_hidden_input(RandomMat(2, 5), 99, 1)
  160. || test_rnn_layer_with_hidden_input(RandomMat(4, 2), 1, 0)
  161. || test_rnn_layer_with_hidden_input(RandomMat(8, 2), 2, 0)
  162. || test_rnn_layer_with_hidden_input(RandomMat(16, 8), 7, 0)
  163. || test_rnn_layer_with_hidden_input(RandomMat(17, 8), 8, 0)
  164. || test_rnn_layer_with_hidden_input(RandomMat(19, 15), 8, 0)
  165. || test_rnn_layer_with_hidden_input(RandomMat(5, 16), 16, 0)
  166. || test_rnn_layer_with_hidden_input(RandomMat(3, 16), 8, 0)
  167. || test_rnn_layer_with_hidden_input(RandomMat(2, 5), 17, 0)
  168. || test_rnn_layer_with_hidden_output(RandomMat(4, 4), 1, 2)
  169. || test_rnn_layer_with_hidden_output(RandomMat(8, 2), 2, 2)
  170. || test_rnn_layer_with_hidden_output(RandomMat(16, 8), 7, 2)
  171. || test_rnn_layer_with_hidden_output(RandomMat(17, 8), 8, 2)
  172. || test_rnn_layer_with_hidden_output(RandomMat(19, 15), 8, 2)
  173. || test_rnn_layer_with_hidden_output(RandomMat(5, 16), 16, 2)
  174. || test_rnn_layer_with_hidden_output(RandomMat(3, 16), 8, 2)
  175. || test_rnn_layer_with_hidden_output(RandomMat(2, 5), 99, 2)
  176. || test_rnn_layer_with_hidden_output(RandomMat(4, 4), 1, 1)
  177. || test_rnn_layer_with_hidden_output(RandomMat(8, 2), 2, 1)
  178. || test_rnn_layer_with_hidden_output(RandomMat(16, 8), 7, 1)
  179. || test_rnn_layer_with_hidden_output(RandomMat(17, 8), 8, 1)
  180. || test_rnn_layer_with_hidden_output(RandomMat(19, 15), 8, 1)
  181. || test_rnn_layer_with_hidden_output(RandomMat(5, 16), 16, 1)
  182. || test_rnn_layer_with_hidden_output(RandomMat(3, 16), 8, 1)
  183. || test_rnn_layer_with_hidden_output(RandomMat(2, 5), 99, 1)
  184. || test_rnn_layer_with_hidden_output(RandomMat(4, 2), 1, 0)
  185. || test_rnn_layer_with_hidden_output(RandomMat(8, 2), 2, 0)
  186. || test_rnn_layer_with_hidden_output(RandomMat(16, 8), 7, 0)
  187. || test_rnn_layer_with_hidden_output(RandomMat(17, 8), 8, 0)
  188. || test_rnn_layer_with_hidden_output(RandomMat(19, 15), 8, 0)
  189. || test_rnn_layer_with_hidden_output(RandomMat(5, 16), 16, 0)
  190. || test_rnn_layer_with_hidden_output(RandomMat(3, 16), 8, 0)
  191. || test_rnn_layer_with_hidden_output(RandomMat(2, 5), 17, 0);
  192. }
  193. static int test_rnn_2()
  194. {
  195. return 0
  196. || test_rnn(RandomMat(4, 1), 1, 0)
  197. || test_rnn(RandomMat(8, 2), 2, 0)
  198. || test_rnn(RandomMat(16, 8), 7, 0)
  199. || test_rnn(RandomMat(17, 8), 8, 0)
  200. || test_rnn(RandomMat(19, 15), 8, 0)
  201. || test_rnn(RandomMat(5, 16), 16, 0)
  202. || test_rnn(RandomMat(3, 16), 8, 0)
  203. || test_rnn(RandomMat(8, 16), 16, 0)
  204. || test_rnn(RandomMat(2, 5), 17, 0);
  205. }
  206. static int test_rnn_3()
  207. {
  208. return 0
  209. || test_rnn(RandomMat(4, 1), 1, 1)
  210. || test_rnn(RandomMat(8, 2), 2, 1)
  211. || test_rnn(RandomMat(16, 8), 7, 1)
  212. || test_rnn(RandomMat(17, 8), 8, 1)
  213. || test_rnn(RandomMat(19, 15), 8, 1)
  214. || test_rnn(RandomMat(5, 16), 16, 1)
  215. || test_rnn(RandomMat(3, 16), 8, 1)
  216. || test_rnn(RandomMat(8, 16), 16, 1)
  217. || test_rnn(RandomMat(2, 5), 17, 1);
  218. }
  219. int main()
  220. {
  221. SRAND(7767517);
  222. return test_rnn_0() || test_rnn_1() || test_rnn_2() || test_rnn_3();
  223. }