huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
135.05k stars 27.02k forks source link

Refactor image features selection in LlaVa #33696

Closed kenza-bouzid closed 1 month ago

kenza-bouzid commented 1 month ago

What does this PR do?

Wrap image features selection in LLaVa in a separate function to make it easier to override for custom use cases (e.g. applying a layer norm on the image features before projection, etc).

Fixes #33695

Before submitting

Who can review?

@ArthurZucker, @younesbelkada

kenza-bouzid commented 1 month ago

Hi @zucchini-nlp,

Thanks for your reply.

WDYT about moving the whole image feature preparation to a separate method, including MM-proj. So the method takes in pixels and return features ready to be merged with text embeddings?

Good idea, however I would suggest having a separate method for that as well to make it easy to customize the projection. I can think of many use cases where you may want to change the projection.

We can have get_image_features and project_image_features that are then called sequentially in a get_image_embeddings ready to be merged with text embeddings as you suggested.

If you have bandwidth, would be nice to propagate change to all VLMs. But don't stress if you can't, I'll make an overall standardization soon on that :)

I'm afraid I won't have time to propagate the change to all VLLMs. Is that a blocker for this PR to pass the CI? You probably have more context for an overall standardization!

zucchini-nlp commented 1 month ago

I would suggest having a separate method for that as well to make it easy to customize the projection. I can think of many use cases where you may want to change the projection.

Hmm, imo that is a bit redundant, and since "select+proj" is mostly interlinked and isn't a huge piece of code we may ask users to override that one method, Independently of whether they tweak at selection stage or projection stage. So that we don't end up with separate methods that perform a one-liner, which imo is a bad practice. Lmk if you have any other ideas or objections

I'm afraid I won't have time to propagate the change to all VLLMs. Is that a blocker for this PR to pass the CI? You probably have more context for an overall standardization!

No worries, I just realized it is the very first llava being modified here. So our CI should be happy after running make fix-copies and make style. For other VLMs that are more tricky, I'll make one round of standardization later. Not a blocker at all :)

kenza-bouzid commented 1 month ago

Sounds great! Let me make the changes you suggested. Thank youu!

kenza-bouzid commented 1 month ago

@zucchini-nlp can you please approve/trigger the CI workflows for tests? Thank you!

kenza-bouzid commented 1 month ago

@LysandreJik can you please have a look? Thanks

HuggingFaceDocBuilderDev commented 1 month ago

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.