Skip to content

fix(pipeline): preserve model.generation_config precedence over pipeline defaults (#47752) - #47953

Merged
zucchini-nlp merged 5 commits into
huggingface:mainfrom
nithin42:fix-pipeline-generation-config-precedence
Aug 18, 2026
Merged

zucchini-nlp merged 5 commits into
huggingface:mainfrom
nithin42:fix-pipeline-generation-config-precedence

Conversation

@nithin42

@nithin42 nithin42 commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

CI

Fixes #47752

What does this PR do?

This PR fixes incorrect precedence of generation_config values in Pipeline.__init__ (src/transformers/pipelines/base.py).

Currently, when a user modifies model.generation_config (e.g. setting model.generation_config.max_new_tokens = 500) before creating a pipeline, the pipeline's generation_config fails to reflect these values because default_pipeline_generation_config (which sets defaults like max_new_tokens = 256) was applied as the base config inside _prepare_generation_config() with defaults_only=True. Since default_pipeline_generation_config already set non-None values, defaults_only=True silently ignored the user's model.generation_config settings.

Fix

Instead of starting with the pipeline's default config and applying model.generation_config with defaults_only=True, we now:

  1. Start from a copy.deepcopy(self.model.generation_config), preserving any explicit user settings on the model.
  2. Update unset (None) fields using default_pipeline_generation_config with defaults_only=True.
  3. Apply explicit kwargs passed to pipeline(...) at the highest priority.

This aligns execution with the documented priority order:

  1. User-defined config options in **kwargs
  2. Model's generation config values
  3. Pipeline's default generation config values

Verification

Verified locally against text generation and speech (Whisper) pipelines:

# All checks & tests pass
python -m ruff check src/transformers/pipelines/base.py tests/pipelines/test_pipelines_text_generation.py
git diff --check

Added regression unit test test_pipeline_respects_model_generation_config to tests/pipelines/test_pipelines_text_generation.py.

Who can review?

cc @Rocketknight1 @zucchini-nlp

@zucchini-nlp zucchini-nlp left a comment

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.

oops, forgot to submit comments

Comment thread src/transformers/pipelines/base.py Outdated
Comment on lines +889 to +894
if user_generation_config is not None:
prepared_generation_config, kwargs = self.model._prepare_generation_config(
generation_config=user_generation_config, **kwargs
)
else:
base_config = copy.deepcopy(self.model.generation_config)

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.

we defi need a comment here showing expected prio order:

user_config > pipeline_default > model_config > generation_config_defaults

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added the priority order comment as requested!

Comment thread src/transformers/pipelines/base.py Outdated
Comment on lines +889 to +903
if user_generation_config is not None:
prepared_generation_config, kwargs = self.model._prepare_generation_config(
generation_config=user_generation_config, **kwargs
)
else:
base_config = copy.deepcopy(self.model.generation_config)
if default_pipeline_generation_config is not None:
base_config.update(
**default_pipeline_generation_config.to_dict(),
defaults_only=True,
allow_custom_entries=True,
)
prepared_generation_config, kwargs = self.model._prepare_generation_config(
generation_config=base_config, **kwargs
)

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 think all we need is to merge "user + pipe_default" to make a single config and pass down the line, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated to the unified single-path merge logic as suggested!

Comment on lines +637 to +640
def test_pipeline_respects_model_generation_config(self):
"""Test for #47752: Verify model.generation_config modifications take precedence over pipeline defaults."""
model_id = "hf-internal-testing/tiny-random-gpt2"
model = AutoModelForCausalLM.from_pretrained(model_id)

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.

and for test, lets also check different orders of inputs to make sure the priority-list takes effect

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Expanded the unit test to verify all priority order combinations!

@nithin42
nithin42 requested a review from zucchini-nlp August 17, 2026 13:46

@zucchini-nlp zucchini-nlp left a comment

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.

Niiiice, i think this is the best we can do while keeping BC, though I have to admit the whole generation-config legacy is confusing

@Rocketknight1 for another review

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

@Rocketknight1 Rocketknight1 left a comment

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.

Agree that it's confusing but this PR is definitely an improvement on discarding model.generation_config, so I'm happy to accept!

Comment thread src/transformers/pipelines/base.py Resolved
Co-authored-by: Matt <Rocketknight1@users.noreply.github.com>
@zucchini-nlp
zucchini-nlp enabled auto-merge August 18, 2026 13:54
@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 32035085466:2
Result: success | Jobs: 16 | Tests: 182,023 | Failures: 0 | Duration: 11h 15m

@zucchini-nlp
zucchini-nlp added this pull request to the merge queue Aug 18, 2026
Merged via the queue into huggingface:main with commit bc77726 Aug 18, 2026
114 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants