Skip to content

chore(typing): Add type checking to src/transformers/generation#44233

Merged
Cyrilvallez merged 22 commits intomainfrom
tarekziade-typing-generation
Mar 4, 2026
Merged

chore(typing): Add type checking to src/transformers/generation#44233
Cyrilvallez merged 22 commits intomainfrom
tarekziade-typing-generation

Conversation

@tarekziade
Copy link
Copy Markdown
Collaborator

@tarekziade tarekziade commented Feb 23, 2026

Extends ty coverage to src/transformers/generation

  • Added a dedicated type-check wrapper script: utils/check_types.py.
  • Updated Makefile to run ty checks through the wrapper in both style and check-repo.
  • merged all typing in a single module src/transformers/_typing.py
    • TransformersLogger
    • GenerativePreTrainedModel
    • WhisperGenerationConfigLike
  • Aligned model overrides with updated generation method signatures in clvp, musicgen, musicgen_melody

@tarekziade tarekziade self-assigned this Feb 23, 2026
self.loop = asyncio.get_running_loop()
self.has_asyncio_timeout = hasattr(asyncio, "timeout")
timeout_context = getattr(asyncio, "timeout", None)
self.has_asyncio_timeout = sys.version_info >= (3, 11) and callable(timeout_context)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

we can tighten this with the python version (function was added in 3.11)

@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.

@tarekziade tarekziade force-pushed the tarekziade-typing-generation branch from 8c59847 to 1dfddc1 Compare February 23, 2026 19:16
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.

Apart from a few nits, my only question is about audio-model specific attributes on generation config. We indeed have several special attributes, and I think whisper has the largest amount

Is there any way to not add them all as class attributes in the general GenerationConfig, because they don't need to be accessible when loading a text only model?

Comment thread src/transformers/generation/_typing.py Outdated
Comment thread src/transformers/generation/candidate_generator.py Outdated
Comment thread src/transformers/generation/configuration_utils.py Outdated
Comment thread src/transformers/generation/streamers.py Outdated
Comment thread src/transformers/generation/utils.py Outdated
Comment thread src/transformers/generation/utils.py Outdated
Comment thread src/transformers/generation/watermarking.py Outdated
@tarekziade tarekziade force-pushed the tarekziade-typing-generation branch 2 times, most recently from a0a0445 to a212761 Compare February 24, 2026 11:18
@tarekziade
Copy link
Copy Markdown
Collaborator Author

Apart from a few nits, my only question is about audio-model specific attributes on generation config. We indeed have several special attributes, and I think whisper has the largest amount

Is there any way to not add them all as class attributes in the general GenerationConfig, because they don't need to be accessible when loading a text only model?

Yeah we don't want to pollute GenerationConfig for sure,

The right pattern imho:

  1. Keep GenerationConfig generic (no Whisper/audio field declarations).
  2. Define small model-specific Protocols (e.g. WhisperGenerationConfigLike).
  3. Cast locally at Whisper/audio-only use sites.

That avoids polluting text-only configs while keeping static typing strict where those attrs are actually used.
This is what I added for whisper in _typing after your review

Copy link
Copy Markdown
Member

@Cyrilvallez Cyrilvallez left a comment

Choose a reason for hiding this comment

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

Nice in general! Just left a few comments to try to make it a bit simpler!

Comment thread src/transformers/generation/utils.py Outdated
Comment thread src/transformers/generation/utils.py Outdated
Comment on lines -1750 to +1786
cache_to_check = self._cache.self_attention_cache if self.config.is_encoder_decoder else self._cache
if isinstance(self._cache, EncoderDecoderCache):
cache_to_check = self._cache.self_attention_cache
elif isinstance(self._cache, StaticCache):
cache_to_check = self._cache
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe both checks are equivalent indeed, but curious to know why we need it to change?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I changed it to use isinstance-based narrowing for two reasons:

  1. static typing => config flags don’t narrow self._cache’s runtime type, so this avoids attribute-type issues;
  2. robustness => if self._cache is ever a StaticCache while config.is_encoder_decoder is True (stale/custom cache state), the old ternary can hit .self_attention_cache on the wrong type. The isinstance version is defensive and keeps behavior the same in expected cases.

Comment thread src/transformers/generation/utils.py Outdated
Comment thread src/transformers/generation/utils.py Outdated
Comment thread src/transformers/generation/utils.py Outdated
Comment thread src/transformers/generation/utils.py Outdated
Comment thread src/transformers/generation/_typing.py Outdated
Comment thread src/transformers/generation/_typing.py Outdated
Comment thread src/transformers/generation/utils.py Outdated
@tarekziade tarekziade force-pushed the tarekziade-typing-generation branch from 388547f to b3e6799 Compare March 2, 2026 12:44
@tarekziade
Copy link
Copy Markdown
Collaborator Author

