Skip to content

better guarding to handle torch compiled with USE_DISTRIBUTED=0 - #47619

Merged
3outeille merged 20 commits into
mainfrom
fix_distributed_guarding_audio
Jul 31, 2026
Merged

better guarding to handle torch compiled with USE_DISTRIBUTED=0#47619
3outeille merged 20 commits into
mainfrom
fix_distributed_guarding_audio

Conversation

@3outeille

@3outeille 3outeille commented Jul 29, 2026

Copy link
Copy Markdown
Member

This PR fixes #47603 when supported torch version has been compiled without distributed features which prevents import of transformers to work properly (due to non robust distributed guarding)

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@3outeille
3outeille requested a review from ArthurZucker July 29, 2026 07:56
Comment thread tests/utils/test_import_utils.py Outdated
Comment on lines +278 to +284
for name in (
"torch.distributed.tensor",
"torch.distributed.checkpoint",
"torch.distributed.fsdp",
"torch.distributed._composable",
):
sys.modules[name] = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These do exist in USE_DISTRIBUTED=0 build, it's the compiled torch._C._distributed_c10d that is missing.

sys.modules["torch._C._distributed_c10d"] = None would be more faithful to the actual case, the modules may still need evicting del sys.modules[name] though.

@3outeille
3outeille requested a review from tarekziade July 30, 2026 00:40

@vasqu vasqu left a comment

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.

Smaller comments, the one that is important to me is the test design to please use mocks and the sort so we dont play around real object -> less chance of any side effect

if is_torch_available():
import torch

if is_torch_distributed_available() and is_torch_greater_or_equal("2.5"):

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.

Suggested change
if is_torch_distributed_available() and is_torch_greater_or_equal("2.5"):
if is_torch_distributed_available():

We removed support for 2.4.x 🫡

Comment thread src/transformers/distributed/utils.py Outdated
if is_torch_available():

@lru_cache
def is_torch_distributed_available() -> bool:

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.

nit but why not have this live under import utils?

Comment thread src/transformers/core_model_loading.py Outdated
materialize_device = param_device

if isinstance(empty_param, DTensor):
if is_torch_distributed_available() and isinstance(empty_param, DTensor):

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.

Maybe small helper instead of all the time checking both manually

Comment thread tests/utils/test_import_utils.py Outdated
Comment on lines +270 to +287
# Forget transformers, so that importing it below actually re-runs its module-scope imports.
for name in list(sys.modules):
if name.startswith("transformers"):
del sys.modules[name]

# Emulate USE_DISTRIBUTED=0 by faking torch.distributed availability to False and deleting the distributed submodules in sys.modules.
torch.distributed.is_available = lambda: False
sys.modules["torch._C._distributed_c10d"] = None
for name in list(sys.modules):
if name.startswith(
(
"torch.distributed.tensor",
"torch.distributed.checkpoint",
"torch.distributed.fsdp",
"torch.distributed._composable",
)
):
del sys.modules[name]

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.

Could we rather temporarily monkey patch, not feeling too well about setting on "real" objects

@3outeille
3outeille enabled auto-merge July 31, 2026 05:04
@3outeille
3outeille disabled auto-merge July 31, 2026 05:10
@3outeille
3outeille enabled auto-merge July 31, 2026 06:03
@3outeille
3outeille added this pull request to the merge queue Jul 31, 2026
Merged via the queue into main with commit 5e413aa Jul 31, 2026
112 checks passed
@3outeille
3outeille deleted the fix_distributed_guarding_audio branch July 31, 2026 06:58
Sainava pushed a commit to Sainava/Sai-transformers that referenced this pull request Aug 3, 2026
…ingface#47619)

* better guarding to handle torch compiled with USE_DISTRIBUTED=0

* better test

* no private variable

* remove 2.5 guarding

* move is_torch_distributed_available() to utils.py

* create an is_dtensor function

* monkey patch test instead

* linting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Importing AutoImageProcessor fails when PyTorch is built without torch.distributed

4 participants