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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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__
  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. else:
  26. try:
  27. from ..hub.hub import _get_megengine_home
  28. cache_dir = os.path.expanduser(
  29. os.path.join(_get_megengine_home(), "persistent_cache")
  30. )
  31. os.makedirs(cache_dir, exist_ok=True)
  32. self.cache_file = os.path.join(cache_dir, "cache")
  33. self._dict = shelve.open(self.cache_file)
  34. self._is_shelve = True
  35. except:
  36. self._dict = {}
  37. self._is_shelve = False
  38. def get(self, key):
  39. if self._is_shelve and isinstance(key, bytes):
  40. key = key.decode("utf-8")
  41. return self._dict.get(key)
  42. def set(self, key, val):
  43. if self._is_shelve and isinstance(key, bytes):
  44. key = key.decode("utf-8")
  45. self._dict[key] = val
  46. def clear(self):
  47. print("{} cache item in {} deleted".format(len(self._dict), self.cache_file))
  48. self._dict.clear()
  49. def __del__(self):
  50. if self._is_shelve:
  51. self._dict.close()
  52. class PersistentCacheOnServer(_PersistentCache):
  53. _cached_conn = None
  54. _prefix = None
  55. _prev_get_refkeep = None
  56. @property
  57. def _conn(self):
  58. """get redis connection"""
  59. if self._cached_conn is None:
  60. self._cached_conn = _FakeRedisConn()
  61. self._prefix = self.make_user_prefix()
  62. return self._cached_conn
  63. @classmethod
  64. def make_user_prefix(cls):
  65. return "mgbcache:{}".format(getpass.getuser())
  66. def _make_key(self, category, key):
  67. prefix_with_version = "{}:MGB{}".format(self._prefix, __version__)
  68. return b"@".join(
  69. (prefix_with_version.encode("ascii"), category.encode("ascii"), key)
  70. )
  71. def put(self, category, key, value):
  72. conn = self._conn
  73. key = self._make_key(category, key)
  74. conn.set(key, value)
  75. def get(self, category, key):
  76. conn = self._conn
  77. key = self._make_key(category, key)
  78. self._prev_get_refkeep = conn.get(key)
  79. return self._prev_get_refkeep
  80. def clean(self):
  81. conn = self._conn
  82. if isinstance(conn, _FakeRedisConn):
  83. conn.clear()

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