Skip to content

fix: add PyTorch version check for torch.accelerator in gather_size_by_comm#13093

Open
Mr-Neutr0n wants to merge 2 commits into
huggingface:mainfrom
Mr-Neutr0n:fix/pytorch-version-accelerator-check
Open

fix: add PyTorch version check for torch.accelerator in gather_size_by_comm#13093
Mr-Neutr0n wants to merge 2 commits into
huggingface:mainfrom
Mr-Neutr0n:fix/pytorch-version-accelerator-check

Conversation

@Mr-Neutr0n

Copy link
Copy Markdown

Summary

Add backward compatibility check for torch.accelerator.current_accelerator() which only exists in PyTorch 2.6+.

Problem

gather_size_by_comm() in _modeling_parallel.py uses torch.accelerator.current_accelerator() without checking if it exists. Since diffusers officially supports PyTorch 2.1+, this causes AttributeError: module 'torch' has no attribute 'accelerator' on PyTorch versions 2.1-2.5.

Solution

Added hasattr(torch, "accelerator") check with fallback to "cuda" for older PyTorch versions. This matches the pattern already used in other parts of the codebase:

  • src/diffusers/loaders/lora_pipeline.py (line 111)
  • src/diffusers/hooks/group_offloading.py (lines 121-125)
  • src/diffusers/quantizers/gguf/gguf_quantizer.py (lines 159-163)

Fixes #13074

…y_comm

torch.accelerator.current_accelerator() only exists in PyTorch 2.6+.
Since diffusers officially supports PyTorch 2.1+, this causes
AttributeError on versions 2.1-2.5.

Added hasattr check with fallback to 'cuda' for older PyTorch versions,
matching the pattern already used in lora_pipeline.py.

Fixes huggingface#13074
@Mr-Neutr0n

Copy link
Copy Markdown
Author

Friendly bump! Let me know if there's anything I should update or improve to help move this forward.

elif hasattr(torch, "accelerator"):
gather_device = torch.accelerator.current_accelerator()
else:
gather_device = "cuda"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can actually be reduced to a one liner if we use

gather_device = "cpu" if "cpu" in comm_backends else get_device()

@DN6 (2026-02-13) suggested using the existing get_device() helper from
torch_utils instead of the inline hasattr(torch, "accelerator") check.
get_device() already returns the appropriate device string for the
current environment and falls back to cpu when no accelerator is
available, which makes the three-branch if/elif/else a one-liner.
@Mr-Neutr0n

Copy link
Copy Markdown
Author

@DN6 — done. Pushed as 3a12e93 on the same branch: the three-branch if/elif/else is now a one-liner that uses get_device() from torch_utils. Behavior is preserved (cpu for cpu backends, otherwise the appropriate device via get_device(), which already handles accelerator version compatibility internally).

@github-actions github-actions Bot added models fixes-issue size/S PR with diff < 50 LOC labels Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes-issue models size/S PR with diff < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Distributed gather crashes on PyTorch < 2.6

2 participants