Transformers 4.41.0 does not recognize 'gemma2' model type for google/gemma-2-2b
Author: Ultiautomation
Pull request: no
Labels: name: bug

### System Info

Loading google/gemma-2-2b model raises KeyError: 'gemma2' even with Transformers 4.41.0 and trust_remote_code=True.
Error message is as below: 
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:951, in AutoConfig.from_pretrained(cls, pretrained_model_name_or_path, **kwargs)
    [950](https://vscode-remote+ssh-002dremote-002b157-002e157-002e221-002e29.vscode-resource.vscode-cdn.net/usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:950) try:
--> [951](https://vscode-remote+ssh-002dremote-002b157-002e157-002e221-002e29.vscode-resource.vscode-cdn.net/usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:951)     config_class = CONFIG_MAPPING[config_dict["model_type"]]
    [952](https://vscode-remote+ssh-002dremote-002b157-002e157-002e221-002e29.vscode-resource.vscode-cdn.net/usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:952) except KeyError:

File /usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:653, in _LazyConfigMapping.__getitem__(self, key)
    [652](https://vscode-remote+ssh-002dremote-002b157-002e157-002e221-002e29.vscode-resource.vscode-cdn.net/usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:652) if key not in self._mapping:
--> [653](https://vscode-remote+ssh-002dremote-002b157-002e157-002e221-002e29.vscode-resource.vscode-cdn.net/usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:653)     raise KeyError(key)
    [654](https://vscode-remote+ssh-002dremote-002b157-002e157-002e221-002e29.vscode-resource.vscode-cdn.net/usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:654) value = self._mapping[key]

KeyError: 'gemma2'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
Cell In[12], [line 6](vscode-notebook-cell:?execution_count=12&line=6)
      [3](vscode-notebook-cell:?execution_count=12&line=3) import torch
      [5](vscode-notebook-cell:?execution_count=12&line=5) tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b")
----> [6](vscode-notebook-cell:?execution_count=12&line=6) model = AutoModelForCausalLM.from_pretrained(
      [7](vscode-notebook-cell:?execution_count=12&line=7)     "google/gemma-2-2b",
      [8](vscode-notebook-cell:?execution_count=12&line=8)     device_map="auto",
      [9](vscode-notebook-cell:?execution_count=12&line=9) )
     [11](vscode-notebook-cell:?execution_count=12&line=11) input_text = "Write me a poem about Machine Learning."
...
    [959](https://vscode-remote+ssh-002dremote-002b157-002e157-002e221-002e29.vscode-resource.vscode-cdn.net/usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:959) else:
    [960](https://vscode-remote+ssh-002dremote-002b157-002e157-002e221-002e29.vscode-resource.vscode-cdn.net/usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:960)     # Fallback: use pattern matching on the string.
    [961](https://vscode-remote+ssh-002dremote-002b157-002e157-002e221-002e29.vscode-resource.vscode-cdn.net/usr/local/lib/python3.10/dist-packages/transformers/models/auto/configuration_auto.py:961)     # We go from longer names to shorter names to catch roberta before bert (for instance)

ValueError: The checkpoint you are trying to load has model type `gemma2` but Transformers does not recognize this architecture. This could be because of an issue with the checkpoint, or because your version of Transformers is out of date.
Output is truncated. View as a [scrollable element](command:cellOutput.enableScrolling?edd877b5-d4f7-49d8-b419-74d2aac2418e) or open in a [text editor](command:workbench.action.openLargeOutput?edd877b5-d4f7-49d8-b419-74d2aac2418e). Adjust cell output [settings](command:workbench.action.openSettings?%5B%22%40tag%3AnotebookOutputLayout%22%5D)...

For reference - environment details with versions - 
Verification successful. Libraries imported.
Torch version: 2.2.2+cu121
Transformers version: 4.41.0
Huggingface Hub version: 0.32.2
Captum version: 0.7.0
Numpy version: 1.26.4

Code I am using : 

from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "google/gemma-2-2b"

model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)

print("Model and tokenizer loaded successfully!")


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


The code that triggers the issue:

from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "google/gemma-2-2b"

model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)

### Expected behavior

This should ideally load the model without an error. The code was working earlier too. 

Comments:
@Ultiautomation you need to upgrade the transformer. pip install --upgrade transformers

@Ultiautomation the model that you are using will not work after the release of the Gemma 3 model, as the Gemma 2 model gets deprecated. So whenever you try to run a model, make sure to run the latest model to avoid such an error.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

upgrading transformers helped