






'''simple docstring'''

import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch
   from torch import nn

   from transformers import (
       MODEL_MAPPING,
       BeitForImageClassification,
       BeitForMaskedImageModeling,
       BeitForSemanticSegmentation,
       BeitModel,
   )
   from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   import PIL
   from PIL import Image

   from transformers import BeitImageProcessor







class 						SCREAMING_SNAKE_CASE						:




       def __init__(       self   :			int						,     A__   :			Any						,     A__   :			Tuple=100						,     A__   :			Union[str, Any]=13						,     A__   :			Any=30						,     A__   :			Any=2						,     A__   :			Optional[Any]=3						,     A__   :			Optional[int]=True						,     A__   :			Any=True						,     A__   :			List[str]=32						,     A__   :			Tuple=4						,     A__   :			Optional[int]=4						,     A__   :			List[str]=37						,     A__   :			Union[str, Any]="gelu"						,     A__   :			Optional[Any]=0.1						,     A__   :			Any=0.1						,     A__   :			List[Any]=10						,     A__   :			Optional[int]=0.02						,     A__   :			Optional[Any]=3						,     A__   :			Union[str, Any]=None						,     A__   :			Tuple=[0, 1, 2, 3]						,     ):


            """simple docstring"""







            __lowerCamelCase    :							Any					 =					parent
            __lowerCamelCase    :							Any					 =					100
            __lowerCamelCase    :							Tuple					 =					batch_size
            __lowerCamelCase    :							Dict					 =					image_size
            __lowerCamelCase    :							List[Any]					 =					patch_size
            __lowerCamelCase    :							Optional[Any]					 =					num_channels
            __lowerCamelCase    :							Optional[int]					 =					is_training
            __lowerCamelCase    :							Dict					 =					use_labels
            __lowerCamelCase    :							Union[str, Any]					 =					hidden_size
            __lowerCamelCase    :							str					 =					num_hidden_layers
            __lowerCamelCase    :							Tuple					 =					num_attention_heads
            __lowerCamelCase    :							Optional[Any]					 =					intermediate_size
            __lowerCamelCase    :							List[str]					 =					hidden_act
            __lowerCamelCase    :							str					 =					hidden_dropout_prob
            __lowerCamelCase    :							List[Any]					 =					attention_probs_dropout_prob
            __lowerCamelCase    :							Optional[int]					 =					type_sequence_label_size
            __lowerCamelCase    :							Optional[Any]					 =					initializer_range
            __lowerCamelCase    :							List[Any]					 =					scope
            __lowerCamelCase    :							Tuple					 =					out_indices
            __lowerCamelCase    :							Optional[int]					 =					num_labels

            # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
            __lowerCamelCase    :							Dict					 =					(image_size // patch_size) ** 2
            __lowerCamelCase    :							Tuple					 =					num_patches + 1




       def        a_       (       self   :			Optional[Any]       ):


            """simple docstring"""







            __lowerCamelCase    :							Any					 =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

            __lowerCamelCase    :							Tuple					 =					None
            __lowerCamelCase    :							Dict					 =					None
            if self.use_labels:
                 __lowerCamelCase    :							Dict					 =					ids_tensor([self.batch_size]						,     self.type_sequence_label_size       )
                 __lowerCamelCase    :							List[str]					 =					ids_tensor([self.batch_size, self.image_size, self.image_size]						,     self.num_labels       )

            __lowerCamelCase    :							List[Any]					 =					self.get_config()

            return config, pixel_values, labels, pixel_labels




       def        a_       (       self   :			Optional[Any]       ):


            """simple docstring"""







            return BeitConfig(
                vocab_size=self.vocab_size						,     image_size=self.image_size						,     patch_size=self.patch_size						,     num_channels=self.num_channels						,     hidden_size=self.hidden_size						,     num_hidden_layers=self.num_hidden_layers						,     num_attention_heads=self.num_attention_heads						,     intermediate_size=self.intermediate_size						,     hidden_act=self.hidden_act						,     hidden_dropout_prob=self.hidden_dropout_prob						,     attention_probs_dropout_prob=self.attention_probs_dropout_prob						,     is_decoder=SCREAMING_SNAKE_CASE__						,     initializer_range=self.initializer_range						,     out_indices=self.out_indices						,     )




       def        a_       (       self   :			List[Any]						,     A__   :			Union[str, Any]						,     A__   :			Optional[int]						,     A__   :			List[str]						,     A__   :			Tuple       ):


            """simple docstring"""







            __lowerCamelCase    :							Dict					 =					BeitModel(config=SCREAMING_SNAKE_CASE__       )
            model.to(SCREAMING_SNAKE_CASE__       )
            model.eval()
            __lowerCamelCase    :							str					 =					model(SCREAMING_SNAKE_CASE__       )
            self.parent.assertEqual(result.last_hidden_state.shape						,     (self.batch_size, self.seq_length, self.hidden_size)       )




       def        a_       (       self   :			Optional[int]						,     A__   :			int						,     A__   :			Dict						,     A__   :			Optional[int]						,     A__   :			Union[str, Any]       ):


            """simple docstring"""







            __lowerCamelCase    :							List[str]					 =					BeitForMaskedImageModeling(config=SCREAMING_SNAKE_CASE__       )
            model.to(SCREAMING_SNAKE_CASE__       )
            model.eval()
            __lowerCamelCase    :							int					 =					model(SCREAMING_SNAKE_CASE__       )
            self.parent.assertEqual(result.logits.shape						,     (self.batch_size, self.seq_length - 1, self.vocab_size)       )




       def        a_       (       self   :			Optional[int]						,     A__   :			Optional[Any]						,     A__   :			Dict						,     A__   :			int						,     A__   :			str       ):


            """simple docstring"""







            __lowerCamelCase    :							Tuple					 =					self.type_sequence_label_size
            __lowerCamelCase    :							List[str]					 =					BeitForImageClassification(SCREAMING_SNAKE_CASE__       )
            model.to(SCREAMING_SNAKE_CASE__       )
            model.eval()
            __lowerCamelCase    :							Any					 =					model(SCREAMING_SNAKE_CASE__						,     labels=SCREAMING_SNAKE_CASE__       )
            self.parent.assertEqual(result.logits.shape						,     (self.batch_size, self.type_sequence_label_size)       )

            # test greyscale images
            __lowerCamelCase    :							Dict					 =					1
            __lowerCamelCase    :							Optional[Any]					 =					BeitForImageClassification(SCREAMING_SNAKE_CASE__       )
            model.to(SCREAMING_SNAKE_CASE__       )
            model.eval()

            __lowerCamelCase    :							str					 =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
            __lowerCamelCase    :							List[Any]					 =					model(SCREAMING_SNAKE_CASE__						,     labels=SCREAMING_SNAKE_CASE__       )
            self.parent.assertEqual(result.logits.shape						,     (self.batch_size, self.type_sequence_label_size)       )




       def        a_       (       self   :			Dict						,     A__   :			Any						,     A__   :			Optional[int]						,     A__   :			List[Any]						,     A__   :			List[Any]       ):


            """simple docstring"""







            __lowerCamelCase    :							Any					 =					self.num_labels
            __lowerCamelCase    :							int					 =					BeitForSemanticSegmentation(SCREAMING_SNAKE_CASE__       )
            model.to(SCREAMING_SNAKE_CASE__       )
            model.eval()
            __lowerCamelCase    :							Dict					 =					model(SCREAMING_SNAKE_CASE__       )
            self.parent.assertEqual(
                result.logits.shape						,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)       )
            __lowerCamelCase    :							Optional[int]					 =					model(SCREAMING_SNAKE_CASE__						,     labels=SCREAMING_SNAKE_CASE__       )
            self.parent.assertEqual(
                result.logits.shape						,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)       )




       def        a_       (       self   :			int       ):


            """simple docstring"""







            __lowerCamelCase    :							int					 =					self.prepare_config_and_inputs()
            __lowerCamelCase    :							List[str]					 =					config_and_inputs
            __lowerCamelCase    :							str					 =					{'''pixel_values''': pixel_values}
            return config, inputs_dict







