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.

persistent_cache.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import argparse
  10. import getpass
  11. import json
  12. import os
  13. import shelve
  14. from ..core._imperative_rt import PersistentCache as _PersistentCache
  15. from ..logger import get_logger
  16. from ..version import __version__, git_version
  17. class _FakeRedisConn:
  18. cache_file = None
  19. _is_shelve = False
  20. _dict = {}
  21. def __init__(self):
  22. if os.getenv("MGE_FASTRUN_CACHE_TYPE") == "MEMORY":
  23. self._dict = {}
  24. self._is_shelve = False
  25. get_logger().info("fastrun use in-memory cache")
  26. else:
  27. try:
  28. from ..hub.hub import _get_megengine_home
  29. cache_dir = os.path.expanduser(
  30. os.path.join(_get_megengine_home(), "persistent_cache")
  31. )
  32. os.makedirs(cache_dir, exist_ok=True)
  33. self.cache_file = os.path.join(cache_dir, "cache")
  34. self._dict = shelve.open(self.cache_file)
  35. self._is_shelve = True
  36. get_logger().info(
  37. "fastrun use in-file cache {}".format(self._cached_conn.cache_file)
  38. )
  39. except:
  40. self._dict = {}
  41. self._is_shelve = False
  42. get_logger().error(
  43. "failed to create cache file {}; fallback to "
  44. "in-memory cache".format(self.cache_file)
  45. )
  46. def get(self, key):
  47. if self._is_shelve and isinstance(key, bytes):
  48. key = key.decode("utf-8")
  49. return self._dict.get(key)
  50. def set(self, key, val):
  51. if self._is_shelve and isinstance(key, bytes):
  52. key = key.decode("utf-8")
  53. self._dict[key] = val
  54. def clear(self):
  55. print("{} cache item deleted".format(len(self._dict)))
  56. self._dict.clear()
  57. def __del__(self):
  58. if self._is_shelve:
  59. self._dict.close()
  60. class PersistentCacheOnServer(_PersistentCache):
  61. _cached_conn = None
  62. _prefix = None
  63. _prev_get_refkeep = None
  64. @property
  65. def _conn(self):
  66. """get redis connection"""
  67. if self._cached_conn is None:
  68. self._cached_conn = _FakeRedisConn()
  69. self._prefix = self.make_user_prefix()
  70. return self._cached_conn
  71. @classmethod
  72. def make_user_prefix(cls):
  73. return "mgbcache:{}".format(getpass.getuser())
  74. def _make_key(self, category, key):
  75. prefix_with_version = "{}:MGB{}:GIT:{}".format(
  76. self._prefix, __version__, git_version
  77. )
  78. return b"@".join(
  79. (prefix_with_version.encode("ascii"), category.encode("ascii"), key)
  80. )
  81. def put(self, category, key, value):
  82. conn = self._conn
  83. key = self._make_key(category, key)
  84. conn.set(key, value)
  85. def get(self, category, key):
  86. conn = self._conn
  87. key = self._make_key(category, key)
  88. self._prev_get_refkeep = conn.get(key)
  89. return self._prev_get_refkeep
  90. def clean(self):
  91. conn = self._conn
  92. if isinstance(conn, _FakeRedisConn):
  93. conn.clear()

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