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_bmark.cpp 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #include "test/x86/fixture.h"
  2. #include "megdnn/oprs.h"
  3. #include "test/common/benchmarker.h"
  4. #include "test/common/checker.h"
  5. #include "test/common/rng.h"
  6. using namespace megdnn;
  7. using namespace test;
  8. #define TEST_IN_DIFF_DISTRUBUTION(proportion_of_inf, dataset_number) \
  9. max_val = 88.3762626647949f / (1 - proportion_of_inf); \
  10. UniformFloatRNG rng##dataset_number(0.f, max_val); \
  11. B.set_rng(0, &rng##dataset_number); \
  12. B.execs({{355600}, {}});
  13. TEST_F(X86, BENCHMARK_ELEM_EXP_BASED_OPTRS) {
  14. Benchmarker<ElemwiseForward> B(handle());
  15. using Mode = ElemwiseForward::Param::Mode;
  16. // UniformFloatWithZeroRNG rng(80, 100, 0.1);
  17. printf("Test Optr exp(x)\n");
  18. B.set_param(Mode::EXP);
  19. B.execs({{355600}, {}});
  20. float max_val = 0;
  21. TEST_IN_DIFF_DISTRUBUTION(0.25, 1)
  22. TEST_IN_DIFF_DISTRUBUTION(0.5, 2)
  23. TEST_IN_DIFF_DISTRUBUTION(0.75, 3)
  24. TEST_IN_DIFF_DISTRUBUTION(0.9999, 4)
  25. printf("Test Optr tanh(x)\n");
  26. B.set_param(Mode::TANH);
  27. B.execs({{355600}, {}});
  28. max_val = 0;
  29. TEST_IN_DIFF_DISTRUBUTION(0.25, 5)
  30. TEST_IN_DIFF_DISTRUBUTION(0.5, 6)
  31. TEST_IN_DIFF_DISTRUBUTION(0.75, 7)
  32. TEST_IN_DIFF_DISTRUBUTION(0.9999, 8)
  33. printf("Test Optr fast_tanh(x)\n");
  34. B.set_param(Mode::FAST_TANH);
  35. B.execs({{355600}, {}});
  36. printf("Test Optr sigmoid(x)\n");
  37. B.set_param(Mode::SIGMOID);
  38. B.execs({{355600}, {}});
  39. max_val = 0;
  40. TEST_IN_DIFF_DISTRUBUTION(0.25, 13)
  41. TEST_IN_DIFF_DISTRUBUTION(0.5, 14)
  42. TEST_IN_DIFF_DISTRUBUTION(0.75, 15)
  43. TEST_IN_DIFF_DISTRUBUTION(0.9999, 16)
  44. printf("Test Optr tanh_grad(x)\n");
  45. B.set_param(Mode::TANH_GRAD);
  46. B.execs({{355600}, {355600}, {}});
  47. printf("Test Optr fast_tanh_grad(x)\n");
  48. B.set_param(Mode::FAST_TANH_GRAD);
  49. B.execs({{355600}, {355600}, {}});
  50. }
  51. // 1. Unary
  52. #define BENCHMARK_UNARY(Optr, size) \
  53. printf("Test for %s \n", #Optr); \
  54. B.set_param(Mode::Optr); \
  55. B.execs( \
  56. {{ \
  57. 4, \
  58. 4, \
  59. 4, \
  60. 1 + size / 64, \
  61. }, \
  62. {}});
  63. // 2. Binary
  64. #define BENCHMARK_BINARY(Optr, size) \
  65. B.set_param(Mode::Optr); \
  66. B.execs({{size}, {size}, {}});
  67. #define BENCHMARK_BINARY_SCALAR(Optr, size) \
  68. B.set_param(Mode::Optr); \
  69. B.execs({{size}, {1}, {}});
  70. #define BENCHMARK_BINARY_1C11(Optr, chan) \
  71. B.set_param(Mode::Optr); \
  72. B.execs({{9, chan, 33, 127}, {1, chan, 1, 1}, {}});
  73. #define BENCHMARK_BINARY_ALL_KINDS(Optr, size) \
  74. printf("Test for %s \n", #Optr); \
  75. BENCHMARK_BINARY(Optr, size) \
  76. BENCHMARK_BINARY_SCALAR(Optr, size) \
  77. BENCHMARK_BINARY_1C11(Optr, (1 + size / 37719))
  78. // 3. Ternary
  79. #define BENCHMARK_TERNARY(Optr, size) \
  80. B.set_param(Mode::Optr); \
  81. B.execs({{size}, {size}, {size}, {}});
  82. #define BENCHMARK_TERNARY_SCALAR(Optr, size) \
  83. B.set_param(Mode::Optr); \
  84. B.execs({{size}, {size}, {1}, {}});
  85. #define BENCHMARK_TERNARY_1C11(Optr, chan) \
  86. B.set_param(Mode::Optr); \
  87. B.execs({{1, chan, 1, 1}, {9, chan, 33, 127}, {1, chan, 1, 1}, {}});
  88. #define BENCHMARK_TERNARY_ALL_KINDS(Optr, size) \
  89. printf("Test for %s \n", #Optr); \
  90. BENCHMARK_TERNARY(Optr, size) \
  91. BENCHMARK_TERNARY_SCALAR(Optr, size) \
  92. BENCHMARK_TERNARY_1C11(Optr, (size / 37719))
  93. #define BENCHMARK_CASE_INT(size) \
  94. BENCHMARK_BINARY_ALL_KINDS(ADD, size) \
  95. BENCHMARK_BINARY_ALL_KINDS(SUB, size) \
  96. BENCHMARK_BINARY_ALL_KINDS(MUL, size) \
  97. BENCHMARK_BINARY_ALL_KINDS(TRUE_DIV, size) \
  98. BENCHMARK_BINARY_ALL_KINDS(MIN, size) \
  99. BENCHMARK_BINARY_ALL_KINDS(MAX, size) \
  100. BENCHMARK_UNARY(RELU, size) \
  101. BENCHMARK_UNARY(ABS, size) \
  102. BENCHMARK_BINARY_ALL_KINDS(FUSE_ADD_RELU, size) \
  103. BENCHMARK_TERNARY_ALL_KINDS(FUSE_MUL_ADD3, size)
  104. #define BENCHMARK_CASE_FLOAT(size) \
  105. BENCHMARK_CASE_INT(size) \
  106. BENCHMARK_BINARY_ALL_KINDS(FUSE_ADD_TANH, size) \
  107. BENCHMARK_BINARY_ALL_KINDS(FUSE_ADD_SIGMOID, size)
  108. TEST_F(X86, BENCHMARK_ELEM_EVERY_DTYPE) {
  109. Benchmarker<ElemwiseForward> B(handle());
  110. using Mode = ElemwiseForward::Param::Mode;
  111. printf("\nTest case float32:\n");
  112. B.set_dtype(0, dtype::Float32());
  113. B.set_dtype(1, dtype::Float32());
  114. B.set_dtype(2, dtype::Float32());
  115. BENCHMARK_CASE_FLOAT(1556011)
  116. // printf("\nTest case int32:\n");
  117. // B.set_dtype(0, dtype::Int32());
  118. // B.set_dtype(1, dtype::Int32());
  119. // B.set_dtype(2, dtype::Int32());
  120. // BENCHMARK_CASE_INT(1556011)
  121. // printf("\nTest case int16:\n");
  122. // B.set_dtype(0, dtype::Int16());
  123. // B.set_dtype(1, dtype::Int16());
  124. // B.set_dtype(2, dtype::Int16());
  125. // BENCHMARK_CASE_INT(1556011)
  126. // printf("\nTest case int8:\n");
  127. // B.set_dtype(0, dtype::Int8());
  128. // B.set_dtype(1, dtype::Int8());
  129. // B.set_dtype(2, dtype::Int8());
  130. // BENCHMARK_CASE_INT(1556011)
  131. }
  132. #if MEGDNN_WITH_BENCHMARK
  133. namespace {
  134. void run_elemwise_benchmark(
  135. const TensorShapeArray& shapes, param::Elemwise::Mode mode,
  136. const char* mode_str, DType type, Handle* handle_bench) {
  137. auto handle_fallback = create_cpu_handle(1);
  138. Benchmarker<Elemwise> benchmarker_bench(handle_bench);
  139. Benchmarker<Elemwise> benchmarker_fallback(handle_fallback.get());
  140. float throughput = 0;
  141. SmallVector<TensorLayout> layouts;
  142. std::string src_strs;
  143. for (size_t i = 0; i < shapes.size(); i++) {
  144. layouts.emplace_back(shapes[i], type);
  145. throughput += layouts.back().span().dist_byte();
  146. src_strs += layouts.back().to_string();
  147. if (i != shapes.size() - 1) {
  148. src_strs += ",";
  149. }
  150. }
  151. constexpr size_t RUN = 50;
  152. benchmarker_fallback.set_times(RUN).set_display(false);
  153. benchmarker_bench.set_times(RUN).set_display(false);
  154. benchmarker_fallback.set_param(mode);
  155. benchmarker_bench.set_param(mode);
  156. TensorLayout dst_layout;
  157. auto opr = handle_bench->create_operator<Elemwise>();
  158. opr->param() = mode;
  159. opr->deduce_layout(layouts, dst_layout);
  160. float computations =
  161. dst_layout.total_nr_elems() * (std::max<size_t>(shapes.size(), 2) - 1);
  162. throughput += dst_layout.span().dist_byte();
  163. computations *= (1e3 / (1024.0 * 1024));
  164. throughput *= (1e3 / (1024.0 * 1024));
  165. layouts.emplace_back(dst_layout);
  166. auto fallback_time = benchmarker_fallback.execl(layouts) / RUN;
  167. auto bench_time = benchmarker_bench.execl(layouts) / RUN;
  168. float fallback_flops = computations / fallback_time;
  169. float bench_flops = computations / bench_time;
  170. float fallback_thr = throughput / fallback_time;
  171. float bench_thr = throughput / bench_time;
  172. printf("%s = %s (type: %s, mode: %s) cpu=%fMFLOPS %fMB/s, bench=%fMFLOPS "
  173. "%fMB/s "
  174. "computations: %fx, throughput: %fx\n",
  175. src_strs.c_str(), dst_layout.to_string().c_str(), type.name(), mode_str,
  176. fallback_flops, fallback_thr, bench_flops, bench_thr,
  177. bench_flops / fallback_flops, bench_thr / fallback_thr);
  178. }
  179. } // namespace
  180. #define INT_RUN(shape, mode) \
  181. run_elemwise_benchmark(shape, mode, #mode, dtype::Int8{}, handle()); \
  182. run_elemwise_benchmark(shape, mode, #mode, dtype::Int16{}, handle()); \
  183. run_elemwise_benchmark(shape, mode, #mode, dtype::Int32{}, handle());
  184. #define FLOAT_RUN(shape, mode) \
  185. run_elemwise_benchmark(shape, mode, #mode, dtype::Float32{}, handle()); \
  186. run_elemwise_benchmark(shape, mode, #mode, dtype::Float16{}, handle());
  187. #define BENCHMARK_CASES(shape) \
  188. INT_BENCHMARK_CASES(shape) \
  189. FLOAT_BENCHMARK_CASES(shape)
  190. TEST_F(X86, BENCHMARK_UNARY) {
  191. #define INT_BENCHMARK_CASES(shape) \
  192. INT_RUN(shape, Mode::RELU); \
  193. INT_RUN(shape, Mode::ABS);
  194. #define FLOAT_BENCHMARK_CASES(shape) \
  195. FLOAT_RUN(shape, Mode::RELU); \
  196. FLOAT_RUN(shape, Mode::ABS); \
  197. FLOAT_RUN(shape, Mode::SIGMOID); \
  198. FLOAT_RUN(shape, Mode::EXP); \
  199. FLOAT_RUN(shape, Mode::TANH); \
  200. FLOAT_RUN(shape, Mode::FAST_TANH);
  201. using Mode = param::Elemwise::Mode;
  202. BENCHMARK_CASES({{10000}});
  203. BENCHMARK_CASES({{50000}});
  204. #undef INT_BENCHMARK_CASES
  205. #undef FLOAT_BENCHMARK_CASES
  206. }
  207. TEST_F(X86, BENCHMARK_BINARY) {
  208. #define INT_BENCHMARK_CASES(shape) \
  209. INT_RUN(shape, Mode::MIN); \
  210. INT_RUN(shape, Mode::MAX); \
  211. INT_RUN(shape, Mode::ADD); \
  212. INT_RUN(shape, Mode::SUB); \
  213. INT_RUN(shape, Mode::MUL); \
  214. INT_RUN(shape, Mode::RMULH); \
  215. INT_RUN(shape, Mode::FUSE_ADD_RELU);
  216. #define FLOAT_BENCHMARK_CASES(shape) \
  217. FLOAT_RUN(shape, Mode::MIN); \
  218. FLOAT_RUN(shape, Mode::MAX); \
  219. FLOAT_RUN(shape, Mode::ADD); \
  220. FLOAT_RUN(shape, Mode::SUB); \
  221. FLOAT_RUN(shape, Mode::MUL); \
  222. FLOAT_RUN(shape, Mode::POW); \
  223. FLOAT_RUN(shape, Mode::TRUE_DIV); \
  224. FLOAT_RUN(shape, Mode::FUSE_ADD_RELU);
  225. using Mode = param::Elemwise::Mode;
  226. TensorShapeArray shapes = {{1, 112, 28, 28}, {1, 112, 28, 28}};
  227. BENCHMARK_CASES(shapes);
  228. shapes = {{1, 16, 1, 1}, {1, 16, 112, 112}};
  229. BENCHMARK_CASES(shapes);
  230. shapes = {{1, 448, 7, 7}, {1, 448, 7, 7}};
  231. BENCHMARK_CASES(shapes);
  232. #undef INT_BENCHMARK_CASES
  233. #undef FLOAT_BENCHMARK_CASES
  234. }
  235. TEST_F(X86, BENCHMARK_TERNARY_FMA3) {
  236. #define INT_BENCHMARK_CASES(shape) INT_RUN(shape, Mode::FUSE_MUL_ADD3);
  237. #define FLOAT_BENCHMARK_CASES(shape) FLOAT_RUN(shape, Mode::FUSE_MUL_ADD3);
  238. using Mode = param::Elemwise::Mode;
  239. TensorShapeArray shapes = {{30, 40, 70}, {30, 40, 70}, {30, 40, 70}};
  240. BENCHMARK_CASES(shapes);
  241. shapes = {{1, 4, 1, 1}, {3, 4, 5, 7}, {1, 4, 1, 1}};
  242. BENCHMARK_CASES(shapes);
  243. shapes = {{3, 4, 5, 7}, {3, 4, 5, 7}, {1, 1, 1, 1}};
  244. BENCHMARK_CASES(shapes);
  245. #undef INT_BENCHMARK_CASES
  246. #undef FLOAT_BENCHMARK_CASES
  247. }
  248. #undef BENCHMARK_CASES
  249. #undef INT_RUN
  250. #undef FLOAT_RUN
  251. #endif