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

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

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