Skip to content

Fix: Conditionally import torch.distributed.fsdp in trainer_seq2seq.py#44507

Merged
3outeille merged 4 commits intohuggingface:mainfrom
0xDELUXA:fix-torch-distributed-import
Mar 9, 2026
Merged

Fix: Conditionally import torch.distributed.fsdp in trainer_seq2seq.py#44507
3outeille merged 4 commits intohuggingface:mainfrom
0xDELUXA:fix-torch-distributed-import

Conversation

@0xDELUXA
Copy link
Copy Markdown
Contributor

@0xDELUXA 0xDELUXA commented Mar 6, 2026

What does this PR do?

torch.distributed.fsdp is not available in all PyTorch builds (for example, Windows ROCm). Importing it unconditionally at the top level causes an immediate crash with:

ModuleNotFoundError: No module named 'torch._C._distributed_c10d'; 'torch._C' is not a package

This makes import transformers fail entirely on those builds, even for users who have no intention of using FSDP or distributed training.

Changes

  • Moved from torch.distributed.fsdp import FullyShardedDataParallel to after the unconditional imports, guarded with if torch.distributed.is_available()
  • Added the same guard to the isinstance(self.model, FullyShardedDataParallel) check to avoid a NameError at runtime on non-distributed builds

Prior art

A previous fix for the same class of issue in this repo was made in:

The same fix was applied to huggingface/diffusers in:

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@SunMarc @3outeille

@0xDELUXA 0xDELUXA changed the title fix: conditionally import torch.distributed.fsdp in trainer_seq2seq Fix: Conditionally import torch.distributed.fsdp in trainer_seq2seq.py Mar 6, 2026
@3outeille
Copy link
Copy Markdown
Member

lgtm

@3outeille 3outeille enabled auto-merge (squash) March 9, 2026 14:17
@3outeille 3outeille merged commit adc0d9a into huggingface:main Mar 9, 2026
17 checks passed
@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.

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.

3 participants