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.

pooling.cpp 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "megdnn/oprs.h"
  2. #include "src/common/utils.h"
  3. namespace megdnn {
  4. void PoolingBase::deduce_layout_impl(
  5. const TensorLayout& src, const Param& param, TensorLayout& dst) {
  6. auto pformat = param.format;
  7. // the overhead of generating error message is about 18x of the other part of this
  8. // function so we use a function to wrap the error message and get it only when need.
  9. auto get_errmsg = [&](void) -> std::string {
  10. std::string errmsg =
  11. megdnn_layout_msg(src) + ", " + megdnn_layout_msg(dst) + ", " +
  12. "pad_h=" + std::to_string(param.pad_h) + ", " +
  13. "pad_w=" + std::to_string(param.pad_w) + ", " +
  14. "stride_h=" + std::to_string(param.stride_h) + ", " +
  15. "stride_w=" + std::to_string(param.stride_w) + ", " +
  16. "window_h=" + std::to_string(param.window_h) + ", " +
  17. "window_w=" + std::to_string(param.window_w) + ", " +
  18. "is_max=" + std::to_string(param.mode == Mode::MAX) + ", " +
  19. "is_nhwc=" + std::to_string(pformat == Param::Format::NHWC) + ", " +
  20. "is_nhwcd4=" + std::to_string(pformat == Param::Format::NHWCD4);
  21. return errmsg;
  22. };
  23. MEGDNN_MARK_USED_VAR(get_errmsg);
  24. megdnn_assert_contiguous(src);
  25. size_t spatial_pos, c_pos, batch_pos = 0;
  26. if (pformat == Param::Format::NCHW) {
  27. megdnn_assert(src.ndim == 4_z, "%s", get_errmsg().c_str());
  28. spatial_pos = 2;
  29. c_pos = 1;
  30. } else if (pformat == Param::Format::NHWC) {
  31. megdnn_assert(src.ndim == 4_z, "%s", get_errmsg().c_str());
  32. spatial_pos = 1;
  33. c_pos = 3;
  34. } else if (
  35. pformat == Param::Format::NCHW4 || pformat == Param::Format::NCHW44 ||
  36. pformat == Param::Format::NCHW88 || pformat == Param::Format::NCHW32 ||
  37. pformat == Param::Format::NCHW64) {
  38. megdnn_assert(src.ndim == 5_z, "%s", get_errmsg().c_str());
  39. spatial_pos = 2;
  40. c_pos = 1;
  41. } else if (pformat == Param::Format::CHWN4) {
  42. spatial_pos = 1;
  43. c_pos = 0;
  44. batch_pos = 3;
  45. } else {
  46. megdnn_assert(
  47. pformat == Param::Format::NHWCD4 && src.ndim == 5_z, "%s",
  48. get_errmsg().c_str());
  49. spatial_pos = 1;
  50. c_pos = 2;
  51. }
  52. size_t n = src[batch_pos];
  53. size_t c = src[c_pos];
  54. size_t ih = src[spatial_pos];
  55. size_t iw = src[spatial_pos + 1];
  56. if (pformat == Param::Format::NHWCD4) {
  57. c *= 4;
  58. iw = src[spatial_pos + 2];
  59. }
  60. if (pformat == Param::Format::NCHW4 || pformat == Param::Format::NCHW44 ||
  61. pformat == Param::Format::CHWN4) {
  62. c *= 4;
  63. }
  64. if (pformat == Param::Format::NCHW88) {
  65. c *= 8;
  66. }
  67. if (pformat == Param::Format::NCHW32) {
  68. c *= 32;
  69. }
  70. if (pformat == Param::Format::NCHW64) {
  71. c *= 64;
  72. }
  73. size_t oh, ow;
  74. size_t fh = param.window_h;
  75. size_t fw = param.window_w;
  76. size_t sh = param.stride_h;
  77. size_t sw = param.stride_w;
  78. size_t ph = param.pad_h;
  79. size_t pw = param.pad_w;
  80. // moving some python assert to here
  81. // megdnn_assert()
  82. if (ph >= fh || pw >= fw) {
  83. megdnn_log_warn(
  84. "pooling padding size (%zu %zu) should not be bigger than "
  85. "window size (%zu %zu), it only can be used in CaffePooling",
  86. pw, ph, fw, fh);
  87. }
  88. infer_conv_shape2d(ih, iw, fh, fw, sh, sw, ph, pw, oh, ow);
  89. if (pformat == Param::Format::NCHW) {
  90. dst = TensorLayout(TensorShape({n, c, oh, ow}), src.dtype);
  91. } else if (pformat == Param::Format::NHWC) {
  92. megdnn_assert(pformat == Param::Format::NHWC, "invalid pooling format");
  93. dst = TensorLayout({n, oh, ow, c}, src.dtype, src.format);
  94. } else if (pformat == Param::Format::NCHW4 || pformat == Param::Format::NCHW44) {
  95. dst = TensorLayout{{n, c / 4, oh, ow, 4}, src.dtype, src.format};
  96. } else if (pformat == Param::Format::NCHW88) {
  97. dst = TensorLayout{{n, c / 8, oh, ow, 8}, src.dtype, src.format};
  98. } else if (pformat == Param::Format::NCHW32) {
  99. dst = TensorLayout{{n, c / 32, oh, ow, 32}, src.dtype, src.format};
  100. } else if (pformat == Param::Format::NCHW64) {
  101. dst = TensorLayout{{n, c / 64, oh, ow, 64}, src.dtype, src.format};
  102. } else if (pformat == Param::Format::CHWN4) {
  103. dst = TensorLayout{{c / 4, oh, ow, n, 4}, src.dtype, src.format};
  104. } else {
  105. megdnn_assert(pformat == Param::Format::NHWCD4, "invalid pooling format");
  106. dst = TensorLayout{{n, oh, c / 4, ow, 4}, src.dtype, src.format};
  107. }
  108. }
  109. void PoolingBase::deduce_layout_fwd(const TensorLayout& src, TensorLayout& dst) {
  110. deduce_layout_impl(src, param(), dst);
  111. }
  112. void PoolingBase::check_layout_fwd(const TensorLayout& src, const TensorLayout& dst) {
  113. TensorLayout dst_expected;
  114. megdnn_assert_eq_dtype(src, dst);
  115. deduce_layout_fwd(src, dst_expected);
  116. megdnn_assert_eq_layout(dst_expected, dst);
  117. megdnn_assert(
  118. src.dtype.category() == DTypeCategory::FLOAT ||
  119. src.dtype == dtype::Int8() ||
  120. src.dtype.category() == DTypeCategory::QUANTIZED);
  121. }
  122. void PoolingForward::deduce_layout(const TensorLayout& src, TensorLayout& dst) {
  123. deduce_layout_fwd(src, dst);
  124. }
  125. void PoolingForward::check_exec(
  126. const TensorLayout& src, const TensorLayout& dst, size_t workspace_in_bytes) {
  127. check_layout_fwd(src, dst);
  128. auto required_workspace_in_bytes = get_workspace_in_bytes(src, dst);
  129. megdnn_assert(workspace_in_bytes >= required_workspace_in_bytes);
  130. }
  131. void PoolingBackward::check_exec(
  132. const TensorLayout& src, const TensorLayout& dst, const TensorLayout& diff,
  133. const TensorLayout& grad, size_t workspace_in_bytes) {
  134. check_layout_fwd(src, dst);
  135. megdnn_assert_eq_layout(src, grad);
  136. megdnn_assert_eq_layout(dst, diff);
  137. auto required_workspace_in_bytes = get_workspace_in_bytes(src, dst, diff, grad);
  138. megdnn_assert(workspace_in_bytes >= required_workspace_in_bytes);
  139. }
  140. } // namespace megdnn
  141. // vim: syntax=cpp.doxygen