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.

remap.cpp 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * \file dnn/src/common/remap.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
  10. * implied.
  11. */
  12. #include "megdnn/oprs.h"
  13. #include "src/common/cv/common.h"
  14. #include "src/common/cv/helper.h"
  15. #include "src/common/utils.h"
  16. namespace megdnn {
  17. void RemapBase::deduce_layout_fwd(
  18. const TensorLayout& src, const TensorLayout& map_xy, TensorLayout& dst) {
  19. dst.dtype = src.dtype;
  20. dst.ndim = src.ndim;
  21. dst.shape[0] = src.shape[0];
  22. size_t height_index, channel_index;
  23. if (param().format == param::Remap::Format::NHWC) {
  24. height_index = 1;
  25. channel_index = 3;
  26. } else {
  27. megdnn_assert(param().format == param::Remap::Format::NCHW);
  28. height_index = 2;
  29. channel_index = 1;
  30. }
  31. dst.shape[height_index] = map_xy.shape[1];
  32. dst.shape[height_index + 1] = map_xy.shape[2];
  33. dst.shape[channel_index] = src.shape[channel_index];
  34. }
  35. void RemapBase::check_layout_fwd(
  36. const TensorLayout& src, const TensorLayout& map_xy, const TensorLayout& dst) {
  37. auto errmsg = [&]() {
  38. return megdnn_layout_msg(src) + ", " + megdnn_layout_msg(map_xy) + ", " +
  39. megdnn_layout_msg(dst);
  40. };
  41. MEGDNN_MARK_USED_VAR(errmsg);
  42. megdnn_assert(src.ndim == map_xy.ndim && src.ndim == dst.ndim && src.ndim == 4);
  43. megdnn_assert(dst.dtype == src.dtype);
  44. megdnn_assert(dst.shape[0] == src.shape[0], "%s", errmsg().c_str());
  45. megdnn_assert(map_xy.shape[3] == 2);
  46. megdnn_assert(map_xy.shape[0] == src.shape[0]);
  47. megdnn_assert_contiguous(src);
  48. // map_xy only support floa32 type
  49. // map_xy always in NHWC format
  50. megdnn_assert(map_xy.dtype.enumv() == DTypeEnum::Float32);
  51. // In remap opr, H, W is same as H W in map_xy.
  52. if (param().format == param::Remap::Format::NHWC) {
  53. megdnn_assert(src.shape[3] == dst.shape[3], "%s", errmsg().c_str());
  54. megdnn_assert(
  55. dst.shape[2] == map_xy.shape[2] && dst.shape[1] == map_xy.shape[1],
  56. "%s", errmsg().c_str());
  57. } else if (param().format == param::Remap::Format::NCHW) {
  58. megdnn_assert(src.shape[1] == dst.shape[1], "%s", errmsg().c_str());
  59. megdnn_assert(
  60. dst.shape[2] == map_xy.shape[1] && dst.shape[3] == map_xy.shape[2],
  61. "%s", errmsg().c_str());
  62. } else {
  63. megdnn_throw(
  64. "currently do not support other param.format except NHWC and "
  65. "NCHW");
  66. }
  67. }
  68. void Remap::deduce_layout(
  69. const TensorLayout& src, const TensorLayout& map_xy, TensorLayout& dst) {
  70. deduce_layout_fwd(src, map_xy, dst);
  71. }
  72. void Remap::check_exec(
  73. const TensorLayout& src, const TensorLayout& map_xy, const TensorLayout& dst,
  74. size_t workspace_in_bytes) {
  75. check_layout_fwd(src, map_xy, dst);
  76. auto required_workspace_in_bytes = get_workspace_in_bytes(src, map_xy, dst);
  77. megdnn_assert(workspace_in_bytes >= required_workspace_in_bytes);
  78. }
  79. void RemapBackwardData::check_exec(
  80. const TensorLayout& map_xy, const TensorLayout& diff, const TensorLayout& grad,
  81. size_t workspace_in_bytes) {
  82. check_layout_fwd(grad, map_xy, diff);
  83. megdnn_assert(
  84. grad.dtype ==
  85. dtype::Float32() DNN_INC_FLOAT16(|| grad.dtype == dtype::BFloat16())
  86. DNN_INC_FLOAT16(|| grad.dtype == dtype::Float16()),
  87. "Backward Remap only supports Float32/BFloat16.");
  88. auto required_workspace_in_bytes = get_workspace_in_bytes(map_xy, diff, grad);
  89. megdnn_assert(workspace_in_bytes >= required_workspace_in_bytes);
  90. }
  91. void RemapBackwardMat::check_exec(
  92. const TensorLayout& src, const TensorLayout& map_xy, const TensorLayout& diff,
  93. const TensorLayout& grad, size_t workspace_in_bytes) {
  94. check_layout_fwd(src, map_xy, diff);
  95. megdnn_assert_eq_layout(map_xy, grad);
  96. megdnn_assert(
  97. grad.dtype ==
  98. dtype::Float32() DNN_INC_FLOAT16(|| grad.dtype == dtype::BFloat16())
  99. DNN_INC_FLOAT16(|| grad.dtype == dtype::Float16()),
  100. "Backward Remap only supports Float32/BFloat16.");
  101. auto required_workspace_in_bytes = get_workspace_in_bytes(src, map_xy, diff, grad);
  102. megdnn_assert(workspace_in_bytes >= required_workspace_in_bytes);
  103. }
  104. } // namespace megdnn
  105. // vim: syntax=cpp.doxygen

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