run-slow: clvp, musicgen, musicgen_melody

@tarekziade tarekziade changed the title chore(qa): Add type checking to src/transformers/generation chore(typing): Add type checking to src/transformers/generation Mar 2, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 2, 2026

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/clvp", "models/musicgen", "models/musicgen_melody"]
quantizations: []

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 2, 2026

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 2679192e workflow commit (merge commit)
PR 51ab3d06 branch commit (from PR)
main 744639ab base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@tarekziade
Copy link
Copy Markdown
Collaborator Author

run-slow: clvp, musicgen, musicgen_melody

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/clvp", "models/musicgen", "models/musicgen_melody"]
quantizations: []

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN b35c6e35 workflow commit (merge commit)
PR c10883fe branch commit (from PR)
main 28d02a31 base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

Copy link
Copy Markdown
Member

@Cyrilvallez Cyrilvallez left a comment

Choose a reason for hiding this comment

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

Nice! Would love to be able to remove the conditional inheritance though, I believe we cam by annotating a few more self?

Comment thread src/transformers/generation/utils.py Outdated
Comment thread src/transformers/generation/utils.py
Comment thread src/transformers/generation/utils.py Outdated
@tarekziade tarekziade force-pushed the tarekziade-typing-generation branch from 9c65644 to db703e7 Compare March 4, 2026 13:21
Copy link
Copy Markdown
Member

@Cyrilvallez Cyrilvallez left a comment

Choose a reason for hiding this comment

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

Alright, thanks for updating! Let's just avoid redefining a new GenerativePreTrainedModel class in generation/utils.py, everything could and should be inside _typing.py! Feel free to merge afterwards!

Comment thread src/transformers/generation/utils.py Outdated
Add type declarations for mixin host-class attributes on GenerationMixin,
class-level annotations for dynamically-set attributes on GenerationConfig,
and fix minor typing issues in candidate_generator, watermarking, and
stopping_criteria. Create _typing.py Protocol for documentation/reuse.
…with if guards

- Replace assert narrowing with if guards (candidate_generator.py)
- Add if-guard for model_kwargs None in _prepare_model_inputs and
  _maybe_initialize_input_ids_for_generation
- Add if-guard for encoder_input_ids None checks
- Fix save_directory.split() for PathLike by wrapping with str()
- Fix tuple[Tensor] reassignment in compute_transition_scores
- Add type annotation for self.model in WhisperTimeStampLogitsProcessor
- Add type annotation for self.sequence_bias in SequenceBiasLogitsProcessor
- Add no-assert rule to AGENTS.md typing strategy
@tarekziade tarekziade force-pushed the tarekziade-typing-generation branch from e21d1d5 to ba7ae36 Compare March 4, 2026 15:27
@tarekziade
Copy link
Copy Markdown
Collaborator Author

run-slow: clvp, musicgen, musicgen_melody

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 4, 2026

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/clvp", "models/musicgen", "models/musicgen_melody"]
quantizations: []

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 4, 2026

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 3c3727ce workflow commit (merge commit)
PR 881e25d7 branch commit (from PR)
main fd6bc380 base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 4, 2026

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

run-slow: clvp, musicgen, musicgen_melody

@tarekziade tarekziade enabled auto-merge (squash) March 4, 2026 16:47
@Cyrilvallez Cyrilvallez disabled auto-merge March 4, 2026 17:24
@Cyrilvallez Cyrilvallez merged commit a2da4b1 into main Mar 4, 2026
23 of 27 checks passed
@Cyrilvallez Cyrilvallez deleted the tarekziade-typing-generation branch March 4, 2026 17:24
BobYue-01 added a commit to TinyLLaVA/TinyLLaVA_Factory that referenced this pull request Apr 16, 2026
Improve LLM factory typing so generated model classes expose generation
APIs for static analysis.

  - Add GenerationMixin import in the LLM registry module.
  - Introduce PreTrainedModelWithGenerationMixin as a typed base.
  - Update ModelAndTokenizer to return this mixed model type.

Note:
  - Recent upstream PR introduced GenerativePreTrainedModel Protocol:
    huggingface/transformers#44233
  - This appears to trigger analyzer warnings from protocol checks.
  - The warning behavior is suspected upstream typing friction and is
    temporarily ignored.
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.

5 participants