Skip to content

Fix: Add correct return behaviour when output_hidden_states=True for CLIP and SIGLIP vision models#44952

Open
Jess-Co-Del wants to merge 2 commits intohuggingface:mainfrom
Jess-Co-Del:fix/hidden_states_output_clip_siglip
Open

Fix: Add correct return behaviour when output_hidden_states=True for CLIP and SIGLIP vision models#44952
Jess-Co-Del wants to merge 2 commits intohuggingface:mainfrom
Jess-Co-Del:fix/hidden_states_output_clip_siglip

Conversation

@Jess-Co-Del
Copy link

@Jess-Co-Del Jess-Co-Del commented Mar 23, 2026

What does this PR do?

Fixes the non existence of output dictionary change, when parameter output_hidden_states=True is passed to models like CLIP or SigLip. This is especially pertinent for the vision model config.

According to #42759 not sure what the behaviour should be for text model, it seems that the SiglipModel class does not successfully cascade this config to the vision and text model.

Reproduction Code

import torch
from PIL import Image
import requests
from transformers import AutoProcessor, AutoModel, AutoConfig

model_id= "google/medsiglip-448"

model = AutoModel.from_pretrained(
    model_id, device_map=torch.device('cuda'),
    output_hidden_states=True, output_attentions=True)

processor = AutoProcessor.from_pretrained(model_id)

images = [Image.open(requests.get("http://images.cocodataset.org/val2017/000000039769.jpg", stream=True).raw)]
texts = ["a photo of 2 cats"]
inputs = processor(text=texts, images=images, padding="max_length", return_tensors="pt")

with torch.no_grad():
    outputs = model.get_image_features(**inputs)

print(len(outputs.hidden_states))  # 28 for MedSigLip

Fixes #42759 #43618

System Info

transformers 5.3.0
python 3.10.6

Code Agent Policy

The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. We are currently bottlenecked by our ability to review and respond to them. As a result,
we ask that new users do not submit pure code agent PRs at this time.
You may use code agents in drafting or to help you diagnose issues. We'd also ask autonomous "OpenClaw"-like agents
not to open any PRs or issues for the moment.

PRs that appear to be fully agent-written will probably be closed without review, and we may block users who do this
repeatedly or maliciously.

This is a rapidly-evolving situation that's causing significant shockwaves in the open-source community. As a result,
this policy is likely to be updated regularly in the near future. For more information, please read CONTRIBUTING.md.

  • I confirm that this is not a pure code agent PR.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).

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.

@yonigozlan @zucchini-nlp

@github-actions
Copy link
Contributor

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

run-slow: clip, siglip

@zucchini-nlp
Copy link
Member

I think it is quite the same as #44431, no?

@Jess-Co-Del
Copy link
Author

Ah nice, it is corrected in the mentioned PR. Works when using:

    outputs = model.get_image_features(**inputs, output_hidden_states=True)
    len(outputs.hidden_states)

or

    outputs = model(**inputs, output_hidden_states=True)
    len(outputs.vision_model_output.hidden_states)

So, the parameter output_hidden_states=True in AutoModel.from_pretrained() becomes deprecated correct?

Thank you 😊

@zucchini-nlp
Copy link
Member

So, the parameter output_hidden_states=True in AutoModel.from_pretrained() becomes deprecated correct?

Not really. The issue you are seeing seems to be more of a general behavior for multimodal models since they have at least two configs embedded inside a bugger config. So you are not changing config.text_config.output_hidden_states and only config.output_hidden_states. There is currently no support for that and we expect each config to hold its own attributes for common fields

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.

output_hidden_states is not working correctly with SiglipModel class

2 participants