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_1.cpp 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2023 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, int TILE_M, int TILE_N, int TILE_K, float alpha, int transA, int transB, int output_transpose)
  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. pd.set(14, output_transpose);
  24. pd.set(20, TILE_M);
  25. pd.set(21, TILE_N);
  26. pd.set(22, TILE_K);
  27. std::vector<ncnn::Mat> weights(0);
  28. std::vector<ncnn::Mat> a(2);
  29. a[0] = transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M);
  30. a[1] = transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K);
  31. Randomize(a[0]);
  32. Randomize(a[1]);
  33. int ret = test_layer<ncnn::Gemm>("Gemm", pd, weights, a);
  34. if (ret != 0)
  35. {
  36. fprintf(stderr, "test_gemm failed M=%d N=%d K=%d TILE_M=%d TILE_N=%d TILE_K=%d alpha=%f transA=%d transB=%d output_transpose=%d\n", M, N, K, TILE_M, TILE_N, TILE_K, alpha, transA, transB, output_transpose);
  37. }
  38. return ret;
  39. }
  40. static int test_gemm_0(int M, int N, int K, int TILE_M, int TILE_N, int TILE_K)
  41. {
  42. return 0
  43. || test_gemm(M, N, K, TILE_M, TILE_N, TILE_K, 2.1f, 0, 0, 0)
  44. || test_gemm(M, N, K, TILE_M, TILE_N, TILE_K, 3.1f, 0, 1, 0)
  45. || test_gemm(M, N, K, TILE_M, TILE_N, TILE_K, 4.1f, 1, 0, 0)
  46. || test_gemm(M, N, K, TILE_M, TILE_N, TILE_K, 5.1f, 1, 1, 0)
  47. || test_gemm(M, N, K, TILE_M, TILE_N, TILE_K, 2.1f, 0, 0, 1)
  48. || test_gemm(M, N, K, TILE_M, TILE_N, TILE_K, 3.1f, 0, 1, 1)
  49. || test_gemm(M, N, K, TILE_M, TILE_N, TILE_K, 4.1f, 1, 0, 1)
  50. || test_gemm(M, N, K, TILE_M, TILE_N, TILE_K, 5.1f, 1, 1, 1);
  51. }
  52. int main()
  53. {
  54. SRAND(7767517);
  55. int mnk[][3] = {
  56. {1, 1, 1},
  57. {2, 2, 2},
  58. {3, 3, 3},
  59. {4, 4, 4},
  60. {5, 5, 5},
  61. {6, 6, 6},
  62. {7, 7, 7},
  63. {8, 8, 8},
  64. {15, 15, 15},
  65. {16, 16, 16},
  66. {24, 24, 24},
  67. {31, 31, 31},
  68. {31, 32, 31},
  69. {32, 31, 32},
  70. {32, 32, 32},
  71. {20, 32, 20},
  72. {40, 40, 40},
  73. {47, 47, 47},
  74. {48, 48, 48},
  75. {52, 52, 52},
  76. {63, 64, 63},
  77. {64, 63, 64},
  78. {64, 64, 64}
  79. };
  80. int tile_mnk[][3] = {
  81. {1, 1, 1},
  82. {2, 2, 2},
  83. {4, 4, 4},
  84. {8, 8, 8},
  85. {12, 12, 12},
  86. {16, 16, 16},
  87. {20, 20, 20},
  88. {24, 24, 24},
  89. {28, 28, 28}
  90. };
  91. int mnk_count = sizeof(mnk) / sizeof(int) / 3;
  92. int tile_mnk_count = sizeof(tile_mnk) / sizeof(int) / 3;
  93. for (int i = 0; i < mnk_count; i++)
  94. {
  95. int M = mnk[i][0];
  96. int N = mnk[i][1];
  97. int K = mnk[i][2];
  98. for (int j = 0; j < tile_mnk_count; j++)
  99. {
  100. int TILE_M = tile_mnk[j][0];
  101. int TILE_N = tile_mnk[j][1];
  102. int TILE_K = tile_mnk[j][2];
  103. if (TILE_M >= M && TILE_N >= N && TILE_K >= K)
  104. continue;
  105. int ret = test_gemm_0(M, N, K, TILE_M, TILE_N, TILE_K);
  106. if (ret != 0)
  107. return 0;
  108. }
  109. // test no tiling
  110. int ret = test_gemm_0(M, N, K, 100, 100, 100);
  111. if (ret != 0)
  112. return 0;
  113. }
  114. return 0;
  115. }