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.8 kB

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

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