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 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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-2020 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/matrix_mul.h"
  15. #include "test/common/random_state.h"
  16. #include "test/common/extra_impl_helper.h"
  17. using namespace megdnn;
  18. using namespace test;
  19. namespace {
  20. void run_matmul_mk_format(Handle* handle, param::MatrixMul::Format format,
  21. DType Atype, DType Btype, DType Ctype) {
  22. using namespace matrix_mul;
  23. std::vector<TestArg> args = get_matmul_args();
  24. Checker<MatrixMul> checker(handle);
  25. auto extra_impl = [](const TensorNDArray& tensors, param::MatrixMul param,
  26. Handle* handle, size_t pack_size) {
  27. megdnn_assert((param.format == param::MatrixMul::Format::MK4 ||
  28. param.format == param::MatrixMul::Format::MK8) &&
  29. tensors.size() == 3);
  30. param::MatrixMul new_param = param;
  31. new_param.format = param::MatrixMul::Format::DEFAULT;
  32. size_t M = tensors[2].layout[0] * pack_size;
  33. size_t N = tensors[2].layout[1];
  34. size_t K = tensors[0].layout[1 - param.transposeA] * pack_size;
  35. TensorLayoutArray default_layouts, mk4_layouts;
  36. if (param.transposeA) {
  37. default_layouts.emplace_back(tensors[0].layout.reshape({K, M}));
  38. mk4_layouts.emplace_back(
  39. default_layouts.back()
  40. .reshape({K / pack_size, M / pack_size, pack_size,
  41. pack_size})
  42. .dimshuffle({0, 2, 1, 3}));
  43. } else {
  44. default_layouts.emplace_back(tensors[0].layout.reshape({M, K}));
  45. mk4_layouts.emplace_back(
  46. default_layouts.back()
  47. .reshape({M / pack_size, K / pack_size, pack_size,
  48. pack_size})
  49. .dimshuffle({0, 3, 1, 2}));
  50. }
  51. if (param.transposeB) {
  52. default_layouts.emplace_back(tensors[1].layout.reshape({N, K}));
  53. mk4_layouts.emplace_back(
  54. default_layouts.back()
  55. .reshape({N, K / pack_size, pack_size})
  56. .dimshuffle({0, 1, 2}));
  57. } else {
  58. default_layouts.emplace_back(tensors[1].layout.reshape({K, N}));
  59. mk4_layouts.emplace_back(
  60. default_layouts.back()
  61. .reshape({K / pack_size, N, pack_size})
  62. .dimshuffle({0, 2, 1}));
  63. }
  64. default_layouts.emplace_back(tensors[2].layout.reshape({M, N}));
  65. mk4_layouts.emplace_back(default_layouts.back()
  66. .reshape({M / pack_size, N, pack_size})
  67. .dimshuffle({0, 2, 1}));
  68. auto matmul_opr = handle->create_operator<MatrixMul>();
  69. matmul_opr->param() = new_param;
  70. size_t matmul_workspace = matmul_opr->get_workspace_in_bytes(
  71. default_layouts[0], default_layouts[1], default_layouts[2]);
  72. auto relayout_opr = handle->create_operator<Relayout>();
  73. WorkspaceBundle wb(nullptr, {default_layouts[0].span().dist_byte(),
  74. default_layouts[1].span().dist_byte(),
  75. default_layouts[2].span().dist_byte(),
  76. matmul_workspace});
  77. wb.set(malloc(wb.total_size_in_bytes()));
  78. TensorNDArray default_tensors, mk4_tensors;
  79. for (size_t i = 0; i < 3; i++) {
  80. default_tensors.emplace_back(wb.get(i), default_layouts[i]);
  81. mk4_tensors.emplace_back(tensors[i].raw_ptr, mk4_layouts[i]);
  82. }
  83. relayout_opr->exec(mk4_tensors[0], default_tensors[0]);
  84. relayout_opr->exec(mk4_tensors[1], default_tensors[1]);
  85. matmul_opr->exec(default_tensors[0], default_tensors[1],
  86. default_tensors[2], wb.get_workspace(3));
  87. relayout_opr->exec(default_tensors[2], mk4_tensors[2]);
  88. free(wb.ptr());
  89. };
  90. size_t pack_size = MatrixMulForward::pack_size(format);
  91. for (auto&& arg : args) {
  92. if (arg.m % pack_size != 0 || arg.k % pack_size != 0)
  93. continue;
  94. param::MatrixMul param;
  95. param.transposeA = arg.mask & 0x1;
  96. param.transposeB = arg.mask & 0x2;
  97. param.format = format;
  98. size_t m = arg.m, n = arg.n, k = arg.k;
  99. TensorShape A, B;
  100. if (param.transposeA) {
  101. A = TensorShape{k / pack_size, m / pack_size, pack_size, pack_size};
  102. } else {
  103. A = TensorShape{m / pack_size, k / pack_size, pack_size, pack_size};
  104. }
  105. if (param.transposeB) {
  106. B = TensorShape{n, k / pack_size, pack_size};
  107. } else {
  108. B = TensorShape{k / pack_size, n, pack_size};
  109. }
  110. checker.set_extra_opr_impl(std::bind(extra_impl, std::placeholders::_1,
  111. param, handle, pack_size));
  112. checker.set_dtype(0, Atype)
  113. .set_dtype(1, Btype)
  114. .set_dtype(2, Ctype)
  115. .set_epsilon(1e-3)
  116. .set_param(param)
  117. .execs({A, B, {}});
  118. }
  119. }
  120. } // namespace
  121. TEST_F(NAIVE, MATRIX_MUL_QUANTIZED4x4x32) {
  122. Checker<MatrixMul> checker(handle(), /* check_dispatch */ false);
  123. auto GenTensorValueQuint4 = [](const TensorShape& shape,
  124. dtype::Quantized4Asymm dtype,
  125. const std::vector<int>& values) {
  126. TensorND tensor;
  127. tensor.layout = {shape, dtype};
  128. tensor.raw_ptr =
  129. static_cast<dt_byte*>(malloc(tensor.layout.span().dist_byte()));
  130. uint8_t* ptr = static_cast<uint8_t*>(tensor.raw_ptr);
  131. megdnn_assert(values.size() == tensor.layout.span().dist_elem());
  132. for (size_t i = 0; i < tensor.layout.span().dist_elem(); i += 2) {
  133. int val0 = values[i], val1 = values[i + 1];
  134. ptr[i / 2] = val0 | (val1 << 4);
  135. }
  136. return tensor;
  137. };
  138. using Param = MatrixMul::Param;
  139. Param param;
  140. checker.set_param(param);
  141. checker.set_dtype(2, dtype::QuantizedS32(0.3f * 0.3f));
  142. checker.exect(
  143. Testcase{
  144. GenTensorValueQuint4(
  145. {8, 8}, dtype::Quantized4Asymm(0.3f, (uint8_t)8),
  146. {13, 2, 4, 13, 9, 3, 14, 14, 14, 5, 3, 3, 15,
  147. 11, 8, 8, 5, 7, 14, 15, 8, 2, 11, 1, 15, 9,
  148. 13, 14, 2, 3, 11, 11, 15, 10, 11, 0, 13, 12, 3,
  149. 11, 9, 9, 10, 5, 2, 5, 8, 4, 6, 9, 0, 0,
  150. 3, 9, 9, 8, 8, 15, 7, 5, 0, 3, 9, 10}),
  151. GenTensorValueQuint4(
  152. {8, 8}, dtype::Quantized4Asymm(0.3f, (uint8_t)8),
  153. {5, 14, 13, 11, 4, 7, 12, 12, 11, 7, 13, 10, 5,
  154. 6, 4, 2, 3, 12, 2, 2, 13, 3, 14, 0, 15, 15,
  155. 0, 2, 2, 13, 3, 14, 10, 8, 9, 11, 0, 14, 15,
  156. 4, 14, 7, 1, 6, 13, 2, 12, 5, 2, 15, 7, 11,
  157. 13, 9, 8, 10, 0, 11, 6, 10, 12, 2, 2, 12}),
  158. {}},
  159. Testcase{
  160. {},
  161. {},
  162. TensorValue(
  163. {8, 8}, dtype::QuantizedS32(0.3f * 0.3f),
  164. {-90, 120, -3, 40, -31, 58, -54, 165, -5, -19,
  165. 71, 87, -51, 24, 92, 15, 27, 62, -59, -82,
  166. -40, 91, 11, -16, -85, 138, -18, -36, 8, -25,
  167. -56, 75, -46, -34, 67, 53, -4, -83, 111, -86,
  168. -29, -17, 45, -9, 38, -22, -3, -19, -17, -95,
  169. 94, 78, 63, -35, -51, 21, -63, -14, 87, 31,
  170. 44, -53, -107, 5}),
  171. });
  172. }
  173. TEST_F(NAIVE, MATRIX_MUL_QUANTIZED8x8x32) {
  174. Checker<MatrixMul> checker(handle(), /* check_dispatch */ false);
  175. MatrixMul::Param param;
  176. param.transposeA = false;
  177. param.transposeB = false;
  178. checker.set_param(param).exect(
  179. Testcase{TensorValue(
  180. {4, 7}, dtype::Quantized8Asymm(0.1f, (uint8_t)128),
  181. {6, 97, 210, 47, 213, 246, 92, 121, 132, 133,
  182. 37, 31, 87, 71, 0, 5, 198, 11, 97, 141,
  183. 222, 166, 76, 212, 190, 108, 245, 143}),
  184. TensorValue({7, 5},
  185. dtype::Quantized8Asymm(0.2f, (uint8_t)233),
  186. {89, 207, 79, 135, 43, 29, 235, 171, 40,
  187. 78, 119, 145, 254, 162, 184, 139, 248, 214,
  188. 201, 183, 127, 75, 48, 200, 96, 109, 63,
  189. 60, 100, 120, 111, 182, 150, 227, 92}),
  190. {}},
  191. Testcase{{},
  192. {},
  193. TensorValue({4, 5}, dtype::QuantizedS32(0.1f * 0.2f),
  194. {2908, -36975, -9180, -3574, 8114,
  195. 30496, 23588, 32433, 11467, 30974,
  196. 36748, -6939, 26715, 33787, 35329,
  197. -24486, -25049, -19828, -16627, -18972})});
  198. param.transposeA = true;
  199. checker.set_param(param).exect(
  200. Testcase{TensorValue({2, 1},
  201. dtype::Quantized8Asymm(0.7f, (uint8_t)128),
  202. {129, 129}),
  203. TensorValue({2, 1},
  204. dtype::Quantized8Asymm(0.4f, (uint8_t)128),
  205. {129, 129}),
  206. {}},
  207. Testcase{{},
  208. {},
  209. TensorValue({1, 1}, dtype::QuantizedS32(0.7f * 0.4f),
  210. {2})});
  211. }
  212. TEST_F(NAIVE, MATRIX_MUL_MK4) {
  213. run_matmul_mk_format(handle(), param::MatrixMul::Format::MK4,
  214. dtype::Float32(), dtype::Float32(), dtype::Float32());
  215. }
  216. TEST_F(NAIVE, MATRIX_MUL_MK8) {
  217. run_matmul_mk_format(handle(), param::MatrixMul::Format::MK8,
  218. dtype::Int16(), dtype::Int16(), dtype::Int32());
  219. }
  220. TEST_F(NAIVE, MATRIX_MUL_BFLOAT16) {
  221. Checker<MatrixMul> checker(handle(), /* check_dispatch */ false);
  222. MatrixMul::Param param, fp32_param;
  223. fp32_param = param;
  224. param.compute_mode = param::MatrixMul::ComputeMode::FLOAT32;
  225. checker.set_param(param);
  226. checker.set_dtype(0, dtype::BFloat16());
  227. checker.set_dtype(1, dtype::BFloat16());
  228. checker.set_dtype(2, dtype::BFloat16());
  229. auto extra_impl = extra_impl_helper<MatrixMul>(handle(), fp32_param);
  230. checker.set_extra_opr_impl(extra_impl);
  231. checker.set_epsilon(1.5e-2);
  232. UniformFloatRNG frng{1e-2, 5.f};
  233. checker.set_rng(0, &frng);
  234. checker.set_rng(1, &frng);
  235. checker.execs({{8, 8}, {8, 8}, {}});
  236. param.compute_mode = param::MatrixMul::ComputeMode::DEFAULT;
  237. checker.set_param(param);
  238. checker.execs({{8, 8}, {8, 8}, {}});
  239. }
  240. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台