No module named 'torch._C._distributed_c10d'; 'torch._C' is not a package on darwin
Author: GaetanLepage
Pull request: no
Labels: name: bug

### System Info

I am a [nixpkgs](https://github.com/NixOS/nixpkgs/) maintainer and manage several python packages there.

https://github.com/huggingface/transformers/commit/20142ab5422fbafcd10e221e37e95f8aaf1bde3c has introduced a regression on `darwin` (both ARM and Intel):
```
import transformers.models.auto.modeling_auto
```
now fails with:

```
ModuleNotFoundError: No module named 'torch._C._distributed_c10d'; 'torch._C' is not a package
```

<details>

```
Traceback (most recent call last):
  File "/nix/store/cgcy6sfgcqwpzfpixwp3r6c24rfsndc3-python3.12-transformers-4.47.0/lib/python3.12/site-packages/transformers/utils/import_utils.py", line 1793, in _get_module
    return importlib.import_module("." + module_name, self.__name__)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/7c494qcmh62av43zsxr3wvzh8hcpy1vl-python3-3.12.7/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/nix/store/cgcy6sfgcqwpzfpixwp3r6c24rfsndc3-python3.12-transformers-4.47.0/lib/python3.12/site-packages/transformers/generation/utils.py", line 41, in <module>
    from ..pytorch_utils import isin_mps_friendly
  File "/nix/store/cgcy6sfgcqwpzfpixwp3r6c24rfsndc3-python3.12-transformers-4.47.0/lib/python3.12/site-packages/transformers/pytorch_utils.py", line 43, in <module>
    from torch.distributed.tensor import Replicate
  File "/nix/store/fgzf3xwls552byw9vdjxqiznxbg62zak-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/distributed/tensor/__init__.py", line 4, in <module>
    import torch.distributed.tensor._ops  # force import all built-in dtensor ops
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/fgzf3xwls552byw9vdjxqiznxbg62zak-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/distributed/tensor/_ops/__init__.py", line 2, in <module>
    from ._conv_ops import *  # noqa: F403
    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/fgzf3xwls552byw9vdjxqiznxbg62zak-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/distributed/tensor/_ops/_conv_ops.py", line 7, in <module>
    from torch.distributed.tensor._dtensor_spec import DTensorSpec, TensorMeta
  File "/nix/store/fgzf3xwls552byw9vdjxqiznxbg62zak-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/distributed/tensor/_dtensor_spec.py", line 6, in <module>
    from torch.distributed.tensor.placement_types import (
  File "/nix/store/fgzf3xwls552byw9vdjxqiznxbg62zak-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/distributed/tensor/placement_types.py", line 8, in <module>
    import torch.distributed._functional_collectives as funcol
  File "/nix/store/fgzf3xwls552byw9vdjxqiznxbg62zak-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/distributed/_functional_collectives.py", line 8, in <module>
    import torch.distributed.distributed_c10d as c10d
  File "/nix/store/fgzf3xwls552byw9vdjxqiznxbg62zak-python3.12-torch-2.5.1/lib/python3.12/site-packages/torch/distributed/distributed_c10d.py", line 22, in <module>
    from torch._C._distributed_c10d import (
ModuleNotFoundError: No module named 'torch._C._distributed_c10d'; 'torch._C' is not a package

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <lambda>
  File "/nix/store/7c494qcmh62av43zsxr3wvzh8hcpy1vl-python3-3.12.7/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/nix/store/cgcy6sfgcqwpzfpixwp3r6c24rfsndc3-python3.12-transformers-4.47.0/lib/python3.12/site-packages/transformers/models/auto/modeling_auto.py", line 21, in <module>
    from .auto_factory import (
  File "/nix/store/cgcy6sfgcqwpzfpixwp3r6c24rfsndc3-python3.12-transformers-4.47.0/lib/python3.12/site-packages/transformers/models/auto/auto_factory.py", line 40, in <module>
    from ...generation import GenerationMixin
  File "<frozen importlib._bootstrap>", line 1412, in _handle_fromlist
  File "/nix/store/cgcy6sfgcqwpzfpixwp3r6c24rfsndc3-python3.12-transformers-4.47.0/lib/python3.12/site-packages/transformers/utils/import_utils.py", line 1781, in __getattr__
    module = self._get_module(self._class_to_module[name])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/cgcy6sfgcqwpzfpixwp3r6c24rfsndc3-python3.12-transformers-4.47.0/lib/python3.12/site-packages/transformers/utils/import_utils.py", line 1795, in _get_module
    raise RuntimeError(
RuntimeError: Failed to import transformers.generation.utils because of the following error (look up to see its traceback):
No module named 'torch._C._distributed_c10d'; 'torch._C' is not a package
```

</details>

This has been detected when running the test suite of [accelerate](https://github.com/huggingface/accelerate).

Thank you in advance for helping us :)

### Who can help?

cc @natsukium

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

```
import transformers.models.auto.modeling_auto
```

### Expected behavior

Imports correctly.

Comments: