[Fast Processor] BEiT
Author: ariG23498
Pull request: yes

Linked: https://github.com/huggingface/transformers/issues/36978

Adding fast processor for BEiT.


Here is how I tested for image classification:
```py
from transformers import (
    BeitImageProcessor, BeitImageProcessorFast, BeitForImageClassification
)
import torch
from datasets import load_dataset

dataset = load_dataset("huggingface/cats-image", trust_remote_code=True)
image = dataset["test"]["image"][0]

image_processor = BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224")
image_processor_fast = BeitImageProcessorFast.from_pretrained("microsoft/beit-base-patch16-224")

model = BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224")

inputs = image_processor(image, return_tensors="pt")
inputs_fast = image_processor_fast(image, return_tensors="pt")

with torch.no_grad():
    logits = model(**inputs).logits

with torch.no_grad():
    logits_fast = model(**inputs_fast).logits


torch.testing.assert_close(
    actual=inputs_fast["pixel_values"],
    expected=inputs["pixel_values"],
    rtol=1e-2,
    atol=1e-2,
)

torch.testing.assert_close(
    actual=logits,
    expected=logits_fast,
    rtol=1e-2,
    atol=1e-2,
)

# model predicts one of the 1000 ImageNet classes
predicted_label = logits.argmax(-1).item()
predicted_label_fast = logits_fast.argmax(-1).item()

print(model.config.id2label[predicted_label])
print(model.config.id2label[predicted_label_fast])
```

While the `inputs` and `inputs_fast` pass the assertion with `1e-2` the logits do not pass. Can I get some advice on the next steps of debugging?

CC: @yonigozlan 

Comments:
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.

@yonigozlan doing this
```
from transformers import (
    BeitImageProcessor, BeitImageProcessorFast
)
import torch
from datasets import load_dataset

dataset = load_dataset("huggingface/cats-image", trust_remote_code=True)
image = dataset["test"]["image"][0]

image_processor = BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224")
image_processor_fast = BeitImageProcessorFast.from_pretrained("microsoft/beit-base-patch16-224")

inputs = image_processor(image, return_tensors="pt")
inputs_fast = image_processor_fast(image, return_tensors="pt")

print(
    torch.mean(inputs_fast["pixel_values"] - inputs["pixel_values"])
)
```

Results in 
```
tensor(-9.0261e-05)
```

@yonigozlan could I get a quick review on the tests to let me know if I am proceeding in the right direction?

There are some tests breaking, but an overview of the way I am implementing it would be nice to have 🤗 

@ariG23498 Looks great to me! Also looks like the failing tests are unrelated from what I see. Are the beit specific image processing tests passing for you locally?

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_37005). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

I have three failing tests locally

```
FAILED tests/models/beit/test_image_processing_beit.py::BeitImageProcessingTest::test_call_segmentation_maps - ValueError: Could not make a flat list of images from tensor([[0, 0, 0,  ..., 0, 0, 0],
FAILED tests/models/beit/test_image_processing_beit.py::BeitImageProcessingTest::test_can_compile_fast_image_processor - torch._dynamo.exc.BackendCompilerFailed: backend='inductor' raised:
FAILED tests/models/beit/test_image_processing_beit.py::BeitImageProcessingTest::test_removed_deprecated_kwargs - AssertionError: False != True
```

I might need a little help here 😅 

Hi @yonigozlan, apologies for the late commits, with the current changes I have three failing tests all due to PIL segmentation maps issue due to `make_list_of_images` api.

```
FAILED tests/models/beit/test_image_processing_beit.py::BeitImageProcessingTest::test_call_segmentation_maps - AttributeError: 'PngImageFile' object has no attribute 'shape'. Did you mean: 'save'?
FAILED tests/models/beit/test_image_processing_beit.py::BeitImageProcessingTest::test_reduce_labels - AttributeError: 'PngImageFile' object has no attribute 'shape'. Did you mean: 'save'?
FAILED tests/models/beit/test_image_processing_beit.py::BeitImageProcessingTest::test_slow_fast_equivalence - AttributeError: 'PngImageFile' object has no attribute 'shape'. Did you mean: 'save'?
```

Is there a better way to handle this?

@yonigozlan all the tests pass on my end!

@yonigozlan I was able to make the style tests pass, but the current CI issues seem irrelevant to the changes of the PR. Should I make a blank commit to check if they are flaky test failures?

@yonigozlan done!

@yonigozlan @ariG23498 

Looks like in case `do_reduce_labels=True` image processor reduces not only labels, but also the images

```py
import transformers
import torch


processor = transformers.BeitImageProcessorFast(
    do_resize=False, 
    do_center_crop=False, 
    do_rescale=False, 
    do_normalize=False, 
    do_convert_rgb=False,
    do_reduce_labels=True,
)

image = torch.zeros([1, 3, 256, 256])

segmap = torch.zeros([1, 256, 256]) 

batch = processor.preprocess(
    images = image, 
    segmentation_maps = segmap, 
    return_tensors="pt", 
    do_reduce_labels=True,
)
```

pixel_values will be 255, should be 0

UPD: opened #38042 that fixes it