Skip to content

Fix AttributeError in Gemma3ForConditionalGeneration and Gemma3ForSequenceClassification when config.return_dict=False#45277

Merged
zucchini-nlp merged 2 commits intohuggingface:mainfrom
kamalrajkannan78:kkannan/fix_gemma3_attribute_error
Apr 10, 2026
Merged

Fix AttributeError in Gemma3ForConditionalGeneration and Gemma3ForSequenceClassification when config.return_dict=False#45277
zucchini-nlp merged 2 commits intohuggingface:mainfrom
kamalrajkannan78:kkannan/fix_gemma3_attribute_error

Conversation

@kamalrajkannan78
Copy link
Copy Markdown
Contributor

@kamalrajkannan78 kamalrajkannan78 commented Apr 7, 2026

What does this PR do?

  • Gemma3ForConditionalGeneration.forward & Gemma3ForSequenceClassification.forward calls self.model() without return_dict=True, so @can_return_tuple silently converts the output to a plain tuple, causing outputs.past_key_values attribute access to crash.
venv/lib/python3.12/site-packages/transformers/utils/generic.py:841: in wrapper
    output = func(self, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
venv/lib/python3.12/site-packages/transformers/models/gemma3/modeling_gemma3.py:1127: in forward
    past_key_values=outputs.past_key_values,
                    ^^^^^^^^^^^^^^^^^^^^^^^
E   AttributeError: 'tuple' object has no attribute 'past_key_values'
  • Fix: pass return_dict=True explicitly to self.model(...) in both classes, consistent with how Gemma3Model.forward already does for its inner self.language_model(...) call. The caller's return_dict=False preference is still honored by @can_return_tuple on the outer forward at the boundary.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? 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, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@zucchini-nlp this is related to the Gemma3Model wrapper introduced in #37033, would appreciate your review!

Copy link
Copy Markdown
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, can_return_tuple already would force-set return_dict=True before calling the forward. Could you provide a reproducer?

@kamalrajkannan78
Copy link
Copy Markdown
Contributor Author

Interesting, can_return_tuple already would force-set return_dict=True before calling the forward. Could you provide a reproducer?

pip install transformers==5.5.0

import torch
from transformers import AutoModelForImageTextToText, AutoProcessor
from loguru import logger

def test_gemma3():
    
    model_id = "google/translategemma-4b-it"
    processor = AutoProcessor.from_pretrained(model_id)
    model = AutoModelForImageTextToText.from_pretrained(model_id,dtype=torch.bfloat16,return_dict=False)
    logger.info("model={}",model)
    
    # ---- Text Extraction and Translation ----
    messages = [
        {
            "role": "user",
            "content": [
                {
                    "type": "image",
                    "source_lang_code": "cs",
                    "target_lang_code": "de-DE",
                    "url": "https://c7.alamy.com/comp/2YAX36N/traffic-signs-in-czech-republic-pedestrian-zone-2YAX36N.jpg",
                },
            ],
        }
    ]

    inputs = processor.apply_chat_template(
        messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt"
    )
    
    for key, value in inputs.items():
        if isinstance(value, torch.Tensor) and value.dtype == torch.float32:
            inputs[key] = value.to(torch.bfloat16)
    
    
    with torch.no_grad():
        outputs = model(**inputs)
        
    logger.info("outputs={}",outputs)

Error log - apr7_gemma3_v5_5_0.log

Copy link
Copy Markdown
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh indeed, i guess I am confusing it with another helper. Could you mark PR as ready for review?

I think there might be more models so if you have bandwidth, would be great to fix all at once. Dont worry if not :)

zucchini-nlp

This comment was marked as duplicate.

@zucchini-nlp
Copy link
Copy Markdown
Member

and run make fix-repo pls to make CI ✅

@kamalrajkannan78 kamalrajkannan78 force-pushed the kkannan/fix_gemma3_attribute_error branch from db6c323 to 6c20ba1 Compare April 8, 2026 16:22
@kamalrajkannan78
Copy link
Copy Markdown
Contributor Author

Huh indeed, i guess I am confusing it with another helper. Could you mark PR as ready for review?

I think there might be more models so if you have bandwidth, would be great to fix all at once. Dont worry if not :)

Sorry, I don’t have the bandwidth to look into other models at the moment. However, if I come across similar issues in other models in the future, I’ll make sure to raise a PR to fix them as well.

Thanks for the review!

@kamalrajkannan78 kamalrajkannan78 marked this pull request as ready for review April 8, 2026 16:25
@zucchini-nlp
Copy link
Copy Markdown
Member

To fix CI with make fix-repo, you'd need to apply the changes in modular and if needed, in Paligemma files. Then I'll merge

…uenceClassification when config.return_dict=False
@kamalrajkannan78 kamalrajkannan78 force-pushed the kkannan/fix_gemma3_attribute_error branch from 6c20ba1 to f1b2e19 Compare April 8, 2026 16:53
@kamalrajkannan78
Copy link
Copy Markdown
Contributor Author

kamalrajkannan78 commented Apr 8, 2026

To fix CI with make fix-repo, you'd need to apply the changes in modular and if needed, in Paligemma files. Then I'll merge

  • I've applied the fix in modular_gemma3.py and ran make fix-repo — all 14 checks pass locally. I also checked Paligemma but it doesn't have the same pattern (no missing return_dict in internal model calls).
  • The tests_hub / run_tests failure is due to a HuggingFace Hub 401 authentication error when downloading test fixtures (cow_beach_1.png from hf-internal-testing/fixtures-captioning), not related to my code change. Could you please re-trigger the CI?
Screenshot 2026-04-08 at 10 36 26 PM

@zucchini-nlp zucchini-nlp enabled auto-merge April 10, 2026 10:21
@github-actions
Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: gemma3

@zucchini-nlp zucchini-nlp added this pull request to the merge queue Apr 10, 2026
@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Merged via the queue into huggingface:main with commit 507a37d Apr 10, 2026
21 checks passed
sirzechs66 pushed a commit to sirzechs66/transformers that referenced this pull request Apr 18, 2026
…uenceClassification when config.return_dict=False (huggingface#45277)

Co-authored-by: ctr-kkannan <ctr-kkannan@ext.tenstorrent.com>
Co-authored-by: Raushan Turganbay <raushan@huggingface.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants