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.

opr_impl.cpp 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * \file dnn/src/cuda/roi_pooling/opr_impl.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 "src/cuda/roi_pooling/opr_impl.h"
  12. #include "src/cuda/roi_pooling/roi_pooling.cuh"
  13. #include "src/cuda/utils.h"
  14. #include "src/common/roi_pooling_helper.h"
  15. namespace megdnn {
  16. namespace cuda {
  17. void ROIPoolingForwardImpl::exec(_megdnn_tensor_in src,
  18. _megdnn_tensor_in rois,
  19. _megdnn_tensor_out dst,
  20. _megdnn_tensor_out index,
  21. _megdnn_workspace workspace)
  22. {
  23. check_exec(src.layout, rois.layout, dst.layout, index.layout,
  24. workspace.size);
  25. auto stream = cuda_stream(handle());
  26. auto nthreads = dst.layout.total_nr_elems();
  27. auto spatial_scale = m_param.scale;
  28. auto channels = src.layout.shape[1];
  29. auto height = src.layout.shape[2];
  30. auto width = src.layout.shape[3];
  31. auto pooled_height = dst.layout.shape[2];
  32. auto pooled_width = dst.layout.shape[3];
  33. using namespace ::megdnn::roi_pooling;
  34. using namespace ::megdnn::cuda::roi_pooling;
  35. #define cb(DType) \
  36. if (src.layout.dtype == DType()) { \
  37. using T = typename DTypeTrait<DType>::ctype; \
  38. switch (param().mode) { \
  39. case param::ROIPooling::Mode::MAX: \
  40. forward_proxy<T, MaxPooler<T>>(nthreads, \
  41. src.ptr<T>(), spatial_scale, channels, height, width, \
  42. pooled_height, pooled_width, \
  43. rois.ptr<T>(), dst.ptr<T>(), \
  44. index.ptr<dt_int32>(), \
  45. stream); \
  46. break; \
  47. case param::ROIPooling::Mode::AVERAGE: \
  48. forward_proxy<T, AveragePooler<T>>(nthreads, \
  49. src.ptr<T>(), spatial_scale, channels, height, width, \
  50. pooled_height, pooled_width, \
  51. rois.ptr<T>(), dst.ptr<T>(), \
  52. index.ptr<dt_int32>(), \
  53. stream); \
  54. break; \
  55. default: \
  56. megdnn_assert_internal(false); \
  57. } \
  58. }
  59. MEGDNN_FOREACH_COMPUTING_DTYPE_FLOAT(cb)
  60. #undef cb
  61. }
  62. void ROIPoolingBackwardImpl::exec(_megdnn_tensor_in diff,
  63. _megdnn_tensor_in src,
  64. _megdnn_tensor_in rois,
  65. _megdnn_tensor_in index,
  66. _megdnn_tensor_out grad,
  67. _megdnn_workspace workspace)
  68. {
  69. check_exec(diff.layout, src.layout, rois.layout, index.layout, grad.layout,
  70. workspace.size);
  71. auto stream = cuda_stream(handle());
  72. auto nthreads = grad.layout.total_nr_elems();
  73. auto num_rois = rois.layout.shape[0];
  74. auto spatial_scale = m_param.scale;
  75. auto channels = src.layout.shape[1];
  76. auto height = src.layout.shape[2];
  77. auto width = src.layout.shape[3];
  78. auto pooled_height = diff.layout.shape[2];
  79. auto pooled_width = diff.layout.shape[3];
  80. using namespace ::megdnn::roi_pooling;
  81. using namespace ::megdnn::cuda::roi_pooling;
  82. #define cb(DType) \
  83. if (src.layout.dtype == DType()) { \
  84. using T = typename DTypeTrait<DType>::ctype; \
  85. switch (param().mode) { \
  86. case param::ROIPooling::Mode::MAX: \
  87. roi_pooling::backward_proxy<T, BwdMaxPooler<T>>(nthreads, \
  88. diff.ptr<T>(), index.ptr<dt_int32>(), \
  89. num_rois, spatial_scale, \
  90. channels, height, width, \
  91. pooled_height, pooled_width, \
  92. grad.ptr<T>(), rois.ptr<T>(), \
  93. stream); \
  94. break; \
  95. case param::ROIPooling::Mode::AVERAGE: \
  96. roi_pooling::backward_proxy<T, BwdAveragePooler<T>>(nthreads, \
  97. diff.ptr<T>(), index.ptr<dt_int32>(), \
  98. num_rois, spatial_scale, \
  99. channels, height, width, \
  100. pooled_height, pooled_width, \
  101. grad.ptr<T>(), rois.ptr<T>(), \
  102. stream); \
  103. break; \
  104. default: \
  105. megdnn_assert_internal(false); \
  106. } \
  107. }
  108. MEGDNN_FOREACH_COMPUTING_DTYPE_FLOAT(cb)
  109. #undef cb
  110. }
  111. } // namespace cuda
  112. } // namespace megdnn
  113. // vim: syntax=cpp.doxygen

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