Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix Idefics3 processor - handle gracefully cases with text and no images #35363

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

mfarre
Copy link

@mfarre mfarre commented Dec 20, 2024

What does this PR do?

Fixing Idefics3 processor to work with batches that do not include images

Who can review?

@andimarafioti

@mfarre mfarre requested a review from andimarafioti December 20, 2024 13:32
@HuggingFaceDocBuilderDev

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.

Copy link
Member

@andimarafioti andimarafioti left a comment

Choose a reason for hiding this comment

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

LGTM!

@andimarafioti
Copy link
Member

Let's wait for @yonigozlan to be sure!

Copy link
Member

@yonigozlan yonigozlan left a comment

Choose a reason for hiding this comment

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

Yes that looks much better! Just suggested a check to avoid hallucinations like this:

messages = [
    {
        "role": "user",
        "content": [
            {"type": "image"},
            {"type": "text", "text": "What do we see in this image?"},
        ]
    }
]
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(text=prompt, return_tensors="pt")
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}


# Generate
generated_ids = model.generate(**inputs, max_new_tokens=50)
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)

print(generated_texts)

['User:What do we see in this image?\nAssistant: The image depicts a scene from a historical or fictional setting, likely from the medieval period, given the attire and architecture. The central focus is on a large, ornate gate, which appears to be the entrance to a castle or a fortified structure.']

also in Idefics3ProcessorTest could we add a test for text only inference and one to check that an error is raised if we have an image in the conversation, but no images are passed to the processor?

src/transformers/models/idefics3/processing_idefics3.py Outdated Show resolved Hide resolved
@mfarre
Copy link
Author

mfarre commented Dec 20, 2024

thanks @andimarafioti
thanks @yonigozlan if you give me your LGTM I will merge the changes: I followed your suggestions adding some tests and adding your code proposal.

Yes that looks much better! Just suggested a check to avoid hallucinations like this:

messages = [
    {
        "role": "user",
        "content": [
            {"type": "image"},
            {"type": "text", "text": "What do we see in this image?"},
        ]
    }
]
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(text=prompt, return_tensors="pt")
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}


# Generate
generated_ids = model.generate(**inputs, max_new_tokens=50)
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)

print(generated_texts)

['User:What do we see in this image?\nAssistant: The image depicts a scene from a historical or fictional setting, likely from the medieval period, given the attire and architecture. The central focus is on a large, ornate gate, which appears to be the entrance to a castle or a fortified structure.']

also in Idefics3ProcessorTest could we add a test for text only inference and one to check that an error is raised if we have an image in the conversation, but no images are passed to the processor?

@yonigozlan
Copy link
Member

Looks great thanks @mfarre for fixing this! LGTM for me, but let's maybe wait for @ArthurZucker 's review before merging :)

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.

4 participants