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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * \file dnn/test/fallback/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/fallback/fixture.h"
  12. #include "test/common/checker.h"
  13. #include "test/common/relayout.h"
  14. #include "test/common/tensor.h"
  15. #include "megdnn/basic_types.h"
  16. #include <ctime>
  17. using namespace megdnn;
  18. using namespace test;
  19. namespace {
  20. template <typename tag>
  21. class FALLBACK_RELAYOUT : public FALLBACK {};
  22. TYPED_TEST_CASE(FALLBACK_RELAYOUT, relayout::test_types);
  23. TYPED_TEST(FALLBACK_RELAYOUT, run) {
  24. relayout::run_test<TypeParam>(this->handle());
  25. }
  26. } // namespace
  27. #if MEGDNN_WITH_BENCHMARK
  28. TEST_F(FALLBACK, BENCHMARK_RELAYOUT_CV) {
  29. relayout::run_cv_benchmark(handle());
  30. }
  31. TEST_F(FALLBACK, BENCHMARK_RELAYOUT) {
  32. auto naive_handle = create_cpu_handle(2);
  33. bool verbose = false;
  34. auto run = [&](bool out_cont, const TensorLayout& cont_layout,
  35. const TensorLayout& noncont_layout) {
  36. megdnn_assert(
  37. cont_layout.dtype == dtype::Int32() &&
  38. noncont_layout.dtype == dtype::Int32() &&
  39. noncont_layout.span().low_byte == 0);
  40. auto noncont_storage_size = noncont_layout.span().high_elem;
  41. Tensor<dt_int32> noncont_storage0(
  42. handle(), {{noncont_storage_size}, dtype::Int32()}),
  43. noncont_storage1(handle(), {{noncont_storage_size}, dtype::Int32()}),
  44. cont_storage0(handle(), cont_layout),
  45. cont_storage1(handle(), cont_layout);
  46. auto noncont0 = noncont_storage0.tensornd(),
  47. noncont1 = noncont_storage1.tensornd();
  48. noncont0.layout = noncont_layout;
  49. noncont1.layout = noncont_layout;
  50. TensorND src, dst0, dst1;
  51. if (out_cont) {
  52. src = noncont0;
  53. dst0 = cont_storage0.tensornd();
  54. dst1 = cont_storage1.tensornd();
  55. auto ptr = src.ptr<int>();
  56. for (size_t i = 0; i < noncont_storage_size; ++i) {
  57. ptr[i] = i;
  58. }
  59. } else {
  60. memset(noncont_storage0.ptr(), -1,
  61. noncont_storage0.layout().span().dist_byte());
  62. memset(noncont_storage1.ptr(), -1,
  63. noncont_storage1.layout().span().dist_byte());
  64. src = cont_storage0.tensornd();
  65. dst0 = noncont0;
  66. dst1 = noncont1;
  67. auto ptr = src.ptr<int>();
  68. for (size_t i = 0, it = src.layout.total_nr_elems(); i < it; ++i) {
  69. ptr[i] = i;
  70. }
  71. }
  72. auto opr_cur = handle()->create_operator<Relayout>();
  73. auto opr_naive = naive_handle->create_operator<Relayout>();
  74. auto timeit = [&src](Relayout* opr, TensorND out) {
  75. opr->exec(src, out);
  76. auto start = clock();
  77. opr->exec(src, out);
  78. auto stop = clock();
  79. return (stop - start) * 1e3 / CLOCKS_PER_SEC;
  80. };
  81. auto t1 = timeit(opr_naive.get(), dst1), t0 = timeit(opr_cur.get(), dst0);
  82. double tot_size_gb_ms = cont_layout.total_nr_elems() * sizeof(int) / 1024.0 /
  83. 1024.0 / 1024.0 * 1e3;
  84. if (verbose) {
  85. printf("noncont-%zu dir=%d: fallback=%7.3fms,%5.2fGiB/s "
  86. "naive=%7.3fms,%5.2fGiB/s\n",
  87. noncont_layout.collapse_contiguous().ndim, out_cont, t0,
  88. tot_size_gb_ms / t0, t1, tot_size_gb_ms / t1);
  89. }
  90. ASSERT_EQ(
  91. 0, memcmp(dst0.ptr<int>(), dst1.ptr<int>(),
  92. dst0.layout.span().dist_byte()));
  93. };
  94. auto run_preset = [&](const TensorShape& noncont_shp, int swap, bool sub,
  95. bool out_cont) {
  96. TensorLayout noncont_layout(noncont_shp, dtype::Int32());
  97. if (swap) {
  98. auto a = swap - 1, b = swap;
  99. std::swap(noncont_layout.shape[a], noncont_layout.shape[b]);
  100. std::swap(noncont_layout.stride[a], noncont_layout.stride[b]);
  101. }
  102. TensorLayout cont_layout = noncont_layout;
  103. cont_layout.init_contiguous_stride();
  104. TensorShape noncont_storage_shp(cont_layout);
  105. if (sub) {
  106. ++noncont_storage_shp[noncont_layout.ndim - 1];
  107. noncont_layout.init_contiguous_stride(noncont_storage_shp);
  108. --noncont_layout.shape[noncont_layout.ndim - 1];
  109. }
  110. run(out_cont, cont_layout, noncont_layout);
  111. };
  112. for (bool out_cont : {false, true}) {
  113. verbose = false;
  114. run_preset({2, 3}, 1, false, out_cont);
  115. run_preset({2, 2, 2}, 0, true, out_cont);
  116. {
  117. // padding-like
  118. TensorLayout cont{{2, 3, 3}, dtype::Int32()}, noncont = cont;
  119. noncont.stride[1] = 5;
  120. noncont.stride[0] = 25;
  121. run(out_cont, cont, noncont);
  122. }
  123. verbose = true;
  124. run_preset({1234, 5678}, 0, false, out_cont);
  125. run_preset({256, 256, 256}, 0, true, out_cont);
  126. run_preset({2, 3, 1024, 1024}, 1, false, out_cont);
  127. run_preset({1025, 2049}, 1, false, out_cont);
  128. run_preset({2049, 1025}, 1, false, out_cont);
  129. run_preset({10, 1024, 1024}, 2, false, out_cont);
  130. {
  131. // padding-like
  132. TensorLayout cont{{60, 60, 60}, dtype::Int32()}, noncont = cont;
  133. noncont.stride[1] = 63;
  134. noncont.stride[0] = 63 * 63;
  135. run(out_cont, cont, noncont);
  136. }
  137. }
  138. }
  139. #endif
  140. // vim: syntax=cpp.doxygen

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