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.

tools.py 1.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 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 importlib.util
  10. import os
  11. import types
  12. from contextlib import contextmanager
  13. from typing import Iterator
  14. def load_module(name: str, path: str) -> types.ModuleType:
  15. """
  16. Loads module specified by name and path.
  17. :param name: module name.
  18. :param path: module path.
  19. """
  20. spec = importlib.util.spec_from_file_location(name, path)
  21. module = importlib.util.module_from_spec(spec)
  22. spec.loader.exec_module(module)
  23. return module
  24. def check_module_exists(module: str) -> bool:
  25. """Checks whether python module exists or not.
  26. :param module: name of module.
  27. """
  28. return importlib.util.find_spec(module) is not None
  29. @contextmanager
  30. def cd(target: str) -> Iterator[None]:
  31. """Changes current directory to target.
  32. :param target: target directory.
  33. """
  34. prev = os.getcwd()
  35. os.chdir(os.path.expanduser(target))
  36. try:
  37. yield
  38. finally:
  39. os.chdir(prev)

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