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.

heuristic_cache.h 2.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * \file dnn/include/megdnn/heuristic_cache.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
  10. * implied.
  11. */
  12. #pragma once
  13. #include "megdnn/basic_types.h"
  14. #include "megdnn/oprs/base.h"
  15. #include <mutex>
  16. #include <string>
  17. #include <unordered_map>
  18. namespace megdnn {
  19. class HeuristicCache {
  20. private:
  21. HeuristicCache() = default;
  22. public:
  23. static HeuristicCache& instance();
  24. struct KeyStorage {
  25. std::string category;
  26. std::string input;
  27. bool operator==(const KeyStorage& k) const {
  28. return category == k.category && input == k.input;
  29. }
  30. };
  31. class Key {
  32. Handle* m_handle;
  33. uint32_t m_opr_type;
  34. const TensorLayout* m_inp_layouts_ptr;
  35. size_t m_inp_layouts_size;
  36. const void* m_param_ptr;
  37. size_t m_param_size;
  38. mutable std::string m_category;
  39. mutable std::string m_input;
  40. public:
  41. Key(Handle* opr_handle, Algorithm::OprType opr_type,
  42. const TensorLayout* inp_layouts_ptr, size_t inp_layouts_size,
  43. const void* param_ptr = nullptr, size_t param_size = 0)
  44. : m_handle{opr_handle},
  45. m_opr_type{static_cast<uint32_t>(opr_type)},
  46. m_inp_layouts_ptr{inp_layouts_ptr},
  47. m_inp_layouts_size{inp_layouts_size},
  48. m_param_ptr{param_ptr},
  49. m_param_size{param_size} {}
  50. KeyStorage build_key_storage() const;
  51. };
  52. struct Result {
  53. ExecutionPolicy policy;
  54. size_t workspace;
  55. };
  56. void put(const Key& key, Result& result);
  57. Result get(const Key& key);
  58. void clear();
  59. private:
  60. struct Hash {
  61. size_t operator()(const KeyStorage& k) const {
  62. size_t h1 = std::hash<std::string>{}(k.category);
  63. size_t h2 = std::hash<std::string>{}(k.input);
  64. h1 ^= h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2);
  65. return h1;
  66. }
  67. };
  68. std::unordered_map<KeyStorage, Result, Hash> m_heuristic_cache;
  69. #if __DEPLOY_ON_XP_SP2__
  70. size_t m_mtx;
  71. #else
  72. std::mutex m_mtx;
  73. #endif
  74. };
  75. } // namespace megdnn

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