






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





					"""simple docstring"""






					def __init__( self					:  List[str]							,		__A					:  Dict							,		__A					:  List[Any]=1_3							,		__A					:  str=6_4							,		__A					:  Dict=2							,		__A					:  Any=3							,		__A					:  Dict=True							,		__A					:  Tuple=True							,		__A					:  Union[str, Any]=3_2							,		__A					:  Any=5							,		__A					:  Dict=4							,		__A					:  Union[str, Any]=3_7							,		__A					:  Optional[int]="gelu"							,		__A					:  List[Any]=0.1							,		__A					:  Tuple=0.1							,		__A					:  Any=1_0							,		__A					:  List[Any]=0.0_2							,		__A					:  Union[str, Any]=[1, 1_6, 4, 4]							,		__A					:  List[Any]=None							,		):
						snake_case__			:				Any       =  parent
						snake_case__			:				int       =  batch_size
						snake_case__			:				str       =  image_size
						snake_case__			:				Tuple       =  patch_size
						snake_case__			:				Tuple       =  num_channels
						snake_case__			:				Dict       =  is_training
						snake_case__			:				Any       =  use_labels
						snake_case__			:				Optional[Any]       =  hidden_size
						snake_case__			:				Any       =  num_hidden_layers
						snake_case__			:				Optional[Any]       =  num_attention_heads
						snake_case__			:				Optional[Any]       =  intermediate_size
						snake_case__			:				Optional[int]       =  hidden_act
						snake_case__			:				Optional[int]       =  hidden_dropout_prob
						snake_case__			:				List[str]       =  attention_probs_dropout_prob
						snake_case__			:				Union[str, Any]       =  type_sequence_label_size
						snake_case__			:				Optional[Any]       =  initializer_range
						snake_case__			:				str       =  scope
						snake_case__			:				Optional[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
						snake_case__			:				Optional[Any]       =  (self.image_size // 3_2) ** 2
						snake_case__			:				Tuple       =  num_patches + 1



					def 	_lowercase					( self					:  str	):
						snake_case__			:				List[str]       =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

						snake_case__			:				Optional[int]       =  None
						if self.use_labels:
							snake_case__			:				Optional[int]       =  ids_tensor([self.batch_size]							,		self.type_sequence_label_size	)

						snake_case__			:				Optional[Any]       =  self.get_config()

						return config, pixel_values, labels



					def 	_lowercase					( self					:  str	):
						snake_case__			:				str       =  {
						    "global_padding": "same",
						    "layer_type": "bottleneck",
						    "depths": [3, 4, 9],
						    "out_features": ["stage1", "stage2", "stage3"],
						    "embedding_dynamic_padding": True,
						    "hidden_sizes": [4, 8, 1_6, 3_2],
						    "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=__A							,		initializer_range=self.initializer_range							,		backbone_featmap_shape=self.backbone_featmap_shape							,		backbone_config=__A							,		)



					def 	_lowercase					( self					:  str							,		__A					:  Optional[Any]							,		__A					:  List[Any]							,		__A					:  Optional[Any]	):
						snake_case__			:				Tuple       =  ViTHybridModel(config=__A	)
						model.to(__A	)
						model.eval()
						snake_case__			:				Optional[Any]       =  model(__A	)
						self.parent.assertEqual(result.last_hidden_state.shape							,		(self.batch_size, self.seq_length, self.hidden_size)	)



					def 	_lowercase					( self					:  str							,		__A					:  int							,		__A					:  Optional[Any]							,		__A					:  List[Any]	):
						snake_case__			:				Any       =  self.type_sequence_label_size
						snake_case__			:				List[Any]       =  ViTHybridForImageClassification(__A	)
						model.to(__A	)
						model.eval()
						snake_case__			:				Dict       =  model(__A							,		labels=__A	)
						self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.type_sequence_label_size)	)






					def 	_lowercase					( self					:  int	):
						snake_case__			:				Union[str, Any]       =  self.prepare_config_and_inputs()
						snake_case__,						snake_case__,						snake_case__			:				int       =  config_and_inputs
						snake_case__			:				Dict       =  {"pixel_values": pixel_values}
						return config, inputs_dict



@require_torch
class 							SCREAMING_SNAKE_CASE__						(    UpperCamelCase_			,       UpperCamelCase_			,       unittest.TestCase						):





					"""simple docstring"""






					a_     =		(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
					a_     =		(
					    {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
					    if is_torch_available()
					    else {}
					)
					a_     =		False
					a_     =		False
					a_     =		False



					def 	_lowercase					( self					:  Any	):
						snake_case__			:				str       =  ViTHybridModelTester(self	)
						snake_case__			:				Optional[int]       =  ConfigTester(self							,		config_class=__A							,		has_text_modality=__A							,		hidden_size=3_7	)



					def 	_lowercase					( self					:  List[str]	):
						self.config_tester.run_common_tests()



					@unittest.skip(reason="ViT does not use inputs_embeds"	)
					def 	_lowercase					( self					:  Any	):
						pass



					def 	_lowercase					( self					:  Dict	):
						snake_case__,						snake_case__			:				List[str]       =  self.model_tester.prepare_config_and_inputs_for_common()

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



					def 	_lowercase					( self					:  str	):
						snake_case__,						snake_case__			:				Optional[Any]       =  self.model_tester.prepare_config_and_inputs_for_common()

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

							snake_case__			:				str       =  ["pixel_values"]
							self.assertListEqual(arg_names[:1]							,		__A	)



					def 	_lowercase					( self					:  int	):
						snake_case__			:				List[str]       =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__A	)



					def 	_lowercase					( self					:  str	):
						snake_case__			:				List[str]       =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__A	)



					def 	_lowercase					( self					:  Tuple	):
						snake_case__,						snake_case__			:				Optional[Any]       =  self.model_tester.prepare_config_and_inputs_for_common()

						snake_case__			:				Tuple       =  _config_zero_init(__A	)
						for model_class in self.all_model_classes:
							snake_case__			:				Dict       =  model_class(config=__A	)
							# Skip the check for the backbone
							for name, module in model.named_modules():
								if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
									snake_case__			:				List[str]       =  [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 	_lowercase					( self					:  Optional[int]	):
						for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							snake_case__			:				Optional[int]       =  ViTHybridModel.from_pretrained(__A	)
							self.assertIsNotNone(__A	)







def 					SCREAMING_SNAKE_CASE    (			):
	snake_case__			:				Dict       =  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 	_lowercase					( self					:  Optional[Any]	):
						return (
						    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
						    if is_vision_available()
						    else None
						)



					@slow
					def 	_lowercase					( self					:  Any	):
						snake_case__			:				int       =  ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]	).to(
						    __A	)

						snake_case__			:				Tuple       =  self.default_image_processor
						snake_case__			:				Optional[int]       =  prepare_img()
						snake_case__			:				List[str]       =  image_processor(images=__A							,		return_tensors="pt"	).to(__A	)

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

						# verify the logits
						snake_case__			:				str       =  torch.Size((1, 1_0_0_0)	)
						self.assertEqual(outputs.logits.shape							,		__A	)

						snake_case__			:				List[Any]       =  torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9]	).to(__A	)

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






					@slow
					@require_accelerate
					def 	_lowercase					( self					:  Dict	):
						snake_case__			:				Optional[Any]       =  ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384"	)
						snake_case__			:				List[Any]       =  ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384"							,		device_map="auto"	)

						snake_case__			:				Tuple       =  prepare_img()

						snake_case__			:				List[Any]       =  image_processor(images=__A							,		return_tensors="pt"	)
						snake_case__			:				List[str]       =  model(**__A	)
						snake_case__			:				Optional[int]       =  outputs.logits
						# model predicts one of the 1000 ImageNet classes
						snake_case__			:				List[str]       =  logits.argmax(-1	).item()

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











