Proposed fix for V4.49.0 gemma 3 release to solve issue #36758
Author: Zephyr271828
Pull request: yes

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

This PR is a proposed solution for [issue #36758](https://github.com/huggingface/transformers/issues/36758). In a word, this issue points out two bugs in the [v4.49.0-Gemma-3-release](https://github.com/huggingface/transformers/tree/v4.49.0-Gemma-3-release) branch of transformers, and this PR uses two fixes to fix these two bugs respectively.

Reproduction:
```python
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
    "./models/Llama-2-7b-hf",
    device_map="cpu"
)
```

Bug 1:
```bash
AttributeError: 'LlamaForCausalLM' object has no attribute 'model.layers.0.self_attn.rotary_emb.inv_freq'
```

Fix 1:
After checking the differences between the main branch and this branch, I noticed the bug lies in `caching_allocator_warmup()` [main](https://github.com/huggingface/transformers/blob/7f5077e53682ca855afc826162b204ebf809f1f9/src/transformers/modeling_utils.py#L5816) | [branch](https://github.com/huggingface/transformers/blob/46350f5eae87ac1d168ddfdc57a0b39b64b9a029/src/transformers/modeling_utils.py#L5904). The new version removes the function `model.get_parameter_or_buffer()` and implements the function directly in the loop. However, the main branch is using `model.get_parameter(name)` whereas the new version `getattr(model, name)`, which is likely the cause of the issue. Therefore, I changed `getattr()` to `model.get_parameter()`.

Bug 2:
```bash
AttributeError: LlamaAttention has no attribute `rotary_emb`
```

Fix 2:
Again, after reviewing the difference between the branch and the new branch, I noticed the new version is likely passing a wrong `expanded_device_map` to [`caching_allocator_warmup()`](https://github.com/huggingface/transformers/blob/46350f5eae87ac1d168ddfdc57a0b39b64b9a029/src/transformers/modeling_utils.py#L5886). The `expanded_device_map` is created with `expanded_device_map = expand_device_map(device_map, expected_keys)` whereas `expanded_device_map = expand_device_map(device_map, original_loaded_keys, start_prefix)` in the new version. After checking the context, I decided to modify the new version to `expanded_device_map = expand_device_map(device_map, expected_keys, start_prefix)`. See [main](https://github.com/huggingface/transformers/blob/7f5077e53682ca855afc826162b204ebf809f1f9/src/transformers/modeling_utils.py#L4826) | [branch](https://github.com/huggingface/transformers/blob/46350f5eae87ac1d168ddfdc57a0b39b64b9a029/src/transformers/modeling_utils.py#L4892C13-L4892C100) for comparison.

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? 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](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker
- vision models: @amyeroberts, @qubvel
- speech models: @eustlb
- graph models: @clefourrier

Library:

- flax: @gante and @Rocketknight1
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc @MekkCyber

Documentation: @stevhliu

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @Rocketknight1
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page).

Hi @Zephyr271828, thank you for the PR but I believe these lines have already been changed on `main`! We generally don't accept PRs for branches like `v4.49.0-Gemma-3-release` because these are intended as "temporary" release branches to rapidly add support for new models until we can fully support them in the next version.

Can you check if the bug is still present on `main`, and if so, can you raise a PR to the `main` branch to fix it?

> Hi @Zephyr271828, thank you for the PR but I believe these lines have already been changed on `main`! We generally don't accept PRs for branches like `v4.49.0-Gemma-3-release` because these are intended as "temporary" release branches to rapidly add support for new models until we can fully support them in the next version.
> 
> Can you check if the bug is still present on `main`, and if so, can you raise a PR to the `main` branch to fix it?

Hi Matt @Rocketknight1 , thank you for your clarification! I just checked the main branch and it seems the causes of the bugs mentioned in issue #36758 are no longer present in the main branch. I will try to make other PRs to the main branch in the future. Should I close this PR?

Hi @Zephyr271828, yes, if it's already fixed on main then you can close it. It was a good PR with an excellent explanation though, so don't be discouraged - I hope to see more PRs from you in future!

> Hi @Zephyr271828, yes, if it's already fixed on main then you can close it. It was a good PR with an excellent explanation though, so don't be discouraged - I hope to see more PRs from you in future!

Hi Matt @Rocketknight1 , thank you for your comments! Will do that in the future:)