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

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

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

Contributors (1)