From bd41b9c1ac35f81b7672d0b908bad6784dfd768b Mon Sep 17 00:00:00 2001 From: Yuan Wu Date: Mon, 31 Mar 2025 16:23:47 +0800 Subject: [PATCH] Gaudi: Fix the pipeline failed issue with hpu device (#36990) * Gaudi: fix the issue of is_torch_hpu_available() returns false Signed-off-by: yuanwu * Fix make fixup Signed-off-by: yuanwu * Add comments for the implicit behavior of import Signed-off-by: yuanwu * Update src/transformers/utils/import_utils.py * Update src/transformers/utils/import_utils.py --------- Signed-off-by: yuanwu Co-authored-by: Ilyas Moutawwakil <57442720+IlyasMoutawwakil@users.noreply.github.com> --- src/transformers/pipelines/base.py | 10 ++++++++-- src/transformers/utils/import_utils.py | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/transformers/pipelines/base.py b/src/transformers/pipelines/base.py index 70b2ec8ba5..714d69baf4 100644 --- a/src/transformers/pipelines/base.py +++ b/src/transformers/pipelines/base.py @@ -947,12 +947,18 @@ class Pipeline(_ScikitCompat, PushToHubMixin): if device == -1 and self.model.device is not None: device = self.model.device if isinstance(device, torch.device): - if device.type == "xpu" and not is_torch_xpu_available(check_device=True): + if (device.type == "xpu" and not is_torch_xpu_available(check_device=True)) or ( + device.type == "hpu" and not is_torch_hpu_available() + ): raise ValueError(f'{device} is not available, you should use device="cpu" instead') + self.device = device elif isinstance(device, str): - if "xpu" in device and not is_torch_xpu_available(check_device=True): + if ("xpu" in device and not is_torch_xpu_available(check_device=True)) or ( + "hpu" in device and not is_torch_hpu_available() + ): raise ValueError(f'{device} is not available, you should use device="cpu" instead') + self.device = torch.device(device) elif device < 0: self.device = torch.device("cpu") diff --git a/src/transformers/utils/import_utils.py b/src/transformers/utils/import_utils.py index 3897080516..65a0d1bf37 100644 --- a/src/transformers/utils/import_utils.py +++ b/src/transformers/utils/import_utils.py @@ -811,6 +811,10 @@ def is_torch_hpu_available(): import torch + if os.environ.get("PT_HPU_LAZY_MODE", "1") == "1": + # import habana_frameworks.torch in case of lazy mode to patch torch with torch.hpu + import habana_frameworks.torch # noqa: F401 + if not hasattr(torch, "hpu") or not torch.hpu.is_available(): return False