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_gemm.cpp 6.8 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/gemm.h"
  15. #include "testutil.h"
  16. static int test_gemm(int M, int N, int K, float alpha, int transA, int transB)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, alpha);
  20. pd.set(1, 1.f); // beta
  21. pd.set(2, transA);
  22. pd.set(3, transB);
  23. std::vector<ncnn::Mat> weights(0);
  24. std::vector<ncnn::Mat> a(2);
  25. a[0] = transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M);
  26. a[1] = transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K);
  27. Randomize(a[0]);
  28. Randomize(a[1]);
  29. int ret = test_layer<ncnn::Gemm>("Gemm", pd, weights, a);
  30. if (ret != 0)
  31. {
  32. fprintf(stderr, "test_gemm failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d\n", M, N, K, alpha, transA, transB);
  33. }
  34. return ret;
  35. }
  36. static int test_gemm_bias(int M, int N, int K, const ncnn::Mat& C, float alpha, float beta, int transA, int transB)
  37. {
  38. ncnn::ParamDict pd;
  39. pd.set(0, alpha);
  40. pd.set(1, beta);
  41. pd.set(2, transA);
  42. pd.set(3, transB);
  43. std::vector<ncnn::Mat> weights(0);
  44. std::vector<ncnn::Mat> a(3);
  45. a[0] = transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M);
  46. a[1] = transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K);
  47. a[2] = C;
  48. Randomize(a[0]);
  49. Randomize(a[1]);
  50. int ret = test_layer<ncnn::Gemm>("Gemm", pd, weights, a);
  51. if (ret != 0)
  52. {
  53. fprintf(stderr, "test_gemm_bias failed M=%d N=%d K=%d C.dims=%d C=(%d %d %d) alpha=%f transA=%d transB=%d\n", M, N, K, C.dims, C.w, C.h, C.c, alpha, transA, transB);
  54. }
  55. return ret;
  56. }
  57. static int test_gemm_0()
  58. {
  59. return 0
  60. || test_gemm(13, 14, 15, 0.1f, 0, 0)
  61. || test_gemm(13, 14, 15, 0.3f, 1, 0)
  62. || test_gemm(13, 14, 15, -0.4f, 0, 1)
  63. || test_gemm(13, 14, 15, 1.7f, 1, 1)
  64. || test_gemm(16, 24, 15, 0.1f, 0, 0)
  65. || test_gemm(16, 24, 15, 0.3f, 1, 0)
  66. || test_gemm(16, 24, 15, -0.4f, 0, 1)
  67. || test_gemm(16, 24, 15, 1.7f, 1, 1);
  68. }
  69. static int test_gemm_1()
  70. {
  71. return 0
  72. || test_gemm_bias(13, 14, 15, RandomMat(1), 0.1f, 0.2f, 0, 0)
  73. || test_gemm_bias(13, 14, 15, RandomMat(1), 0.4f, -1.2f, 1, 0)
  74. || test_gemm_bias(13, 14, 15, RandomMat(1), -0.3f, 3.f, 0, 1)
  75. || test_gemm_bias(13, 14, 15, RandomMat(1), 1.7f, 1.f, 1, 1)
  76. || test_gemm_bias(16, 24, 15, RandomMat(1), 0.1f, 0.2f, 0, 0)
  77. || test_gemm_bias(16, 24, 15, RandomMat(1), 0.4f, -1.2f, 1, 0)
  78. || test_gemm_bias(16, 24, 15, RandomMat(1), -0.3f, 3.f, 0, 1)
  79. || test_gemm_bias(16, 24, 15, RandomMat(1), 1.7f, 1.f, 1, 1);
  80. }
  81. static int test_gemm_2()
  82. {
  83. return 0
  84. || test_gemm_bias(13, 14, 15, RandomMat(13), 0.1f, 1.f, 0, 0)
  85. || test_gemm_bias(13, 14, 15, RandomMat(13), 0.4f, 2.f, 1, 0)
  86. || test_gemm_bias(13, 14, 15, RandomMat(13), -0.3f, 0.11f, 0, 1)
  87. || test_gemm_bias(13, 14, 15, RandomMat(13), 1.7f, -20.f, 1, 1)
  88. || test_gemm_bias(16, 24, 15, RandomMat(13), 0.1f, 1.f, 0, 0)
  89. || test_gemm_bias(16, 24, 15, RandomMat(13), 0.4f, 2.f, 1, 0)
  90. || test_gemm_bias(16, 24, 15, RandomMat(13), -0.3f, 0.11f, 0, 1)
  91. || test_gemm_bias(16, 24, 15, RandomMat(13), 1.7f, -20.f, 1, 1);
  92. }
  93. static int test_gemm_3()
  94. {
  95. return 0
  96. || test_gemm_bias(13, 14, 15, RandomMat(13, 1), 0.1f, 4.f, 0, 0)
  97. || test_gemm_bias(13, 14, 15, RandomMat(13, 1), 0.4f, 1.f, 1, 0)
  98. || test_gemm_bias(13, 14, 15, RandomMat(13, 1), -0.3f, -0.01f, 0, 1)
  99. || test_gemm_bias(13, 14, 15, RandomMat(13, 1), 1.7f, 0.3f, 1, 1)
  100. || test_gemm_bias(16, 24, 15, RandomMat(13, 1), 0.1f, 4.f, 0, 0)
  101. || test_gemm_bias(16, 24, 15, RandomMat(13, 1), 0.4f, 1.f, 1, 0)
  102. || test_gemm_bias(16, 24, 15, RandomMat(13, 1), -0.3f, -0.01f, 0, 1)
  103. || test_gemm_bias(16, 24, 15, RandomMat(13, 1), 1.7f, 0.3f, 1, 1);
  104. }
  105. static int test_gemm_4()
  106. {
  107. return 0
  108. || test_gemm_bias(13, 14, 15, RandomMat(13, 14), 0.1f, 6.f, 0, 0)
  109. || test_gemm_bias(13, 14, 15, RandomMat(13, 14), 0.4f, 1.22f, 1, 0)
  110. || test_gemm_bias(13, 14, 15, RandomMat(13, 14), -0.3f, 1.01f, 0, 1)
  111. || test_gemm_bias(13, 14, 15, RandomMat(13, 14), 1.7f, 0.3f, 1, 1)
  112. || test_gemm_bias(16, 24, 15, RandomMat(13, 14), 0.1f, 6.f, 0, 0)
  113. || test_gemm_bias(16, 24, 15, RandomMat(13, 14), 0.4f, 1.22f, 1, 0)
  114. || test_gemm_bias(16, 24, 15, RandomMat(13, 14), -0.3f, 1.01f, 0, 1)
  115. || test_gemm_bias(16, 24, 15, RandomMat(13, 14), 1.7f, 0.3f, 1, 1);
  116. }
  117. static int test_gemm_5()
  118. {
  119. return 0
  120. || test_gemm_bias(13, 14, 15, RandomMat(1, 14), 0.1f, 0.4f, 0, 0)
  121. || test_gemm_bias(13, 14, 15, RandomMat(1, 14), 0.4f, -1.f, 1, 0)
  122. || test_gemm_bias(13, 14, 15, RandomMat(1, 14), -0.3f, -0.21f, 0, 1)
  123. || test_gemm_bias(13, 14, 15, RandomMat(1, 14), 1.7f, 1.3f, 1, 1)
  124. || test_gemm_bias(16, 24, 15, RandomMat(1, 14), 0.1f, 0.4f, 0, 0)
  125. || test_gemm_bias(16, 24, 15, RandomMat(1, 14), 0.4f, -1.f, 1, 0)
  126. || test_gemm_bias(16, 24, 15, RandomMat(1, 14), -0.3f, -0.21f, 0, 1)
  127. || test_gemm_bias(16, 24, 15, RandomMat(1, 14), 1.7f, 1.3f, 1, 1);
  128. }
  129. static int test_gemm_6()
  130. {
  131. return 0
  132. || test_gemm_bias(13, 14, 15, RandomMat(14), 0.1f, 0.4f, 0, 0)
  133. || test_gemm_bias(13, 14, 15, RandomMat(14), 0.4f, -1.f, 1, 0)
  134. || test_gemm_bias(13, 14, 15, RandomMat(14), -0.3f, -0.21f, 0, 1)
  135. || test_gemm_bias(13, 14, 15, RandomMat(14), 1.7f, 1.3f, 1, 1)
  136. || test_gemm_bias(16, 24, 15, RandomMat(14), 0.1f, 0.4f, 0, 0)
  137. || test_gemm_bias(16, 24, 15, RandomMat(14), 0.4f, -1.f, 1, 0)
  138. || test_gemm_bias(16, 24, 15, RandomMat(14), -0.3f, -0.21f, 0, 1)
  139. || test_gemm_bias(16, 24, 15, RandomMat(14), 1.7f, 1.3f, 1, 1);
  140. }
  141. int main()
  142. {
  143. SRAND(7767517);
  144. return 0
  145. || test_gemm_0()
  146. || test_gemm_1()
  147. || test_gemm_2()
  148. || test_gemm_3()
  149. || test_gemm_4()
  150. || test_gemm_5()
  151. || test_gemm_6();
  152. }