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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 os
  10. import re
  11. from typing import Optional
  12. from .core._imperative_rt.common import CompNode, DeviceType
  13. from .core._imperative_rt.common import (
  14. get_cuda_compute_capability as _get_cuda_compute_capability,
  15. )
  16. from .core._imperative_rt.common import set_prealloc_config as _set_prealloc_config
  17. from .core._imperative_rt.common import what_is_xpu as _what_is_xpu
  18. __all__ = [
  19. "is_cuda_available",
  20. "get_device_count",
  21. "get_default_device",
  22. "set_default_device",
  23. "get_mem_status_bytes",
  24. "get_cuda_compute_capability",
  25. "set_prealloc_config",
  26. "DeviceType",
  27. ]
  28. class _stream_helper:
  29. def __init__(self):
  30. self.stream = 1
  31. def get_next(self):
  32. out = self.stream
  33. self.stream = self.stream + 1
  34. return out
  35. _sh = _stream_helper()
  36. def _valid_device(inp):
  37. if isinstance(inp, str) and re.match("^([cxg]pu|rocm)(\d+|\d+:\d+|x)$", inp):
  38. return True
  39. return False
  40. def _str2device_type(type_str: str, allow_unspec: bool = True):
  41. type_str = type_str.upper()
  42. if type_str == "CPU":
  43. return DeviceType.CPU
  44. elif type_str == "GPU" or type_str == "CUDA":
  45. return DeviceType.CUDA
  46. elif type_str == "CAMBRICON":
  47. return DeviceType.CAMBRICON
  48. elif type_str == "ATLAS":
  49. return DeviceType.ATLAS
  50. elif type_str == "ROCM" or type_str == "AMDGPU":
  51. return DeviceType.ROCM
  52. else:
  53. assert (
  54. allow_unspec and type_str == "XPU"
  55. ), "device type can only be cpu, gpu or xpu"
  56. return DeviceType.UNSPEC
  57. _device_type_set = {"cpu", "gpu", "xpu", "rocm"}
  58. def get_device_count(device_type: str) -> int:
  59. r"""Gets number of devices installed on this system.
  60. Args:
  61. device_type: device type, one of 'gpu' or 'cpu'
  62. """
  63. assert device_type in _device_type_set, "device must be one of {}".format(
  64. _device_type_set
  65. )
  66. device_type = _str2device_type(device_type)
  67. return CompNode._get_device_count(device_type, False)
  68. def is_cuda_available() -> bool:
  69. r"""Returns whether cuda device is available on this system."""
  70. t = _str2device_type("gpu")
  71. return CompNode._get_device_count(t, False) > 0
  72. def is_cambricon_available() -> bool:
  73. r"""Returns whether cambricon device is available on this system."""
  74. t = _str2device_type("cambricon")
  75. return CompNode._get_device_count(t, False) > 0
  76. def is_atlas_available() -> bool:
  77. r"""Returns whether atlas device is available on this system."""
  78. t = _str2device_type("atlas")
  79. return CompNode._get_device_count(t, False) > 0
  80. def is_rocm_available() -> bool:
  81. r"""Returns whether rocm device is available on this system."""
  82. t = _str2device_type("rocm")
  83. return CompNode._get_device_count(t, False) > 0
  84. def set_default_device(device: str = "xpux"):
  85. r"""Sets default computing node.
  86. Args:
  87. device: default device type.
  88. Note:
  89. * The type can be 'cpu0', 'cpu1', etc., or 'gpu0', 'gpu1', etc.,
  90. to specify the particular CPU or GPU to use.
  91. * 'cpux' and 'gpux' can also be used to specify any number of CPU or GPU devices.
  92. * The default value is 'xpux' to specify any device available.
  93. * The priority of using GPU is higher when both GPU and CPU are available.
  94. * 'multithread' device type is avaliable when inference,
  95. which implements multi-threading parallelism at the operator level.
  96. For example, 'multithread4' will compute with 4 threads.
  97. * It can also be set by environment variable ``MGE_DEFAULT_DEVICE``.
  98. """
  99. assert _valid_device(device), "Invalid device name {}".format(device)
  100. CompNode._set_default_device(device)
  101. def get_default_device() -> str:
  102. r"""Gets default computing node.
  103. It returns the value set by :func:`~.set_default_device`.
  104. """
  105. return CompNode._get_default_device()
  106. def get_mem_status_bytes(device: Optional[str] = None):
  107. r"""Get total and free memory on the computing device in bytes."""
  108. if device is None:
  109. device = get_default_device()
  110. tot, free = CompNode(device).get_mem_status_bytes
  111. return tot, free
  112. def get_cuda_compute_capability(device: int, device_type=DeviceType.CUDA) -> int:
  113. r"""Gets compute capability of the specified device.
  114. Args:
  115. device: device number.
  116. Returns:
  117. a version number, or `SM version`.
  118. """
  119. return _get_cuda_compute_capability(device, device_type)
  120. set_default_device(os.getenv("MGE_DEFAULT_DEVICE", "xpux"))
  121. def set_prealloc_config(
  122. alignment: int = 1,
  123. min_req: int = 32 * 1024 * 1024,
  124. max_overhead: int = 0,
  125. growth_factor=2.0,
  126. device_type=DeviceType.CUDA,
  127. ):
  128. r"""Specifies how to pre-allocate from raw device allocator.
  129. Args:
  130. alignment: specifies the alignment in bytes.
  131. min_req: min request size in bytes.
  132. max_overhead: max overhead above required size in bytes.
  133. growth_factor: request size / cur allocated`
  134. device_type: the device type
  135. alignment: int:
  136. min_req: int:
  137. max_overhead: int:
  138. """
  139. assert alignment > 0
  140. assert min_req > 0
  141. assert max_overhead >= 0
  142. assert growth_factor >= 1
  143. _set_prealloc_config(alignment, min_req, max_overhead, growth_factor, device_type)
  144. def what_is_xpu():
  145. return _what_is_xpu().name.lower()

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