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.

separableConv.cpp 3.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * \file dnn/src/common/separableConv.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 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 "megdnn/oprs.h"
  12. #include "src/common/utils.h"
  13. namespace megdnn {
  14. void SeparableConvBase::deduce_layout_fwd(
  15. const TensorLayout& src, const TensorLayout& filter_x,
  16. const TensorLayout& filter_y, TensorLayout& dst) {
  17. auto errmsg = [&]() {
  18. return megdnn_layout_msg(src) + ", " + megdnn_layout_msg(filter_x) + ", " +
  19. megdnn_layout_msg(dst) + ", " + "is_xcorr=" + "borderMode=" +
  20. std::to_string((param().mode == Mode::CROSS_CORRELATION)) + ", " +
  21. std::to_string((int)(param().borderMode)) + ", " +
  22. "pad_h=" + std::to_string(param().pad_h) + ", " +
  23. "pad_w=" + std::to_string(param().pad_w) + ", " +
  24. "stride_h=" + std::to_string(param().stride_h) + ", " +
  25. "stride_w=" + std::to_string(param().stride_w);
  26. };
  27. MEGDNN_MARK_USED_VAR(errmsg);
  28. megdnn_assert_contiguous(src);
  29. megdnn_assert_contiguous(filter_x);
  30. megdnn_assert(src.ndim == 4_z, "%s", errmsg().c_str());
  31. megdnn_assert(filter_x.ndim == 4_z, "%s", errmsg().c_str());
  32. size_t n = src[0];
  33. size_t ic = src[1];
  34. size_t ih = src[2];
  35. size_t iw = src[3];
  36. size_t oc = filter_x[0];
  37. megdnn_assert_eq_layout(filter_x, filter_y);
  38. megdnn_assert(filter_x[1] == ic, "%s", errmsg().c_str());
  39. size_t fw = filter_x[3];
  40. size_t fh = fw;
  41. size_t sh = this->param().stride_h;
  42. size_t sw = this->param().stride_w;
  43. size_t ph = this->param().pad_h;
  44. size_t pw = this->param().pad_w;
  45. size_t oh, ow;
  46. infer_conv_shape2d(ih, iw, fh, fw, sh, sw, ph, pw, oh, ow);
  47. dst = TensorLayout(TensorShape({n, oc, oh, ow}), src.dtype);
  48. }
  49. void SeparableConvBase::check_layout_fwd(
  50. const TensorLayout& src, const TensorLayout& filter_x,
  51. const TensorLayout& filter_y, const TensorLayout& dst) {
  52. TensorLayout dst_expected;
  53. megdnn_assert_eq_dtype(src, filter_x);
  54. megdnn_assert_eq_dtype(src, filter_y);
  55. megdnn_assert_eq_layout(filter_x, filter_y);
  56. megdnn_assert_eq_dtype(src, dst);
  57. deduce_layout_fwd(src, filter_x, filter_y, dst_expected);
  58. megdnn_assert_eq_layout(dst_expected, dst);
  59. }
  60. void SeparableConvForward::deduce_layout(
  61. const TensorLayout& src, const TensorLayout& filter_x,
  62. const TensorLayout& filter_y, TensorLayout& dst) {
  63. deduce_layout_fwd(src, filter_x, filter_y, dst);
  64. }
  65. void SeparableConvForward::check_exec(
  66. const TensorLayout& src, const TensorLayout& filter_x,
  67. const TensorLayout& filter_y, const TensorLayout& dst,
  68. size_t workspace_in_bytes) {
  69. check_layout_fwd(src, filter_x, filter_y, dst);
  70. auto required_workspace_in_bytes =
  71. get_workspace_in_bytes(src, filter_x, filter_y, dst);
  72. megdnn_assert(workspace_in_bytes >= required_workspace_in_bytes);
  73. }
  74. } // namespace megdnn
  75. // vim: syntax=cpp.doxygen