NameError: name 'init_empty_weights' is not defined
Author: icode
Pull request: no
Labels: name: bug

### System Info

python 3.13
M4 macos 15.4

### Who can help?

_No response_

### 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

do just
```python
if torch.cuda.is_available():
    device = torch.device("cuda")
else:
    device = torch.device("cpu")
model = CLIPModel.from_pretrained(MODEL_NAME).to(device)
```

### Expected behavior

```log
Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model. `use_fast=True` will be the default behavior in v4.52, even if the model was saved with a slow processor. This will result in minor differences in outputs. You'll still be able to use a slow processor with `use_fast=False`.

Traceback (most recent call last):

  File "/Users/icode/Projects/self/fanfan/ai-backend/app.py", line 5, in <module>

    import handlers

  File "/Users/icode/Projects/self/fanfan/ai-backend/handlers.py", line 16, in <module>

    model = CLIPModel.from_pretrained(MODEL_NAME).to(device)

            ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^

  File "/Users/icode/Projects/self/fanfan/ai-backend/.venv/lib/python3.13/site-packages/transformers/modeling_utils.py", line 279, in _wrapper

    return func(*args, **kwargs)

  File "/Users/icode/Projects/self/fanfan/ai-backend/.venv/lib/python3.13/site-packages/transformers/modeling_utils.py", line 4333, in from_pretrained

    model_init_context = cls.get_init_context(is_quantized, _is_ds_init_called)

  File "/Users/icode/Projects/self/fanfan/ai-backend/.venv/lib/python3.13/site-packages/transformers/modeling_utils.py", line 3736, in get_init_context

    init_contexts = [no_init_weights(), init_empty_weights()]

                                        ^^^^^^^^^^^^^^^^^^

NameError: name 'init_empty_weights' is not defined
```

Comments:
same issue. regression in transformers

Caused by https://github.com/huggingface/transformers/pull/37306.

Maybe fixed by https://github.com/huggingface/transformers/pull/37324


Same issue https://github.com/huggingface/transformers/issues/37311

I was facing the same issue, and downgrading to `transformers==4.50.3` resolved it for me.
I found this solution by referring to https://github.com/huggingface/transformers/issues/37311#issuecomment-2781377634

You can also add `accelerate` as a dependency. This issue pops up in the first place because `init_empty_weights` is from that library, and is only conditionally imported (if it exists).

Ran into the same error. Can 4.51.0 be yanked?

we will patch today

sorry everyone for this! cc @Cyrilvallez 

This will be fixed in https://github.com/huggingface/transformers/pull/37337! In the meantime, you can run `pip install accelerate` to solve it! Super sorry for the disturbance!

Closing as this was fixed on main https://github.com/huggingface/transformers/pull/37337 patching asap

Can we get a patch release for this?

whats the eta on the patch

1h max 

Patch is out for 3 hours sorry for not mentionning ! 

Hi, I'm still hitting this issue on sentence-transformers==5.1.1. Installing `accelerate` fixes the problem; for context, I was installing `model = SentenceTransformer("google/embeddinggemma-300m", token="token")`

Looks like you should raise the issue on sentence-transformers instead of transformers!