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.

backward_data.cpp 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * \file dnn/src/cuda/remap/backward_data.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
  10. * implied.
  11. */
  12. #include "src/cuda/remap/common.h"
  13. #include "src/cuda/remap/opr_impl.h"
  14. #include "src/cuda/utils.h"
  15. using namespace megdnn;
  16. using namespace cuda;
  17. void RemapBackwardDataImpl::exec(_megdnn_tensor_in map_xy,
  18. _megdnn_tensor_in diff,
  19. _megdnn_tensor_out grad,
  20. _megdnn_workspace workspace) {
  21. check_exec(map_xy.layout, diff.layout, grad.layout, workspace.size);
  22. megdnn_assert(param().imode == param::Remap::InterpolationMode::LINEAR,
  23. "only support LINEAR interpolationMode");
  24. megdnn_assert(param().format == param::Remap::Format::NCHW,
  25. "only support NCHW format for remap backward");
  26. auto stream = cuda_stream(this->handle());
  27. int N, C, IH, IW, OH, OW;
  28. N = grad.layout.shape[0];
  29. C = grad.layout.shape[1];
  30. IH = grad.layout.shape[2];
  31. IW = grad.layout.shape[3];
  32. OH = map_xy.layout.shape[1];
  33. OW = map_xy.layout.shape[2];
  34. #define cb(dt, _format, bmode) \
  35. if (param().format == param::Remap::Format::_format && \
  36. param().border_type == param::Remap::BorderMode::bmode) { \
  37. using ctype = DTypeTrait<dt>::ctype; \
  38. remap::backwarddata_proxy<ctype, param_enumv::Remap::Format::_format, \
  39. ::BorderMode::BORDER_##bmode>( \
  40. grad.compatible_ptr<ctype>(), \
  41. map_xy.compatible_ptr<dt_float32>(), \
  42. diff.compatible_ptr<ctype>(), N, C, IH, IW, OH, OW, stream); \
  43. break; \
  44. }
  45. #define support_dtype(dt) \
  46. case DTypeTrait<dt>::enumv: { \
  47. cb(dt, NCHW, CONSTANT); \
  48. cb(dt, NCHW, REPLICATE); \
  49. cb(dt, NCHW, REFLECT); \
  50. cb(dt, NCHW, REFLECT_101); \
  51. cb(dt, NCHW, WRAP); \
  52. megdnn_throw("unsupported border type in remap cuda"); \
  53. }
  54. switch (grad.layout.dtype.enumv()) {
  55. support_dtype(dtype::Float32);
  56. support_dtype(dtype::BFloat16);
  57. default:
  58. megdnn_throw("unsupported dtype in remap backward cuda\n");
  59. }
  60. #undef support_dtype
  61. #undef cb
  62. }
  63. // vim: syntax=cpp.doxygen

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