device.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. from .errors import raise_unsupported_device_error
  16. SUPPORTED_DEVICE_TYPE = ["cpu", "gpu", "xpu", "npu", "mlu"]
  17. def get_device(device_cfg, using_device_number=None):
  18. """get running device setting
  19. """
  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() == "npu":
  24. os.environ["FLAGS_npu_jit_compile"] = "0"
  25. os.environ["FLAGS_use_stride_kernel"] = "0"
  26. os.environ["FLAGS_allocator_strategy"] = "auto_growth"
  27. os.environ[
  28. "CUSTOM_DEVICE_BLACK_LIST"] = "pad3d,pad3d_grad,set_value,set_value_with_tensor"
  29. if len(device_cfg.split(":")) == 2:
  30. device_ids = device_cfg.split(":")[1]
  31. else:
  32. device_ids = 0
  33. if using_device_number:
  34. device_ids = f"{device_ids[:using_device_number]}"
  35. return "{}:{}".format(device.lower(), device_ids)
  36. if device.lower() == "cpu":
  37. return "cpu"
  38. else:
  39. raise_unsupported_device_error(device, SUPPORTED_DEVICE_TYPE)