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.

elemwise.cpp 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * \file dnn/test/arm_common/elemwise.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
  10. * implied.
  11. */
  12. #include "test/common/elemwise.h"
  13. #include "test/arm_common/fixture.h"
  14. #include "test/common/benchmarker.h"
  15. #include "test/common/checker.h"
  16. #include "megdnn/oprs/general.h"
  17. using namespace megdnn;
  18. using namespace test;
  19. template <typename tag>
  20. class ARM_ELEMWISE : public ARM_COMMON {};
  21. TYPED_TEST_CASE(ARM_ELEMWISE, elemwise::test_types);
  22. TYPED_TEST(ARM_ELEMWISE, run) {
  23. elemwise::run_test<TypeParam>(this->handle());
  24. }
  25. #define TERNARY_COMPLATE_TEST_CASE(_optr) \
  26. printf("Check binary optr %s by all cases.\n", #_optr); \
  27. checker.set_param(Mode::_optr) \
  28. .execs({{3, 4, 7}, {3, 4, 7}, {3, 4, 7}, {}}); \
  29. checker.set_param(Mode::_optr) \
  30. .execs({{1, 4, 1, 1}, {3, 4, 5, 7}, {1, 4, 1, 1}, {}}); \
  31. checker.set_param(Mode::_optr) \
  32. .execs({{1, 4, 1}, {3, 4, 7}, {1, 4, 1}, {}}); \
  33. checker.set_param(Mode::_optr) \
  34. .execs({{3, 4, 5, 7}, {3, 4, 5, 7}, {1, 1, 1, 1}, {}}); \
  35. checker.set_param(Mode::_optr).execs({{1, 7}, {1, 7}, {1, 7}, {}}); \
  36. checker.set_param(Mode::_optr) \
  37. .execs({{1, 2, 1}, {1, 2, 2}, {1, 2, 1}, {}}); \
  38. checker.set_param(Mode::_optr) \
  39. .execs({{1, 2, 2}, {1, 2, 2}, {1, 1, 1}, {}}); \
  40. checker.set_param(Mode::_optr) \
  41. .execs({{3, 4, 1}, {3, 4, 1}, {3, 4, 1}, {}}); \
  42. checker.set_param(Mode::_optr).execs({{3, 4, 5}, {1}, {1}, {}}); \
  43. checker.set_param(Mode::_optr).execs({{1}, {3, 4, 5}, {1}, {}});
  44. #define BUILD_TERNARY_COMPLATE_TEST_CASE \
  45. TERNARY_COMPLATE_TEST_CASE(FUSE_MUL_ADD3)
  46. TEST_F(ARM_COMMON, ELEMWISE_FORWARD_TERNARY) {
  47. using Mode = ElemwiseForward::Param::Mode;
  48. Checker<ElemwiseForward> checker(handle());
  49. // case int
  50. checker.set_dtype(0, dtype::Int8());
  51. checker.set_dtype(1, dtype::Int8());
  52. checker.set_dtype(2, dtype::Int8());
  53. // BUILD_TERNARY_TEST_CASE
  54. BUILD_TERNARY_COMPLATE_TEST_CASE
  55. checker.set_dtype(0, dtype::Int16());
  56. checker.set_dtype(1, dtype::Int16());
  57. checker.set_dtype(2, dtype::Int16());
  58. // BUILD_TERNARY_TEST_CASE
  59. BUILD_TERNARY_COMPLATE_TEST_CASE
  60. checker.set_dtype(0, dtype::Int32());
  61. checker.set_dtype(1, dtype::Int32());
  62. checker.set_dtype(2, dtype::Int32());
  63. // BUILD_TERNARY_TEST_CASE
  64. BUILD_TERNARY_COMPLATE_TEST_CASE
  65. // case float
  66. UniformFloatRNG rng(1e-5, 7e1);
  67. checker.set_rng(0, &rng);
  68. checker.set_epsilon(1e-5);
  69. checker.set_dtype(0, dtype::Float32());
  70. checker.set_dtype(1, dtype::Float32());
  71. checker.set_dtype(2, dtype::Float32());
  72. // BUILD_TERNARY_TEST_CASE
  73. BUILD_TERNARY_COMPLATE_TEST_CASE
  74. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  75. // case half
  76. UniformFloatRNG rng_float16(1, 10);
  77. checker.set_rng(0, &rng_float16);
  78. checker.set_epsilon(1e-2);
  79. checker.set_dtype(0, dtype::Float16());
  80. checker.set_dtype(1, dtype::Float16());
  81. checker.set_dtype(2, dtype::Float16());
  82. // BUILD_TERNARY_TEST_CASE
  83. BUILD_TERNARY_COMPLATE_TEST_CASE
  84. #endif
  85. }
  86. TEST_F(ARM_COMMON, ELEMWISE_FORWARD_NCHW44_INT8_INT16_INT32) {
  87. using Mode = ElemwiseForward::Param::Mode;
  88. Checker<ElemwiseForward> checker(handle());
  89. auto run = [&]() {
  90. // VEC_BCAST101x not PowOp
  91. checker.set_param(Mode::ADD).execs(
  92. {{1, 3, 2, 2, 4}, {1, 3, 1, 1, 4}, {}});
  93. checker.set_param(Mode::ADD).execs(
  94. {{2, 3, 2, 2, 4}, {1, 3, 1, 1, 4}, {}});
  95. checker.set_param(Mode::ADD).execs(
  96. {{3, 8, 5, 3, 4}, {1, 8, 1, 1, 4}, {}});
  97. checker.set_param(Mode::ADD).execs(
  98. {{3, 4, 5, 7, 4}, {3, 4, 5, 7, 4}, {}});
  99. checker.set_param(Mode::ADD).execs(
  100. {{1, 2, 5, 7, 4}, {1, 2, 1, 1, 4}, {}});
  101. checker.set_param(Mode::RMULH)
  102. .execs({{1, 3, 2, 2, 4}, {1, 3, 1, 1, 4}, {}});
  103. checker.set_param(Mode::RMULH)
  104. .execs({{2, 3, 2, 2, 4}, {1, 3, 1, 1, 4}, {}});
  105. checker.set_param(Mode::RMULH)
  106. .execs({{3, 8, 5, 3, 4}, {1, 8, 1, 1, 4}, {}});
  107. checker.set_param(Mode::RMULH)
  108. .execs({{3, 4, 5, 7, 4}, {3, 4, 5, 7, 4}, {}});
  109. checker.set_param(Mode::RMULH)
  110. .execs({{1, 2, 5, 7, 4}, {1, 2, 1, 1, 4}, {}});
  111. checker.set_param(Mode::FUSE_ADD_RELU)
  112. .execs({{1, 3, 2, 2, 4}, {1, 3, 1, 1, 4}, {}});
  113. checker.set_param(Mode::FUSE_ADD_RELU)
  114. .execs({{2, 3, 2, 2, 4}, {1, 3, 1, 1, 4}, {}});
  115. checker.set_param(Mode::FUSE_ADD_RELU)
  116. .execs({{3, 8, 5, 3, 4}, {1, 8, 1, 1, 4}, {}});
  117. checker.set_param(Mode::FUSE_ADD_RELU)
  118. .execs({{3, 4, 5, 7, 4}, {3, 4, 5, 7, 4}, {}});
  119. checker.set_param(Mode::FUSE_ADD_RELU)
  120. .execs({{1, 2, 5, 7, 4}, {1, 2, 1, 1, 4}, {}});
  121. // BCAST101x_VEC not PowOp
  122. checker.set_param(Mode::ADD).execs(
  123. {{1, 3, 1, 1, 4}, {1, 3, 2, 2, 4}, {}});
  124. checker.set_param(Mode::ADD).execs(
  125. {{1, 3, 1, 1, 4}, {2, 3, 2, 2, 4}, {}});
  126. checker.set_param(Mode::ADD).execs(
  127. {{1, 8, 1, 1, 4}, {3, 8, 5, 3, 4}, {}});
  128. checker.set_param(Mode::ADD).execs(
  129. {{3, 4, 5, 7, 4}, {3, 4, 5, 7, 4}, {}});
  130. checker.set_param(Mode::ADD).execs(
  131. {{1, 2, 1, 1, 4}, {1, 2, 5, 7, 4}, {}});
  132. checker.set_param(Mode::FUSE_ADD_RELU)
  133. .execs({{1, 3, 1, 1, 4}, {1, 3, 2, 2, 4}, {}});
  134. checker.set_param(Mode::FUSE_ADD_RELU)
  135. .execs({{1, 3, 1, 1, 4}, {2, 3, 2, 2, 4}, {}});
  136. checker.set_param(Mode::FUSE_ADD_RELU)
  137. .execs({{1, 8, 1, 1, 4}, {3, 8, 5, 3, 4}, {}});
  138. checker.set_param(Mode::FUSE_ADD_RELU)
  139. .execs({{3, 4, 5, 7, 4}, {3, 4, 5, 7, 4}, {}});
  140. checker.set_param(Mode::FUSE_ADD_RELU)
  141. .execs({{1, 2, 1, 1, 4}, {1, 2, 5, 7, 4}, {}});
  142. };
  143. checker.set_dtype(0, dtype::Int8());
  144. checker.set_dtype(1, dtype::Int8());
  145. run();
  146. checker.set_dtype(0, dtype::Int16());
  147. checker.set_dtype(1, dtype::Int16());
  148. run();
  149. checker.set_dtype(0, dtype::Int32());
  150. checker.set_dtype(1, dtype::Int32());
  151. run();
  152. }
  153. TEST_F(ARM_COMMON, ELEMWISE_FORWARD_NCHW44_FP32) {
  154. using Mode = ElemwiseForward::Param::Mode;
  155. Checker<ElemwiseForward> checker(handle());
  156. UniformFloatRNG rng(1e-5, 7e1);
  157. checker.set_rng(0, &rng);
  158. checker.set_epsilon(1e-5);
  159. checker.set_dtype(0, dtype::Float32());
  160. checker.set_dtype(1, dtype::Float32());
  161. checker.set_param(Mode::FUSE_ADD_RELU)
  162. .execs({{1, 3, 1, 1, 4}, {1, 3, 2, 2, 4}, {}});
  163. checker.set_param(Mode::FUSE_ADD_RELU)
  164. .execs({{1, 3, 1, 1, 4}, {2, 3, 2, 2, 4}, {}});
  165. checker.set_param(Mode::FUSE_ADD_RELU)
  166. .execs({{1, 8, 1, 1, 4}, {3, 8, 5, 3, 4}, {}});
  167. checker.set_param(Mode::FUSE_ADD_RELU)
  168. .execs({{3, 4, 5, 7, 4}, {3, 4, 5, 7, 4}, {}});
  169. checker.set_param(Mode::FUSE_ADD_RELU)
  170. .execs({{1, 2, 1, 1, 4}, {1, 2, 5, 7, 4}, {}});
  171. checker.set_param(Mode::FUSE_ADD_RELU)
  172. .execs({{1, 3, 2, 2, 4}, {1, 3, 1, 1, 4}, {}});
  173. checker.set_param(Mode::FUSE_ADD_RELU)
  174. .execs({{2, 3, 2, 2, 4}, {1, 3, 1, 1, 4}, {}});
  175. checker.set_param(Mode::FUSE_ADD_RELU)
  176. .execs({{3, 8, 5, 3, 4}, {1, 8, 1, 1, 4}, {}});
  177. checker.set_param(Mode::FUSE_ADD_RELU)
  178. .execs({{3, 4, 5, 7, 4}, {3, 4, 5, 7, 4}, {}});
  179. checker.set_param(Mode::FUSE_ADD_RELU)
  180. .execs({{1, 2, 5, 7, 4}, {1, 2, 1, 1, 4}, {}});
  181. auto run = [&](Mode mode) {
  182. // VEC_BCAST101x
  183. checker.set_param(mode).execs({{1, 3, 2, 2, 4}, {1, 3, 1, 1, 4}, {}});
  184. checker.set_param(mode).execs({{2, 3, 2, 2, 4}, {1, 3, 1, 1, 4}, {}});
  185. checker.set_param(mode).execs({{3, 8, 5, 3, 4}, {1, 8, 1, 1, 4}, {}});
  186. checker.set_param(mode).execs({{3, 4, 5, 7, 4}, {3, 4, 5, 7, 4}, {}});
  187. checker.set_param(mode).execs({{1, 2, 5, 7, 4}, {1, 2, 1, 1, 4}, {}});
  188. // BCAST101x_VEC not powOp
  189. checker.set_param(mode).execs({{1, 3, 1, 1, 4}, {1, 3, 2, 2, 4}, {}});
  190. checker.set_param(mode).execs({{1, 3, 1, 1, 4}, {2, 3, 2, 2, 4}, {}});
  191. checker.set_param(mode).execs({{1, 8, 1, 1, 4}, {3, 8, 5, 3, 4}, {}});
  192. checker.set_param(mode).execs({{3, 4, 5, 7, 4}, {3, 4, 5, 7, 4}, {}});
  193. checker.set_param(mode).execs({{1, 2, 1, 1, 4}, {1, 2, 5, 7, 4}, {}});
  194. };
  195. run(Mode::ADD);
  196. run(Mode::FUSE_ADD_H_SWISH);
  197. run(Mode::FUSE_ADD_RELU);
  198. run(Mode::MAX);
  199. run(Mode::MIN);
  200. run(Mode::MUL);
  201. run(Mode::SUB);
  202. run(Mode::TRUE_DIV);
  203. run(Mode::POW);
  204. }
  205. #if MEGDNN_WITH_BENCHMARK
  206. namespace {
  207. void run_elemwise_benchmark(const TensorShapeArray& shapes,
  208. param::Elemwise::Mode mode, const char* mode_str,
  209. DType type, Handle* handle_bench) {
  210. auto handle_fallback = create_cpu_handle(1);
  211. Benchmarker<Elemwise> benchmarker_bench(handle_bench);
  212. Benchmarker<Elemwise> benchmarker_fallback(handle_fallback.get());
  213. float throughput = 0;
  214. SmallVector<TensorLayout> layouts;
  215. std::string src_strs;
  216. for (size_t i = 0; i < shapes.size(); i++) {
  217. layouts.emplace_back(shapes[i], type);
  218. throughput += layouts.back().span().dist_byte();
  219. src_strs += layouts.back().to_string();
  220. if (i != shapes.size() - 1) {
  221. src_strs += ",";
  222. }
  223. }
  224. constexpr size_t RUN = 50;
  225. benchmarker_fallback.set_times(RUN).set_display(false);
  226. benchmarker_bench.set_times(RUN).set_display(false);
  227. benchmarker_fallback.set_param(mode);
  228. benchmarker_bench.set_param(mode);
  229. TensorLayout dst_layout;
  230. auto opr = handle_bench->create_operator<Elemwise>();
  231. opr->param() = mode;
  232. opr->deduce_layout(layouts, dst_layout);
  233. float computations = dst_layout.total_nr_elems() *
  234. (std::max<size_t>(shapes.size(), 2) - 1);
  235. throughput += dst_layout.span().dist_byte();
  236. computations *= (1e3 / (1024.0 * 1024));
  237. throughput *= (1e3 / (1024.0 * 1024));
  238. layouts.emplace_back(dst_layout);
  239. auto fallback_time = benchmarker_fallback.execl(layouts) / RUN;
  240. auto bench_time = benchmarker_bench.execl(layouts) / RUN;
  241. float fallback_flops = computations / fallback_time;
  242. float bench_flops = computations / bench_time;
  243. float fallback_thr = throughput / fallback_time;
  244. float bench_thr = throughput / bench_time;
  245. printf("%s = %s (type: %s, mode: %s) cpu=%fMFLOPS %fMB/s, bench=%fMFLOPS "
  246. "%fMB/s "
  247. "computations: %fx, throughput: %fx\n",
  248. src_strs.c_str(), dst_layout.to_string().c_str(), type.name(),
  249. mode_str, fallback_flops, fallback_thr, bench_flops, bench_thr,
  250. bench_flops / fallback_flops, bench_thr / fallback_thr);
  251. }
  252. } // namespace
  253. #define INT_RUN(shape, mode) \
  254. run_elemwise_benchmark(shape, mode, #mode, dtype::Int8{}, handle()); \
  255. run_elemwise_benchmark(shape, mode, #mode, dtype::Int16{}, handle()); \
  256. run_elemwise_benchmark(shape, mode, #mode, dtype::Int32{}, handle());
  257. #define FLOAT_RUN(shape, mode) \
  258. run_elemwise_benchmark(shape, mode, #mode, dtype::Float32{}, handle()); \
  259. run_elemwise_benchmark(shape, mode, #mode, dtype::Float16{}, handle());
  260. #define BENCHMARK_CASES(shape) \
  261. INT_BENCHMARK_CASES(shape) \
  262. FLOAT_BENCHMARK_CASES(shape)
  263. TEST_F(ARM_COMMON, BENCHMARK_UNARY) {
  264. #define INT_BENCHMARK_CASES(shape) \
  265. INT_RUN(shape, Mode::RELU); \
  266. INT_RUN(shape, Mode::ABS);
  267. #define FLOAT_BENCHMARK_CASES(shape) \
  268. FLOAT_RUN(shape, Mode::RELU); \
  269. FLOAT_RUN(shape, Mode::ABS); \
  270. FLOAT_RUN(shape, Mode::SIGMOID); \
  271. FLOAT_RUN(shape, Mode::EXP); \
  272. FLOAT_RUN(shape, Mode::TANH); \
  273. FLOAT_RUN(shape, Mode::FAST_TANH);
  274. using Mode = param::Elemwise::Mode;
  275. BENCHMARK_CASES({{10000}});
  276. BENCHMARK_CASES({{50000}});
  277. #undef INT_BENCHMARK_CASES
  278. #undef FLOAT_BENCHMARK_CASES
  279. }
  280. TEST_F(ARM_COMMON, BENCHMARK_BINARY) {
  281. #define INT_BENCHMARK_CASES(shape) \
  282. INT_RUN(shape, Mode::MIN); \
  283. INT_RUN(shape, Mode::MAX); \
  284. INT_RUN(shape, Mode::ADD); \
  285. INT_RUN(shape, Mode::SUB); \
  286. INT_RUN(shape, Mode::MUL); \
  287. INT_RUN(shape, Mode::RMULH); \
  288. INT_RUN(shape, Mode::FUSE_ADD_RELU);
  289. #define FLOAT_BENCHMARK_CASES(shape) \
  290. FLOAT_RUN(shape, Mode::MIN); \
  291. FLOAT_RUN(shape, Mode::MAX); \
  292. FLOAT_RUN(shape, Mode::ADD); \
  293. FLOAT_RUN(shape, Mode::SUB); \
  294. FLOAT_RUN(shape, Mode::MUL); \
  295. FLOAT_RUN(shape, Mode::POW); \
  296. FLOAT_RUN(shape, Mode::TRUE_DIV); \
  297. FLOAT_RUN(shape, Mode::FUSE_ADD_RELU);
  298. using Mode = param::Elemwise::Mode;
  299. TensorShapeArray shapes = {{1, 112, 28, 28}, {1, 112, 28, 28}};
  300. BENCHMARK_CASES(shapes);
  301. shapes = {{1, 16, 1, 1}, {1, 16, 112, 112}};
  302. BENCHMARK_CASES(shapes);
  303. shapes = {{1, 448, 7, 7}, {1, 448, 7, 7}};
  304. BENCHMARK_CASES(shapes);
  305. #undef INT_BENCHMARK_CASES
  306. #undef FLOAT_BENCHMARK_CASES
  307. }
  308. TEST_F(ARM_COMMON, BENCHMARK_TERNARY_FMA3) {
  309. #define INT_BENCHMARK_CASES(shape) INT_RUN(shape, Mode::FUSE_MUL_ADD3);
  310. #define FLOAT_BENCHMARK_CASES(shape) FLOAT_RUN(shape, Mode::FUSE_MUL_ADD3);
  311. using Mode = param::Elemwise::Mode;
  312. TensorShapeArray shapes = {{30, 40, 70}, {30, 40, 70}, {30, 40, 70}};
  313. BENCHMARK_CASES(shapes);
  314. shapes = {{1, 4, 1, 1}, {3, 4, 5, 7}, {1, 4, 1, 1}};
  315. BENCHMARK_CASES(shapes);
  316. shapes = {{3, 4, 5, 7}, {3, 4, 5, 7}, {1, 1, 1, 1}};
  317. BENCHMARK_CASES(shapes);
  318. #undef INT_BENCHMARK_CASES
  319. #undef FLOAT_BENCHMARK_CASES
  320. }
  321. #undef BENCHMARK_CASES
  322. #undef INT_RUN
  323. #undef FLOAT_RUN
  324. #endif
  325. // vim: syntax=cpp.doxygen

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