Add dynamic resolution input/interpolate position embedding to deit
Author: p-kris10
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 -->
This PR adds ability to handle dynamic input image resolutions to the DeIT model
Addresses #30579 

## 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?
- [X] 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.
- [X] 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.

@amyeroberts 
<!-- 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: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- 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:
## PR Review 🔍

<table>
<tr>
<tr><td> ⏱️&nbsp;<strong>Estimated&nbsp;effort&nbsp;to&nbsp;review [1-5]</strong></td><td>

4, because the PR introduces significant changes to the model's behavior by adding dynamic resolution support and position embedding interpolation. The changes are spread across multiple methods and files, including both PyTorch and TensorFlow implementations, which increases the complexity of the review.


</td></tr>
<tr><td> 🧪&nbsp;<strong>Relevant tests</strong></td><td>

Yes


</td></tr>
<tr><td rowspan=2> ⚡&nbsp;<strong>Possible issues</strong></td>
<td>

<strong>Floating Point Precision:</strong> The addition of a small number (0.1) to avoid floating point errors during interpolation might not be the best approach. This could potentially lead to inconsistencies or unexpected behavior under different conditions or platforms.</td></tr>
<tr>
<td>

<strong>Hardcoded Values:</strong> The use of hardcoded values (like adding 0.1) in the interpolation logic could lead to less flexibility or errors if the underlying assumptions change.</td></tr>
<tr><td> 🔒&nbsp;<strong>Security concerns</strong></td><td>

No

</td></tr>
</table>


<details><summary> <strong>Code feedback:</strong></summary>

<hr><table><tr><td>relevant file</td><td>src/transformers/models/deit/modeling_deit.py
</td></tr><tr><td>suggestion &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>

<strong>

Consider using a configuration parameter for the small value added to avoid floating point errors instead of hardcoding it. This would allow easier adjustments and maintainability. [important]

</strong>
</td></tr><tr><td>relevant line</td><td><a href='https://github.com/huggingface/transformers/pull/31131/files#diff-24837982afd9a1761cbbce0654ac0a191386c96652f9ea4439361613e2d57e34R101'>h0, w0 = h0 + 0.1, w0 + 0.1</a></td></tr></table><hr>

<table><tr><td>relevant file</td><td>src/transformers/models/deit/modeling_tf_deit.py
</td></tr><tr><td>suggestion &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>

<strong>

Similar to the PyTorch implementation, use a configuration parameter for the small value adjustment in the TensorFlow version to maintain consistency and configurability. [important]

</strong>
</td></tr><tr><td>relevant line</td><td><a href='https://github.com/huggingface/transformers/pull/31131/files#diff-d8a9a4a182509f1903e7dbcd751d605285c8b62d0c8213a1a9ae1ba15e9fcc77R164'>h0, w0 = h0 + 0.1, w0 + 0.1</a></td></tr></table><hr>

<table><tr><td>relevant file</td><td>src/transformers/models/deit/modeling_deit.py
</td></tr><tr><td>suggestion &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>

<strong>

Add error handling for the interpolation process to manage cases where the new dimensions might not be valid or the interpolation fails. [medium]

</strong>
</td></tr><tr><td>relevant line</td><td><a href='https://github.com/huggingface/transformers/pull/31131/files#diff-24837982afd9a1761cbbce0654ac0a191386c96652f9ea4439361613e2d57e34R104'>patch_pos_embed = nn.functional.interpolate(</a></td></tr></table><hr>

<table><tr><td>relevant file</td><td>src/transformers/models/deit/modeling_tf_deit.py
</td></tr><tr><td>suggestion &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>

<strong>

Implement error handling for the TensorFlow interpolation logic to ensure robustness, especially for edge cases where resizing parameters may lead to non-integer values or other anomalies. [medium]

</strong>
</td></tr><tr><td>relevant line</td><td><a href='https://github.com/huggingface/transformers/pull/31131/files#diff-d8a9a4a182509f1903e7dbcd751d605285c8b62d0c8213a1a9ae1ba15e9fcc77R168'>patch_pos_embed = tf.image.resize(patch_pos_embed, size=(int(h0), int(w0)), method="bicubic")</a></td></tr></table><hr>

</details>