@require_torch
class 						SCREAMING_SNAKE_CASE						(   lowerCamelCase__     , lowerCamelCase__     , unittest.TestCase	):
       snake_case__     :  List[Any] 		=      (
           (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
           if is_torch_available()
           else ()
       )
       snake_case__     :  Union[str, Any] 		=      (
           {
               'feature-extraction': BeitModel,
               'image-classification': BeitForImageClassification,
               'image-segmentation': BeitForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       snake_case__     :  Optional[Any] 		=      False
       snake_case__     :  int 		=      False
       snake_case__     :  Optional[Any] 		=      False




       def        a_       (       self   :			Any       ):


            """simple docstring"""







            __lowerCamelCase    :							int					 =					BeitModelTester(self       )
            __lowerCamelCase    :							Tuple					 =					ConfigTester(self						,     config_class=SCREAMING_SNAKE_CASE__						,     has_text_modality=SCREAMING_SNAKE_CASE__						,     hidden_size=37       )




       def        a_       (       self   :			Optional[Any]       ):


            """simple docstring"""







            self.config_tester.run_common_tests()




       @unittest.skip(reason="""BEiT does not use inputs_embeds"""       )
       def        a_       (       self   :			Dict       ):


            """simple docstring"""







            pass




       @require_torch_multi_gpu
       @unittest.skip(reason="""BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`"""       )
       def        a_       (       self   :			Union[str, Any]       ):


            """simple docstring"""







            pass




       def        a_       (       self   :			Optional[Any]       ):


            """simple docstring"""







            __lowerCamelCase    :							List[str]					 =					self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 __lowerCamelCase    :							Any					 =					model_class(SCREAMING_SNAKE_CASE__       )
                 self.assertIsInstance(model.get_input_embeddings()						,     (nn.Module)       )
                 __lowerCamelCase    :							Optional[int]					 =					model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE__						,     nn.Linear       )       )




       def        a_       (       self   :			List[Any]       ):


            """simple docstring"""







            __lowerCamelCase    :							str					 =					self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 __lowerCamelCase    :							Tuple					 =					model_class(SCREAMING_SNAKE_CASE__       )
                 __lowerCamelCase    :							str					 =					inspect.signature(model.forward       )
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 __lowerCamelCase    :							Any					 =					[*signature.parameters.keys()]

                 __lowerCamelCase    :							int					 =					['''pixel_values''']
                 self.assertListEqual(arg_names[:1]						,     SCREAMING_SNAKE_CASE__       )




       def        a_       (       self   :			int       ):


            """simple docstring"""







            __lowerCamelCase    :							str					 =					self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__       )




       def        a_       (       self   :			Any       ):


            """simple docstring"""







            __lowerCamelCase    :							Optional[int]					 =					self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE__       )




       def        a_       (       self   :			Optional[Any]       ):


            """simple docstring"""







            __lowerCamelCase    :							Optional[Any]					 =					self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__       )




       def        a_       (       self   :			int       ):


            """simple docstring"""







            __lowerCamelCase    :							Optional[int]					 =					self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE__       )




       def        a_       (       self   :			Any       ):


            """simple docstring"""







            if not self.model_tester.is_training:
                 return

            __lowerCamelCase    :							str					 =					self.model_tester.prepare_config_and_inputs_for_common()
            __lowerCamelCase    :							Union[str, Any]					 =					True

            for model_class in self.all_model_classes:
                 # we don't test BeitForMaskedImageModeling
                 if model_class in [*get_values(SCREAMING_SNAKE_CASE__       ), BeitForMaskedImageModeling]:
                      continue

                 __lowerCamelCase    :							Any					 =					model_class(SCREAMING_SNAKE_CASE__       )
                 model.to(SCREAMING_SNAKE_CASE__       )
                 model.train()
                 __lowerCamelCase    :							Union[str, Any]					 =					self._prepare_for_class(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__						,     return_labels=SCREAMING_SNAKE_CASE__       )
                 __lowerCamelCase    :							Any					 =					model(**SCREAMING_SNAKE_CASE__       ).loss
                 loss.backward()




       def        a_       (       self   :			Any       ):


            """simple docstring"""







            __lowerCamelCase    :							Union[str, Any]					 =					self.model_tester.prepare_config_and_inputs_for_common()
            if not self.model_tester.is_training:
                 return

            __lowerCamelCase    :							Tuple					 =					False
            __lowerCamelCase    :							Optional[int]					 =					True

            for model_class in self.all_model_classes:
                 # we don't test BeitForMaskedImageModeling
                 if (
                     model_class in [*get_values(SCREAMING_SNAKE_CASE__       ), BeitForMaskedImageModeling]
                     or not model_class.supports_gradient_checkpointing
                 ):
                      continue

                 __lowerCamelCase    :							Tuple					 =					model_class(SCREAMING_SNAKE_CASE__       )
                 model.gradient_checkpointing_enable()
                 model.to(SCREAMING_SNAKE_CASE__       )
                 model.train()
                 __lowerCamelCase    :							List[str]					 =					self._prepare_for_class(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__						,     return_labels=SCREAMING_SNAKE_CASE__       )
                 __lowerCamelCase    :							int					 =					model(**SCREAMING_SNAKE_CASE__       ).loss
                 loss.backward()




       def        a_       (       self   :			List[Any]       ):


            """simple docstring"""







            __lowerCamelCase    :							List[str]					 =					self.model_tester.prepare_config_and_inputs_for_common()

            __lowerCamelCase    :							Optional[int]					 =					_config_zero_init(SCREAMING_SNAKE_CASE__       )
            for model_class in self.all_model_classes:
                 __lowerCamelCase    :							List[str]					 =					model_class(config=SCREAMING_SNAKE_CASE__       )
                 for name, param in model.named_parameters():
                      # we skip lambda parameters as these require special initial values
                      # determined by config.layer_scale_init_value
                      if "lambda" in name:
                           continue
                      if param.requires_grad:
                           self.assertIn(
                               ((param.data.mean() * 1e9).round() / 1e9).item()						,     [0.0, 1.0]						,     msg=f"Parameter {name} of model {model_class} seems not properly initialized"						,     )




       @slow
       def        a_       (       self   :			List[str]       ):


            """simple docstring"""







            for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 __lowerCamelCase    :							Optional[Any]					 =					BeitModel.from_pretrained(SCREAMING_SNAKE_CASE__       )
                 self.assertIsNotNone(SCREAMING_SNAKE_CASE__       )

