






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

						"""simple docstring"""



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




											'''simple docstring'''



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

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



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Any:




											'''simple docstring'''



											__a					:						int      =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

											__a					:						str      =					None
											__a					:						Dict      =					None
											if self.use_labels:
																__a					:						Optional[int]      =					ids_tensor([self.batch_size] ,					self.type_sequence_label_size   )
																__a					:						Dict      =					ids_tensor([self.batch_size, self.image_size, self.image_size] ,					self.num_labels   )

											__a					:						int      =					self.get_config()

											return config, pixel_values, labels, pixel_labels



						def        UpperCAmelCase__					(self:					Optional[Any]   )    ->     Union[str, 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=__UpperCAmelCase ,					initializer_range=self.initializer_range ,					out_indices=self.out_indices ,					)



						def        UpperCAmelCase__					(self:					Optional[int] ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					Tuple   )    ->     Tuple:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					BeitModel(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						List[str]      =					model(__UpperCAmelCase   )
											self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)   )



						def        UpperCAmelCase__					(self:					str ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					Union[str, Any] ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					List[str]   )    ->     Optional[Any]:




											'''simple docstring'''



											__a					:						Optional[Any]      =					BeitForMaskedImageModeling(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						int      =					model(__UpperCAmelCase   )
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length - 1, self.vocab_size)   )



						def        UpperCAmelCase__					(self:					Union[str, Any] ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					int ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					Union[str, Any]   )    ->     Tuple:




											'''simple docstring'''



											__a					:						Any      =					self.type_sequence_label_size
											__a					:						str      =					BeitForImageClassification(__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						Dict      =					model(__UpperCAmelCase ,					labels=__UpperCAmelCase   )
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.type_sequence_label_size)   )

											# test greyscale images
											__a					:						Optional[int]      =					1
											__a					:						List[Any]      =					BeitForImageClassification(__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()

											__a					:						Any      =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
											__a					:						Dict      =					model(__UpperCAmelCase ,					labels=__UpperCAmelCase   )
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.type_sequence_label_size)   )



						def        UpperCAmelCase__					(self:					str ,					__UpperCAmelCase:					Optional[Any] ,					__UpperCAmelCase:					int ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					int   )    ->     Tuple:




											'''simple docstring'''



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



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     List[str]:




											'''simple docstring'''



											__a					:						Dict      =					self.prepare_config_and_inputs()
											__a      ,			__a      ,			__a      ,			__a					:						Dict      =					config_and_inputs
											__a					:						List[Any]      =					{"pixel_values": pixel_values}
											return config, inputs_dict




