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_gru.cpp 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/gru.h"
  15. #include "testutil.h"
  16. static int test_gru(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 * 3 * num_directions);
  23. pd.set(2, direction);
  24. std::vector<ncnn::Mat> weights(3);
  25. weights[0] = RandomMat(outch * input_size * 3 * num_directions);
  26. weights[1] = RandomMat(outch * 4 * num_directions);
  27. weights[2] = RandomMat(outch * outch * 3 * num_directions);
  28. int ret = test_layer<ncnn::GRU>("GRU", pd, weights, a);
  29. if (ret != 0)
  30. {
  31. fprintf(stderr, "test_gru 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_gru_layer_with_hidden(const ncnn::Mat& a, int outch, int direction)
  36. {
  37. int input_size = a.w;
  38. ncnn::ParamDict pd;
  39. pd.set(0, outch);
  40. pd.set(1, outch * input_size * 3);
  41. pd.set(2, direction);
  42. std::vector<ncnn::Mat> weights(3);
  43. weights[0] = RandomMat(outch * input_size * 3);
  44. weights[1] = RandomMat(outch * 4);
  45. weights[2] = RandomMat(outch * outch * 3);
  46. // initial hidden state
  47. ncnn::Mat hidden = RandomMat(outch);
  48. std::vector<ncnn::Mat> as(2);
  49. as[0] = a;
  50. as[1] = hidden;
  51. int ret = test_layer<ncnn::GRU>("GRU", pd, weights, as, 2);
  52. if (ret != 0)
  53. {
  54. fprintf(stderr, "test_gru_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);
  55. }
  56. return ret;
  57. }
  58. static int test_gru_0()
  59. {
  60. return 0
  61. || test_gru(RandomMat(4, 1), 2, 2)
  62. || test_gru(RandomMat(8, 2), 2, 2)
  63. || test_gru(RandomMat(16, 8), 7, 2)
  64. || test_gru(RandomMat(17, 8), 8, 2)
  65. || test_gru(RandomMat(19, 15), 8, 2)
  66. || test_gru(RandomMat(5, 16), 16, 2)
  67. || test_gru(RandomMat(3, 16), 8, 2)
  68. || test_gru(RandomMat(8, 16), 16, 2)
  69. || test_gru(RandomMat(2, 5), 17, 2);
  70. }
  71. static int test_gru_1()
  72. {
  73. return 0
  74. || test_gru_layer_with_hidden(RandomMat(4, 4), 1, 1)
  75. || test_gru_layer_with_hidden(RandomMat(8, 2), 2, 1)
  76. || test_gru_layer_with_hidden(RandomMat(16, 8), 7, 1)
  77. || test_gru_layer_with_hidden(RandomMat(17, 8), 8, 1)
  78. || test_gru_layer_with_hidden(RandomMat(19, 15), 8, 1)
  79. || test_gru_layer_with_hidden(RandomMat(5, 16), 16, 1)
  80. || test_gru_layer_with_hidden(RandomMat(3, 16), 8, 1)
  81. || test_gru_layer_with_hidden(RandomMat(2, 5), 99, 1)
  82. || test_gru_layer_with_hidden(RandomMat(4, 2), 1, 0)
  83. || test_gru_layer_with_hidden(RandomMat(8, 2), 2, 0)
  84. || test_gru_layer_with_hidden(RandomMat(16, 8), 7, 0)
  85. || test_gru_layer_with_hidden(RandomMat(17, 8), 8, 0)
  86. || test_gru_layer_with_hidden(RandomMat(19, 15), 8, 0)
  87. || test_gru_layer_with_hidden(RandomMat(5, 16), 16, 0)
  88. || test_gru_layer_with_hidden(RandomMat(3, 16), 8, 0)
  89. || test_gru_layer_with_hidden(RandomMat(2, 5), 17, 0);
  90. }
  91. static int test_gru_2()
  92. {
  93. return 0
  94. || test_gru(RandomMat(4, 1), 1, 0)
  95. || test_gru(RandomMat(8, 2), 2, 0)
  96. || test_gru(RandomMat(16, 8), 7, 0)
  97. || test_gru(RandomMat(17, 8), 8, 0)
  98. || test_gru(RandomMat(19, 15), 8, 0)
  99. || test_gru(RandomMat(5, 16), 16, 0)
  100. || test_gru(RandomMat(3, 16), 8, 0)
  101. || test_gru(RandomMat(8, 16), 16, 0)
  102. || test_gru(RandomMat(2, 5), 17, 0);
  103. }
  104. static int test_gru_3()
  105. {
  106. return 0
  107. || test_gru(RandomMat(4, 1), 1, 1)
  108. || test_gru(RandomMat(8, 2), 2, 1)
  109. || test_gru(RandomMat(16, 8), 7, 1)
  110. || test_gru(RandomMat(17, 8), 8, 1)
  111. || test_gru(RandomMat(19, 15), 8, 1)
  112. || test_gru(RandomMat(5, 16), 16, 1)
  113. || test_gru(RandomMat(3, 16), 8, 1)
  114. || test_gru(RandomMat(8, 16), 16, 1)
  115. || test_gru(RandomMat(2, 5), 17, 1);
  116. }
  117. int main()
  118. {
  119. SRAND(7767517);
  120. return test_gru_0() || test_gru_1() || test_gru_2() || test_gru_3();
  121. }