import os
import unittest

from transformers.models.phobert.tokenization_phobert import VOCAB_FILES_NAMES, PhobertTokenizer

from ...test_tokenization_common import TokenizerTesterMixin



class 							SCREAMING_SNAKE_CASE__						(    UpperCamelCase_			,       unittest.TestCase						):





					"""simple docstring"""






					a_     =		PhobertTokenizer
					a_     =		False



					def 	_lowercase					( self					:  List[str]	):
						super().setUp()

						# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
						snake_case__			:				Optional[int]       =  ["T@@", "i", "I", "R@@", "r", "e@@"]
						snake_case__			:				int       =  dict(zip(__A							,		range(len(__A	)	)	)	)
						snake_case__			:				Dict       =  ["#version: 0.2", "l à</w>"]
						snake_case__			:				Optional[Any]       =  {"unk_token": "<unk>"}

						snake_case__			:				Tuple       =  os.path.join(self.tmpdirname							,		VOCAB_FILES_NAMES["vocab_file"]	)
						snake_case__			:				Union[str, Any]       =  os.path.join(self.tmpdirname							,		VOCAB_FILES_NAMES["merges_file"]	)

						with open(self.vocab_file							,		"w"							,		encoding="utf-8"	) as fp:
							for token in vocab_tokens:
								fp.write(f'''{token} {vocab_tokens[token]}\n'''	)
						with open(self.merges_file							,		"w"							,		encoding="utf-8"	) as fp:
							fp.write("\n".join(__A	)	)



					def 	_lowercase					( self					:  List[str]							,		**__A					:  Any	):
						kwargs.update(self.special_tokens_map	)
						return PhobertTokenizer.from_pretrained(self.tmpdirname							,		**__A	)



					def 	_lowercase					( self					:  Tuple							,		__A					:  List[Any]	):
						snake_case__			:				str       =  "Tôi là VinAI Research"
						snake_case__			:				int       =  "T<unk> i <unk> <unk> <unk> <unk> <unk> <unk> I Re<unk> e<unk> <unk> <unk> <unk>"
						return input_text, output_text






					def 	_lowercase					( self					:  Optional[int]	):
						snake_case__			:				int       =  PhobertTokenizer(self.vocab_file							,		self.merges_file							,		**self.special_tokens_map	)
						snake_case__			:				Tuple       =  "Tôi là VinAI Research"
						snake_case__			:				List[Any]       =  "T@@ ô@@ i l@@ à V@@ i@@ n@@ A@@ I R@@ e@@ s@@ e@@ a@@ r@@ c@@ h".split()
						snake_case__			:				int       =  tokenizer.tokenize(__A	)
						print(__A	)
						self.assertListEqual(__A							,		__A	)

						snake_case__			:				Any       =  tokens + [tokenizer.unk_token]

						snake_case__			:				Any       =  [4, 3, 5, 3, 3, 3, 3, 3, 3, 6, 7, 9, 3, 9, 3, 3, 3, 3, 3]
						self.assertListEqual(tokenizer.convert_tokens_to_ids(__A	)							,		__A	)



