



'''simple docstring'''




import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch
						from torch import nn

						from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
						from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import ViTImageProcessor






class 						_UpperCamelCase  :


			'''simple docstring'''

			def __init__(							self     : List[Any]							,     lowerCAmelCase__     : Optional[Any]							,     lowerCAmelCase__     : Optional[Any]=1_3							,     lowerCAmelCase__     : Optional[int]=3_0							,     lowerCAmelCase__     : Union[str, Any]=2							,     lowerCAmelCase__     : Dict=3							,     lowerCAmelCase__     : List[Any]=True							,     lowerCAmelCase__     : Optional[Any]=True							,     lowerCAmelCase__     : int=3_2							,     lowerCAmelCase__     : Union[str, Any]=5							,     lowerCAmelCase__     : Dict=4							,     lowerCAmelCase__     : List[str]=3_7							,     lowerCAmelCase__     : List[str]="gelu"							,     lowerCAmelCase__     : List[str]=0.1							,     lowerCAmelCase__     : Any=0.1							,     lowerCAmelCase__     : List[str]=1_0							,     lowerCAmelCase__     : Any=0.02							,     lowerCAmelCase__     : List[str]=None							,     lowerCAmelCase__     : str=2							,     ):





					"""simple docstring"""






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

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

			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		List[str]           =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

					__SCREAMING_SNAKE_CASE       :		str           =		self.get_config()

					return config, pixel_values, labels

			def UpperCamelCase__					(							self     : int						):





					"""simple docstring"""






					return ViTConfig(
					    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=lowercase_							,     initializer_range=self.initializer_range							,     encoder_stride=self.encoder_stride							,     )

			def UpperCamelCase__					(							self     : Optional[int]							,     lowerCAmelCase__     : str							,     lowerCAmelCase__     : List[str]							,     lowerCAmelCase__     : Union[str, Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Tuple           =		ViTModel(config=lowercase_						)
					model.to(lowercase_						)
					model.eval()
					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		model(lowercase_						)
					self.parent.assertEqual(result.last_hidden_state.shape							,     (self.batch_size, self.seq_length, self.hidden_size)						)

			def UpperCamelCase__					(							self     : List[str]							,     lowerCAmelCase__     : str							,     lowerCAmelCase__     : Optional[Any]							,     lowerCAmelCase__     : Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		int           =		ViTForMaskedImageModeling(config=lowercase_						)
					model.to(lowercase_						)
					model.eval()
					__SCREAMING_SNAKE_CASE       :		Tuple           =		model(lowercase_						)
					self.parent.assertEqual(
					    result.reconstruction.shape							,     (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

					# test greyscale images
					__SCREAMING_SNAKE_CASE       :		List[str]           =		1
					__SCREAMING_SNAKE_CASE       :		List[str]           =		ViTForMaskedImageModeling(lowercase_						)
					model.to(lowercase_						)
					model.eval()

					__SCREAMING_SNAKE_CASE       :		str           =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
					__SCREAMING_SNAKE_CASE       :		str           =		model(lowercase_						)
					self.parent.assertEqual(result.reconstruction.shape							,     (self.batch_size, 1, self.image_size, self.image_size)						)

			def UpperCamelCase__					(							self     : str							,     lowerCAmelCase__     : Dict							,     lowerCAmelCase__     : Dict							,     lowerCAmelCase__     : List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Dict           =		self.type_sequence_label_size
					__SCREAMING_SNAKE_CASE       :		Any           =		ViTForImageClassification(lowercase_						)
					model.to(lowercase_						)
					model.eval()
					__SCREAMING_SNAKE_CASE       :		int           =		model(lowercase_							,     labels=lowercase_						)
					self.parent.assertEqual(result.logits.shape							,     (self.batch_size, self.type_sequence_label_size)						)

					# test greyscale images
					__SCREAMING_SNAKE_CASE       :		int           =		1
					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		ViTForImageClassification(lowercase_						)
					model.to(lowercase_						)
					model.eval()

					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
					__SCREAMING_SNAKE_CASE       :		str           =		model(lowercase_						)
					self.parent.assertEqual(result.logits.shape							,     (self.batch_size, self.type_sequence_label_size)						)



			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Dict           =		self.prepare_config_and_inputs()
					(
					    __SCREAMING_SNAKE_CASE						
					)       :		Union[str, Any]           =		config_and_inputs
					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		{"""pixel_values""": pixel_values}
					return config, inputs_dict






@require_torch
class 						_UpperCamelCase  (							_UpperCAmelCase    ,					_UpperCAmelCase    ,					unittest.TestCase    ):


			'''simple docstring'''

			_A      :		Dict			    =		(
			    (
			        ViTModel,
			        ViTForImageClassification,
			        ViTForMaskedImageModeling,
			    )
			    if is_torch_available()
			    else ()
			)
			_A      :		Tuple			    =		(
			    {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
			    if is_torch_available()
			    else {}
			)
			_A      :		Any			    =		True

			_A      :		int			    =		False
			_A      :		Union[str, Any]			    =		False
			_A      :		Dict			    =		False

			def UpperCamelCase__					(							self     : Any						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Any           =		ViTModelTester(self						)
					__SCREAMING_SNAKE_CASE       :		str           =		ConfigTester(self							,     config_class=lowercase_							,     has_text_modality=lowercase_							,     hidden_size=3_7						)

			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					self.config_tester.run_common_tests()

			@unittest.skip(reason="""ViT does not use inputs_embeds"""						)
			def UpperCamelCase__					(							self     : Optional[Any]						):





					"""simple docstring"""






					pass

			def UpperCamelCase__					(							self     : List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Union[str, 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(lowercase_						)
							self.assertIsInstance(model.get_input_embeddings()							,     (nn.Module)						)
							__SCREAMING_SNAKE_CASE       :		Dict           =		model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(lowercase_							,     nn.Linear						)						)

			def UpperCamelCase__					(							self     : Tuple						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Tuple           =		self.model_tester.prepare_config_and_inputs_for_common()

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

							__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		["""pixel_values"""]
							self.assertListEqual(arg_names[:1]							,     lowercase_						)

			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowercase_						)

			def UpperCamelCase__					(							self     : Union[str, Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Any           =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*lowercase_						)

			def UpperCamelCase__					(							self     : Optional[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		List[Any]           =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*lowercase_						)



			@slow
			def UpperCamelCase__					(							self     : Union[str, Any]						):





					"""simple docstring"""






					for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__SCREAMING_SNAKE_CASE       :		List[Any]           =		ViTModel.from_pretrained(lowercase_						)
							self.assertIsNotNone(lowercase_						)


def 				lowerCAmelCase_   (	):
		__SCREAMING_SNAKE_CASE       :		Optional[int]           =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
		return image






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


			'''simple docstring'''

			@cached_property
			def UpperCamelCase__					(							self     : Any						):





					"""simple docstring"""






					return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""						) if is_vision_available() else None

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





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Any           =		ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""						).to(lowercase_						)

					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		self.default_image_processor
					__SCREAMING_SNAKE_CASE       :		List[Any]           =		prepare_img()
					__SCREAMING_SNAKE_CASE       :		str           =		image_processor(images=lowercase_							,     return_tensors="""pt"""						).to(lowercase_						)

					# forward pass
					with torch.no_grad():
							__SCREAMING_SNAKE_CASE       :		int           =		model(**lowercase_						)

					# verify the logits
					__SCREAMING_SNAKE_CASE       :		List[Any]           =		torch.Size((1, 1_0_0_0)						)
					self.assertEqual(outputs.logits.shape							,     lowercase_						)

					__SCREAMING_SNAKE_CASE       :		List[str]           =		torch.tensor([-0.27_44, 0.82_15, -0.08_36]						).to(lowercase_						)

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

			@slow
			def UpperCamelCase__					(							self     : str						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		ViTModel.from_pretrained("""facebook/dino-vits8"""						).to(lowercase_						)

					__SCREAMING_SNAKE_CASE       :		str           =		ViTImageProcessor.from_pretrained("""facebook/dino-vits8"""							,     size=4_8_0						)
					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		prepare_img()
					__SCREAMING_SNAKE_CASE       :		int           =		image_processor(images=lowercase_							,     return_tensors="""pt"""						)
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		inputs.pixel_values.to(lowercase_						)

					# forward pass
					with torch.no_grad():
							__SCREAMING_SNAKE_CASE       :		Any           =		model(lowercase_							,     interpolate_pos_encoding=lowercase_						)

					# verify the logits
					__SCREAMING_SNAKE_CASE       :		List[Any]           =		torch.Size((1, 3_6_0_1, 3_8_4)						)
					self.assertEqual(outputs.last_hidden_state.shape							,     lowercase_						)

					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		torch.tensor(
					    [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]						).to(lowercase_						)

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



			@slow
			@require_accelerate
			@require_torch_gpu
			def UpperCamelCase__					(							self     : Dict						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Any           =		ViTModel.from_pretrained("""facebook/dino-vits8"""							,     torch_dtype=torch.floataa							,     device_map="""auto"""						)
					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		self.default_image_processor

					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		prepare_img()
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		image_processor(images=lowercase_							,     return_tensors="""pt"""						)
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		inputs.pixel_values.to(lowercase_						)

					# forward pass to make sure inference works in fp16
					with torch.no_grad():
							__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		model(lowercase_						)
'''simple docstring'''




from collections import defaultdict




def 							lowerCamelCase	(      UpperCAmelCase__       :					str      ,      UpperCAmelCase__       :					str )			->      bool:
							lowercase_					:			Tuple										=       first_str.lower().strip()
							lowercase_					:			List[Any]										=       second_str.lower().strip()

							# Remove whitespace
							lowercase_					:			Dict										=       first_str.replace(""" """      ,      """""" )
							lowercase_					:			Tuple										=       second_str.replace(""" """      ,      """""" )

							# Strings of different lengths are not anagrams
							if len(UpperCAmelCase__ ) != len(UpperCAmelCase__ ):
														return False

							# Default values for count should be 0
							lowercase_					:			defaultdict[str, int]										=       defaultdict(UpperCAmelCase__ )

							# For each character in input strings,
							# increment count in the corresponding
							for i in range(len(UpperCAmelCase__ ) ):
														count[first_str[i]] += 1
														count[second_str[i]] -= 1

							return all(_count == 0 for _count in count.values() )


if __name__ == "__main__":
						from doctest import testmod

						testmod()
						_lowercase						: int						=						input("Enter the first string ").strip()
						_lowercase						: Tuple						=						input("Enter the second string ").strip()

						_lowercase						: Union[str, Any]						=						check_anagrams(input_a, input_b)
						print(f"""{input_a} and {input_b} are {"" if status else "not "}anagrams.""")





