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_base.cpp 2.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * \file dnn/src/common/algo_base.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/common/algo_base.h"
  13. #include "src/common/utils.h"
  14. using namespace megdnn;
  15. #define FOREACH_ALGO_ATTRIBUTE(cb) \
  16. cb(DEFAULT) \
  17. cb(REPRODUCIBLE) \
  18. cb(NAIVE) \
  19. cb(USABLE_DEPEND_ON_SHAPE) \
  20. cb(ACCURACY_DEPEND_ON_BATCH)
  21. namespace {
  22. inline const char* attr_str(const AlgoAttribute& attr) {
  23. #define cb(attr) \
  24. case AlgoAttribute::attr: \
  25. return #attr;
  26. switch (attr) { FOREACH_ALGO_ATTRIBUTE(cb) }
  27. #undef cb
  28. return "UNKNOWN";
  29. }
  30. } // namespace
  31. std::string Algorithm::attribute_str(const Attribute& attr) {
  32. std::string ret;
  33. uint32_t attr_val = static_cast<uint32_t>(attr);
  34. while(attr_val) {
  35. uint32_t mask = ~(attr_val & (attr_val - 1));
  36. Attribute sub_attr = static_cast<Attribute>(mask & attr_val);
  37. if (!ret.empty()) {
  38. ret.append(" | ");
  39. }
  40. ret.append(attr_str(sub_attr));
  41. attr_val = attr_val & (attr_val - 1);
  42. }
  43. if (ret.empty()) {
  44. ret = "DEFAULT";
  45. }
  46. return ret;
  47. }
  48. bool Algorithm::contain_attribute_all(const Attribute& attr) const {
  49. return attr == static_cast<Attribute>(attribute() & attr);
  50. }
  51. bool Algorithm::contain_attribute_any(const Attribute& attr) const {
  52. return static_cast<bool>(attribute() & attr);
  53. }
  54. void Algorithm::check_attribute(const Attribute& positive_attr,
  55. const Attribute& negative_attr) const {
  56. megdnn_assert(contain_attribute_all(positive_attr) &&
  57. !contain_attribute_any(negative_attr),
  58. "require algorithm with attribute(%s) and without "
  59. "attribute(%s), but get"
  60. "algorithm(%s) with attribute(%s) ",
  61. Algorithm::attribute_str(positive_attr).c_str(),
  62. Algorithm::attribute_str(negative_attr).c_str(), name(),
  63. Algorithm::attribute_str(attribute()).c_str());
  64. }
  65. // vim: syntax=cpp.doxygen

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