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.

convolution.cpp 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * \file dnn/test/armv7/convolution.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/armv7/fixture.h"
  12. #include "test/common/benchmarker.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/convolution.h"
  15. #include "test/common/rng.h"
  16. using namespace megdnn;
  17. using namespace test;
  18. #if MEGDNN_WITH_BENCHMARK
  19. TEST_F(ARMV7, BENCHMARK_CONVOLUTION_STRIDE2) {
  20. using Param = param::Convolution;
  21. auto run = [&](const TensorShapeArray& shapes, Param param) {
  22. Benchmarker<Convolution> benchmarker_float(handle());
  23. size_t RUN = 100;
  24. auto tfloat = benchmarker_float.set_display(false)
  25. .set_times(RUN)
  26. .set_param(param)
  27. .exec(shapes);
  28. size_t IC = shapes[1][1];
  29. size_t FH = shapes[1][2];
  30. size_t FW = shapes[1][3];
  31. TensorLayout dst_layout;
  32. auto opr = handle()->create_operator<Convolution>();
  33. opr->param() = param;
  34. opr->deduce_layout(
  35. {shapes[0], dtype::Float32()}, {shapes[1], dtype::Float32()},
  36. dst_layout);
  37. printf("flops: %.3f mflops\n", (IC * dst_layout.total_nr_elems() * FH * FW *
  38. 2) / (tfloat / RUN * 1000));
  39. };
  40. auto profile = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  41. size_t stride) {
  42. Param param;
  43. param.stride_h = stride;
  44. param.stride_w = stride;
  45. param.pad_h = kernel / 2;
  46. param.pad_w = kernel / 2;
  47. printf("oc: %zd ic: %zd w: %zd h: %zd stride: %zd kernel_size: %zd\n", oc, ic,
  48. w, h, stride, kernel);
  49. run({{1, ic, h, w}, {oc, ic, kernel, kernel}, {}}, param);
  50. };
  51. for (size_t kernel : {2, 3, 5, 7}) {
  52. for (size_t ic : {3, 6, 12, 24}) {
  53. for (size_t oc : {3, 6, 12, 24}) {
  54. for (size_t size : {4, 7, 8, 14, 16, 17, 28, 32, 34, 64, 112}) {
  55. profile(oc, ic, size, size, kernel, 2);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. #endif
  62. TEST_F(ARMV7, BENCHMARK_CONVOLUTION_1X1) {
  63. int exec_times = 50;
  64. Benchmarker<MatrixMul> benchmarker_gemm(handle());
  65. benchmarker_gemm.set_times(exec_times);
  66. Benchmarker<Convolution> benchmarker(handle());
  67. benchmarker.set_times(exec_times);
  68. float mod = 1000 * exec_times / 1e9;
  69. auto run = [&](size_t IC, size_t OC, size_t H, size_t W) {
  70. float time = 1.f, perf = 1.f;
  71. std::cout << std::endl;
  72. std::cout << "CONV: IC " << IC << ", OC " << OC << ", H " << H << ", W " << W
  73. << std::endl;
  74. time = benchmarker.exec({{1, IC, H, W}, {OC, IC, 1, 1}, {1, OC, H, W}});
  75. perf = OC * (2 * H * W - 1) * IC / time * mod;
  76. std::cout << "Performance is " << perf << " Gflops" << std::endl;
  77. std::cout << "GEMM: (" << OC << ", " << H * W << ", " << IC << ")" << std::endl;
  78. // time = benchmarker_gemm.exec({{OC, H*W}, {H*W, IC}, {}});
  79. // perf = OC * (2 * H * W - 1) * IC / time * mod;
  80. time = benchmarker_gemm.exec({{OC, IC}, {IC, H * W}, {}});
  81. perf = OC * (2 * IC - 1) * H * W / time * mod;
  82. std::cout << "Performance is " << perf << " Gflops" << std::endl;
  83. };
  84. // run(32, 32, 64, 64);
  85. // run(8, 8, 32, 32);
  86. // run(32, 32, 128, 128);
  87. // run(32, 32, 512, 512);
  88. // run(10,10,2,5);
  89. // run(100,100,2,50);
  90. run(16, 4, 240, 135);
  91. run(8, 32, 120, 67);
  92. run(16, 64, 60, 33);
  93. run(1, 1, 28, 28);
  94. run(8, 1, 28, 28);
  95. run(2, 2, 28, 28);
  96. run(8, 2, 28, 28);
  97. run(4, 4, 28, 28);
  98. run(16, 4, 28, 28);
  99. }
  100. TEST_F(ARMV7, BENCHMARK_GROUP_CONVOLUTION_1X1) {
  101. int exec_times = 50;
  102. Benchmarker<Convolution> benchmarker_gconv1x1(handle());
  103. benchmarker_gconv1x1.set_times(exec_times);
  104. float mod = 1000 * exec_times / 1e9;
  105. auto run = [&](size_t IC, size_t OC, size_t H, size_t W, size_t group) {
  106. float time = 1.f, perf = 1.f;
  107. std::cout << std::endl;
  108. std::cout << "GCONV: IC " << IC << ", OC " << OC << ", H " << H << ", W " << W
  109. << ", GROUP " << group << std::endl;
  110. auto ICg = IC / group;
  111. auto OCg = OC / group;
  112. param::Convolution param;
  113. param.sparse = param::Convolution::Sparse::GROUP;
  114. time = benchmarker_gconv1x1.set_param(param).exec(
  115. {{1, IC, H, W}, {group, OCg, ICg, 1, 1}, {}});
  116. perf = group * OCg * ICg * H * W / time * mod;
  117. std::cout << "Performance is " << perf << " Gflops" << std::endl;
  118. };
  119. run(8 * 4, 1 * 4, 28, 28, 4);
  120. run(2 * 4, 2 * 4, 28, 28, 4);
  121. run(8 * 4, 2 * 4, 28, 28, 4);
  122. run(4 * 4, 4 * 4, 28, 28, 4);
  123. run(16 * 4, 4 * 4, 28, 28, 4);
  124. }
  125. // vim: syntax=cpp.doxygen