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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, const TensorLayout* inp_layouts_ptr,
  42. size_t inp_layouts_size, const void* param_ptr = nullptr, size_t param_size = 0)
  43. : m_handle{opr_handle},
  44. m_opr_type{static_cast<uint32_t>(opr_type)},
  45. m_inp_layouts_ptr{inp_layouts_ptr},
  46. m_inp_layouts_size{inp_layouts_size},
  47. m_param_ptr{param_ptr},
  48. m_param_size{param_size} {}
  49. KeyStorage build_key_storage() const;
  50. };
  51. struct Result {
  52. ExecutionPolicy policy;
  53. size_t workspace;
  54. };
  55. void put(const Key& key, Result& result);
  56. Result get(const Key& key);
  57. void clear();
  58. private:
  59. struct Hash {
  60. size_t operator()(const KeyStorage& k) const {
  61. size_t h1 = std::hash<std::string>{}(k.category);
  62. size_t h2 = std::hash<std::string>{}(k.input);
  63. h1 ^= h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2);
  64. return h1;
  65. }
  66. };
  67. std::unordered_map<KeyStorage, Result, Hash> m_heuristic_cache;
  68. #if __DEPLOY_ON_XP_SP2__
  69. size_t m_mtx;
  70. #else
  71. std::mutex m_mtx;
  72. #endif
  73. };
  74. } // namespace megdnn

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