






'''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			:







		"""simple docstring"""






		def __init__(   self		,						__A		,						__A=100		,						__A=13		,						__A=30		,						__A=2		,						__A=3		,						__A=True		,						__A=True		,						__A=32		,						__A=4		,						__A=4		,						__A=37		,						__A="gelu"		,						__A=0.1		,						__A=0.1		,						__A=10		,						__A=0.02		,						__A=3		,						__A=None		,						__A=[0, 1, 2, 3]		,						) ->     Optional[int]:
								_lowerCAmelCase										=parent
								_lowerCAmelCase										=100
								_lowerCAmelCase										=batch_size
								_lowerCAmelCase										=image_size
								_lowerCAmelCase										=patch_size
								_lowerCAmelCase										=num_channels
								_lowerCAmelCase										=is_training
								_lowerCAmelCase										=use_labels
								_lowerCAmelCase										=hidden_size
								_lowerCAmelCase										=num_hidden_layers
								_lowerCAmelCase										=num_attention_heads
								_lowerCAmelCase										=intermediate_size
								_lowerCAmelCase										=hidden_act
								_lowerCAmelCase										=hidden_dropout_prob
								_lowerCAmelCase										=attention_probs_dropout_prob
								_lowerCAmelCase										=type_sequence_label_size
								_lowerCAmelCase										=initializer_range
								_lowerCAmelCase										=scope
								_lowerCAmelCase										=out_indices
								_lowerCAmelCase										=num_labels

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






		def 		UpperCamelCase__       (   self				) ->     Union[str, Any]:
								_lowerCAmelCase										=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

								_lowerCAmelCase										=None
								_lowerCAmelCase										=None
								if self.use_labels:
														_lowerCAmelCase										=ids_tensor([self.batch_size]		,						self.type_sequence_label_size				)
														_lowerCAmelCase										=ids_tensor([self.batch_size, self.image_size, self.image_size]		,						self.num_labels				)

								_lowerCAmelCase										=self.get_config()

								return config, pixel_values, labels, pixel_labels






		def 		UpperCamelCase__       (   self				) ->     Dict:
								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=a_		,						initializer_range=self.initializer_range		,						out_indices=self.out_indices		,						)






		def 		UpperCamelCase__       (   self		,						__A		,						__A		,						__A		,						__A				) ->     Union[str, Any]:
								_lowerCAmelCase										=BeitModel(config=a_				)
								model.to(a_				)
								model.eval()
								_lowerCAmelCase										=model(a_				)
								self.parent.assertEqual(result.last_hidden_state.shape		,						(self.batch_size, self.seq_length, self.hidden_size)				)






		def 		UpperCamelCase__       (   self		,						__A		,						__A		,						__A		,						__A				) ->     Optional[int]:
								_lowerCAmelCase										=BeitForMaskedImageModeling(config=a_				)
								model.to(a_				)
								model.eval()
								_lowerCAmelCase										=model(a_				)
								self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.seq_length - 1, self.vocab_size)				)






		def 		UpperCamelCase__       (   self		,						__A		,						__A		,						__A		,						__A				) ->     Tuple:
								_lowerCAmelCase										=self.type_sequence_label_size
								_lowerCAmelCase										=BeitForImageClassification(a_				)
								model.to(a_				)
								model.eval()
								_lowerCAmelCase										=model(a_		,						labels=a_				)
								self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.type_sequence_label_size)				)

								# test greyscale images
								_lowerCAmelCase										=1
								_lowerCAmelCase										=BeitForImageClassification(a_				)
								model.to(a_				)
								model.eval()

								_lowerCAmelCase										=floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
								_lowerCAmelCase										=model(a_		,						labels=a_				)
								self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.type_sequence_label_size)				)






		def 		UpperCamelCase__       (   self		,						__A		,						__A		,						__A		,						__A				) ->     List[str]:
								_lowerCAmelCase										=self.num_labels
								_lowerCAmelCase										=BeitForSemanticSegmentation(a_				)
								model.to(a_				)
								model.eval()
								_lowerCAmelCase										=model(a_				)
								self.parent.assertEqual(
								    result.logits.shape		,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)				)
								_lowerCAmelCase										=model(a_		,						labels=a_				)
								self.parent.assertEqual(
								    result.logits.shape		,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)				)






		def 		UpperCamelCase__       (   self				) ->     str:
								_lowerCAmelCase										=self.prepare_config_and_inputs()
								_lowerCAmelCase										=config_and_inputs
								_lowerCAmelCase										={"""pixel_values""": pixel_values}
								return config, inputs_dict

@require_torch
class   SCREAMING_SNAKE_CASE			(						__a    ,			__a    ,			unittest.TestCase):







		"""simple docstring"""
		lowercase   :      Optional[int]       =							(
		    (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
		    if is_torch_available()
		    else ()
		)
		lowercase   :      Optional[int]       =							(
		    {
		        'feature-extraction': BeitModel,
		        'image-classification': BeitForImageClassification,
		        'image-segmentation': BeitForSemanticSegmentation,
		    }
		    if is_torch_available()
		    else {}
		)

		lowercase   :      Optional[int]       =							False
		lowercase   :      int       =							False
		lowercase   :      Dict       =							False






		def 		UpperCamelCase__       (   self				) ->     str:
								_lowerCAmelCase										=BeitModelTester(self				)
								_lowerCAmelCase										=ConfigTester(self		,						config_class=a_		,						has_text_modality=a_		,						hidden_size=37				)






		def 		UpperCamelCase__       (   self				) ->     Any:
								self.config_tester.run_common_tests()






		@unittest.skip(reason='BEiT does not use inputs_embeds'				)
		def 		UpperCamelCase__       (   self				) ->     str:
								pass






		@require_torch_multi_gpu
		@unittest.skip(reason='BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'				)
		def 		UpperCamelCase__       (   self				) ->     List[str]:
								pass






		def 		UpperCamelCase__       (   self				) ->     Tuple:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
														_lowerCAmelCase										=model_class(a_				)
														self.assertIsInstance(model.get_input_embeddings()		,						(nn.Module)				)
														_lowerCAmelCase										=model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(a_		,						nn.Linear				)				)






		def 		UpperCamelCase__       (   self				) ->     str:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs_for_common()

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

														_lowerCAmelCase										=["""pixel_values"""]
														self.assertListEqual(arg_names[:1]		,						a_				)






		def 		UpperCamelCase__       (   self				) ->     Optional[int]:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*a_				)






		def 		UpperCamelCase__       (   self				) ->     List[Any]:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*a_				)






		def 		UpperCamelCase__       (   self				) ->     List[Any]:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*a_				)






		def 		UpperCamelCase__       (   self				) ->     Any:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*a_				)






		def 		UpperCamelCase__       (   self				) ->     int:



								if not self.model_tester.is_training:
														return

								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs_for_common()
								_lowerCAmelCase										=True

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

														_lowerCAmelCase										=model_class(a_				)
														model.to(a_				)
														model.train()
														_lowerCAmelCase										=self._prepare_for_class(a_		,						a_		,						return_labels=a_				)
														_lowerCAmelCase										=model(**a_				).loss
														loss.backward()






		def 		UpperCamelCase__       (   self				) ->     int:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs_for_common()
								if not self.model_tester.is_training:
														return

								_lowerCAmelCase										=False
								_lowerCAmelCase										=True

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

														_lowerCAmelCase										=model_class(a_				)
														model.gradient_checkpointing_enable()
														model.to(a_				)
														model.train()
														_lowerCAmelCase										=self._prepare_for_class(a_		,						a_		,						return_labels=a_				)
														_lowerCAmelCase										=model(**a_				).loss
														loss.backward()






		def 		UpperCamelCase__       (   self				) ->     Optional[int]:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs_for_common()

								_lowerCAmelCase										=_config_zero_init(a_				)
								for model_class in self.all_model_classes:
														_lowerCAmelCase										=model_class(config=a_				)
														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 		UpperCamelCase__       (   self				) ->     str:



								for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_lowerCAmelCase										=BeitModel.from_pretrained(a_				)
														self.assertIsNotNone(a_				)



def 							UpperCamelCase__			(			):


						'''simple docstring'''






						_lowerCAmelCase										=Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
						return image


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







		"""simple docstring"""






		@cached_property
		def 		UpperCamelCase__       (   self				) ->     Dict:
								return BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224'				) if is_vision_available() else None






		@slow
		def 		UpperCamelCase__       (   self				) ->     Optional[Any]:
								_lowerCAmelCase										=BeitForMaskedImageModeling.from_pretrained('microsoft/beit-base-patch16-224-pt22k'				).to(a_				)

								_lowerCAmelCase										=self.default_image_processor
								_lowerCAmelCase										=prepare_img()
								_lowerCAmelCase										=image_processor(images=a_		,						return_tensors='pt'				).pixel_values.to(a_				)

								# prepare bool_masked_pos
								_lowerCAmelCase										=torch.ones((1, 196)		,						dtype=torch.bool				).to(a_				)

								# forward pass
								with torch.no_grad():
														_lowerCAmelCase										=model(pixel_values=a_		,						bool_masked_pos=a_				)
								_lowerCAmelCase										=outputs.logits

								# verify the logits
								_lowerCAmelCase										=torch.Size((1, 196, 8192)				)
								self.assertEqual(logits.shape		,						a_				)

								_lowerCAmelCase										=torch.tensor(
								    [[-3.2_437, 0.5_072, -13.9_174], [-3.2_456, 0.4_948, -13.9_401], [-3.2_033, 0.5_121, -13.8_550]]				).to(a_				)

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






		@slow
		def 		UpperCamelCase__       (   self				) ->     Optional[int]:
								_lowerCAmelCase										=BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224'				).to(a_				)

								_lowerCAmelCase										=self.default_image_processor
								_lowerCAmelCase										=prepare_img()
								_lowerCAmelCase										=image_processor(images=a_		,						return_tensors='pt'				).to(a_				)

								# forward pass
								with torch.no_grad():
														_lowerCAmelCase										=model(**a_				)
								_lowerCAmelCase										=outputs.logits

								# verify the logits
								_lowerCAmelCase										=torch.Size((1, 1000)				)
								self.assertEqual(logits.shape		,						a_				)

								_lowerCAmelCase										=torch.tensor([-1.2_385, -1.0_987, -1.0_108]				).to(a_				)

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

								_lowerCAmelCase										=281
								self.assertEqual(logits.argmax(-1				).item()		,						a_				)






		@slow
		def 		UpperCamelCase__       (   self				) ->     Union[str, Any]:
								_lowerCAmelCase										=BeitForImageClassification.from_pretrained('microsoft/beit-large-patch16-224-pt22k-ft22k'				).to(
								    a_				)

								_lowerCAmelCase										=self.default_image_processor
								_lowerCAmelCase										=prepare_img()
								_lowerCAmelCase										=image_processor(images=a_		,						return_tensors='pt'				).to(a_				)

								# forward pass
								with torch.no_grad():
														_lowerCAmelCase										=model(**a_				)
								_lowerCAmelCase										=outputs.logits

								# verify the logits
								_lowerCAmelCase										=torch.Size((1, 2_1841)				)
								self.assertEqual(logits.shape		,						a_				)

								_lowerCAmelCase										=torch.tensor([1.6_881, -0.2_787, 0.5_901]				).to(a_				)

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

								_lowerCAmelCase										=2396
								self.assertEqual(logits.argmax(-1				).item()		,						a_				)






		@slow
		def 		UpperCamelCase__       (   self				) ->     Union[str, Any]:
								_lowerCAmelCase										=BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'				)
								_lowerCAmelCase										=model.to(a_				)

								_lowerCAmelCase										=BeitImageProcessor(do_resize=a_		,						size=640		,						do_center_crop=a_				)

								_lowerCAmelCase										=load_dataset('hf-internal-testing/fixtures_ade20k'		,						split='test'				)
								_lowerCAmelCase										=Image.open(ds[0]['file']				)
								_lowerCAmelCase										=image_processor(images=a_		,						return_tensors='pt'				).to(a_				)

								# forward pass
								with torch.no_grad():
														_lowerCAmelCase										=model(**a_				)
								_lowerCAmelCase										=outputs.logits

								# verify the logits
								_lowerCAmelCase										=torch.Size((1, 150, 160, 160)				)
								self.assertEqual(logits.shape		,						a_				)

								_lowerCAmelCase										=version.parse(PIL.__version__				) < version.parse('9.0.0'				)

								if is_pillow_less_than_a:
														_lowerCAmelCase										=torch.tensor(
														    [
														        [[-4.9_225, -2.3_954, -3.0_522], [-2.8_822, -1.0_046, -1.7_561], [-2.9_549, -1.3_228, -2.1_347]],
														        [[-5.8_168, -3.4_129, -4.0_778], [-3.8_651, -2.2_214, -3.0_277], [-3.8_356, -2.4_643, -3.3_535]],
														        [[-0.0_078, 3.9_952, 4.0_754], [2.9_856, 4.6_944, 5.0_035], [3.2_413, 4.7_813, 4.9_969]],
														    ]		,						device=a_		,						)
								else:
														_lowerCAmelCase										=torch.tensor(
														    [
														        [[-4.8_960, -2.3_688, -3.0_355], [-2.8_478, -0.9_836, -1.7_418], [-2.9_449, -1.3_332, -2.1_456]],
														        [[-5.8_081, -3.4_124, -4.1_006], [-3.8_561, -2.2_081, -3.0_323], [-3.8_365, -2.4_601, -3.3_669]],
														        [[-0.0_309, 3.9_868, 4.0_540], [2.9_640, 4.6_877, 4.9_976], [3.2_081, 4.7_690, 4.9_942]],
														    ]		,						device=a_		,						)

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






		@slow
		def 		UpperCamelCase__       (   self				) ->     Any:
								_lowerCAmelCase										=BeitForSemanticSegmentation.from_pretrained('microsoft/beit-base-finetuned-ade-640-640'				)
								_lowerCAmelCase										=model.to(a_				)

								_lowerCAmelCase										=BeitImageProcessor(do_resize=a_		,						size=640		,						do_center_crop=a_				)

								_lowerCAmelCase										=load_dataset('hf-internal-testing/fixtures_ade20k'		,						split='test'				)
								_lowerCAmelCase										=Image.open(ds[0]['file']				)
								_lowerCAmelCase										=image_processor(images=a_		,						return_tensors='pt'				).to(a_				)

								# forward pass
								with torch.no_grad():
														_lowerCAmelCase										=model(**a_				)

								_lowerCAmelCase										=outputs.logits.detach().cpu()

								_lowerCAmelCase										=image_processor.post_process_semantic_segmentation(outputs=a_		,						target_sizes=[(500, 300)]				)
								_lowerCAmelCase										=torch.Size((500, 300)				)
								self.assertEqual(segmentation[0].shape		,						a_				)

								_lowerCAmelCase										=image_processor.post_process_semantic_segmentation(outputs=a_				)
								_lowerCAmelCase										=torch.Size((160, 160)				)
								self.assertEqual(segmentation[0].shape		,						a_				)






'''simple docstring'''

import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class   SCREAMING_SNAKE_CASE			(						__lowercase):







		"""simple docstring"""
		lowercase   :      Any       =							['image_processor', 'tokenizer']
		lowercase   :      Any       =							'CLIPImageProcessor'
		lowercase   :      int       =							('CLIPTokenizer', 'CLIPTokenizerFast')






		def __init__(   self		,						__A=None		,						__A=None		,						**__A				) ->     str:
								_lowerCAmelCase										=None
								if "feature_extractor" in kwargs:
														warnings.warn(
														    'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
														    ' instead.'		,						__A		,						)
														_lowerCAmelCase										=kwargs.pop('feature_extractor'				)

								_lowerCAmelCase										=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__(__A		,						__A				)






		def __call__(   self		,						__A=None		,						__A=None		,						__A=None		,						**__A				) ->     Optional[int]:

								if text is None and images is None:
														raise ValueError('You have to specify either text or images. Both cannot be none.'				)

								if text is not None:
														_lowerCAmelCase										=self.tokenizer(__A		,						return_tensors=__A		,						**__A				)

								if images is not None:
														_lowerCAmelCase										=self.image_processor(__A		,						return_tensors=__A		,						**__A				)

								if text is not None and images is not None:
														_lowerCAmelCase										=image_features.pixel_values
														return encoding
								elif text is not None:
														return encoding
								else:
														return BatchEncoding(data=dict(**__A				)		,						tensor_type=__A				)






		def 		UpperCamelCase__       (   self		,						*__A		,						**__A				) ->     Any:
								return self.tokenizer.batch_decode(*__A		,						**__A				)






		def 		UpperCamelCase__       (   self		,						*__A		,						**__A				) ->     Optional[int]:
								return self.tokenizer.decode(*__A		,						**__A				)






		@property
		def 		UpperCamelCase__       (   self				) ->     Tuple:
								_lowerCAmelCase										=self.tokenizer.model_input_names
								_lowerCAmelCase										=self.image_processor.model_input_names
								return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names				)				)






		@property
		def 		UpperCamelCase__       (   self				) ->     Optional[int]:
								warnings.warn(
								    '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'		,						__A		,						)
								return self.image_processor_class






		@property
		def 		UpperCamelCase__       (   self				) ->     Optional[Any]:
								warnings.warn(
								    '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'		,						__A		,						)
								return self.image_processor





