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 2.6 kB

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

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