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.

matrix_mul.cpp 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * \file dnn/test/naive/matrix_mul.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "test/naive/fixture.h"
  12. #include "megdnn/oprs/linalg.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/extra_impl_helper.h"
  15. #include "test/common/matrix_mul.h"
  16. #include "test/common/random_state.h"
  17. using namespace megdnn;
  18. using namespace test;
  19. namespace {
  20. void run_matmul_mk_format(
  21. Handle* handle, param::MatrixMul::Format format, DType Atype, DType Btype,
  22. DType Ctype) {
  23. using namespace matrix_mul;
  24. std::vector<TestArg> args = get_matmul_args();
  25. Checker<MatrixMul> checker(handle);
  26. auto extra_impl = [](const TensorNDArray& tensors, param::MatrixMul param,
  27. Handle* handle, size_t pack_size) {
  28. megdnn_assert(
  29. (param.format == param::MatrixMul::Format::MK4 ||
  30. param.format == param::MatrixMul::Format::MK4_DOT ||
  31. param.format == param::MatrixMul::Format::MK8) &&
  32. tensors.size() == 3);
  33. param::MatrixMul new_param = param;
  34. new_param.format = param::MatrixMul::Format::DEFAULT;
  35. size_t M = tensors[2].layout[0] * pack_size;
  36. size_t N = tensors[2].layout[1];
  37. size_t K = tensors[0].layout[1 - param.transposeA] * pack_size;
  38. TensorLayoutArray default_layouts, mk4_layouts;
  39. if (param.transposeA) {
  40. default_layouts.emplace_back(tensors[0].layout.reshape({K, M}));
  41. if (param.format == param::MatrixMul::Format::MK4_DOT) {
  42. mk4_layouts.emplace_back(default_layouts.back()
  43. .reshape(
  44. {K / pack_size, M / pack_size,
  45. pack_size, pack_size})
  46. .dimshuffle({0, 3, 1, 2}));
  47. } else {
  48. mk4_layouts.emplace_back(default_layouts.back()
  49. .reshape(
  50. {K / pack_size, M / pack_size,
  51. pack_size, pack_size})
  52. .dimshuffle({0, 2, 1, 3}));
  53. }
  54. } else {
  55. default_layouts.emplace_back(tensors[0].layout.reshape({M, K}));
  56. if (param.format == param::MatrixMul::Format::MK4_DOT) {
  57. mk4_layouts.emplace_back(default_layouts.back()
  58. .reshape(
  59. {M / pack_size, K / pack_size,
  60. pack_size, pack_size})
  61. .dimshuffle({0, 2, 1, 3}));
  62. } else {
  63. mk4_layouts.emplace_back(default_layouts.back()
  64. .reshape(
  65. {M / pack_size, K / pack_size,
  66. pack_size, pack_size})
  67. .dimshuffle({0, 3, 1, 2}));
  68. }
  69. }
  70. if (param.transposeB) {
  71. default_layouts.emplace_back(tensors[1].layout.reshape({N, K}));
  72. mk4_layouts.emplace_back(default_layouts.back()
  73. .reshape({N, K / pack_size, pack_size})
  74. .dimshuffle({0, 1, 2}));
  75. } else {
  76. default_layouts.emplace_back(tensors[1].layout.reshape({K, N}));
  77. mk4_layouts.emplace_back(default_layouts.back()
  78. .reshape({K / pack_size, N, pack_size})
  79. .dimshuffle({0, 2, 1}));
  80. }
  81. default_layouts.emplace_back(tensors[2].layout.reshape({M, N}));
  82. mk4_layouts.emplace_back(default_layouts.back()
  83. .reshape({M / pack_size, N, pack_size})
  84. .dimshuffle({0, 2, 1}));
  85. auto matmul_opr = handle->create_operator<MatrixMul>();
  86. matmul_opr->param() = new_param;
  87. size_t matmul_workspace = matmul_opr->get_workspace_in_bytes(
  88. default_layouts[0], default_layouts[1], default_layouts[2]);
  89. auto relayout_opr = handle->create_operator<Relayout>();
  90. WorkspaceBundle wb(
  91. nullptr, {default_layouts[0].span().dist_byte(),
  92. default_layouts[1].span().dist_byte(),
  93. default_layouts[2].span().dist_byte(), matmul_workspace});
  94. wb.set(malloc(wb.total_size_in_bytes()));
  95. TensorNDArray default_tensors, mk4_tensors;
  96. for (size_t i = 0; i < 3; i++) {
  97. default_tensors.emplace_back(wb.get(i), default_layouts[i]);
  98. mk4_tensors.emplace_back(tensors[i].raw_ptr(), mk4_layouts[i]);
  99. }
  100. relayout_opr->exec(mk4_tensors[0], default_tensors[0]);
  101. relayout_opr->exec(mk4_tensors[1], default_tensors[1]);
  102. matmul_opr->exec(
  103. default_tensors[0], default_tensors[1], default_tensors[2],
  104. wb.get_workspace(3));
  105. relayout_opr->exec(default_tensors[2], mk4_tensors[2]);
  106. free(wb.ptr());
  107. };
  108. size_t pack_size = MatrixMulForward::pack_size(format);
  109. for (auto&& arg : args) {
  110. if (arg.m % pack_size != 0 || arg.k % pack_size != 0)
  111. continue;
  112. param::MatrixMul param;
  113. param.transposeA = arg.mask & 0x1;
  114. param.transposeB = arg.mask & 0x2;
  115. param.format = format;
  116. size_t m = arg.m, n = arg.n, k = arg.k;
  117. TensorShape A, B;
  118. if (param.transposeA) {
  119. A = TensorShape{k / pack_size, m / pack_size, pack_size, pack_size};
  120. } else {
  121. A = TensorShape{m / pack_size, k / pack_size, pack_size, pack_size};
  122. }
  123. if (param.transposeB) {
  124. B = TensorShape{n, k / pack_size, pack_size};
  125. } else {
  126. B = TensorShape{k / pack_size, n, pack_size};
  127. }
  128. checker.set_extra_opr_impl(
  129. std::bind(extra_impl, std::placeholders::_1, param, handle, pack_size));
  130. checker.set_dtype(0, Atype)
  131. .set_dtype(1, Btype)
  132. .set_dtype(2, Ctype)
  133. .set_epsilon(1e-3)
  134. .set_param(param)
  135. .execs({A, B, {}});
  136. }
  137. }
  138. } // namespace
  139. TEST_F(NAIVE, MATRIX_MUL_QUANTIZED4x4x32) {
  140. Checker<MatrixMul> checker(handle(), /* check_dispatch */ false);
  141. auto GenTensorValueQuint4 = [](const TensorShape& shape,
  142. dtype::Quantized4Asymm dtype,
  143. const std::vector<int>& values) {
  144. TensorND tensor;
  145. tensor.layout = {shape, dtype};
  146. tensor.reset_ptr(
  147. static_cast<dt_byte*>(malloc(tensor.layout.span().dist_byte())));
  148. uint8_t* ptr = static_cast<uint8_t*>(tensor.raw_ptr());
  149. megdnn_assert(values.size() == tensor.layout.span().dist_elem());
  150. for (size_t i = 0; i < tensor.layout.span().dist_elem(); i += 2) {
  151. int val0 = values[i], val1 = values[i + 1];
  152. ptr[i / 2] = val0 | (val1 << 4);
  153. }
  154. return tensor;
  155. };
  156. using Param = MatrixMul::Param;
  157. Param param;
  158. checker.set_param(param);
  159. checker.set_dtype(2, dtype::QuantizedS32(0.3f * 0.3f));
  160. checker.exect(
  161. Testcase{
  162. GenTensorValueQuint4(
  163. {8, 8}, dtype::Quantized4Asymm(0.3f, (uint8_t)8),
  164. {13, 2, 4, 13, 9, 3, 14, 14, 14, 5, 3, 3, 15,
  165. 11, 8, 8, 5, 7, 14, 15, 8, 2, 11, 1, 15, 9,
  166. 13, 14, 2, 3, 11, 11, 15, 10, 11, 0, 13, 12, 3,
  167. 11, 9, 9, 10, 5, 2, 5, 8, 4, 6, 9, 0, 0,
  168. 3, 9, 9, 8, 8, 15, 7, 5, 0, 3, 9, 10}),
  169. GenTensorValueQuint4(
  170. {8, 8}, dtype::Quantized4Asymm(0.3f, (uint8_t)8),
  171. {5, 14, 13, 11, 4, 7, 12, 12, 11, 7, 13, 10, 5,
  172. 6, 4, 2, 3, 12, 2, 2, 13, 3, 14, 0, 15, 15,
  173. 0, 2, 2, 13, 3, 14, 10, 8, 9, 11, 0, 14, 15,
  174. 4, 14, 7, 1, 6, 13, 2, 12, 5, 2, 15, 7, 11,
  175. 13, 9, 8, 10, 0, 11, 6, 10, 12, 2, 2, 12}),
  176. {}},
  177. Testcase{
  178. {},
  179. {},
  180. TensorValue(
  181. {8, 8}, dtype::QuantizedS32(0.3f * 0.3f),
  182. {-90, 120, -3, 40, -31, 58, -54, 165, -5, -19, 71,
  183. 87, -51, 24, 92, 15, 27, 62, -59, -82, -40, 91,
  184. 11, -16, -85, 138, -18, -36, 8, -25, -56, 75, -46,
  185. -34, 67, 53, -4, -83, 111, -86, -29, -17, 45, -9,
  186. 38, -22, -3, -19, -17, -95, 94, 78, 63, -35, -51,
  187. 21, -63, -14, 87, 31, 44, -53, -107, 5}),
  188. });
  189. }
  190. TEST_F(NAIVE, MATRIX_MUL_QUANTIZEDS4_4x4x16) {
  191. Checker<MatrixMul> checker(handle(), /* check_dispatch */ false);
  192. auto GenTensorValueQuint4 = [](const TensorShape& shape, dtype::QuantizedS4 dtype,
  193. const std::vector<int>& values) {
  194. TensorND tensor;
  195. tensor.layout = {shape, dtype};
  196. tensor.reset_ptr(
  197. static_cast<dt_byte*>(malloc(tensor.layout.span().dist_byte())));
  198. uint8_t* ptr = static_cast<uint8_t*>(tensor.raw_ptr());
  199. megdnn_assert(values.size() == tensor.layout.span().dist_elem());
  200. for (size_t i = 0; i < tensor.layout.span().dist_elem(); i += 2) {
  201. int val0 = values[i], val1 = values[i + 1];
  202. ptr[i / 2] = (val0 & 0xF) | (val1 << 4);
  203. }
  204. return tensor;
  205. };
  206. using Param = MatrixMul::Param;
  207. Param param;
  208. checker.set_param(param);
  209. checker.set_dtype(2, dtype::QuantizedS16(0.3f * 0.3f));
  210. checker.exect(
  211. Testcase{
  212. GenTensorValueQuint4(
  213. {8, 8}, dtype::QuantizedS4(0.3f),
  214. {-8, 7, 2, 1, 2, 3, 2, 7, 2, 5, 3, 3, 7, 4, -7, 1,
  215. -5, 7, -4, -1, -1, 2, 4, 1, 7, 2, -6, -2, -6, 3, 4, 4,
  216. -2, 2, 3, 0, 6, 5, 3, 4, -1, -1, -5, 5, 2, 5, 1, 4,
  217. 6, 2, 0, 0, 3, 2, 2, 1, -4, -3, 7, 5, 0, 3, 2, 3}),
  218. GenTensorValueQuint4(
  219. {8, 8}, dtype::QuantizedS4(0.3f),
  220. {5, -8, -7, -6, 4, 7, -5, -5, -4, 7, -3, -2, 5,
  221. 6, 4, 2, 3, -1, 2, 2, 7, 3, 6, 0, 5, 4,
  222. 0, 2, 2, 3, 3, 2, 1, -8, -7, -6, 0, -5, -4,
  223. 4, -3, 7, 1, 6, -2, 2, -1, 5, 2, 0, 7, 6,
  224. 5, 4, 3, 2, 0, 0, 1, 0, 5, 2, 2, 6}),
  225. {}},
  226. Testcase{
  227. {},
  228. {},
  229. TensorValue(
  230. {8, 8}, dtype::QuantizedS16(0.3f * 0.3f),
  231. {-60, 120, 49, 58, 58, 13, 92, 125, -5, 0, -116,
  232. -70, 22, 9, -14, 46, -69, 111, 44, 48, 6, 19,
  233. 42, 57, -8, 25, 10, 16, 26, 97, -28, -12, -12,
  234. 14, 2, 26, 48, 7, 24, 93, -2, 45, 2, 32,
  235. -19, -1, -16, 72, 23, -44, -52, -34, 45, 53, -28,
  236. 6, 33, 45, 71, 84, 47, 10, 74, 61})
  237. });
  238. }
  239. TEST_F(NAIVE, MATRIX_MUL_QUANTIZED8x8x32) {
  240. Checker<MatrixMul> checker(handle(), /* check_dispatch */ false);
  241. MatrixMul::Param param;
  242. param.transposeA = false;
  243. param.transposeB = false;
  244. checker.set_param(param).exect(
  245. Testcase{
  246. TensorValue(
  247. {4, 7}, dtype::Quantized8Asymm(0.1f, (uint8_t)128),
  248. {6, 97, 210, 47, 213, 246, 92, 121, 132, 133,
  249. 37, 31, 87, 71, 0, 5, 198, 11, 97, 141,
  250. 222, 166, 76, 212, 190, 108, 245, 143}),
  251. TensorValue(
  252. {7, 5}, dtype::Quantized8Asymm(0.2f, (uint8_t)233),
  253. {89, 207, 79, 135, 43, 29, 235, 171, 40, 78, 119, 145,
  254. 254, 162, 184, 139, 248, 214, 201, 183, 127, 75, 48, 200,
  255. 96, 109, 63, 60, 100, 120, 111, 182, 150, 227, 92}),
  256. {}},
  257. Testcase{
  258. {},
  259. {},
  260. TensorValue(
  261. {4, 5}, dtype::QuantizedS32(0.1f * 0.2f),
  262. {2908, -36975, -9180, -3574, 8114, 30496, 23588,
  263. 32433, 11467, 30974, 36748, -6939, 26715, 33787,
  264. 35329, -24486, -25049, -19828, -16627, -18972})});
  265. param.transposeA = true;
  266. checker.set_param(param).exect(
  267. Testcase{
  268. TensorValue(
  269. {2, 1}, dtype::Quantized8Asymm(0.7f, (uint8_t)128),
  270. {129, 129}),
  271. TensorValue(
  272. {2, 1}, dtype::Quantized8Asymm(0.4f, (uint8_t)128),
  273. {129, 129}),
  274. {}},
  275. Testcase{
  276. {},
  277. {},
  278. TensorValue({1, 1}, dtype::QuantizedS32(0.7f * 0.4f), {2})});
  279. }
  280. TEST_F(NAIVE, MATRIX_MUL_MK4) {
  281. run_matmul_mk_format(
  282. handle(), param::MatrixMul::Format::MK4, dtype::Float32(), dtype::Float32(),
  283. dtype::Float32());
  284. }
  285. TEST_F(NAIVE, MATRIX_MUL_MK8) {
  286. run_matmul_mk_format(
  287. handle(), param::MatrixMul::Format::MK8, dtype::Int16(), dtype::Int16(),
  288. dtype::Int32());
  289. }
  290. TEST_F(NAIVE, MATRIX_MUL_MK4_DOT) {
  291. run_matmul_mk_format(
  292. handle(), param::MatrixMul::Format::MK4_DOT, dtype::Int8(), dtype::Int8(),
  293. dtype::Int32());
  294. }
  295. TEST_F(NAIVE, MATRIX_MUL_BFLOAT16) {
  296. Checker<MatrixMul> checker(handle(), /* check_dispatch */ false);
  297. MatrixMul::Param param, fp32_param;
  298. fp32_param = param;
  299. param.compute_mode = param::MatrixMul::ComputeMode::FLOAT32;
  300. checker.set_param(param);
  301. checker.set_dtype(0, dtype::BFloat16());
  302. checker.set_dtype(1, dtype::BFloat16());
  303. checker.set_dtype(2, dtype::BFloat16());
  304. auto extra_impl = extra_impl_helper<MatrixMul>(handle(), fp32_param);
  305. checker.set_extra_opr_impl(extra_impl);
  306. checker.set_epsilon(1.5e-2);
  307. UniformFloatRNG frng{1e-2, 5.f};
  308. checker.set_rng(0, &frng);
  309. checker.set_rng(1, &frng);
  310. checker.execs({{8, 8}, {8, 8}, {}});
  311. param.compute_mode = param::MatrixMul::ComputeMode::DEFAULT;
  312. checker.set_param(param);
  313. checker.execs({{8, 8}, {8, 8}, {}});
  314. }
  315. // vim: syntax=cpp.doxygen