Fix Llava Next Check for Visual Encoders Without CLS
Author: alex-jw-brooks
Pull request: yes

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Hello! I am working on a few PRs to enable some upcoming multimodal (granite) models from IBM, which are based on Llava Next.

Currently, the visual encoder in Llava Next is abstracted to allow for alternate visual encoders besides CLIP, but there is some validation [here](https://github.com/huggingface/transformers/blob/main/src/transformers/models/llava_next/modeling_llava_next.py#L672) that causes issues if the choice of visual encoder does not have CLS in its features, e.g., Siglip, because the number of features is off by one, regardless of selection strategy. I.e., if it's full, it's off by one because it's got no CLS, and if it's default, it's off by one because we are slicing one of the non-CLS features when applying the selection strategy.

This PR updates the feature selection strategy logic in llava next such that if there is no CLS token, then the selection strategy is always treated as `full`, since `default` doesn't make sense in this case (no CLS to remove).

I've added some unit tests that run the validation here on mocked outputs from the vision tower with the combinations of feature selection strategies + whether or not there should be a cis, and have also run our upcoming Siglip + llava next model to validate that this works as expected.

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [x] Did you write any new necessary tests?


## 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.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker
- vision models: @amyeroberts, @qubvel
- speech models: @ylacombe, @eustlb
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc @MekkCyber

Documentation: @stevhliu

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->
@amyeroberts, @qubvel

Comments:
I suppose @zucchini-nlp was working on something relevant

Sounds good, thanks! I went ahead and made the corresponding changes for llava next video and the video selection strategy so that the behavior in this PR is consistent

Cool, thanks @zucchini-nlp, that all makes sense and I agree. Do you have any thoughts on how the `default``vision_feature_select_strategy` should be handled for Visual Encoders Without CLS? This PR changes the feature selection to always act as `full` if there seems to be no CLS to prevent the removal of non-CLS features by mistake.

I'm happy to make any changes, update the PR to just remove the validation for image features to decouple it from the model PR you are working on, or close this one if you'd prefer. Thank you for the guidance and awesome work!

@alex-jw-brooks 

> Do you have any thoughts on how the default``vision_feature_select_strategy should be handled for Visual Encoders Without CLS? 

Yes, I agree that in models like SigLIP it makes no sense to remove 1 token but I believe we should leave it to user's responsibility. What we can do is to leave the explicit docstring from this PR, to make it clear how vision selection works

Are you planning to integrate IBM granite in transformers natively? If yes, I think it's better to make the necessary changes while adding the model (if still required). Otherwise if you have a code hosted on the hub which calls `LlavaNext` class, feel free to simply remove the check and refine docstring in this PR :)

Hi @zucchini-nlp, apologies for the delayed response. Yes, I am planning to integrate IBM multimodal granite models into Transformers natively! I had only split this apart because I thought it might be easier to review as a more atomic change 🙂  

The change for multimodal granite models is a pretty simple one - for now, I will close this PR, and will make the docstring update + remove the validation in the upcoming one, which I plan to open in the next day or so! I will tag you on it as well, thanks for the quick feedback!