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.

relayout.cpp 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * \file dnn/test/aarch64/relayout.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/aarch64/fixture.h"
  12. #include "test/common/benchmarker.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/relayout.h"
  15. #include "test/common/rng.h"
  16. namespace megdnn {
  17. namespace test {
  18. namespace {
  19. template <typename tag>
  20. class AARCH64_RELAYOUT : public AARCH64 {};
  21. TYPED_TEST_CASE(AARCH64_RELAYOUT, relayout::test_types);
  22. TYPED_TEST(AARCH64_RELAYOUT, run) {
  23. relayout::run_test<TypeParam>(this->handle());
  24. }
  25. } // namespace
  26. TEST_F(AARCH64, Relayout) {
  27. Checker<Relayout> checker(handle());
  28. std::vector<::megdnn::DType> dtype_vec;
  29. dtype_vec.push_back(dtype::Float32());
  30. dtype_vec.push_back(dtype::Int16());
  31. dtype_vec.push_back(dtype::Uint16());
  32. dtype_vec.push_back(dtype::Int8());
  33. for (auto dtype : dtype_vec) {
  34. TensorLayout src({1, 54, 112, 256}, {54, 1, 16384, 64}, dtype);
  35. TensorLayout dst({1, 54, 112, 256}, {1548288, 28672, 256, 1}, dtype);
  36. checker.execl({src, dst});
  37. }
  38. }
  39. TEST_F(AARCH64, RelayoutNonContig) {
  40. Checker<Relayout> checker(handle());
  41. std::vector<::megdnn::DType> dtype_vec;
  42. dtype_vec.push_back(dtype::Float32());
  43. dtype_vec.push_back(dtype::Int16());
  44. dtype_vec.push_back(dtype::Uint16());
  45. dtype_vec.push_back(dtype::Int8());
  46. for (auto dtype : dtype_vec) {
  47. TensorLayout src({4, 90, 15, 29}, {41760, 1, 2784, 96}, dtype);
  48. TensorLayout dst({4, 90, 15, 29}, {39150, 435, 29, 1}, dtype);
  49. checker.execl({src, dst});
  50. }
  51. }
  52. TEST_F(AARCH64, RelayoutBig) {
  53. Checker<Relayout> checker(handle());
  54. ConsecutiveRNG rng;
  55. checker.set_rng(0, &rng);
  56. int m = 512;
  57. int n = 512;
  58. TensorLayout src({(size_t)m, (size_t)n}, {1, n}, dtype::Float32());
  59. TensorLayout dst({(size_t)m, (size_t)n}, {n, 1}, dtype::Float32());
  60. checker.execl({src, dst});
  61. }
  62. #if MEGDNN_WITH_BENCHMARK
  63. TEST_F(AARCH64, BENCHMARK_Relayout) {
  64. constexpr size_t WARM_RUNS = 100;
  65. constexpr size_t RUNS = 600;
  66. auto dtype = dtype::Float32();
  67. Benchmarker<Relayout> benchmarker_relayout(handle());
  68. Benchmarker<Relayout> benchmarker_fbk_relayout(fallback_handle());
  69. benchmarker_relayout.set_times(WARM_RUNS);
  70. benchmarker_fbk_relayout.set_times(WARM_RUNS);
  71. int m = 512;
  72. int n = 512;
  73. TensorLayout src({(size_t)m, (size_t)n}, {1, n}, dtype);
  74. TensorLayout dst({(size_t)m, (size_t)n}, {n, 1}, dtype);
  75. TensorLayoutArray tensor_case;
  76. tensor_case.push_back(src);
  77. tensor_case.push_back(dst);
  78. benchmarker_relayout.exec(tensor_case);
  79. benchmarker_fbk_relayout.exec(tensor_case);
  80. benchmarker_relayout.set_times(RUNS);
  81. benchmarker_fbk_relayout.set_times(RUNS);
  82. auto used = benchmarker_relayout.exec(tensor_case) / RUNS;
  83. auto fbk_used = benchmarker_fbk_relayout.exec(tensor_case) / RUNS;
  84. float bw = 2.f * m * n * 1e-6 / used * dtype.size();
  85. float fbk_bw = 2.f * m * n * 1e-6 / fbk_used * dtype.size();
  86. printf("run: %s -> %s , %f GB/s, fbk %f GB/s, speedup %f\n",
  87. src.to_string().c_str(), dst.to_string().c_str(), bw, fbk_bw, bw / fbk_bw);
  88. }
  89. TEST_F(AARCH64, BENCHMARK_Relayout_2) {
  90. constexpr size_t WARM_RUNS = 100;
  91. constexpr size_t RUNS = 600;
  92. auto dtype = dtype::Float32();
  93. Benchmarker<Relayout> benchmarker_relayout(handle());
  94. Benchmarker<Relayout> benchmarker_fbk_relayout(fallback_handle());
  95. benchmarker_relayout.set_times(WARM_RUNS);
  96. benchmarker_fbk_relayout.set_times(WARM_RUNS);
  97. int m = 54;
  98. int n = 28762;
  99. TensorLayout src({1, 54, 112, 256}, {54, 1, 16384, 64}, dtype);
  100. TensorLayout dst({1, 54, 112, 256}, {1548288, 28672, 256, 1}, dtype);
  101. TensorLayoutArray tensor_case;
  102. tensor_case.push_back(src);
  103. tensor_case.push_back(dst);
  104. benchmarker_relayout.exec(tensor_case);
  105. benchmarker_fbk_relayout.exec(tensor_case);
  106. benchmarker_relayout.set_times(RUNS);
  107. benchmarker_fbk_relayout.set_times(RUNS);
  108. auto used = benchmarker_relayout.exec(tensor_case) / RUNS;
  109. auto fbk_used = benchmarker_fbk_relayout.exec(tensor_case) / RUNS;
  110. float bw = 2.f * m * n * 1e-6 / used * dtype.size();
  111. float fbk_bw = 2.f * m * n * 1e-6 / fbk_used * dtype.size();
  112. printf("run: %s -> %s , %f GB/s, fbk %f GB/s, speedup %f\n",
  113. src.to_string().c_str(), dst.to_string().c_str(), bw, fbk_bw, bw / fbk_bw);
  114. }
  115. #endif
  116. } // namespace test
  117. } // namespace megdnn
  118. // vim: syntax=cpp.doxygen

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