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 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * \file dnn/src/cuda/pooling/opr_impl.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 "src/cuda/pooling/opr_impl.h"
  13. #include "./algo.h"
  14. #include "./pooling2d_qint.cuh"
  15. #include "src/common/algo_chooser.h"
  16. #include "src/cuda/relayout_format/opr_impl.h"
  17. #include "src/cuda/utils.h"
  18. namespace megdnn {
  19. namespace cuda {
  20. size_t PoolingForwardImpl::get_workspace_in_bytes(const TensorLayout& src,
  21. const TensorLayout& dst) {
  22. return get_dnn_workspace(this, src, dst);
  23. }
  24. const char* PoolingForwardImpl::get_algorithm_set_name() const {
  25. return "CUDA_POOLING_FORWARD";
  26. }
  27. std::vector<PoolingForwardImpl::Algorithm*>
  28. PoolingForwardImpl::get_all_algorithms(const TensorLayout& src,
  29. const TensorLayout& dst) {
  30. return megdnn::get_all_algorithms<PoolingForwardImpl>({this, src, dst});
  31. }
  32. std::vector<PoolingForwardImpl::Algorithm*>
  33. PoolingForwardImpl::get_all_algorithms_safe(const TensorLayout& src,
  34. const TensorLayout& dst) {
  35. return megdnn::get_all_algorithms_safe<PoolingForwardImpl>({this, src, dst});
  36. }
  37. PoolingForwardImpl::Algorithm* PoolingForwardImpl::get_algorithm_heuristic(
  38. const TensorLayout& src, const TensorLayout& dst,
  39. size_t workspace_limit_in_bytes, const AlgoAttribute& positive_attr,
  40. const AlgoAttribute& negative_attr) {
  41. MEGDNN_MARK_USED_VAR(workspace_limit_in_bytes);
  42. AlgoBase::SizeArgs args(this, src, dst);
  43. for (auto&& iter : sm_algo_pack.all_algos) {
  44. if (iter->is_available_attribute(args, positive_attr, negative_attr)) {
  45. return iter;
  46. }
  47. }
  48. megdnn_throw(
  49. ssprintf("require algorithm with attribute(%s) and without "
  50. "attribute(%s), but can't get suitable algo.\n",
  51. Algorithm::attribute_str(positive_attr).c_str(),
  52. Algorithm::attribute_str(negative_attr).c_str()));
  53. return nullptr;
  54. }
  55. void PoolingForwardImpl::exec(_megdnn_tensor_in ssrc, _megdnn_tensor_out sdst,
  56. _megdnn_workspace sworkspace) {
  57. check_exec(ssrc.layout, sdst.layout, sworkspace.size);
  58. {
  59. AlgoBase::ExecArgs args(this, ssrc, sdst, sworkspace);
  60. auto algo = get_algorithm(this, ssrc.layout, sdst.layout);
  61. algo->exec(args);
  62. }
  63. }
  64. const char* PoolingBackwardImpl::get_algorithm_set_name() const {
  65. return "CUDA_POOLING_BACKWARD";
  66. }
  67. std::vector<PoolingBackwardImpl::Algorithm*>
  68. PoolingBackwardImpl::get_all_algorithms(const TensorLayout& src,
  69. const TensorLayout& dst,
  70. const TensorLayout& diff,
  71. const TensorLayout& grad) {
  72. return megdnn::get_all_algorithms<PoolingBackwardImpl>(
  73. {this, src, dst, diff, grad});
  74. }
  75. std::vector<PoolingBackwardImpl::Algorithm*>
  76. PoolingBackwardImpl::get_all_algorithms_safe(const TensorLayout& src,
  77. const TensorLayout& dst,
  78. const TensorLayout& diff,
  79. const TensorLayout& grad) {
  80. return megdnn::get_all_algorithms_safe<PoolingBackwardImpl>(
  81. {this, src, dst, diff, grad});
  82. }
  83. PoolingBackwardImpl::Algorithm* PoolingBackwardImpl::get_algorithm_heuristic(
  84. const TensorLayout& src, const TensorLayout& dst,
  85. const TensorLayout& diff, const TensorLayout& grad,
  86. size_t workspace_limit_in_bytes, const AlgoAttribute& positive_attr,
  87. const AlgoAttribute& negative_attr) {
  88. MEGDNN_MARK_USED_VAR(workspace_limit_in_bytes);
  89. AlgoBase::SizeArgs args(this, src, dst, diff, grad);
  90. for (auto iter : sm_algo_pack.all_algos) {
  91. if (iter->is_available_attribute(args, positive_attr, negative_attr)) {
  92. return iter;
  93. }
  94. }
  95. megdnn_throw(
  96. ssprintf("require algorithm with attribute(%s) and without "
  97. "attribute(%s), but can't get suitable algo.\n",
  98. Algorithm::attribute_str(positive_attr).c_str(),
  99. Algorithm::attribute_str(negative_attr).c_str()));
  100. return nullptr;
  101. }
  102. void PoolingBackwardImpl::exec(_megdnn_tensor_in ssrc, _megdnn_tensor_in sdst,
  103. _megdnn_tensor_in sdiff,
  104. _megdnn_tensor_out sgrad,
  105. _megdnn_workspace sworkspace) {
  106. check_exec(ssrc.layout, sdst.layout, sdiff.layout, sgrad.layout,
  107. sworkspace.size);
  108. {
  109. AlgoBase::ExecArgs args(this, ssrc, sdst, sdiff, sgrad, sworkspace);
  110. auto algo = get_algorithm(this, ssrc.layout, sdst.layout, sdiff.layout,
  111. sgrad.layout);
  112. algo->exec(args);
  113. }
  114. }
  115. size_t PoolingBackwardImpl::get_workspace_in_bytes(const TensorLayout& src,
  116. const TensorLayout& dst,
  117. const TensorLayout& diff,
  118. const TensorLayout& grad) {
  119. return get_dnn_workspace(this, src, dst, diff, grad);
  120. }
  121. } // namespace cuda
  122. } // namespace megdnn
  123. // vim: syntax=cpp.doxygen

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