Skip to content

Commit fbce7ae

Browse files
dsoceklibintasayakpaul
authored
Add generic support for Intel Gaudi accelerator (hpu device) (#11328)
* Add generic support for Intel Gaudi accelerator (hpu device) Signed-off-by: Daniel Socek <[email protected]> Co-authored-by: Libin Tang <[email protected]> * Add loggers for generic HPU support Signed-off-by: Daniel Socek <[email protected]> * Refactor hpu support with is_hpu_available() logic Signed-off-by: Daniel Socek <[email protected]> * Fix style for hpu support update Signed-off-by: Daniel Socek <[email protected]> * Decouple soft HPU check from hard device validation to support HPU migration Signed-off-by: Daniel Socek <[email protected]> --------- Signed-off-by: Daniel Socek <[email protected]> Co-authored-by: Libin Tang <[email protected]> Co-authored-by: Sayak Paul <[email protected]>
1 parent 35fada4 commit fbce7ae

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/diffusers/pipelines/pipeline_utils.py

+15
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
_is_valid_type,
5959
is_accelerate_available,
6060
is_accelerate_version,
61+
is_hpu_available,
6162
is_torch_npu_available,
6263
is_torch_version,
6364
is_transformers_version,
@@ -450,6 +451,20 @@ def module_is_offloaded(module):
450451
f"It seems like you have activated model offloading by calling `enable_model_cpu_offload`, but are now manually moving the pipeline to GPU. It is strongly recommended against doing so as memory gains from offloading are likely to be lost. Offloading automatically takes care of moving the individual components {', '.join(self.components.keys())} to GPU when needed. To make sure offloading works as expected, you should consider moving the pipeline back to CPU: `pipeline.to('cpu')` or removing the move altogether if you use offloading."
451452
)
452453

454+
# Enable generic support for Intel Gaudi accelerator using GPU/HPU migration
455+
if device_type == "hpu" and kwargs.pop("hpu_migration", True) and is_hpu_available():
456+
os.environ["PT_HPU_GPU_MIGRATION"] = "1"
457+
logger.debug("Environment variable set: PT_HPU_GPU_MIGRATION=1")
458+
459+
import habana_frameworks.torch # noqa: F401
460+
461+
# HPU hardware check
462+
if not (hasattr(torch, "hpu") and torch.hpu.is_available()):
463+
raise ValueError("You are trying to call `.to('hpu')` but HPU device is unavailable.")
464+
465+
os.environ["PT_HPU_MAX_COMPOUND_OP_SIZE"] = "1"
466+
logger.debug("Environment variable set: PT_HPU_MAX_COMPOUND_OP_SIZE=1")
467+
453468
module_names, _ = self._get_signature_keys(self)
454469
modules = [getattr(self, n, None) for n in module_names]
455470
modules = [m for m in modules if isinstance(m, torch.nn.Module)]

src/diffusers/utils/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
is_gguf_version,
7272
is_google_colab,
7373
is_hf_hub_version,
74+
is_hpu_available,
7475
is_inflect_available,
7576
is_invisible_watermark_available,
7677
is_k_diffusion_available,

src/diffusers/utils/import_utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ def is_timm_available():
353353
return _timm_available
354354

355355

356+
def is_hpu_available():
357+
return all(importlib.util.find_spec(lib) for lib in ("habana_frameworks", "habana_frameworks.torch"))
358+
359+
356360
# docstyle-ignore
357361
FLAX_IMPORT_ERROR = """
358362
{0} requires the FLAX library but it was not found in your environment. Checkout the instructions on the

0 commit comments

Comments
 (0)