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

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