device.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 lazy_paddle as paddle
  16. from .errors import raise_unsupported_device_error
  17. SUPPORTED_DEVICE_TYPE = ["cpu", "gpu", "xpu", "npu", "mlu"]
  18. def get_device(device_cfg, using_device_number=None):
  19. """get running device setting"""
  20. device = device_cfg.split(":")[0]
  21. assert device.lower() in SUPPORTED_DEVICE_TYPE
  22. if device.lower() in ["gpu", "xpu", "npu", "mlu"]:
  23. if device.lower() == "gpu" and paddle.is_compiled_with_rocm():
  24. os.environ["FLAGS_conv_workspace_size_limit"] = "2000"
  25. if device.lower() == "npu":
  26. os.environ["FLAGS_npu_jit_compile"] = "0"
  27. os.environ["FLAGS_use_stride_kernel"] = "0"
  28. os.environ["FLAGS_allocator_strategy"] = "auto_growth"
  29. os.environ["CUSTOM_DEVICE_BLACK_LIST"] = (
  30. "pad3d,pad3d_grad,set_value,set_value_with_tensor"
  31. )
  32. os.environ["FLAGS_npu_scale_aclnn"] = "True"
  33. os.environ["FLAGS_npu_split_aclnn"] = "True"
  34. if device.lower() == "xpu":
  35. os.environ["BKCL_FORCE_SYNC"] = "1"
  36. os.environ["BKCL_TIMEOUT"] = "1800"
  37. os.environ["FLAGS_use_stride_kernel"] = "0"
  38. if device.lower() == "mlu":
  39. os.environ["FLAGS_use_stride_kernel"] = "0"
  40. if len(device_cfg.split(":")) == 2:
  41. device_ids = device_cfg.split(":")[1]
  42. else:
  43. device_ids = 0
  44. if using_device_number:
  45. device_ids = f"{device_ids[:using_device_number]}"
  46. return "{}:{}".format(device.lower(), device_ids)
  47. if device.lower() == "cpu":
  48. return "cpu"
  49. else:
  50. raise_unsupported_device_error(device, SUPPORTED_DEVICE_TYPE)