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.

device.py 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 os
  10. from .core._imperative_rt.common import CompNode, DeviceType
  11. __all__ = [
  12. "is_cuda_available",
  13. "get_device_count",
  14. "get_default_device",
  15. "set_default_device",
  16. ]
  17. def _valid_device(inp):
  18. if isinstance(inp, str) and len(inp) == 4:
  19. if inp[0] in {"x", "c", "g"} and inp[1:3] == "pu":
  20. if inp[3] == "x" or inp[3].isdigit():
  21. return True
  22. return False
  23. def _str2device_type(type_str: str, allow_unspec: bool = True):
  24. type_str = type_str.upper()
  25. if type_str == "CPU":
  26. return DeviceType.CPU
  27. elif type_str == "GPU" or type_str == "CUDA":
  28. return DeviceType.CUDA
  29. else:
  30. assert allow_unspec and str == "XPU", "bad device type"
  31. return DeviceType.UNSPEC
  32. def get_device_count(device_type: str) -> int:
  33. """Gets number of devices installed on this system.
  34. :param device_type: device type, one of 'gpu' or 'cpu'
  35. """
  36. device_type_set = ("cpu", "gpu")
  37. assert device_type in device_type_set, "device must be one of {}".format(
  38. device_type_set
  39. )
  40. device_type = _str2device_type(device_type)
  41. return CompNode._get_device_count(device_type, False)
  42. def is_cuda_available() -> bool:
  43. """Returns whether cuda device is available on this system.
  44. """
  45. t = _str2device_type("gpu")
  46. return CompNode._get_device_count(t, False) > 0
  47. def set_default_device(device: str = "xpux"):
  48. r"""Sets default computing node.
  49. :param device: default device type. The type can be 'cpu0', 'cpu1', etc.,
  50. or 'gpu0', 'gpu1', etc., to specify the particular cpu or gpu to use.
  51. 'cpux' and 'gpux' can also be used to specify any number of cpu or gpu devices.
  52. 'multithread' device type is avaliable when inference, which implements
  53. multi-threading parallelism at the operator level. For example,
  54. 'multithread4' will compute with 4 threads. which implements
  55. The default value is 'xpux' to specify any device available. The priority of using gpu is higher when both gpu and cpu are available.
  56. It can also be set by environmental variable `MGE_DEFAULT_DEVICE`.
  57. """
  58. assert _valid_device(device), "Invalid device name {}".format(device)
  59. CompNode._set_default_device(device)
  60. def get_default_device() -> str:
  61. r"""Gets default computing node.
  62. It returns the value set by :func:`~.set_default_device`.
  63. """
  64. return CompNode._get_default_device()
  65. set_default_device(os.getenv("MGE_DEFAULT_DEVICE", "xpux"))

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