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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * \file dnn/src/common/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
  10. * implied.
  11. */
  12. #include "megdnn/oprs.h"
  13. #include "src/common/relayout_helper.h"
  14. #include "src/common/utils.h"
  15. #include <algorithm>
  16. using namespace megdnn;
  17. using namespace megdnn::relayout;
  18. namespace {
  19. //! whether current shape is [b][n][m][c] and is a transpose of contig
  20. //! [b][m][n][c]
  21. bool is_transpose_single(const TensorLayout& layout, TransposeParam& p) {
  22. /*
  23. * assuming contig layout is:
  24. * shape: b, m, n, c
  25. * stride: mnc, nc, c, 1
  26. *
  27. * then given layout should be:
  28. * shape: b, n, m, c
  29. * stride: mnc, c, nc, 1
  30. *
  31. * if c == 1:
  32. * shape: b, n, m
  33. * stride: mn, 1, n
  34. * if b == 1:
  35. * shape: n, m, c
  36. * stride: c, nc, 1
  37. *
  38. * if b == 1 && c == 1:
  39. * shape: n, m
  40. * stride: 1, n
  41. */
  42. auto strd = [&](size_t idx, ptrdiff_t v) {
  43. return layout.stride[idx] == v;
  44. };
  45. if (layout.ndim == 4) {
  46. p.batch = layout[0];
  47. p.n = layout[1];
  48. p.m = layout[2];
  49. p.c = layout[3];
  50. if (strd(3, 1) && strd(1, p.c)) {
  51. auto t = p.c * p.n;
  52. return strd(2, t) && strd(0, t * p.m);
  53. }
  54. return false;
  55. }
  56. if (layout.ndim == 3) {
  57. if (strd(1, 1)) {
  58. // c == 1
  59. p.batch = layout[0];
  60. p.n = layout[1];
  61. p.m = layout[2];
  62. p.c = 1;
  63. return strd(2, p.n) && strd(0, p.m * p.n);
  64. }
  65. if (strd(2, 1)) {
  66. // b == 1
  67. p.batch = 1;
  68. p.n = layout[0];
  69. p.m = layout[1];
  70. p.c = layout[2];
  71. return strd(0, p.c) && strd(1, p.n * p.c);
  72. }
  73. return false;
  74. }
  75. if (layout.ndim == 2) {
  76. p.batch = 1;
  77. p.n = layout.shape[0];
  78. p.m = layout.shape[1];
  79. p.c = 1;
  80. return strd(0, 1) && strd(1, p.n);
  81. }
  82. return false;
  83. }
  84. } // anonymous namespace
  85. void RelayoutForward::check_layout_and_canonize(TensorLayout& src,
  86. TensorLayout& dst) {
  87. megdnn_assert(dst.is_non_overlapping_strong());
  88. src = src.collapse_contiguous();
  89. dst = dst.collapse_contiguous();
  90. megdnn_assert(src.dtype == dst.dtype &&
  91. src.total_nr_elems() == dst.total_nr_elems(),
  92. "check %s == %s and %zu == %zu", src.dtype.name(),
  93. dst.dtype.name(), src.total_nr_elems(), dst.total_nr_elems());
  94. }
  95. bool relayout::is_transpose(const TensorLayout& src, const TensorLayout& dst,
  96. TransposeParam& p) {
  97. if (is_contig(dst) && is_transpose_single(src, p)) {
  98. // if the original intention is to transpose (m, n) to (n, m),
  99. // then we should use (n, m) as the contig dst and use a corrsponding
  100. // non-contig src with the same (n, m) shape (remember relayout is
  101. // defined on element correspondence on the logical view)
  102. return true;
  103. }
  104. if (is_contig(src) && is_transpose_single(dst, p)) {
  105. std::swap(p.m, p.n);
  106. return true;
  107. }
  108. return false;
  109. }
  110. // vim: syntax=cpp.doxygen

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