Skip to content

Separate check_model_inputs into capture_outputs and merge_with_config_defaults + ensure correctness#43862

Merged
Cyrilvallez merged 20 commits intomainfrom
cleaner-decorators
Feb 10, 2026
Merged

Separate check_model_inputs into capture_outputs and merge_with_config_defaults + ensure correctness#43862
Cyrilvallez merged 20 commits intomainfrom
cleaner-decorators

Conversation

@Cyrilvallez
Copy link
Copy Markdown
Member

@Cyrilvallez Cyrilvallez commented Feb 9, 2026

What does this PR do?

As per the title.
check_model_inputs is becoming very complex and doing more than what it should. Let's separate into 2 clear decorators:

  • capture_outputs: everything related to capture outputs
  • merge_with_config_defaults: everything related to handling of some kwargs

Also, add a test to ensure that capture_outputs is not called several time in a chain wrongly for the same output, and fix affected models.
This is very important for correctness and needs a proper test to ensure it.

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

@Cyrilvallez Cyrilvallez changed the title Separate check_model_inputs into capture_outputs and merge_with_config_defaults Separate check_model_inputs into capture_outputs and merge_with_config_defaults + ensure correctness Feb 10, 2026
@github-actions
Copy link
Copy Markdown
Contributor

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

run-slow: afmoe, aimv2, albert, apertus, arcee, aria, audio_spectrogram_transformer, audioflamingo3, bert, bert_generation, bitnet, blip, blip_2, blt, camembert, chameleon

Copy link
Copy Markdown
Contributor

@vasqu vasqu left a comment

Choose a reason for hiding this comment

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

LGTM overall, does the chain order have any influence?

Comment on lines +238 to +242
# The sam model variants need this annoying exception as well...
if "mask_decoder_attentions" in capturable_flags:
recordable_keys["output_mask_decoder_attentions"] = kwargs.get(
"output_attentions", getattr(self.config, "output_attentions", False)
)
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.

Not super relevant to this PR as it goes a bit beyond the intended scope here but I've seen a few more exceptions I think with image_hidden_states etc. Could be worthwhile to checkout at some point

Copy link
Copy Markdown
Member Author

@Cyrilvallez Cyrilvallez Feb 10, 2026

Choose a reason for hiding this comment

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

If a Model uses a submodel that captures "hidden_states", and then later wants to rename it by doing return ModelOutput(image_hidden_states=vision_model_output.hidden_states,...) it's not a problem.

This issue is when "mask_decoder_attentions" is the key provided in _can_record_outputs directly. I cannot find any occurence of "image_hidden_states" as a direct key in any _can_record_outputs so should be fine here - and there was not any exception for this case either before

Comment on lines -920 to -925
args_with_config_defaults = [
"use_cache",
"vision_feature_layer",
"vision_feature_select_strategy",
"vision_aspect_ratio",
]
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 now merge_with_config_defaults? Do we always apply both decorators now? E.g. llama needs it for use_cache no?

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.

Nvm, yes yes we do

@Cyrilvallez
Copy link
Copy Markdown
Member Author

Cyrilvallez commented Feb 10, 2026

No, none at all! (except obviously how each model is handling the outputs its submodels may capture)

@Cyrilvallez Cyrilvallez merged commit 42791a3 into main Feb 10, 2026
26 checks passed
@Cyrilvallez Cyrilvallez deleted the cleaner-decorators branch February 10, 2026 17:37
jiosephlee pushed a commit to jiosephlee/transformers_latest that referenced this pull request Feb 11, 2026
…config_defaults` + ensure correctness (huggingface#43862)

* start

* switch imports everywhere

* simplify

* fix

* latest model

* add test

* improve test

* fix

* fix

* fix

* oupsi

* doc

* better test

* fix a few

* fix

* more elegant fix

* finalize fixing output capturing layers

* fix all finally

* and the last nit

* damn the test value was plainly wrong
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