def      __lowercase	() ->  List[Any]:


     """simple docstring"""
     __lowerCamelCase    :							Any					 =					Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
     return image







@require_torch
@require_vision
class 						SCREAMING_SNAKE_CASE						(   unittest.TestCase	):




       @cached_property
       def        a_       (       self   :			str       ):


            """simple docstring"""







            return BeitImageProcessor.from_pretrained("""microsoft/beit-base-patch16-224"""       ) if is_vision_available() else None




       @slow
       def        a_       (       self   :			List[Any]       ):


            """simple docstring"""







            __lowerCamelCase    :							List[str]					 =					BeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k"""       ).to(SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							Any					 =					self.default_image_processor
            __lowerCamelCase    :							Any					 =					prepare_img()
            __lowerCamelCase    :							Optional[int]					 =					image_processor(images=SCREAMING_SNAKE_CASE__						,     return_tensors="""pt"""       ).pixel_values.to(SCREAMING_SNAKE_CASE__       )

            # prepare bool_masked_pos
            __lowerCamelCase    :							Any					 =					torch.ones((1, 196)						,     dtype=torch.bool       ).to(SCREAMING_SNAKE_CASE__       )

            # forward pass
            with torch.no_grad():
                 __lowerCamelCase    :							List[str]					 =					model(pixel_values=SCREAMING_SNAKE_CASE__						,     bool_masked_pos=SCREAMING_SNAKE_CASE__       )
            __lowerCamelCase    :							List[str]					 =					outputs.logits

            # verify the logits
            __lowerCamelCase    :							List[str]					 =					torch.Size((1, 196, 8192)       )
            self.assertEqual(logits.shape						,     SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							List[Any]					 =					torch.tensor(
                [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]       ).to(SCREAMING_SNAKE_CASE__       )

            self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]						,     SCREAMING_SNAKE_CASE__						,     atol=1e-2       )       )




       @slow
       def        a_       (       self   :			List[Any]       ):


            """simple docstring"""







            __lowerCamelCase    :							List[str]					 =					BeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224"""       ).to(SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							int					 =					self.default_image_processor
            __lowerCamelCase    :							List[str]					 =					prepare_img()
            __lowerCamelCase    :							Dict					 =					image_processor(images=SCREAMING_SNAKE_CASE__						,     return_tensors="""pt"""       ).to(SCREAMING_SNAKE_CASE__       )

            # forward pass
            with torch.no_grad():
                 __lowerCamelCase    :							Dict					 =					model(**SCREAMING_SNAKE_CASE__       )
            __lowerCamelCase    :							List[Any]					 =					outputs.logits

            # verify the logits
            __lowerCamelCase    :							Any					 =					torch.Size((1, 1000)       )
            self.assertEqual(logits.shape						,     SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							Tuple					 =					torch.tensor([-1.2385, -1.0987, -1.0108]       ).to(SCREAMING_SNAKE_CASE__       )

            self.assertTrue(torch.allclose(logits[0, :3]						,     SCREAMING_SNAKE_CASE__						,     atol=1e-4       )       )

            __lowerCamelCase    :							Optional[int]					 =					281
            self.assertEqual(logits.argmax(-1       ).item()						,     SCREAMING_SNAKE_CASE__       )




       @slow
       def        a_       (       self   :			int       ):


            """simple docstring"""







            __lowerCamelCase    :							Union[str, Any]					 =					BeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k"""       ).to(
                SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							Union[str, Any]					 =					self.default_image_processor
            __lowerCamelCase    :							List[Any]					 =					prepare_img()
            __lowerCamelCase    :							List[Any]					 =					image_processor(images=SCREAMING_SNAKE_CASE__						,     return_tensors="""pt"""       ).to(SCREAMING_SNAKE_CASE__       )

            # forward pass
            with torch.no_grad():
                 __lowerCamelCase    :							Optional[Any]					 =					model(**SCREAMING_SNAKE_CASE__       )
            __lowerCamelCase    :							int					 =					outputs.logits

            # verify the logits
            __lowerCamelCase    :							Any					 =					torch.Size((1, 21841)       )
            self.assertEqual(logits.shape						,     SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							Optional[int]					 =					torch.tensor([1.6881, -0.2787, 0.5901]       ).to(SCREAMING_SNAKE_CASE__       )

            self.assertTrue(torch.allclose(logits[0, :3]						,     SCREAMING_SNAKE_CASE__						,     atol=1e-4       )       )

            __lowerCamelCase    :							Optional[int]					 =					2396
            self.assertEqual(logits.argmax(-1       ).item()						,     SCREAMING_SNAKE_CASE__       )




       @slow
       def        a_       (       self   :			List[str]       ):


            """simple docstring"""







            __lowerCamelCase    :							Any					 =					BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""       )
            __lowerCamelCase    :							int					 =					model.to(SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							Tuple					 =					BeitImageProcessor(do_resize=SCREAMING_SNAKE_CASE__						,     size=640						,     do_center_crop=SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							Any					 =					load_dataset("""hf-internal-testing/fixtures_ade20k"""						,     split="""test"""       )
            __lowerCamelCase    :							Optional[Any]					 =					Image.open(ds[0]["""file"""]       )
            __lowerCamelCase    :							str					 =					image_processor(images=SCREAMING_SNAKE_CASE__						,     return_tensors="""pt"""       ).to(SCREAMING_SNAKE_CASE__       )

            # forward pass
            with torch.no_grad():
                 __lowerCamelCase    :							Optional[int]					 =					model(**SCREAMING_SNAKE_CASE__       )
            __lowerCamelCase    :							List[Any]					 =					outputs.logits

            # verify the logits
            __lowerCamelCase    :							Optional[int]					 =					torch.Size((1, 150, 160, 160)       )
            self.assertEqual(logits.shape						,     SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							Any					 =					version.parse(PIL.__version__       ) < version.parse("""9.0.0"""       )

            if is_pillow_less_than_a:
                 __lowerCamelCase    :							Any					 =					torch.tensor(
                     [
                         [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]],
                         [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]],
                         [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]],
                     ]						,     device=SCREAMING_SNAKE_CASE__						,     )
            else:
                 __lowerCamelCase    :							int					 =					torch.tensor(
                     [
                         [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]],
                         [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]],
                         [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]],
                     ]						,     device=SCREAMING_SNAKE_CASE__						,     )

            self.assertTrue(torch.allclose(logits[0, :3, :3, :3]						,     SCREAMING_SNAKE_CASE__						,     atol=1e-4       )       )




       @slow
       def        a_       (       self   :			Optional[int]       ):


            """simple docstring"""







            __lowerCamelCase    :							Any					 =					BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""       )
            __lowerCamelCase    :							Any					 =					model.to(SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							List[str]					 =					BeitImageProcessor(do_resize=SCREAMING_SNAKE_CASE__						,     size=640						,     do_center_crop=SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							int					 =					load_dataset("""hf-internal-testing/fixtures_ade20k"""						,     split="""test"""       )
            __lowerCamelCase    :							Optional[int]					 =					Image.open(ds[0]["""file"""]       )
            __lowerCamelCase    :							Optional[Any]					 =					image_processor(images=SCREAMING_SNAKE_CASE__						,     return_tensors="""pt"""       ).to(SCREAMING_SNAKE_CASE__       )

            # forward pass
            with torch.no_grad():
                 __lowerCamelCase    :							Optional[Any]					 =					model(**SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							Union[str, Any]					 =					outputs.logits.detach().cpu()

            __lowerCamelCase    :							Union[str, Any]					 =					image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE__						,     target_sizes=[(500, 300)]       )
            __lowerCamelCase    :							Optional[int]					 =					torch.Size((500, 300)       )
            self.assertEqual(segmentation[0].shape						,     SCREAMING_SNAKE_CASE__       )

            __lowerCamelCase    :							Dict					 =					image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE__       )
            __lowerCamelCase    :							Tuple					 =					torch.Size((160, 160)       )
            self.assertEqual(segmentation[0].shape						,     SCREAMING_SNAKE_CASE__       )







"""simple docstring"""






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class      A(  lowerCamelCase__		):

		"""simple docstring"""





		A    =							["image_processor", "tokenizer"]
		A    =							"ViTImageProcessor"
		A    =							("CLIPTokenizer", "CLIPTokenizerFast")
		def __init__( self					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=None					,				**SCREAMING_SNAKE_CASE__   )      ->	List[str]:

					"""simple docstring"""



					_UpperCamelCase					:List[str]					     =	None
					if "feature_extractor" in kwargs:
								warnings.warn(
								    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
								    ''' instead.'''					,				SCREAMING_SNAKE_CASE__					,				)
								_UpperCamelCase					:List[str]					     =	kwargs.pop('''feature_extractor'''   )

					_UpperCamelCase					:Optional[Any]					     =	image_processor if image_processor is not None else feature_extractor
					if image_processor is None:
								raise ValueError('''You need to specify an `image_processor`.'''   )
					if tokenizer is None:
								raise ValueError('''You need to specify a `tokenizer`.'''   )

					super().__init__(SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__   )
		def __call__( self					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=None					,				**SCREAMING_SNAKE_CASE__   )      ->	Tuple:

					"""simple docstring"""



					if text is None and visual_prompt is None and images is None:
								raise ValueError('''You have to specify either text, visual prompt or images.'''   )

					if text is not None and visual_prompt is not None:
								raise ValueError('''You have to specify exactly one type of prompt. Either text or visual prompt.'''   )

					if text is not None:
								_UpperCamelCase					:Dict					     =	self.tokenizer(SCREAMING_SNAKE_CASE__					,				return_tensors=SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )

					if visual_prompt is not None:
								_UpperCamelCase					:Optional[Any]					     =	self.image_processor(SCREAMING_SNAKE_CASE__					,				return_tensors=SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )

					if images is not None:
								_UpperCamelCase					:Union[str, Any]					     =	self.image_processor(SCREAMING_SNAKE_CASE__					,				return_tensors=SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )

					if visual_prompt is not None and images is not None:
								_UpperCamelCase					:List[str]					     =	{
								    '''pixel_values''': image_features.pixel_values,
								    '''conditional_pixel_values''': prompt_features.pixel_values,
								}
								return encoding
					elif text is not None and images is not None:
								_UpperCamelCase					:List[Any]					     =	image_features.pixel_values
								return encoding
					elif text is not None:
								return encoding
					elif visual_prompt is not None:
								_UpperCamelCase					:Optional[Any]					     =	{
								    '''conditional_pixel_values''': prompt_features.pixel_values,
								}
								return encoding
					else:
								return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE__   )					,				tensor_type=SCREAMING_SNAKE_CASE__   )
		def   _UpperCamelCase( self					,				*SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )      ->	Any:

					"""simple docstring"""



					return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )
		def   _UpperCamelCase( self					,				*SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )      ->	Tuple:

					"""simple docstring"""



					return self.tokenizer.decode(*SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )
		@property
		def   _UpperCamelCase( self   )      ->	Tuple:

					"""simple docstring"""



					warnings.warn(
					    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''					,				SCREAMING_SNAKE_CASE__					,				)
					return self.image_processor_class





		@property
		def   _UpperCamelCase( self   )      ->	Union[str, Any]:

					"""simple docstring"""



					warnings.warn(
					    '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''					,				SCREAMING_SNAKE_CASE__					,				)
					return self.image_processor

