AttributeError: 'Resampler' object has no attribute '_initialize_weights'
Author: DarwinYang
Pull request: no
Labels: name: bug

### System Info

transformers version: 4.52.4
Platform: Linux-5.4.282-1.el8.elrepo.x86_64-x86_64-with-glibc2.31
Python version: 3.10.18
Huggingface_hub version: 0.33.0
Safetensors version: 0.5.3
Accelerate version: 1.7.0
Accelerate config: not found
DeepSpeed version: 0.14.4
PyTorch version (GPU?): 2.7.1+cu126 (True)
Tensorflow version (GPU?): not installed (NA)
Flax version (CPU?/GPU?/TPU?): not installed (NA)
Jax version: not installed
JaxLib version: not installed
Using distributed or parallel set-up in script?:
Using GPU in script?:

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

When I was training the model using LLaMa-Factory, I utilized transformers/modeling_utils.py and encountered the following error:

`  File "/home/admin/miniforge3/envs/llama-factory/lib/python3.10/site-packages/transformers/modeling_utils.py", line 2557, in initialize_weights self.smart_apply(self._initialize_weights) File "/home/admin/miniforge3/envs/llama-factory/lib/python3.10/site-packages/transformers/modeling_utils.py", line 2548, in smart_apply module.smart_apply(module._initialize_weights) File "/home/admin/miniforge3/envs/llama-factory/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1940, in __getattr__ raise AttributeError( AttributeError: 'Resampler' object has no attribute '_initialize_weights'`

### Expected behavior

**module.smart_apply(module._initialize_weights)** in the initialize_weights Function of modeling_utils.py

```
if not hasattr(torch.nn.Module, "smart_apply"):
            # This function is equivalent to `torch.nn.Module.apply`, except that it dynamically adjust the function
            # to apply as we go down the graph
            def smart_apply(self, fn):
                for module in self.children():
                    # We found a sub-model: recursively dispatch its own init function now!
                    if hasattr(module, "_init_weights"):
                        module.smart_apply(module._initialize_weights)
                    else:
                        module.smart_apply(fn)
                fn(self)
                return self
```

In my local environment, I made the belowing adjustment to the line of code, and it allowed the Omni large model training to run successfully. Please have the developers of Transformers evaluate this.

**module.smart_apply(module._init_weights)**

Comments:
Hi @DarwinYang , Thank you for detail report, Let me investigation this issue.

@Flink-ddd , Thank you 

Besides, I have identified the same suspected bug in the following versions: v4.52.4, v4.52.3, v4.52.2, v4.52.1, and v4.52.0. As for other versions of Transformers, I have not analyzed them one by one.

Hi @DarwinYang , actually, I original try complete modeling_utils.py logical and run test success in local environment, but it's will effect other models, because almost all of model need rely on  this file, so I already canceled this PR and re-evaluation this issue.

@Flink-ddd , Can you provide some examples of the impact on other models? From the logic of these two lines of code, it seems difficult to see any impact. Thanks ~

```
if hasattr(module, "_init_weights"):
    module.smart_apply(module._initialize_weights)
```

This is not a bug and it's totally intended - see the definition of `_initialize_weights` which is just a small wrapper to avoid re-init. I don't know what you were trying to do since you did not provide any repro, but this is either remote code on the hub or custom code for which we are not responsible. 

Closing this issue, but feel free to reopen if needed 🤗

@DarwinYang  try degrade transformers to 4.49.0

ref: https://github.com/microsoft/OmniParser/issues/304

Another workaround is to just add this function if you want to keep an up-to-date transformers version.