


import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, require_torch, 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 ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
						from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image
class        _lowercase      :






				'''simple docstring'''



				def __init__(       self			:Optional[int]			,			lowerCAmelCase__			:Tuple			,			lowerCAmelCase__			:Optional[int]=13			,			lowerCAmelCase__			:List[str]=64			,			lowerCAmelCase__			:Union[str, Any]=2			,			lowerCAmelCase__			:int=3			,			lowerCAmelCase__			:List[Any]=True			,			lowerCAmelCase__			:Optional[int]=True			,			lowerCAmelCase__			:int=32			,			lowerCAmelCase__			:Optional[Any]=5			,			lowerCAmelCase__			:Optional[Any]=4			,			lowerCAmelCase__			:Tuple=37			,			lowerCAmelCase__			:Optional[int]="gelu"			,			lowerCAmelCase__			:List[Any]=0.1			,			lowerCAmelCase__			:Optional[Any]=0.1			,			lowerCAmelCase__			:Any=10			,			lowerCAmelCase__			:List[Any]=0.02			,			lowerCAmelCase__			:Union[str, Any]=[1, 16, 4, 4]			,			lowerCAmelCase__			:Tuple=None			,			)		->			Tuple:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		parent
										__SCREAMING_SNAKE_CASE						:     List[str]					=		batch_size
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		image_size
										__SCREAMING_SNAKE_CASE						:     List[str]					=		patch_size
										__SCREAMING_SNAKE_CASE						:     str					=		num_channels
										__SCREAMING_SNAKE_CASE						:     Any					=		is_training
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		use_labels
										__SCREAMING_SNAKE_CASE						:     List[str]					=		hidden_size
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		num_hidden_layers
										__SCREAMING_SNAKE_CASE						:     Tuple					=		num_attention_heads
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		intermediate_size
										__SCREAMING_SNAKE_CASE						:     Tuple					=		hidden_act
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		hidden_dropout_prob
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		attention_probs_dropout_prob
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		type_sequence_label_size
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		initializer_range
										__SCREAMING_SNAKE_CASE						:     Tuple					=		scope
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		backbone_featmap_shape

										# in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
										# the number of patches is based on the feature map of the backbone, which by default uses an output stride
										# of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
										__SCREAMING_SNAKE_CASE						:     List[str]					=		(self.image_size // 32) ** 2
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		num_patches + 1


				def 		__magic_name__(       self			:str	)		->			List[str]:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		None
										if self.use_labels:
																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		ids_tensor([self.batch_size]			,			self.type_sequence_label_size	)

										__SCREAMING_SNAKE_CASE						:     str					=		self.get_config()

										return config, pixel_values, labels


				def 		__magic_name__(       self			:List[str]	)		->			str:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		{
										    '''global_padding''': '''same''',
										    '''layer_type''': '''bottleneck''',
										    '''depths''': [3, 4, 9],
										    '''out_features''': ['''stage1''', '''stage2''', '''stage3'''],
										    '''embedding_dynamic_padding''': True,
										    '''hidden_sizes''': [4, 8, 16, 32],
										    '''num_groups''': 2,
										}

										return ViTHybridConfig(
										    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=lowerCAmelCase__			,			initializer_range=self.initializer_range			,			backbone_featmap_shape=self.backbone_featmap_shape			,			backbone_config=lowerCAmelCase__			,			)


				def 		__magic_name__(       self			:List[str]			,			lowerCAmelCase__			:Tuple			,			lowerCAmelCase__			:Optional[int]			,			lowerCAmelCase__			:Optional[Any]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		ViTHybridModel(config=lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		model(lowerCAmelCase__	)
										self.parent.assertEqual(result.last_hidden_state.shape			,			(self.batch_size, self.seq_length, self.hidden_size)	)


				def 		__magic_name__(       self			:List[Any]			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:Tuple			,			lowerCAmelCase__			:Union[str, Any]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.type_sequence_label_size
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		ViTHybridForImageClassification(lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		model(lowerCAmelCase__			,			labels=lowerCAmelCase__	)
										self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.type_sequence_label_size)	)



				def 		__magic_name__(       self			:Optional[int]	)		->			List[str]:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		self.prepare_config_and_inputs()
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Dict					=		config_and_inputs
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		{'''pixel_values''': pixel_values}
										return config, inputs_dict




@require_torch
class        _lowercase      (			A__						,			A__						,			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Optional[Any]						=		(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE__				:			Union[str, Any]						=		(
				    {'''feature-extraction''': ViTHybridModel, '''image-classification''': ViTHybridForImageClassification}
				    if is_torch_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE__				:			Dict						=		False
				SCREAMING_SNAKE_CASE__				:			str						=		False
				SCREAMING_SNAKE_CASE__				:			Optional[int]						=		False


				def 		__magic_name__(       self			:Dict	)		->			str:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		ViTHybridModelTester(self	)
										__SCREAMING_SNAKE_CASE						:     str					=		ConfigTester(self			,			config_class=lowerCAmelCase__			,			has_text_modality=lowerCAmelCase__			,			hidden_size=37	)


				def 		__magic_name__(       self			:List[str]	)		->			List[Any]:
										self.config_tester.run_common_tests()


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


				def 		__magic_name__(       self			:Any	)		->			Optional[int]:
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     str					=		self.model_tester.prepare_config_and_inputs_for_common()

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


				def 		__magic_name__(       self			:Any	)		->			Optional[int]:
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     List[Any]					=		self.model_tester.prepare_config_and_inputs_for_common()

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

																__SCREAMING_SNAKE_CASE						:     str					=		['''pixel_values''']
																self.assertListEqual(arg_names[:1]			,			lowerCAmelCase__	)


				def 		__magic_name__(       self			:List[Any]	)		->			Optional[int]:
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:str	)		->			List[str]:
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:Dict	)		->			Optional[Any]:
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Dict					=		self.model_tester.prepare_config_and_inputs_for_common()

										__SCREAMING_SNAKE_CASE						:     Tuple					=		_config_zero_init(lowerCAmelCase__	)
										for model_class in self.all_model_classes:
																__SCREAMING_SNAKE_CASE						:     Dict					=		model_class(config=lowerCAmelCase__	)
																# Skip the check for the backbone
																for name, module in model.named_modules():
																						if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
																												__SCREAMING_SNAKE_CASE						:     Any					=		[f'''{name}.{key}''' for key in module.state_dict().keys()]
																												break

																for name, param in model.named_parameters():
																						if param.requires_grad:
																												if name in backbone_params:
																																		continue
																												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 		__magic_name__(       self			:Union[str, Any]	)		->			int:
										for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__SCREAMING_SNAKE_CASE						:     List[Any]					=		ViTHybridModel.from_pretrained(lowerCAmelCase__	)
																self.assertIsNotNone(lowerCAmelCase__	)




def        _UpperCamelCase							(      ):
						__SCREAMING_SNAKE_CASE						:     Tuple					=		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
						return image
@require_torch
@require_vision
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				@cached_property
				def 		__magic_name__(       self			:List[Any]	)		->			str:
										return (
										    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
										    if is_vision_available()
										    else None
										)


				@slow
				def 		__magic_name__(       self			:List[str]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     str					=		ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]	).to(
										    lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.default_image_processor
										__SCREAMING_SNAKE_CASE						:     Any					=		prepare_img()
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		image_processor(images=lowerCAmelCase__			,			return_tensors='''pt'''	).to(lowerCAmelCase__	)

										# forward pass
										with torch.no_grad():
																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		model(**lowerCAmelCase__	)

										# verify the logits
										__SCREAMING_SNAKE_CASE						:     Tuple					=		torch.Size((1, 1_000)	)
										self.assertEqual(outputs.logits.shape			,			lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     Tuple					=		torch.tensor([-1.9090, -0.4993, -0.2389]	).to(lowerCAmelCase__	)

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



				@slow
				@require_accelerate
				def 		__magic_name__(       self			:str	)		->			str:
										__SCREAMING_SNAKE_CASE						:     Any					=		ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384'''	)
										__SCREAMING_SNAKE_CASE						:     Dict					=		ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384'''			,			device_map='''auto'''	)

										__SCREAMING_SNAKE_CASE						:     int					=		prepare_img()

										__SCREAMING_SNAKE_CASE						:     Dict					=		image_processor(images=lowerCAmelCase__			,			return_tensors='''pt'''	)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		model(**lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Any					=		outputs.logits
										# model predicts one of the 1000 ImageNet classes
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		logits.argmax(-1	).item()

										self.assertTrue(model.config.idalabel[predicted_class_idx]			,			'''tabby, tabby cat'''	)




import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin
class        _lowercase      (			A__						,			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Optional[int]						=		TransfoXLTokenizer
				SCREAMING_SNAKE_CASE__				:			int						=		False
				SCREAMING_SNAKE_CASE__				:			Optional[Any]						=		False


				def 		__magic_name__(       self			:str	)		->			Dict:
										super().setUp()

										__SCREAMING_SNAKE_CASE						:     List[str]					=		[
										    '''<unk>''',
										    '''[CLS]''',
										    '''[SEP]''',
										    '''want''',
										    '''unwanted''',
										    '''wa''',
										    '''un''',
										    '''running''',
										    ''',''',
										    '''low''',
										    '''l''',
										]
										__SCREAMING_SNAKE_CASE						:     Any					=		os.path.join(self.tmpdirname			,			VOCAB_FILES_NAMES['''vocab_file''']	)
										with open(self.vocab_file			,			'''w'''			,			encoding='''utf-8'''	) as vocab_writer:
																vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]	)	)


				def 		__magic_name__(       self			:Any			,			**lowerCAmelCase__			:int	)		->			str:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		True
										return TransfoXLTokenizer.from_pretrained(self.tmpdirname			,			**lowerCAmelCase__	)


				def 		__magic_name__(       self			:Union[str, Any]			,			lowerCAmelCase__			:List[Any]	)		->			Tuple:
										__SCREAMING_SNAKE_CASE						:     Dict					=		'''<unk> UNwanted , running'''
										__SCREAMING_SNAKE_CASE						:     List[str]					=		'''<unk> unwanted, running'''
										return input_text, output_text


				def 		__magic_name__(       self			:Any	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     int					=		TransfoXLTokenizer(vocab_file=self.vocab_file			,			lower_case=lowerCAmelCase__	)

										__SCREAMING_SNAKE_CASE						:     int					=		tokenizer.tokenize('''<unk> UNwanted , running'''	)
										self.assertListEqual(lowerCAmelCase__			,			['''<unk>''', '''unwanted''', ''',''', '''running''']	)

										self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__	)			,			[0, 4, 8, 7]	)


				def 		__magic_name__(       self			:Tuple	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		TransfoXLTokenizer(lower_case=lowerCAmelCase__	)

										self.assertListEqual(
										    tokenizer.tokenize(''' \tHeLLo ! how  \n Are yoU ?  '''	)			,			['''hello''', '''!''', '''how''', '''are''', '''you''', '''?''']	)


				def 		__magic_name__(       self			:Tuple	)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		TransfoXLTokenizer(lower_case=lowerCAmelCase__	)

										self.assertListEqual(
										    tokenizer.tokenize(''' \tHeLLo ! how  \n Are yoU ?  '''	)			,			['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']	)


				def 		__magic_name__(       self			:Dict	)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     List[str]					=		TransfoXLTokenizer(lower_case=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		'''Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?'''
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		[
										    '''Hello''',
										    '''(''',
										    '''bracket''',
										    ''')''',
										    '''and''',
										    '''side''',
										    '''@-@''',
										    '''scrolled''',
										    '''[''',
										    '''and''',
										    ''']''',
										    '''Henry''',
										    '''\'s''',
										    '''$''',
										    '''5''',
										    '''@,@''',
										    '''000''',
										    '''with''',
										    '''3''',
										    '''@.@''',
										    '''34''',
										    '''m''',
										    '''.''',
										    '''What''',
										    '''\'s''',
										    '''up''',
										    '''!''',
										    '''?''',
										]

										self.assertListEqual(tokenizer.tokenize(lowerCAmelCase__	)			,			lowerCAmelCase__	)

										self.assertEqual(tokenizer.convert_tokens_to_string(lowerCAmelCase__	)			,			lowerCAmelCase__	)



				def 		__magic_name__(       self			:str	)		->			int:
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.get_tokenizer()
										__SCREAMING_SNAKE_CASE						:     Any					=		len(lowerCAmelCase__	)

										tokenizer.add_tokens(['''new1''', '''new2''']	)
										tokenizer.move_added_token('''new1'''			,			1	)

										# Check that moved token is not copied (duplicate)
										self.assertEqual(len(lowerCAmelCase__	)			,			original_len + 2	)
										# Check that token is moved to specified id
										self.assertEqual(tokenizer.encode('''new1'''	)			,			[1]	)
										self.assertEqual(tokenizer.decode([1]	)			,			'''new1'''	)
