device.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. from contextlib import ContextDecorator
  16. import GPUtil
  17. from . import logging
  18. from .custom_device_list import (
  19. DCU_WHITELIST,
  20. GCU_WHITELIST,
  21. MLU_WHITELIST,
  22. NPU_BLACKLIST,
  23. XPU_WHITELIST,
  24. )
  25. from .flags import DISABLE_DEV_MODEL_WL
  26. SUPPORTED_DEVICE_TYPE = ["cpu", "gpu", "xpu", "npu", "mlu", "gcu", "dcu"]
  27. def constr_device(device_type, device_ids):
  28. if device_type == "cpu" and device_ids is not None:
  29. raise ValueError("`device_ids` must be None for CPUs")
  30. if device_ids:
  31. device_ids = ",".join(map(str, device_ids))
  32. return f"{device_type}:{device_ids}"
  33. else:
  34. return f"{device_type}"
  35. def get_default_device():
  36. try:
  37. gpu_list = GPUtil.getGPUs()
  38. except Exception:
  39. logging.debug(
  40. "Failed to query GPU devices. Falling back to CPU.", exc_info=True
  41. )
  42. has_gpus = False
  43. else:
  44. has_gpus = bool(gpu_list)
  45. if not has_gpus:
  46. # HACK
  47. if os.path.exists("/etc/nv_tegra_release"):
  48. logging.debug(
  49. "The current device appears to be an NVIDIA Jetson. GPU 0 will be used as the default device."
  50. )
  51. if not has_gpus:
  52. return "cpu"
  53. else:
  54. return constr_device("gpu", [0])
  55. def parse_device(device):
  56. """parse_device"""
  57. # According to https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/device/set_device_cn.html
  58. parts = device.split(":")
  59. if len(parts) > 2:
  60. raise ValueError(f"Invalid device: {device}")
  61. if len(parts) == 1:
  62. device_type, device_ids = parts[0], None
  63. else:
  64. device_type, device_ids = parts
  65. device_ids = device_ids.split(",")
  66. for device_id in device_ids:
  67. if not device_id.isdigit():
  68. raise ValueError(
  69. f"Device ID must be an integer. Invalid device ID: {device_id}"
  70. )
  71. device_ids = list(map(int, device_ids))
  72. device_type = device_type.lower()
  73. # raise_unsupported_device_error(device_type, SUPPORTED_DEVICE_TYPE)
  74. assert device_type.lower() in SUPPORTED_DEVICE_TYPE
  75. if device_type == "cpu" and device_ids is not None:
  76. raise ValueError("No Device ID should be specified for CPUs")
  77. return device_type, device_ids
  78. def update_device_num(device, num):
  79. device_type, device_ids = parse_device(device)
  80. if device_ids:
  81. assert len(device_ids) >= num
  82. return constr_device(device_type, device_ids[:num])
  83. else:
  84. return constr_device(device_type, device_ids)
  85. def set_env_for_device(device):
  86. device_type, _ = parse_device(device)
  87. return set_env_for_device_type(device_type)
  88. def set_env_for_device_type(device_type):
  89. import paddle
  90. def _set(envs):
  91. for key, val in envs.items():
  92. os.environ[key] = val
  93. logging.debug(f"{key} has been set to {val}.")
  94. # XXX: is_compiled_with_rocm() must be True on dcu platform ?
  95. if device_type.lower() == "dcu" and paddle.is_compiled_with_rocm():
  96. envs = {"FLAGS_conv_workspace_size_limit": "2000"}
  97. _set(envs)
  98. if device_type.lower() == "npu":
  99. envs = {
  100. "FLAGS_npu_jit_compile": "0",
  101. "FLAGS_use_stride_kernel": "0",
  102. "FLAGS_allocator_strategy": "auto_growth",
  103. "CUSTOM_DEVICE_BLACK_LIST": "pad3d,pad3d_grad,set_value,set_value_with_tensor",
  104. "FLAGS_npu_scale_aclnn": "True",
  105. "FLAGS_npu_split_aclnn": "True",
  106. }
  107. _set(envs)
  108. if device_type.lower() == "xpu":
  109. envs = {
  110. "BKCL_FORCE_SYNC": "1",
  111. "BKCL_TIMEOUT": "1800",
  112. "FLAGS_use_stride_kernel": "0",
  113. "XPU_BLACK_LIST": "pad3d",
  114. }
  115. _set(envs)
  116. if device_type.lower() == "mlu":
  117. envs = {
  118. "FLAGS_use_stride_kernel": "0",
  119. "FLAGS_use_stream_safe_cuda_allocator": "0",
  120. }
  121. _set(envs)
  122. if device_type.lower() == "gcu":
  123. envs = {"FLAGS_use_stride_kernel": "0"}
  124. _set(envs)
  125. def check_supported_device_type(device_type, model_name):
  126. if DISABLE_DEV_MODEL_WL:
  127. logging.warning(
  128. "Skip checking if model is supported on device because the flag `PADDLE_PDX_DISABLE_DEV_MODEL_WL` has been set."
  129. )
  130. return
  131. tips = "You could set env `PADDLE_PDX_DISABLE_DEV_MODEL_WL` to `true` to disable this checking."
  132. if device_type == "dcu":
  133. assert model_name in DCU_WHITELIST, (
  134. f"The DCU device does not yet support `{model_name}` model!" + tips
  135. )
  136. elif device_type == "mlu":
  137. assert model_name in MLU_WHITELIST, (
  138. f"The MLU device does not yet support `{model_name}` model!" + tips
  139. )
  140. elif device_type == "npu":
  141. assert model_name not in NPU_BLACKLIST, (
  142. f"The NPU device does not yet support `{model_name}` model!" + tips
  143. )
  144. elif device_type == "xpu":
  145. assert model_name in XPU_WHITELIST, (
  146. f"The XPU device does not yet support `{model_name}` model!" + tips
  147. )
  148. elif device_type == "gcu":
  149. assert model_name in GCU_WHITELIST, (
  150. f"The GCU device does not yet support `{model_name}` model!" + tips
  151. )
  152. def check_supported_device(device, model_name):
  153. device_type, _ = parse_device(device)
  154. return check_supported_device_type(device_type, model_name)
  155. class TemporaryDeviceChanger(ContextDecorator):
  156. """
  157. A context manager to temporarily change global device
  158. """
  159. def __init__(self, new_device):
  160. # if new_device is None, nothing changed
  161. import paddle
  162. self.new_device = new_device
  163. self.original_device = paddle.device.get_device()
  164. def __enter__(self):
  165. import paddle
  166. if self.new_device is None:
  167. return self
  168. paddle.device.set_device(self.new_device)
  169. return self
  170. def __exit__(self, exc_type, exc_val, exc_tb):
  171. import paddle
  172. if self.new_device is None:
  173. return False
  174. paddle.device.set_device(self.original_device)
  175. return False