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.

images2neibs.cpp 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * \file dnn/src/common/images2neibs.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 Images2NeibsBase::deduce_layout_fwd(const TensorLayout &src,
  15. TensorLayout &dst)
  16. {
  17. auto errmsg = [&]() {
  18. return megdnn_layout_msg(src) + ", " +
  19. "pad_h=" + std::to_string(param().pad_h) + ", " +
  20. "pad_w=" + std::to_string(param().pad_w) + ", " +
  21. "stride_h=" + std::to_string(param().stride_h) + ", " +
  22. "stride_w=" + std::to_string(param().stride_w) + ", " +
  23. "window_h=" + std::to_string(param().window_h) + ", " +
  24. "window_w=" + std::to_string(param().window_w);
  25. };
  26. MEGDNN_MARK_USED_VAR(errmsg);
  27. megdnn_assert_contiguous(src);
  28. megdnn_assert(src.ndim == 4_z, "%s", errmsg().c_str());
  29. size_t n = src[0], ic = src[1], ih = src[2], iw = src[3];
  30. size_t ph = this->param().pad_h;
  31. size_t pw = this->param().pad_w;
  32. size_t sh = this->param().stride_h;
  33. size_t sw = this->param().stride_w;
  34. size_t wh = this->param().window_h;
  35. size_t ww = this->param().window_w;
  36. size_t oh, ow;
  37. infer_conv_shape2d(ih, iw, wh, ww, sh, sw, ph, pw, oh, ow);
  38. dst = TensorLayout(TensorShape({n, ic, oh, ow, wh, ww}), src.dtype);
  39. }
  40. void Images2NeibsBase::check_layout_fwd(const TensorLayout &src,
  41. const TensorLayout &dst)
  42. {
  43. TensorLayout dst_expected;
  44. deduce_layout_fwd(src, dst_expected);
  45. megdnn_assert_eq_layout(dst_expected, dst);
  46. }
  47. void Images2NeibsForward::deduce_layout(const TensorLayout &src,
  48. TensorLayout &dst)
  49. {
  50. deduce_layout_fwd(src, dst);
  51. }
  52. void Images2NeibsForward::check_exec(const TensorLayout &src,
  53. const TensorLayout &dst,
  54. size_t workspace_in_bytes)
  55. {
  56. check_layout_fwd(src, dst);
  57. auto required_workspace_in_bytes = get_workspace_in_bytes(src, dst);
  58. megdnn_assert(workspace_in_bytes >= required_workspace_in_bytes);
  59. }
  60. void Images2NeibsBackward::check_exec(const TensorLayout &diff,
  61. const TensorLayout &grad,
  62. size_t workspace_in_bytes)
  63. {
  64. check_layout_fwd(grad, diff);
  65. auto required_workspace_in_bytes = get_workspace_in_bytes(grad, diff);
  66. megdnn_assert(workspace_in_bytes >= required_workspace_in_bytes);
  67. }
  68. } // namespace megdnn
  69. // vim: syntax=cpp.doxygen

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