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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. import re
  11. from .core._imperative_rt.common import CompNode, DeviceType
  12. from .core._imperative_rt.common import set_prealloc_config as _set_prealloc_config
  13. __all__ = [
  14. "is_cuda_available",
  15. "get_device_count",
  16. "get_default_device",
  17. "set_default_device",
  18. "set_prealloc_config",
  19. "DeviceType",
  20. ]
  21. def _valid_device(inp):
  22. if isinstance(inp, str) and re.match("^[cxg]pu(\d+|\d+:\d+|x)$", inp):
  23. return True
  24. return False
  25. def _str2device_type(type_str: str, allow_unspec: bool = True):
  26. type_str = type_str.upper()
  27. if type_str == "CPU":
  28. return DeviceType.CPU
  29. elif type_str == "GPU" or type_str == "CUDA":
  30. return DeviceType.CUDA
  31. else:
  32. assert allow_unspec and str == "XPU", "device type can only be cpu, gpu or xpu"
  33. return DeviceType.UNSPEC
  34. def get_device_count(device_type: str) -> int:
  35. """Gets number of devices installed on this system.
  36. :param device_type: device type, one of 'gpu' or 'cpu'
  37. """
  38. device_type_set = ("cpu", "gpu")
  39. assert device_type in device_type_set, "device must be one of {}".format(
  40. device_type_set
  41. )
  42. device_type = _str2device_type(device_type)
  43. return CompNode._get_device_count(device_type, False)
  44. def is_cuda_available() -> bool:
  45. """Returns whether cuda device is available on this system.
  46. """
  47. t = _str2device_type("gpu")
  48. return CompNode._get_device_count(t, False) > 0
  49. def set_default_device(device: str = "xpux"):
  50. r"""Sets default computing node.
  51. :param device: default device type. The type can be 'cpu0', 'cpu1', etc.,
  52. or 'gpu0', 'gpu1', etc., to specify the particular cpu or gpu to use.
  53. 'cpux' and 'gpux' can also be used to specify any number of cpu or gpu devices.
  54. 'multithread' device type is avaliable when inference, which implements
  55. multi-threading parallelism at the operator level. For example,
  56. 'multithread4' will compute with 4 threads.
  57. The default value is 'xpux' to specify any device available. The priority of using gpu is higher when both gpu and cpu are available.
  58. It can also be set by environment variable `MGE_DEFAULT_DEVICE`.
  59. """
  60. assert _valid_device(device), "Invalid device name {}".format(device)
  61. CompNode._set_default_device(device)
  62. def get_default_device() -> str:
  63. r"""Gets default computing node.
  64. It returns the value set by :func:`~.set_default_device`.
  65. """
  66. return CompNode._get_default_device()
  67. set_default_device(os.getenv("MGE_DEFAULT_DEVICE", "xpux"))
  68. def set_prealloc_config(
  69. alignment: int = 1,
  70. min_req: int = 32 * 1024 * 1024,
  71. max_overhead: int = 0,
  72. growth_factor=2.0,
  73. device_type=DeviceType.CUDA,
  74. ):
  75. """Specifies how to pre-allocate from raw device allocator.
  76. :param alignment: specifies the alignment in bytes.
  77. :param min_req: min request size in bytes.
  78. :param max_overhead: max overhead above required size in bytes.
  79. :param growth_factor: `request size / cur allocated`
  80. :param device_type: the device type
  81. """
  82. assert alignment > 0
  83. assert min_req > 0
  84. assert max_overhead >= 0
  85. assert growth_factor >= 1
  86. _set_prealloc_config(alignment, min_req, max_overhead, growth_factor, device_type)

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