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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "test/fallback/fixture.h"
  2. #include "test/common/checker.h"
  3. #include "test/common/relayout.h"
  4. #include "test/common/tensor.h"
  5. #include <ctime>
  6. #include "megdnn/basic_types.h"
  7. #include "test/common/task_record_check.h"
  8. using namespace megdnn;
  9. using namespace test;
  10. namespace {
  11. template <typename tag>
  12. class FALLBACK_RELAYOUT : public FALLBACK {};
  13. TYPED_TEST_CASE(FALLBACK_RELAYOUT, relayout::test_types);
  14. TYPED_TEST(FALLBACK_RELAYOUT, run) {
  15. relayout::run_test<TypeParam>(this->handle());
  16. }
  17. } // namespace
  18. TEST_F(FALLBACK, RELAYOUT_CONTINUE) {
  19. Checker<Relayout> checker(handle());
  20. checker.set_dtype(0, dtype::Int32());
  21. checker.set_dtype(1, dtype::Int32());
  22. checker.exec({{2, 2, 2}, {2, 2, 2}});
  23. }
  24. TEST_F(FALLBACK, RELAYOUT_RECORD) {
  25. TaskRecordChecker<Relayout> checker(1);
  26. checker.set_dtype(0, dtype::Int32());
  27. checker.set_dtype(1, dtype::Int32());
  28. checker.exec({{2, 2, 2}, {2, 2, 2}});
  29. }
  30. #if MEGDNN_WITH_BENCHMARK
  31. TEST_F(FALLBACK, BENCHMARK_RELAYOUT_CV) {
  32. relayout::run_cv_benchmark(handle());
  33. }
  34. TEST_F(FALLBACK, BENCHMARK_RELAYOUT) {
  35. auto naive_handle = create_cpu_handle(2);
  36. bool verbose = false;
  37. auto run = [&](bool out_cont, const TensorLayout& cont_layout,
  38. const TensorLayout& noncont_layout) {
  39. megdnn_assert(
  40. cont_layout.dtype == dtype::Int32() &&
  41. noncont_layout.dtype == dtype::Int32() &&
  42. noncont_layout.span().low_byte == 0);
  43. auto noncont_storage_size = noncont_layout.span().high_elem;
  44. Tensor<dt_int32> noncont_storage0(
  45. handle(), {{noncont_storage_size}, dtype::Int32()}),
  46. noncont_storage1(handle(), {{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), t0 = timeit(opr_cur.get(), dst0);
  85. double tot_size_gb_ms = cont_layout.total_nr_elems() * sizeof(int) / 1024.0 /
  86. 1024.0 / 1024.0 * 1e3;
  87. if (verbose) {
  88. printf("noncont-%zu dir=%d: fallback=%7.3fms,%5.2fGiB/s "
  89. "naive=%7.3fms,%5.2fGiB/s\n",
  90. noncont_layout.collapse_contiguous().ndim, out_cont, t0,
  91. tot_size_gb_ms / t0, t1, tot_size_gb_ms / t1);
  92. }
  93. ASSERT_EQ(
  94. 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. #endif
  143. // vim: syntax=cpp.doxygen