Dynamic Resolution Input for CLIP
Author: Starlento
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 -->

Fixes # (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
      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).
- [ ] 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 and @younesbelkada
- vision models: @amyeroberts
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @pacman100

Integrations:

- deepspeed: HF Trainer/Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc and @younesbelkada

Documentation: @stevhliu and @MKhalusova

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

 -->


Comments:
For VIT, it supports dynamic resolution: https://discuss.huggingface.co/t/fine-tuning-vit-with-more-patches-higher-resolution/18731.
For Dinov2, it originally support this, so that I copy the code to CLIP model file.
Maybe it should be a config which is setable to user. But I am not familar with transformers Config as I want to add a new member in the class...

For the performance, I just tried the example in hf model-card.
For the original 224 settings, the prob is `tensor([[0.9899, 0.0101]]`.
And for 448 settings, the prob is `tensor([[0.9936, 0.0064]]`.

And I am new to open-source community, so I apologize for not ticking anything above and any unproper behaviors...
Feel free to comment and I am glad to learn.

> Thanks for adding @Starlento!
> 
> Could you protect this behaviour behind an `interpolate_pos_encoding` argument flag, as done in other models e.g. [like here for ViT](https://github.com/huggingface/transformers/blob/b86c54d9ff4504b7287d95a215f8d6fa9388761f/src/transformers/models/vit/modeling_vit.py#L136)?

That's a good idea. But it is somewhat ugly as it is required to involve all the related functions...
I just update the code and two doc strings.

To resolve the code quality tests, run `make fix-copies` and then `make fixup` and push the changes 

> To resolve the code quality tests, run `make fix-copies` and then `make fixup` and push the changes

Thank you for the suggestion. I just run the commands, and I found it is trying to add the interpolate functions to other CLIP (e.g. chinese_clip, x_clip) files. And I checked that the implementation is not complete so I checkout those files.

[check_repository_consistency](https://circleci.com/gh/huggingface/transformers/1000340?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-checks-link&utm_content=summary) - Failed
[check_code_quality](https://circleci.com/gh/huggingface/transformers/1000342?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-checks-link&utm_content=summary) - Success

Should I also solve the check_repository_consistency thing?

> Should I also solve the check_repository_consistency thing?

@Starlento Yep, we want the changes CLIP to applied across to any models copying its architecture. 

> > Should I also solve the check_repository_consistency thing?
> 
> @Starlento Yep, we want the changes CLIP to applied across to any models copying its architecture.

The thing I least wanted to happen still happened... I checked that the fixup and fix-copies only matters the first CI test. And for the failed `test_torch`, I found no reference in the repo and do not think I can have an environment to test...
Could you kindly provide a guide for certain situation?


@Starlento Don't worry! The reason is that not all of CLIPs architecture will have been copied across for all models. Looking at the diff, you'll need to copy directly the `interpolate_pos_encoding` function + any additional argument passing to `modeling_clipseg.py` 

> @Starlento Don't worry! The reason is that not all of CLIPs architecture will have been copied across for all models. Looking at the diff, you'll need to copy directly the `interpolate_pos_encoding` function + any additional argument passing to `modeling_clipseg.py`

Yes, you are right. Actually there was some network issue that I cannot access the CI log.
I found for clipseg, it already has a similar function `interpolate_position_embeddings`, so I keep the original one.

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

Hi @Starlento, any update on this PR? It would be great to add this to the library!

> Hi @Starlento, any update on this PR? It would be great to add this to the library!

Hi, sorry that I am quite busy recently. I would like to check the things to do. Could you kindly check whether my understanding is correct?
Let's take altclip as example,
**1. Docstrings for all models will need to be updated**
Does this mean I need to update `ALTCLIP_TEXT_INPUTS_DOCSTRING` in `modeling_altclip.py`? Is there any other files related to docstrings to be modified?
**2. A test should be added for all models to make sure they can accept images of different sizes when interpolate_pos_encoding=True**
Does this mean I need to modify `test_modeling_altclip.py` while take `test_modeling_vit.py` as reference?
**3. Once the tests are added, then the final step is to run the slow tests for all of these models to make sure their default behaviour is the same**
For running the tests, does that mean I need to execute the @slow tests locally on my machine which means download all pretrained models?

@Starlento Re your questions:

> 1. Docstrings for all models will need to be updated
Does this mean I need to update ALTCLIP_TEXT_INPUTS_DOCSTRING in modeling_altclip.py? Is there any other files related to docstrings to be modified?

It means that for all models which have `interpolate_pos_encoding` added as an argument, the docstrings for the methods will need to be updated to include this argument. In the case of altclip, this would be updating `ALTCLIP_VISION_INPUTS_DOCSTRING` and `ALTCLIP_INPUTS_DOCSTRING` (which I can see you've doing in the PR). You don't need to update `ALTCLIP_TEXT_INPUTS_DOCSTRING` and there aren't any other places relating to the model you;d need to update either. This should be done for all the affected models, not just altclip.  

> 2. A test should be added for all models to make sure they can accept images of different sizes when interpolate_pos_encoding=True
Does this mean I need to modify test_modeling_altclip.py while take test_modeling_vit.py as reference?

Yes, using `test_inference_interpolate_pos_encoding` as a reference specifically 

> 3. Once the tests are added, then the final step is to run the slow tests for all of these models to make sure their default behaviour is the same
For running the tests, does that mean I need to execute the @slow tests locally on my machine which means download all pretrained models?

Yes, it means running the tests marked with `@slow` locally. This will download all the checkpoints specified in the integration tests. 

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.