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

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

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