@require_torch
class 							snake_case_       (    __UpperCamelCase		,							__UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      (
						    (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
						    if is_torch_available()
						    else ()
						)
						snake_case__        =      (
						    {
						        """feature-extraction""": BeitModel,
						        """image-classification""": BeitForImageClassification,
						        """image-segmentation""": BeitForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						snake_case__        =      False
						snake_case__        =      False
						snake_case__        =      False



						def        UpperCAmelCase__					(self:					int   )    ->     Optional[Any]:




											'''simple docstring'''



											__a					:						Tuple      =					BeitModelTester(self   )
											__a					:						int      =					ConfigTester(self ,					config_class=__UpperCAmelCase ,					has_text_modality=__UpperCAmelCase ,					hidden_size=37   )



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Tuple:




											'''simple docstring'''



											self.config_tester.run_common_tests()



						@unittest.skip(reason="BEiT does not use inputs_embeds"   )
						def        UpperCAmelCase__					(self:					Tuple   )    ->     List[Any]:




											'''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        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     Optional[int]:




											'''simple docstring'''



											pass



						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     List[str]:




											'''simple docstring'''



											__a      ,			__a					:						Dict      =					self.model_tester.prepare_config_and_inputs_for_common()

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



						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     Tuple:




											'''simple docstring'''



											__a      ,			__a					:						int      =					self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																__a					:						int      =					model_class(__UpperCAmelCase   )
																__a					:						Optional[Any]      =					inspect.signature(model.forward   )
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																__a					:						Union[str, Any]      =					[*signature.parameters.keys()]

																__a					:						Dict      =					["pixel_values"]
																self.assertListEqual(arg_names[:1] ,					__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Optional[int]   )    ->     Tuple:




											'''simple docstring'''



											__a					:						int      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     str:




											'''simple docstring'''



											__a					:						List[str]      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     Optional[int]:




											'''simple docstring'''



											__a					:						int      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					int   )    ->     List[str]:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Optional[Any]   )    ->     str:




											'''simple docstring'''



											if not self.model_tester.is_training:
																return

											__a      ,			__a					:						Optional[Any]      =					self.model_tester.prepare_config_and_inputs_for_common()
											__a					:						Dict      =					True

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

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



						def        UpperCAmelCase__					(self:					int   )    ->     Optional[int]:




											'''simple docstring'''



											__a      ,			__a					:						List[Any]      =					self.model_tester.prepare_config_and_inputs_for_common()
											if not self.model_tester.is_training:
																return

											__a					:						List[str]      =					False
											__a					:						Dict      =					True

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

																__a					:						Optional[Any]      =					model_class(__UpperCAmelCase   )
																model.gradient_checkpointing_enable()
																model.to(__UpperCAmelCase   )
																model.train()
																__a					:						Optional[Any]      =					self._prepare_for_class(__UpperCAmelCase ,					__UpperCAmelCase ,					return_labels=__UpperCAmelCase   )
																__a					:						List[str]      =					model(**__UpperCAmelCase   ).loss
																loss.backward()



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a      ,			__a					:						str      =					self.model_tester.prepare_config_and_inputs_for_common()

											__a					:						Union[str, Any]      =					_config_zero_init(__UpperCAmelCase   )
											for model_class in self.all_model_classes:
																__a					:						List[str]      =					model_class(config=__UpperCAmelCase   )
																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:					Optional[int]   )    ->     str:




											'''simple docstring'''



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







def     a_   ()				->						Union[str, Any]:



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




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

						"""simple docstring"""



						@cached_property
						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     Optional[int]:




											'''simple docstring'''



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



						@slow
						def        UpperCAmelCase__					(self:					List[str]   )    ->     Optional[int]:




											'''simple docstring'''



											__a					:						Any      =					BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k"   ).to(__UpperCAmelCase   )

											__a					:						Any      =					self.default_image_processor
											__a					:						str      =					prepare_img()
											__a					:						Any      =					image_processor(images=__UpperCAmelCase ,					return_tensors="pt"   ).pixel_values.to(__UpperCAmelCase   )

											# prepare bool_masked_pos
											__a					:						Optional[int]      =					torch.ones((1, 196) ,					dtype=torch.bool   ).to(__UpperCAmelCase   )

											# forward pass
											with torch.no_grad():
																__a					:						Optional[Any]      =					model(pixel_values=__UpperCAmelCase ,					bool_masked_pos=__UpperCAmelCase   )
											__a					:						Any      =					outputs.logits

											# verify the logits
											__a					:						List[Any]      =					torch.Size((1, 196, 8192)   )
											self.assertEqual(logits.shape ,					__UpperCAmelCase   )

											__a					:						Optional[Any]      =					torch.tensor(
											    [[-3.24_37, 0.50_72, -13.91_74], [-3.24_56, 0.49_48, -13.94_01], [-3.20_33, 0.51_21, -13.85_50]]   ).to(__UpperCAmelCase   )

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



						@slow
						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Optional[int]:




											'''simple docstring'''



											__a					:						List[Any]      =					BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224"   ).to(__UpperCAmelCase   )

											__a					:						Union[str, Any]      =					self.default_image_processor
											__a					:						Optional[int]      =					prepare_img()
											__a					:						Dict      =					image_processor(images=__UpperCAmelCase ,					return_tensors="pt"   ).to(__UpperCAmelCase   )

											# forward pass
											with torch.no_grad():
																__a					:						Tuple      =					model(**__UpperCAmelCase   )
											__a					:						Any      =					outputs.logits

											# verify the logits
											__a					:						int      =					torch.Size((1, 1000)   )
											self.assertEqual(logits.shape ,					__UpperCAmelCase   )

											__a					:						List[str]      =					torch.tensor([-1.23_85, -1.09_87, -1.01_08]   ).to(__UpperCAmelCase   )

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

											__a					:						List[str]      =					281
											self.assertEqual(logits.argmax(-1   ).item() ,					__UpperCAmelCase   )



						@slow
						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     Optional[Any]:




											'''simple docstring'''



											__a					:						List[Any]      =					BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k"   ).to(
											    __UpperCAmelCase   )

											__a					:						Optional[int]      =					self.default_image_processor
											__a					:						List[str]      =					prepare_img()
											__a					:						Union[str, Any]      =					image_processor(images=__UpperCAmelCase ,					return_tensors="pt"   ).to(__UpperCAmelCase   )

											# forward pass
											with torch.no_grad():
																__a					:						Optional[int]      =					model(**__UpperCAmelCase   )
											__a					:						Union[str, Any]      =					outputs.logits

											# verify the logits
											__a					:						Tuple      =					torch.Size((1, 21841)   )
											self.assertEqual(logits.shape ,					__UpperCAmelCase   )

											__a					:						List[str]      =					torch.tensor([1.68_81, -0.27_87, 0.59_01]   ).to(__UpperCAmelCase   )

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

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



						@slow
						def        UpperCAmelCase__					(self:					str   )    ->     Tuple:




											'''simple docstring'''



											__a					:						Any      =					BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640"   )
											__a					:						Tuple      =					model.to(__UpperCAmelCase   )

											__a					:						Any      =					BeitImageProcessor(do_resize=__UpperCAmelCase ,					size=640 ,					do_center_crop=__UpperCAmelCase   )

											__a					:						Dict      =					load_dataset("hf-internal-testing/fixtures_ade20k" ,					split="test"   )
											__a					:						List[Any]      =					Image.open(ds[0]["file"]   )
											__a					:						List[Any]      =					image_processor(images=__UpperCAmelCase ,					return_tensors="pt"   ).to(__UpperCAmelCase   )

											# forward pass
											with torch.no_grad():
																__a					:						Dict      =					model(**__UpperCAmelCase   )
											__a					:						Any      =					outputs.logits

											# verify the logits
											__a					:						int      =					torch.Size((1, 150, 160, 160)   )
											self.assertEqual(logits.shape ,					__UpperCAmelCase   )

											__a					:						Optional[Any]      =					version.parse(PIL.__version__   ) < version.parse("9.0.0"   )

											if is_pillow_less_than_a:
																__a					:						Tuple      =					torch.tensor(
																    [
																        [[-4.92_25, -2.39_54, -3.05_22], [-2.88_22, -1.00_46, -1.75_61], [-2.95_49, -1.32_28, -2.13_47]],
																        [[-5.81_68, -3.41_29, -4.07_78], [-3.86_51, -2.22_14, -3.02_77], [-3.83_56, -2.46_43, -3.35_35]],
																        [[-0.00_78, 3.99_52, 4.07_54], [2.98_56, 4.69_44, 5.00_35], [3.24_13, 4.78_13, 4.99_69]],
																    ] ,					device=__UpperCAmelCase ,					)
											else:
																__a					:						Union[str, Any]      =					torch.tensor(
																    [
																        [[-4.89_60, -2.36_88, -3.03_55], [-2.84_78, -0.98_36, -1.74_18], [-2.94_49, -1.33_32, -2.14_56]],
																        [[-5.80_81, -3.41_24, -4.10_06], [-3.85_61, -2.20_81, -3.03_23], [-3.83_65, -2.46_01, -3.36_69]],
																        [[-0.03_09, 3.98_68, 4.05_40], [2.96_40, 4.68_77, 4.99_76], [3.20_81, 4.76_90, 4.99_42]],
																    ] ,					device=__UpperCAmelCase ,					)

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



						@slow
						def        UpperCAmelCase__					(self:					Optional[int]   )    ->     Dict:




											'''simple docstring'''



											__a					:						Any      =					BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640"   )
											__a					:						str      =					model.to(__UpperCAmelCase   )

											__a					:						Union[str, Any]      =					BeitImageProcessor(do_resize=__UpperCAmelCase ,					size=640 ,					do_center_crop=__UpperCAmelCase   )

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

											# forward pass
											with torch.no_grad():
																__a					:						Optional[int]      =					model(**__UpperCAmelCase   )

											__a					:						int      =					outputs.logits.detach().cpu()

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

											__a					:						List[str]      =					image_processor.post_process_semantic_segmentation(outputs=__UpperCAmelCase   )
											__a					:						Tuple      =					torch.Size((160, 160)   )
											self.assertEqual(segmentation[0].shape ,					__UpperCAmelCase   )













from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
			import tensorflow as tf

			from transformers import TFBlipTextModel
			from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST




class 							snake_case_       :

						"""simple docstring"""



						def __init__(self:					Optional[Any] ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					int=12 ,					__UpperCAmelCase:					Dict=7 ,					__UpperCAmelCase:					str=True ,					__UpperCAmelCase:					Tuple=True ,					__UpperCAmelCase:					Tuple=True ,					__UpperCAmelCase:					Optional[int]=99 ,					__UpperCAmelCase:					str=32 ,					__UpperCAmelCase:					Any=32 ,					__UpperCAmelCase:					int=2 ,					__UpperCAmelCase:					List[str]=4 ,					__UpperCAmelCase:					Tuple=37 ,					__UpperCAmelCase:					Optional[int]=0.1 ,					__UpperCAmelCase:					List[Any]=0.1 ,					__UpperCAmelCase:					Dict=512 ,					__UpperCAmelCase:					Union[str, Any]=0.02 ,					__UpperCAmelCase:					List[Any]=0 ,					__UpperCAmelCase:					str=None ,					)    ->     Tuple:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					parent
											__a					:						Dict      =					batch_size
											__a					:						Optional[Any]      =					seq_length
											__a					:						int      =					is_training
											__a					:						Union[str, Any]      =					use_input_mask
											__a					:						str      =					use_labels
											__a					:						Tuple      =					vocab_size
											__a					:						Dict      =					hidden_size
											__a					:						Optional[int]      =					projection_dim
											__a					:						Tuple      =					num_hidden_layers
											__a					:						Dict      =					num_attention_heads
											__a					:						Any      =					intermediate_size
											__a					:						Optional[int]      =					dropout
											__a					:						Dict      =					attention_dropout
											__a					:						List[Any]      =					max_position_embeddings
											__a					:						Any      =					initializer_range
											__a					:						Tuple      =					scope
											__a					:						Dict      =					bos_token_id



						def        UpperCAmelCase__					(self:					str   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a					:						Optional[Any]      =					ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size   )

											__a					:						Any      =					None
											if self.use_input_mask:
																__a					:						Tuple      =					random_attention_mask([self.batch_size, self.seq_length]   )

											if input_mask is not None:
																__a					:						Tuple      =					input_mask.numpy()
																__a      ,			__a					:						int      =					input_mask.shape
																__a					:						Dict      =					np.random.randint(1 ,					seq_length - 1 ,					size=(batch_size,)   )
																for batch_idx, start_index in enumerate(__UpperCAmelCase   ):
																					__a					:						Any      =					1
																					__a					:						int      =					0

											__a					:						Union[str, Any]      =					self.get_config()

											return config, input_ids, tf.convert_to_tensor(__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					List[str]   )    ->     Optional[int]:




											'''simple docstring'''



											return BlipTextConfig(
											    vocab_size=self.vocab_size ,					hidden_size=self.hidden_size ,					projection_dim=self.projection_dim ,					num_hidden_layers=self.num_hidden_layers ,					num_attention_heads=self.num_attention_heads ,					intermediate_size=self.intermediate_size ,					dropout=self.dropout ,					attention_dropout=self.attention_dropout ,					max_position_embeddings=self.max_position_embeddings ,					initializer_range=self.initializer_range ,					bos_token_id=self.bos_token_id ,					)



						def        UpperCAmelCase__					(self:					List[str] ,					__UpperCAmelCase:					Any ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					str   )    ->     List[str]:




											'''simple docstring'''



											__a					:						int      =					TFBlipTextModel(config=__UpperCAmelCase   )
											__a					:						List[str]      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					training=__UpperCAmelCase   )
											__a					:						Any      =					model(__UpperCAmelCase ,					training=__UpperCAmelCase   )
											self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)   )
											self.parent.assertEqual(result.pooler_output.shape ,					(self.batch_size, self.hidden_size)   )



						def        UpperCAmelCase__					(self:					List[str]   )    ->     str:




											'''simple docstring'''



											__a					:						Tuple      =					self.prepare_config_and_inputs()
											__a      ,			__a      ,			__a					:						Dict      =					config_and_inputs
											__a					:						Union[str, Any]      =					{"input_ids": input_ids, "attention_mask": input_mask}
											return config, inputs_dict




@require_tf
class 							snake_case_       (    __UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      (TFBlipTextModel,) if is_tf_available() else ()
						snake_case__        =      False
						snake_case__        =      False
						snake_case__        =      False



						def        UpperCAmelCase__					(self:					List[str]   )    ->     Optional[int]:




											'''simple docstring'''



											__a					:						str      =					BlipTextModelTester(self   )
											__a					:						str      =					ConfigTester(self ,					config_class=__UpperCAmelCase ,					hidden_size=37   )



						def        UpperCAmelCase__					(self:					str   )    ->     str:




											'''simple docstring'''



											self.config_tester.run_common_tests()



						def        UpperCAmelCase__					(self:					Any   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a					:						int      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Dict:




											'''simple docstring'''



											pass



						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     Optional[int]:




											'''simple docstring'''



											pass



						@unittest.skip(reason="Blip does not use inputs_embeds"   )
						def        UpperCAmelCase__					(self:					Any   )    ->     Dict:




											'''simple docstring'''



											pass



						@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"   )
						def        UpperCAmelCase__					(self:					List[Any]   )    ->     List[str]:




											'''simple docstring'''



											pass



						@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"   )
						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     int:




											'''simple docstring'''



											pass



						@slow
						def        UpperCAmelCase__					(self:					Optional[Any]   )    ->     Tuple:




											'''simple docstring'''



											for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__a					:						List[str]      =					TFBlipTextModel.from_pretrained(__UpperCAmelCase   )
																self.assertIsNotNone(__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Dict ,					__UpperCAmelCase:					Optional[Any]=True   )    ->     Any:




											'''simple docstring'''



											super().test_pt_tf_model_equivalence(allow_missing_keys=__UpperCAmelCase   )





