-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Fix MLU device utilities and test backend support #14791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SeptPonts
wants to merge
2
commits into
huggingface:main
Choose a base branch
from
SeptPonts:feature/h3-mlu-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+25
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ | |
| is_sdnq_available, | ||
| is_timm_available, | ||
| is_torch_available, | ||
| is_torch_mlu_available, | ||
| is_torch_neuronx_available, | ||
| is_torch_version, | ||
| is_torchao_available, | ||
|
|
@@ -100,6 +101,8 @@ | |
| else: | ||
| if torch.cuda.is_available(): | ||
| torch_device = "cuda" | ||
| elif is_torch_mlu_available() and hasattr(torch, "mlu") and torch.mlu.is_available(): | ||
| torch_device = "mlu" | ||
| elif torch.xpu.is_available(): | ||
| torch_device = "xpu" | ||
| elif is_torch_neuronx_available() and hasattr(torch, "neuron") and torch.neuron.is_available(): | ||
|
|
@@ -1494,7 +1497,7 @@ def _is_torch_fp64_available(device): | |
| # Guard these lookups for when Torch is not used - alternative accelerator support is for PyTorch | ||
| if is_torch_available(): | ||
| # Behaviour flags | ||
| BACKEND_SUPPORTS_TRAINING = {"cuda": True, "xpu": True, "cpu": True, "mps": False, "default": True} | ||
| BACKEND_SUPPORTS_TRAINING = {"cuda": True, "mlu": True, "xpu": True, "cpu": True, "mps": False, "default": True} | ||
|
|
||
| # Neuron device key: torch.neuron.current_device() returns an int (e.g. 0). | ||
| # We capture it once at import time if torch_neuronx is available so we can add it | ||
|
|
@@ -1508,48 +1511,55 @@ def _is_torch_fp64_available(device): | |
| # Function definitions | ||
| BACKEND_EMPTY_CACHE = { | ||
| "cuda": torch.cuda.empty_cache, | ||
| "mlu": getattr(getattr(torch, "mlu", None), "empty_cache", None), | ||
| "xpu": torch.xpu.empty_cache, | ||
| "cpu": None, | ||
| "mps": torch.mps.empty_cache, | ||
| "default": None, | ||
| } | ||
| BACKEND_DEVICE_COUNT = { | ||
| "cuda": torch.cuda.device_count, | ||
| "mlu": lambda: getattr(getattr(torch, "mlu", None), "device_count", lambda: 0)(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
| "xpu": torch.xpu.device_count, | ||
| "cpu": lambda: 0, | ||
| "mps": lambda: 0, | ||
| "default": 0, | ||
| } | ||
| BACKEND_MANUAL_SEED = { | ||
| "cuda": torch.cuda.manual_seed, | ||
| "mlu": getattr(getattr(torch, "mlu", None), "manual_seed", torch.manual_seed), | ||
| "xpu": torch.xpu.manual_seed, | ||
| "cpu": torch.manual_seed, | ||
| "mps": torch.mps.manual_seed, | ||
| "default": torch.manual_seed, | ||
| } | ||
| BACKEND_RESET_PEAK_MEMORY_STATS = { | ||
| "cuda": torch.cuda.reset_peak_memory_stats, | ||
| "mlu": getattr(getattr(torch, "mlu", None), "reset_peak_memory_stats", None), | ||
| "xpu": getattr(torch.xpu, "reset_peak_memory_stats", None), | ||
| "cpu": None, | ||
| "mps": None, | ||
| "default": None, | ||
| } | ||
| BACKEND_RESET_MAX_MEMORY_ALLOCATED = { | ||
| "cuda": torch.cuda.reset_max_memory_allocated, | ||
| "mlu": getattr(getattr(torch, "mlu", None), "reset_peak_memory_stats", None), | ||
| "xpu": getattr(torch.xpu, "reset_peak_memory_stats", None), | ||
| "cpu": None, | ||
| "mps": None, | ||
| "default": None, | ||
| } | ||
| BACKEND_MAX_MEMORY_ALLOCATED = { | ||
| "cuda": torch.cuda.max_memory_allocated, | ||
| "mlu": getattr(getattr(torch, "mlu", None), "max_memory_allocated", 0), | ||
| "xpu": getattr(torch.xpu, "max_memory_allocated", None), | ||
| "cpu": 0, | ||
| "mps": 0, | ||
| "default": 0, | ||
| } | ||
| BACKEND_SYNCHRONIZE = { | ||
| "cuda": torch.cuda.synchronize, | ||
| "mlu": getattr(getattr(torch, "mlu", None), "synchronize", None), | ||
| "xpu": getattr(torch.xpu, "synchronize", None), | ||
| "cpu": None, | ||
| "mps": None, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this the case? is empty_cache not available generally in Torch MLU?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, empty_cache() is available in torch_mlu, and I verified it on hardware.
The guard is for the optional backend namespace: unlike the built-in torch.cuda and torch.xpu modules, torch.mlu is registered by the external torch_mlu package through PyTorch’s PrivateUse1 integration. Without the extension loaded, accessing torch.mlu.empty_cache while constructing this module-level dictionary raises AttributeError: module 'torch' has no attribute 'mlu'. This prevents the shared utilities from being imported even for CPU/CUDA tests, before any MLU operation is requested.
I followed the guarded-access pattern already used for Neuron’s device_count and synchronize entries in src/diffusers/utils/torch_utils.py. The same reasoning applies to the device_count comment below.