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.

algo_chooser.h 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * \file dnn/src/common/algo_chooser.h
  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 implied.
  10. */
  11. #pragma once
  12. #include <cstddef>
  13. #include <limits>
  14. #include <utility>
  15. #include <vector>
  16. #include "utils.h"
  17. namespace megdnn {
  18. /*!
  19. * \brief get user-configured algorithm, or heuristic algorithm
  20. */
  21. template <class Opr, typename... Args>
  22. typename Opr::AlgoBase* get_algorithm(Opr* opr, Args&&... args) {
  23. typename Opr::AlgorithmDesc ret;
  24. auto set = opr->execution_policy().algo;
  25. if (set.valid()) {
  26. ret = set;
  27. } else {
  28. ret = opr->get_algorithm_info_heuristic(
  29. std::forward<Args>(args)..., std::numeric_limits<size_t>::max(),
  30. AlgoAttribute::DEFAULT, AlgoAttribute::DEFAULT).desc;
  31. }
  32. return static_cast<typename Opr::AlgoBase*>(
  33. opr->get_algorithm_from_desc(ret));
  34. }
  35. /*!
  36. * \brief get user-configured algorithm, or heuristic algorithm. used in opencl
  37. * whose algo need to be constructed each time.
  38. */
  39. template <class Opr, typename... Args>
  40. typename Opr::AlgoBase* get_algorithm_or_construct(Opr* opr, Args&&... args) {
  41. auto set = opr->execution_policy().algo;
  42. if (set.valid()) {
  43. return opr->algo_pack().construct_and_get_algo(set);
  44. } else {
  45. return static_cast<typename Opr::AlgoBase*>(
  46. opr->get_algorithm_heuristic(std::forward<Args>(args)...,
  47. std::numeric_limits<size_t>::max(),
  48. AlgoAttribute::DEFAULT,
  49. AlgoAttribute::DEFAULT));
  50. }
  51. }
  52. /*!
  53. * \brief get all algorithms from algo_pack() that is available for current size
  54. */
  55. template <class Opr>
  56. std::vector<typename Opr::Algorithm*> get_all_algorithms(
  57. const typename Opr::AlgoBase::SizeArgs& args) {
  58. std::vector<typename Opr::Algorithm*> ret;
  59. ret.reserve(Opr::algo_pack().all_algos.size());
  60. for (auto i : Opr::algo_pack().all_algos) {
  61. if (i->is_available(args)) {
  62. ret.push_back(i);
  63. }
  64. }
  65. megdnn_assert(!ret.empty(), "no conv algorithm for %s",
  66. args.to_string().c_str());
  67. return ret;
  68. }
  69. /*!
  70. * \brief a helper function to get an algorithm match attribute. If require a
  71. * algorithm with specified attribute, and the given algorithm match that
  72. * attribute, return the given algorithm. Otherwise return nullptr
  73. */
  74. template <typename Opr>
  75. typename Opr::Algorithm* get_algo_match_attribute(
  76. typename Opr::AlgoBase* algo, const AlgoAttribute& positive_attr,
  77. const AlgoAttribute& negative_attr) {
  78. if (algo->contain_attribute_all(positive_attr) &&
  79. !algo->contain_attribute_any(negative_attr)) {
  80. return algo;
  81. }
  82. return nullptr;
  83. }
  84. template <typename Opr>
  85. typename Opr::Algorithm* get_algo_match_attribute(
  86. const std::vector<typename Opr::AlgoBase*>& algos,
  87. const typename Opr::AlgoBase::SizeArgs& args,
  88. size_t workspace_limit_in_bytes, const char* name,
  89. const AlgoAttribute& positive_attr = AlgoAttribute::REPRODUCIBLE,
  90. const AlgoAttribute& negative_attr = AlgoAttribute::DEFAULT) {
  91. size_t min_workspace_limit_in_bytes = std::numeric_limits<size_t>::max();
  92. bool available_but_limited_by_workspace = false;
  93. bool available_but_attribute_mismatch = false;
  94. for (auto i : algos) {
  95. if (i->is_available_attribute(args, positive_attr, negative_attr,
  96. workspace_limit_in_bytes)) {
  97. return i;
  98. }
  99. if (i->is_available_attribute(args, positive_attr, negative_attr)) {
  100. if (i->get_workspace_in_bytes(args) > workspace_limit_in_bytes) {
  101. available_but_limited_by_workspace = true;
  102. min_workspace_limit_in_bytes =
  103. std::min(min_workspace_limit_in_bytes,
  104. i->get_workspace_in_bytes(args));
  105. }
  106. }
  107. if (i->is_available(args)) {
  108. if (!(i->contain_attribute_all(positive_attr) &&
  109. !i->contain_attribute_any(negative_attr)))
  110. available_but_attribute_mismatch = true;
  111. }
  112. }
  113. MEGDNN_MARK_USED_VAR(name);
  114. if (available_but_limited_by_workspace) {
  115. megdnn_throw(
  116. ssprintf("no %s algorithm without attribute(%s) with "
  117. "attribute(%s) : %s workspace limit %zu is "
  118. "less than mini workspace limit %zu",
  119. name, Algorithm::attribute_str(negative_attr).c_str(),
  120. Algorithm::attribute_str(positive_attr).c_str(),
  121. args.to_string().c_str(), workspace_limit_in_bytes,
  122. min_workspace_limit_in_bytes));
  123. } else if (available_but_attribute_mismatch) {
  124. megdnn_throw(ssprintf(
  125. "no %s algorithm without attribute(%s) with attribute(%s)", name,
  126. Algorithm::attribute_str(negative_attr).c_str(),
  127. Algorithm::attribute_str(positive_attr).c_str()));
  128. } else {
  129. megdnn_throw(ssprintf("no usable %s algorithm", name));
  130. }
  131. }
  132. } // namespace megdnn
  133. // vim: syntax=cpp.doxygen

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