Skip to content

Conversation

@yonigozlan
Copy link
Member

@yonigozlan yonigozlan commented Apr 4, 2025

Simplify the handling of images in both processing and modeling.

Now the images/patches are flattened before being processed and passed to the models. This means that the image processing is simplified (no need for padding in the number of images/patches dimension), along with the modeling code ( No more padding images/patches containing only 0/False needing to be removed).

I tested thoroughly for each models with multiple images, batched images etc. and found no differences.

Cc @andimarafioti @orrzohar

@github-actions github-actions bot marked this pull request as draft April 4, 2025 17:19
@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2025

Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the Ready for review button (at the bottom of the PR page). This will assign reviewers and trigger CI.

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

@yonigozlan yonigozlan marked this pull request as ready for review April 4, 2025 18:09
@github-actions github-actions bot requested review from ArthurZucker and qubvel April 4, 2025 18:09
@yonigozlan yonigozlan requested a review from zucchini-nlp April 7, 2025 23:54
@yonigozlan
Copy link
Member Author

yonigozlan commented Apr 7, 2025

@zucchini-nlp Hello! Pinging you here as smolvlm also handles video inputs, and I'm wondering what you think about having flattened pixel_values by default when processing videos, instead of grouping them by frames or video instance. Also since most image (and maybe video?) processors for vlm using some kind of patching/splitting flatten the patches when preprocessing, we might want to update the base processing tests to account for that? Or at least make them parameterized

Copy link
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.

Cool, thanks for cleaning it! I have a few concerns though.

  1. After this PR idefics models will output pixels where first dim is not necessarily the batch size, whenever an image splitting happens. We had problems in the past with Gemma3 (huggingface/trl#3121 (comment)) and Qwen2-VL (#33666) for the same reason. Tl;DR; train loaders/frameworks iterate over data assuming the first dim is batch and fail when it is not.
    I realize this is not a common case, but we might be breaking train for some users with this. So I'm a bit hesitant to return flat images. LMK what you think about it
  2. Do the model logits stay same if we test with several batches and several images per batch? Let's run slow tests before merging :)

elif inputs_embeds is not None:
batch_size, seq_length, _ = inputs_embeds.shape
else:
if input_ids is None and inputs_embeds is None:
Copy link
Member

Choose a reason for hiding this comment

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

need to also check cases when both are not None:

if (input_ids is None) ^ (inputs_embeds is not None):

elif inputs_embeds is not None:
batch_size, seq_length, _ = inputs_embeds.shape
else:
if input_ids is None and inputs_embeds is None:
Copy link
Member

Choose a reason for hiding this comment

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

same here

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

thanks for working on this!

input_data_format = infer_channel_dimension_format(images_list[0][0])
input_data_format = infer_channel_dimension_format(images[0])

if do_image_splitting:
Copy link
Collaborator

Choose a reason for hiding this comment

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

why don't we try to do a single for loop while we are at it?

@pcuenca
Copy link
Member

pcuenca commented Jun 24, 2025

Just for info, there's a bug in video processing with smolvlm2 where the list of frames is malformed after calling processor.apply_chat_template, which makes generation not work properly. This started happening in transformers 4.52.1 (or maybe 4.52.0, which was yanked). This PR works fine.

I saw it reported here, but it's a transformers issue, not MLX's.

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