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_3.cpp 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2024 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. #if NCNN_INT8
  16. static void RandomizeA(ncnn::Mat& m, int transA, float absmax)
  17. {
  18. if (transA == 0)
  19. {
  20. const int h = m.dims == 3 ? m.c : m.h;
  21. for (int i = 0; i < h; i++)
  22. {
  23. float* p = m.dims == 3 ? m.channel(i) : m.row(i);
  24. float randabsmax = RandomFloat(absmax * 0.5f, absmax);
  25. randabsmax = ncnn::float16_to_float32(ncnn::float32_to_float16(randabsmax));
  26. randabsmax = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(randabsmax));
  27. for (int j = 0; j < m.w; j++)
  28. {
  29. p[j] = RandomFloat(-randabsmax, randabsmax);
  30. }
  31. // set random a and b
  32. p[RandomInt(0, m.w - 1)] = -randabsmax;
  33. p[RandomInt(0, m.w - 1)] = randabsmax;
  34. // drop 0.45 ~ 0.55
  35. for (int j = 0; j < m.w; j++)
  36. {
  37. float v = p[j] * (127.f / randabsmax);
  38. float vv = fabs(v - (int)v);
  39. float hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
  40. float hv = hp * (127.f / randabsmax);
  41. float hvv = fabs(hv - (int)hv);
  42. float bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
  43. float bv = bp * (127.f / randabsmax);
  44. float bvv = fabs(bv - (int)bv);
  45. while ((vv > 0.45f && vv < 0.55f) || (hvv > 0.45f && hvv < 0.55f) || (bvv > 0.45f && bvv < 0.55f))
  46. {
  47. p[j] = RandomFloat(-randabsmax, randabsmax);
  48. v = p[j] * (127.f / randabsmax);
  49. vv = fabs(v - (int)v);
  50. hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
  51. hv = hp * (127.f / randabsmax);
  52. hvv = fabs(hv - (int)hv);
  53. bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
  54. bv = bp * (127.f / randabsmax);
  55. bvv = fabs(bv - (int)bv);
  56. }
  57. }
  58. }
  59. }
  60. else // if (transA == 1)
  61. {
  62. std::vector<float> randabsmaxes(m.w);
  63. for (int j = 0; j < m.w; j++)
  64. {
  65. float randabsmax = RandomFloat(absmax * 0.5f, absmax);
  66. randabsmax = ncnn::float16_to_float32(ncnn::float32_to_float16(randabsmax));
  67. randabsmax = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(randabsmax));
  68. randabsmaxes[j] = randabsmax;
  69. }
  70. const int h = m.dims == 3 ? m.c : m.h;
  71. for (int i = 0; i < h; i++)
  72. {
  73. float* p = m.dims == 3 ? m.channel(i) : m.row(i);
  74. for (int j = 0; j < m.w; j++)
  75. {
  76. const float randabsmax = randabsmaxes[j];
  77. p[j] = RandomFloat(-randabsmax, randabsmax);
  78. }
  79. // drop 0.45 ~ 0.55
  80. for (int j = 0; j < m.w; j++)
  81. {
  82. const float randabsmax = randabsmaxes[j];
  83. float v = p[j] * (127.f / randabsmax);
  84. float vv = fabs(v - (int)v);
  85. float hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
  86. float hv = hp * (127.f / randabsmax);
  87. float hvv = fabs(hv - (int)hv);
  88. float bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
  89. float bv = bp * (127.f / randabsmax);
  90. float bvv = fabs(bv - (int)bv);
  91. while ((vv > 0.45f && vv < 0.55f) || (hvv > 0.45f && hvv < 0.55f) || (bvv > 0.45f && bvv < 0.55f))
  92. {
  93. p[j] = RandomFloat(-randabsmax, randabsmax);
  94. v = p[j] * (127.f / randabsmax);
  95. vv = fabs(v - (int)v);
  96. hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
  97. hv = hp * (127.f / randabsmax);
  98. hvv = fabs(hv - (int)hv);
  99. bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
  100. bv = bp * (127.f / randabsmax);
  101. bvv = fabs(bv - (int)bv);
  102. }
  103. }
  104. }
  105. for (int j = 0; j < m.w; j++)
  106. {
  107. const int randi0 = RandomInt(0, h - 1);
  108. const int randi1 = RandomInt(0, h - 1);
  109. float* p0 = m.dims == 3 ? m.channel(randi0) : m.row(randi0);
  110. float* p1 = m.dims == 3 ? m.channel(randi1) : m.row(randi1);
  111. const float randabsmax = randabsmaxes[j];
  112. // set random a and b
  113. p0[j] = -randabsmax;
  114. p1[j] = randabsmax;
  115. }
  116. }
  117. }
  118. static void RandomizeB(ncnn::Mat& m, float absmax)
  119. {
  120. absmax = ncnn::float16_to_float32(ncnn::float32_to_float16(absmax));
  121. absmax = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(absmax));
  122. const int h = m.dims == 3 ? m.c : m.h;
  123. float* p = m;
  124. for (int i = 0; i < h; i++)
  125. {
  126. float* p = m.dims == 3 ? m.channel(i) : m.row(i);
  127. for (int j = 0; j < m.w; j++)
  128. {
  129. p[j] = RandomFloat(-absmax, absmax);
  130. // drop 0.45 ~ 0.55
  131. float v = p[j] * (127.f / absmax);
  132. float vv = fabs(v - (int)v);
  133. float hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
  134. float hv = hp * (127.f / absmax);
  135. float hvv = fabs(hv - (int)hv);
  136. float bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
  137. float bv = bp * (127.f / absmax);
  138. float bvv = fabs(bv - (int)bv);
  139. while ((vv > 0.45f && vv < 0.55f) || (hvv > 0.45f && hvv < 0.55f) || (bvv > 0.45f && bvv < 0.55f))
  140. {
  141. p[j] = RandomFloat(-absmax, absmax);
  142. v = p[j] * (127.f / absmax);
  143. vv = fabs(v - (int)v);
  144. hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
  145. hv = hp * (127.f / absmax);
  146. hvv = fabs(hv - (int)hv);
  147. bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
  148. bv = bp * (127.f / absmax);
  149. bvv = fabs(bv - (int)bv);
  150. }
  151. }
  152. }
  153. // set random a and b
  154. if (m.dims == 3)
  155. {
  156. m.channel(RandomInt(0, h - 1))[RandomInt(0, m.w - 1)] = -absmax;
  157. m.channel(RandomInt(0, h - 1))[RandomInt(0, m.w - 1)] = absmax;
  158. }
  159. else
  160. {
  161. m.row(RandomInt(0, h - 1))[RandomInt(0, m.w - 1)] = -absmax;
  162. m.row(RandomInt(0, h - 1))[RandomInt(0, m.w - 1)] = absmax;
  163. }
  164. }
  165. static int test_gemm_int8(int M, int N, int K, float alpha, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int output_N1M)
  166. {
  167. ncnn::ParamDict pd;
  168. pd.set(0, alpha);
  169. pd.set(1, 1.f); // beta
  170. pd.set(2, transA);
  171. pd.set(3, transB);
  172. pd.set(4, constantA);
  173. pd.set(5, constantB);
  174. pd.set(6, 1);
  175. pd.set(7, M);
  176. pd.set(8, N);
  177. pd.set(9, K);
  178. pd.set(10, -1);
  179. pd.set(11, output_N1M);
  180. pd.set(13, output_elemtype);
  181. pd.set(14, output_transpose);
  182. pd.set(18, 2); // int8_scale_term
  183. std::vector<ncnn::Mat> weights;
  184. if (constantA) weights.push_back(transA ? RandomS8Mat(M, K) : RandomS8Mat(K, M));
  185. if (constantB) weights.push_back(transB ? RandomS8Mat(K, N) : RandomS8Mat(N, K));
  186. if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f));
  187. if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f));
  188. std::vector<ncnn::Mat> a;
  189. if (!constantA)
  190. {
  191. a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M)));
  192. RandomizeA(a[a.size() - 1], transA, 10.f);
  193. }
  194. if (!constantB)
  195. {
  196. a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K)));
  197. RandomizeB(a[a.size() - 1], 10.f);
  198. }
  199. int ret = test_layer("Gemm", pd, weights, a);
  200. if (ret != 0)
  201. {
  202. fprintf(stderr, "test_gemm_int8 failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d output_N1M=%d\n", M, N, K, alpha, transA, transB, output_elemtype, output_transpose, constantA, constantB, output_N1M);
  203. }
  204. return ret;
  205. }
  206. static int test_gemm_int8_bias(int M, int N, int K, const ncnn::Mat& C, float alpha, float beta, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int constantC)
  207. {
  208. int broadcast_type_C = 0;
  209. if (C.dims == 1 && C.w == 1)
  210. {
  211. // scalar
  212. broadcast_type_C = 0;
  213. }
  214. if (C.dims == 1 && C.w == M)
  215. {
  216. // M
  217. // auto broadcast from h to w is the ncnn-style convention
  218. broadcast_type_C = 1;
  219. }
  220. if (C.dims == 1 && C.w == N)
  221. {
  222. // N
  223. broadcast_type_C = 4;
  224. }
  225. if (C.dims == 2 && C.w == 1 && C.h == M)
  226. {
  227. // Mx1
  228. broadcast_type_C = 2;
  229. }
  230. if (C.dims == 2 && C.w == N && C.h == M)
  231. {
  232. // MxN
  233. broadcast_type_C = 3;
  234. }
  235. if (C.dims == 2 && C.w == N && C.h == 1)
  236. {
  237. // 1xN
  238. broadcast_type_C = 4;
  239. }
  240. ncnn::ParamDict pd;
  241. pd.set(0, alpha);
  242. pd.set(1, beta);
  243. pd.set(2, transA);
  244. pd.set(3, transB);
  245. pd.set(4, constantA);
  246. pd.set(5, constantB);
  247. pd.set(6, constantC);
  248. pd.set(7, M);
  249. pd.set(8, N);
  250. pd.set(9, K);
  251. pd.set(10, broadcast_type_C);
  252. // pd.set(12, 1); // output_elempack
  253. pd.set(13, output_elemtype);
  254. pd.set(14, output_transpose);
  255. pd.set(18, 2); // int8_scale_term
  256. std::vector<ncnn::Mat> weights;
  257. if (constantA) weights.push_back(transA ? RandomS8Mat(M, K) : RandomS8Mat(K, M));
  258. if (constantB) weights.push_back(transB ? RandomS8Mat(K, N) : RandomS8Mat(N, K));
  259. if (constantC) weights.push_back(C);
  260. if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f));
  261. if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f));
  262. std::vector<ncnn::Mat> a;
  263. if (!constantA)
  264. {
  265. a.push_back(transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M));
  266. RandomizeA(a[a.size() - 1], transA, 10.f);
  267. }
  268. if (!constantB)
  269. {
  270. a.push_back(transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K));
  271. RandomizeB(a[a.size() - 1], 10.f);
  272. }
  273. if (!constantC) a.push_back(C);
  274. int ret = test_layer("Gemm", pd, weights, a);
  275. if (ret != 0)
  276. {
  277. fprintf(stderr, "test_gemm_int8_bias failed M=%d N=%d K=%d C.dims=%d C=(%d %d %d) alpha=%f beta=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d constantC=%d\n", M, N, K, C.dims, C.w, C.h, C.c, alpha, beta, transA, transB, output_elemtype, output_transpose, constantA, constantB, constantC);
  278. }
  279. return ret;
  280. }
  281. static int test_gemm_int8_fp16s(int M, int N, int K, float alpha, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int output_N1M)
  282. {
  283. ncnn::ParamDict pd;
  284. pd.set(0, alpha);
  285. pd.set(1, 1.f); // beta
  286. pd.set(2, transA);
  287. pd.set(3, transB);
  288. pd.set(4, constantA);
  289. pd.set(5, constantB);
  290. pd.set(6, 1);
  291. pd.set(7, M);
  292. pd.set(8, N);
  293. pd.set(9, K);
  294. pd.set(10, -1);
  295. pd.set(11, output_N1M);
  296. pd.set(13, output_elemtype);
  297. pd.set(14, output_transpose);
  298. pd.set(18, 2); // int8_scale_term
  299. std::vector<ncnn::Mat> weights;
  300. if (constantA) weights.push_back(transA ? RandomS8Mat(M, K) : RandomS8Mat(K, M));
  301. if (constantB) weights.push_back(transB ? RandomS8Mat(K, N) : RandomS8Mat(N, K));
  302. if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f));
  303. if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f));
  304. std::vector<ncnn::Mat> a;
  305. if (!constantA)
  306. {
  307. a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M)));
  308. RandomizeA(a[a.size() - 1], transA, 10.f);
  309. }
  310. if (!constantB)
  311. {
  312. a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K)));
  313. RandomizeB(a[a.size() - 1], 10.f);
  314. }
  315. ncnn::Option opt;
  316. opt.num_threads = 1;
  317. opt.use_packing_layout = true;
  318. opt.use_fp16_packed = false;
  319. opt.use_fp16_storage = true;
  320. opt.use_fp16_arithmetic = false;
  321. opt.use_bf16_storage = false;
  322. float epsilon = 0.001;
  323. int ret = test_layer_opt("Gemm", pd, weights, opt, a, 1, epsilon);
  324. if (ret != 0)
  325. {
  326. fprintf(stderr, "test_gemm_int8_fp16s failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d output_N1M=%d\n", M, N, K, alpha, transA, transB, output_elemtype, output_transpose, constantA, constantB, output_N1M);
  327. return ret;
  328. }
  329. return 0;
  330. }
  331. static int test_gemm_0(int M, int N, int K)
  332. {
  333. return 0
  334. || test_gemm_int8(M, N, K, 2.1f, 0, 1, 0, 0, 0, 0, 0)
  335. || test_gemm_int8(M, N, K, 3.1f, 1, 1, 0, 0, 0, 0, 0)
  336. || test_gemm_int8(M, N, K, 4.1f, 0, 0, 0, 0, 0, 0, 1)
  337. || test_gemm_int8(M, N, K, 5.1f, 1, 0, 0, 0, 0, 0, 1)
  338. || test_gemm_int8(M, N, K, 0.2f, 0, 1, 0, 0, 1, 0, 1)
  339. || test_gemm_int8(M, N, K, 0.3f, 1, 1, 0, 0, 1, 0, 1)
  340. || test_gemm_int8(M, N, K, 0.4f, 0, 0, 0, 0, 0, 1, 0)
  341. || test_gemm_int8(M, N, K, 0.5f, 0, 1, 0, 0, 0, 1, 0)
  342. || test_gemm_int8(M, N, K, 1.2f, 0, 1, 0, 0, 1, 1, 0)
  343. || test_gemm_int8(M, N, K, 1.3f, 1, 1, 0, 0, 1, 1, 1)
  344. || test_gemm_int8(M, N, K, 1.4f, 0, 0, 0, 0, 1, 1, 0)
  345. || test_gemm_int8(M, N, K, 1.5f, 1, 0, 0, 0, 1, 1, 1)
  346. || test_gemm_int8(M, N, K, -1.2f, 0, 1, 0, 1, 0, 0, 0)
  347. || test_gemm_int8(M, N, K, -1.3f, 1, 1, 0, 1, 0, 0, 0)
  348. || test_gemm_int8(M, N, K, -1.4f, 0, 0, 0, 1, 0, 0, 1)
  349. || test_gemm_int8(M, N, K, -1.5f, 1, 0, 0, 1, 0, 0, 1)
  350. || test_gemm_int8(M, N, K, -2.0f, 0, 1, 0, 1, 1, 0, 1)
  351. || test_gemm_int8(M, N, K, -3.0f, 1, 1, 0, 1, 1, 0, 1)
  352. || test_gemm_int8(M, N, K, -4.0f, 0, 0, 0, 1, 0, 1, 0)
  353. || test_gemm_int8(M, N, K, -5.0f, 0, 1, 0, 1, 0, 1, 0)
  354. || test_gemm_int8(M, N, K, -2.1f, 0, 1, 0, 1, 1, 1, 0)
  355. || test_gemm_int8(M, N, K, -3.1f, 1, 1, 0, 1, 1, 1, 1)
  356. || test_gemm_int8(M, N, K, -4.1f, 0, 0, 0, 1, 1, 1, 0)
  357. || test_gemm_int8(M, N, K, -5.1f, 1, 0, 0, 1, 1, 1, 1)
  358. || test_gemm_int8_fp16s(M, N, K, 1.f, 0, 1, 0, 0, 0, 0, 0)
  359. || test_gemm_int8_fp16s(M, N, K, 1.f, 1, 0, 0, 1, 0, 0, 0);
  360. }
  361. static int test_gemm_1(int M, int N, int K)
  362. {
  363. return 0
  364. || test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 0, 0, 0, 0)
  365. || test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 1, 1, 0, 0, 0)
  366. || test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 2, 0, 0, 0, 0)
  367. || test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 3, 1, 0, 0, 0)
  368. || test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 0, 0, 0, 0, 0)
  369. || test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 1, 0, 0, 0)
  370. || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 2, 0, 0, 0, 0)
  371. || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 3, 1, 0, 0, 0)
  372. || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 0, 0, 0, 0, 0)
  373. || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 1, 1, 0, 0, 0)
  374. || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 2, 0, 0, 0, 0)
  375. || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 3, 1, 0, 0, 0)
  376. || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 0.8f, 1.f, 0, 0, 0, 0, 0, 0, 0)
  377. || test_gemm_int8_bias(M, N, K, RandomMat(N), 0.8f, 1.f, 0, 0, 1, 1, 0, 0, 0)
  378. || test_gemm_int8_bias(M, N, K, RandomMat(N), 3.1f, -0.6f, 0, 1, 2, 0, 0, 0, 0)
  379. || test_gemm_int8_bias(M, N, K, RandomMat(N), 3.1f, -0.6f, 0, 1, 3, 1, 0, 0, 0)
  380. || test_gemm_int8_bias(M, N, K, RandomMat(1), -2.1f, 0.5f, 0, 0, 0, 0, 1, 1, 1)
  381. || test_gemm_int8_bias(M, N, K, RandomMat(1), -2.1f, 0.5f, 0, 0, 1, 1, 1, 1, 1)
  382. || test_gemm_int8_bias(M, N, K, RandomMat(M), -3.1f, 0.6f, 0, 1, 2, 0, 1, 1, 1)
  383. || test_gemm_int8_bias(M, N, K, RandomMat(M), -3.1f, 0.6f, 0, 1, 3, 1, 1, 1, 1)
  384. || test_gemm_int8_bias(M, N, K, RandomMat(1, M), -4.1f, 0.7f, 1, 0, 0, 0, 1, 1, 1)
  385. || test_gemm_int8_bias(M, N, K, RandomMat(1, M), -4.1f, 0.7f, 1, 0, 1, 1, 1, 1, 1)
  386. || test_gemm_int8_bias(M, N, K, RandomMat(N, M), -5.1f, -0.8f, 1, 1, 2, 0, 1, 1, 1)
  387. || test_gemm_int8_bias(M, N, K, RandomMat(N, M), -5.1f, -0.8f, 1, 1, 3, 1, 1, 1, 1)
  388. || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 0, 0, 1, 1, 1)
  389. || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 1, 1, 1, 1, 1)
  390. || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), -2.1f, -0.5f, 0, 0, 2, 0, 1, 1, 1)
  391. || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), -2.1f, -0.5f, 0, 0, 3, 1, 1, 1, 1)
  392. || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 0.8f, 1.f, 0, 0, 0, 0, 1, 1, 1)
  393. || test_gemm_int8_bias(M, N, K, RandomMat(N), 0.8f, 1.f, 0, 0, 1, 1, 1, 1, 1)
  394. || test_gemm_int8_bias(M, N, K, RandomMat(N), -3.1f, -0.6f, 0, 1, 2, 0, 1, 1, 1)
  395. || test_gemm_int8_bias(M, N, K, RandomMat(N), -3.1f, -0.6f, 0, 1, 3, 1, 1, 1, 1);
  396. }
  397. #endif // NCNN_INT8
  398. int main()
  399. {
  400. SRAND(7767517);
  401. #if NCNN_INT8
  402. int mnk[][3] = {
  403. {1, 1, 1},
  404. {1, 1, 23},
  405. {1, 1, 47},
  406. {1, 23, 1},
  407. {1, 23, 23},
  408. {1, 31, 1},
  409. {1, 35, 1},
  410. {1, 35, 47},
  411. {1, 47, 1},
  412. {2, 2, 2},
  413. {3, 3, 3},
  414. {4, 4, 4},
  415. {5, 5, 5},
  416. {6, 6, 6},
  417. {7, 7, 7},
  418. {7, 31, 3},
  419. {8, 8, 8},
  420. {12, 12, 23},
  421. {12, 23, 12},
  422. {12, 31, 12},
  423. {15, 15, 15},
  424. {16, 16, 16},
  425. {19, 44, 7},
  426. {20, 28, 7},
  427. {23, 31, 1},
  428. {23, 31, 23},
  429. {24, 24, 47},
  430. {24, 35, 24},
  431. {24, 47, 24},
  432. {31, 31, 31},
  433. {32, 32, 9},
  434. {35, 47, 48},
  435. {35, 48, 47},
  436. {40, 40, 40},
  437. {47, 48, 47}
  438. };
  439. int mnk_count = sizeof(mnk) / sizeof(int) / 3;
  440. for (int i = 0; i < mnk_count; i++)
  441. {
  442. int M = mnk[i][0];
  443. int N = mnk[i][1];
  444. int K = mnk[i][2];
  445. int ret = test_gemm_0(M, N, K) || test_gemm_1(M, N, K);
  446. if (ret != 0)
  447. return ret;
  448. if (M != N)
  449. {
  450. int ret = test_gemm_0(N, M, K) || test_gemm_1(N, M, K);
  451. if (ret != 0)
  452. return ret;
  453. }
  454. }
  455. #else
  456. // test nothing for non-int8 build
  457. #endif
  458. return 0;
  459. }