Skip to content

Fix 'T5GemmaConfig' object has no attribute 'num_hidden_layers'#40454

Closed
lewtun wants to merge 13 commits intomainfrom
fix-t5-gemma-config
Closed

Fix 'T5GemmaConfig' object has no attribute 'num_hidden_layers'#40454
lewtun wants to merge 13 commits intomainfrom
fix-t5-gemma-config

Conversation

@lewtun
Copy link
Copy Markdown
Member

@lewtun lewtun commented Aug 26, 2025

What does this PR do?

Fixes AttributeError: 'T5GemmaConfig' object has no attribute 'num_hidden_layers' when training. I wasn't sure if this kind of issue is typically unit tested, so I'm happy to add a regression test if you can point me to where it should go :)

Minimal repro script below:

echo -e "Question: Why is the sky blue? Answer:" | transformers run --task text2text-generation --model google/t5gemma-b-b-ul2 --device 0

Stack trace on main (commit 58cebc848baa0af2e4ff159fb11504d94179f376):

Traceback (most recent call last):
  File "/fsx/lewis/git/hf/transformers/transformers/bin/transformers", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/fsx/lewis/git/hf/transformers/src/transformers/commands/transformers_cli.py", line 59, in main
    service.run()
  File "/fsx/lewis/git/hf/transformers/src/transformers/commands/run.py", line 99, in run
    output = nlp(**entry) if self._reader.is_multi_columns else nlp(entry)
                                                                ^^^^^^^^^^
  File "/fsx/lewis/git/hf/transformers/src/transformers/pipelines/text2text_generation.py", line 191, in __call__
    result = super().__call__(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/lewis/git/hf/transformers/src/transformers/pipelines/base.py", line 1467, in __call__
    return self.run_single(inputs, preprocess_params, forward_params, postprocess_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/lewis/git/hf/transformers/src/transformers/pipelines/base.py", line 1474, in run_single
    model_outputs = self.forward(model_inputs, **forward_params)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/lewis/git/hf/transformers/src/transformers/pipelines/base.py", line 1374, in forward
    model_outputs = self._forward(model_inputs, **forward_params)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/lewis/git/hf/transformers/src/transformers/pipelines/text2text_generation.py", line 220, in _forward
    output_ids = self.model.generate(**model_inputs, **generate_kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/lewis/git/hf/transformers/transformers/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/lewis/git/hf/transformers/src/transformers/generation/utils.py", line 2399, in generate
    self._prepare_cache_for_generation(
  File "/fsx/lewis/git/hf/transformers/src/transformers/generation/utils.py", line 2007, in _prepare_cache_for_generation
    else EncoderDecoderCache(DynamicCache(**dynamic_cache_kwargs), DynamicCache(**dynamic_cache_kwargs))
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/lewis/git/hf/transformers/src/transformers/cache_utils.py", line 1019, in __init__
    for _ in range(config.num_hidden_layers)
                   ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/fsx/lewis/git/hf/transformers/src/transformers/configuration_utils.py", line 207, in __getattribute__
    return super().__getattribute__(key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'T5GemmaConfig' object has no attribute 'num_hidden_layers'

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?

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.

@lewtun lewtun requested a review from gante August 26, 2025 11:58
@lewtun
Copy link
Copy Markdown
Member Author

lewtun commented Aug 26, 2025

Ah I see my change led to some failed tests - let me investigate

Comment thread src/transformers/models/t5gemma/modeling_t5gemma.py Outdated
@lewtun lewtun marked this pull request as draft August 26, 2025 13:49
@lewtun lewtun removed the request for review from gante August 26, 2025 13:49
@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.

@lewtun lewtun mentioned this pull request Aug 27, 2025
4 tasks
@lewtun lewtun force-pushed the fix-t5-gemma-config branch from a18531d to 725d8f3 Compare August 27, 2025 08:10
@lewtun lewtun changed the title Fix T5Gemma config Fix 'T5GemmaConfig' object has no attribute 'num_hidden_layers' Aug 27, 2025
@lewtun lewtun marked this pull request as ready for review August 27, 2025 09:24
@lewtun lewtun requested a review from ArthurZucker August 27, 2025 09:24
@gante
Copy link
Copy Markdown
Contributor

gante commented Aug 27, 2025

@lewtun I think this is an issue with the EncoderDecoderCache initialization, rather than with T5Gemma -- #40277 corrected a few things for encoder-decoder models, but I missed the lines listed in your stack trace!

In a nutshell, the EncoderDecoderCache initialization in L2003-2007 in utils.py needs to follow the same pattern as in _get_cache, and pull the right encoder/decoder sub-configs before initializing the corresponding DynamicCache.

Do you want to have a go at it, or would you rather have me fix it? 🤗

@lewtun
Copy link
Copy Markdown
Member Author

lewtun commented Aug 27, 2025

@lewtun I think this is an issue with the EncoderDecoderCache initialization, rather than with T5Gemma -- #40277 corrected a few things for encoder-decoder models, but I missed the lines listed in your stack trace!

In a nutshell, the EncoderDecoderCache initialization in L2003-2007 in utils.py needs to follow the same pattern as in _get_cache, and pull the right encoder/decoder sub-configs before initializing the corresponding DynamicCache.

Do you want to have a go at it, or would you rather have me fix it? 🤗

Thanks for the context! I can have a go at it :)

@lewtun lewtun force-pushed the fix-t5-gemma-config branch from 1aef8f6 to ef13c59 Compare August 27, 2025 09:50
Copy link
Copy Markdown
Contributor

@gante gante left a comment

Choose a reason for hiding this comment

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

A few nits to trim unnecessary logic :D

Comment thread src/transformers/generation/utils.py Outdated
Comment on lines +2012 to +2016
# Access the encoder/decoder sub-configs directly for models like T5Gemma
if hasattr(self.config, "decoder") and hasattr(self.config, "encoder"):
decoder_cache_kwargs["config"] = self.config.decoder
encoder_cache_kwargs["config"] = self.config.encoder
else:
Copy link
Copy Markdown
Contributor

@gante gante Aug 27, 2025

Choose a reason for hiding this comment

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

This shouldn't be needed -- if self.config.get_text_config(...) is not working properly, then it means it is missing logic :)

(I think get_text_config() is missing "encoder" in the encoder_possible_text_config_names list)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for the tip, I tried adding "encoder" to encoder_possible_text_config_names but then hit this constraint when get_text_config() is called with default values:

if len(valid_text_config_names) > 1:
raise ValueError(
f"Multiple valid text configs were found in the model config: {valid_text_config_names}. In this "
"case, using `get_text_config()` would be ambiguous. Please specify the desired text config directly, "
"e.g. `text_config = config.sub_config_name`"

I have now tried to override the get_text_config() method for T5Gemma specifically, but let me know if you think the base method should be made to work out of the box instead

Comment thread src/transformers/generation/utils.py Outdated
Comment on lines +2007 to +2009
# For encoder-decoder models, we need to use separate configs for encoder and decoder
decoder_cache_kwargs = dynamic_cache_kwargs.copy()
encoder_cache_kwargs = dynamic_cache_kwargs.copy()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

dynamic_cache_kwargs only contains a config which we know for sure we won't use in this branch -- we don't need to copy the object, we can create a new dictionary

Comment thread src/transformers/generation/utils.py Outdated
decoder_cache_kwargs = dynamic_cache_kwargs.copy()
encoder_cache_kwargs = dynamic_cache_kwargs.copy()

if "config" in dynamic_cache_kwargs:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is always true in this branch

Comment thread src/transformers/generation/utils.py Outdated
Comment thread src/transformers/models/t5gemma/configuration_t5gemma.py
@github-actions
Copy link
Copy Markdown
Contributor

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

run-slow: t5gemma

@gante
Copy link
Copy Markdown
Contributor

gante commented Aug 29, 2025

(see #40553 )

@gante
Copy link
Copy Markdown
Contributor

gante commented Sep 1, 2025

closing in favor of #40553

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