device.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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. import GPUtil
  16. import lazy_paddle as paddle
  17. from .errors import raise_unsupported_device_error
  18. SUPPORTED_DEVICE_TYPE = ["cpu", "gpu", "xpu", "npu", "mlu"]
  19. def _constr_device(device_type, device_ids):
  20. if device_ids:
  21. device_ids = ",".join(map(str, device_ids))
  22. return f"{device_type}:{device_ids}"
  23. else:
  24. return f"{device_type}"
  25. def get_default_device():
  26. avail_gpus = GPUtil.getAvailable()
  27. if not avail_gpus:
  28. return "cpu"
  29. else:
  30. return _constr_device("gpu", [avail_gpus[0]])
  31. def parse_device(device):
  32. """parse_device"""
  33. # According to https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/device/set_device_cn.html
  34. parts = device.split(":")
  35. if len(parts) > 2:
  36. raise ValueError(f"Invalid device: {device}")
  37. if len(parts) == 1:
  38. device_type, device_ids = parts[0], None
  39. else:
  40. device_type, device_ids = parts
  41. device_ids = device_ids.split(",")
  42. for device_id in device_ids:
  43. if not device_id.isdigit():
  44. raise ValueError(
  45. f"Device ID must be an integer. Invalid device ID: {device_id}"
  46. )
  47. device_ids = list(map(int, device_ids))
  48. device_type = device_type.lower()
  49. # raise_unsupported_device_error(device_type, SUPPORTED_DEVICE_TYPE)
  50. assert device_type.lower() in SUPPORTED_DEVICE_TYPE
  51. return device_type, device_ids
  52. def update_device_num(device, num):
  53. device_type, device_ids = parse_device(device)
  54. if device_ids:
  55. assert len(device_ids) >= num
  56. return _constr_device(device_type, device_ids[:num])
  57. else:
  58. return _constr_device(device_type, device_ids)
  59. def set_env_for_device(device):
  60. device_type, device_ids = parse_device(device)
  61. if device_type.lower() in ["gpu", "xpu", "npu", "mlu"]:
  62. if device_type.lower() == "gpu" and paddle.is_compiled_with_rocm():
  63. os.environ["FLAGS_conv_workspace_size_limit"] = "2000"
  64. if device_type.lower() == "npu":
  65. os.environ["FLAGS_npu_jit_compile"] = "0"
  66. os.environ["FLAGS_use_stride_kernel"] = "0"
  67. os.environ["FLAGS_allocator_strategy"] = "auto_growth"
  68. os.environ["CUSTOM_DEVICE_BLACK_LIST"] = (
  69. "pad3d,pad3d_grad,set_value,set_value_with_tensor"
  70. )
  71. os.environ["FLAGS_npu_scale_aclnn"] = "True"
  72. os.environ["FLAGS_npu_split_aclnn"] = "True"
  73. if device_type.lower() == "xpu":
  74. os.environ["BKCL_FORCE_SYNC"] = "1"
  75. os.environ["BKCL_TIMEOUT"] = "1800"
  76. os.environ["FLAGS_use_stride_kernel"] = "0"
  77. if device_type.lower() == "mlu":
  78. os.environ["FLAGS_use_stride_kernel"] = "0"