



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



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase=13			,				UpperCamelCase=30			,				UpperCamelCase=2			,				UpperCamelCase=3			,				UpperCamelCase=True			,				UpperCamelCase=True			,				UpperCamelCase=32			,				UpperCamelCase=5			,				UpperCamelCase=4			,				UpperCamelCase=37			,				UpperCamelCase="gelu"			,				UpperCamelCase=0.1			,				UpperCamelCase=0.1			,				UpperCamelCase=10			,				UpperCamelCase=0.02			,				UpperCamelCase=None			,				UpperCamelCase=2			,				)     ->			List[str]:
						__lowerCAmelCase												=  parent
						__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												=  encoder_stride

						# in ViT, 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     )     ->			Any:
						__lowerCAmelCase												=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

						__lowerCAmelCase												=  None
						if self.use_labels:
							__lowerCAmelCase												=  ids_tensor([self.batch_size]			,				self.type_sequence_label_size     )

						__lowerCAmelCase												=  self.get_config()

						return config, pixel_values, labels



					def        UpperCAmelCase_ (    self     )     ->			List[str]:
						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=UpperCamelCase			,				initializer_range=self.initializer_range			,				encoder_stride=self.encoder_stride			,				)



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			List[str]:
						__lowerCAmelCase												=  ViTModel(config=UpperCamelCase     )
						model.to(UpperCamelCase     )
						model.eval()
						__lowerCAmelCase												=  model(UpperCamelCase     )
						self.parent.assertEqual(result.last_hidden_state.shape			,				(self.batch_size, self.seq_length, self.hidden_size)     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			Optional[int]:
						__lowerCAmelCase												=  ViTForMaskedImageModeling(config=UpperCamelCase     )
						model.to(UpperCamelCase     )
						model.eval()
						__lowerCAmelCase												=  model(UpperCamelCase     )
						self.parent.assertEqual(
						    result.reconstruction.shape			,				(self.batch_size, self.num_channels, self.image_size, self.image_size)     )

						# test greyscale images
						__lowerCAmelCase												=  1
						__lowerCAmelCase												=  ViTForMaskedImageModeling(UpperCamelCase     )
						model.to(UpperCamelCase     )
						model.eval()

						__lowerCAmelCase												=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
						__lowerCAmelCase												=  model(UpperCamelCase     )
						self.parent.assertEqual(result.reconstruction.shape			,				(self.batch_size, 1, self.image_size, self.image_size)     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  self.type_sequence_label_size
						__lowerCAmelCase												=  ViTForImageClassification(UpperCamelCase     )
						model.to(UpperCamelCase     )
						model.eval()
						__lowerCAmelCase												=  model(UpperCamelCase			,				labels=UpperCamelCase     )
						self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.type_sequence_label_size)     )

						# test greyscale images
						__lowerCAmelCase												=  1
						__lowerCAmelCase												=  ViTForImageClassification(UpperCamelCase     )
						model.to(UpperCamelCase     )
						model.eval()

						__lowerCAmelCase												=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
						__lowerCAmelCase												=  model(UpperCamelCase     )
						self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.type_sequence_label_size)     )



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






@require_torch
class 					UpperCAmelCase__	(		UpperCamelCase__							,       UpperCamelCase__							,       unittest.TestCase ):
					a      :     int        =							(
					    (
					        ViTModel,
					        ViTForImageClassification,
					        ViTForMaskedImageModeling,
					    )
					    if is_torch_available()
					    else ()
					)
					a      :     str        =							(
					    {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
					    if is_torch_available()
					    else {}
					)
					a      :     Optional[int]        =							True

					a      :     Optional[Any]        =							False
					a      :     List[Any]        =							False
					a      :     str        =							False



					def        UpperCAmelCase_ (    self     )     ->			Optional[Any]:
						__lowerCAmelCase												=  ViTModelTester(self     )
						__lowerCAmelCase												=  ConfigTester(self			,				config_class=UpperCamelCase			,				has_text_modality=UpperCamelCase			,				hidden_size=37     )



					def        UpperCAmelCase_ (    self     )     ->			Optional[int]:
						self.config_tester.run_common_tests()



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



					def        UpperCAmelCase_ (    self     )     ->			Any:
						__lowerCAmelCase							,       __lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs_for_common()

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



					def        UpperCAmelCase_ (    self     )     ->			Tuple:
						__lowerCAmelCase							,       __lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							__lowerCAmelCase												=  model_class(UpperCamelCase     )
							__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]			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			Tuple:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			Optional[Any]:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase     )



					def        UpperCAmelCase_ (    self     )     ->			List[Any]:
						__lowerCAmelCase												=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*UpperCamelCase     )



					@slow
					def        UpperCAmelCase_ (    self     )     ->			str:
						for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__lowerCAmelCase												=  ViTModel.from_pretrained(UpperCamelCase     )
							self.assertIsNotNone(UpperCamelCase     )



def   __lowerCAmelCase					(							):






	'''simple docstring'''




	__lowerCAmelCase												=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
	return image






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



					@cached_property
					def        UpperCAmelCase_ (    self     )     ->			Any:
						return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"     ) if is_vision_available() else None



					@slow
					def        UpperCAmelCase_ (    self     )     ->			List[str]:
						__lowerCAmelCase												=  ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"     ).to(UpperCamelCase     )

						__lowerCAmelCase												=  self.default_image_processor
						__lowerCAmelCase												=  prepare_img()
						__lowerCAmelCase												=  image_processor(images=UpperCamelCase			,				return_tensors="pt"     ).to(UpperCamelCase     )

						# forward pass
						with torch.no_grad():
							__lowerCAmelCase												=  model(**UpperCamelCase     )

						# verify the logits
						__lowerCAmelCase												=  torch.Size((1, 1000)     )
						self.assertEqual(outputs.logits.shape			,				UpperCamelCase     )

						__lowerCAmelCase												=  torch.tensor([-0.27_44, 0.82_15, -0.08_36]     ).to(UpperCamelCase     )

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



					@slow
					def        UpperCAmelCase_ (    self     )     ->			Any:
						# ViT models have an `interpolate_pos_encoding` argument in their forward method,
						# allowing to interpolate the pre-trained position embeddings in order to use
						# the model on higher resolutions. The DINO model by Facebook AI leverages this
						# to visualize self-attention on higher resolution images.
						__lowerCAmelCase												=  ViTModel.from_pretrained("facebook/dino-vits8"     ).to(UpperCamelCase     )

						__lowerCAmelCase												=  ViTImageProcessor.from_pretrained("facebook/dino-vits8"			,				size=480     )
						__lowerCAmelCase												=  prepare_img()
						__lowerCAmelCase												=  image_processor(images=UpperCamelCase			,				return_tensors="pt"     )
						__lowerCAmelCase												=  inputs.pixel_values.to(UpperCamelCase     )

						# forward pass
						with torch.no_grad():
							__lowerCAmelCase												=  model(UpperCamelCase			,				interpolate_pos_encoding=UpperCamelCase     )

						# verify the logits
						__lowerCAmelCase												=  torch.Size((1, 3601, 384)     )
						self.assertEqual(outputs.last_hidden_state.shape			,				UpperCamelCase     )

						__lowerCAmelCase												=  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(UpperCamelCase     )

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



					@slow
					@require_accelerate
					@require_torch_gpu
					def        UpperCAmelCase_ (    self     )     ->			List[str]:
						__lowerCAmelCase												=  ViTModel.from_pretrained("facebook/dino-vits8"			,				torch_dtype=torch.floataa			,				device_map="auto"     )
						__lowerCAmelCase												=  self.default_image_processor

						__lowerCAmelCase												=  prepare_img()
						__lowerCAmelCase												=  image_processor(images=UpperCamelCase			,				return_tensors="pt"     )
						__lowerCAmelCase												=  inputs.pixel_values.to(UpperCamelCase     )

						# forward pass to make sure inference works in fp16
						with torch.no_grad():
							__lowerCAmelCase												=  model(UpperCamelCase     )




'''simple docstring'''



import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_squeezebert import SqueezeBertTokenizer


lowerCAmelCase	:		Optional[Any]				=   logging.get_logger(__name__)

lowerCAmelCase	:		Optional[Any]				=   {'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

lowerCAmelCase	:		str				=   {
    '''vocab_file''': {
        '''squeezebert/squeezebert-uncased''': (
            '''https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/vocab.txt'''
        ),
        '''squeezebert/squeezebert-mnli''': '''https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/vocab.txt''',
        '''squeezebert/squeezebert-mnli-headless''': (
            '''https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/vocab.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''squeezebert/squeezebert-uncased''': (
            '''https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/tokenizer.json'''
        ),
        '''squeezebert/squeezebert-mnli''': (
            '''https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/tokenizer.json'''
        ),
        '''squeezebert/squeezebert-mnli-headless''': (
            '''https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/tokenizer.json'''
        ),
    },
}

lowerCAmelCase	:		Optional[Any]				=   {
    '''squeezebert/squeezebert-uncased''': 5_1_2,
    '''squeezebert/squeezebert-mnli''': 5_1_2,
    '''squeezebert/squeezebert-mnli-headless''': 5_1_2,
}


lowerCAmelCase	:		Tuple				=   {
    '''squeezebert/squeezebert-uncased''': {'''do_lower_case''': True},
    '''squeezebert/squeezebert-mnli''': {'''do_lower_case''': True},
    '''squeezebert/squeezebert-mnli-headless''': {'''do_lower_case''': True},
}






class 					UpperCAmelCase__	(		UpperCamelCase__ ):
					a      :     Dict        =							VOCAB_FILES_NAMES
					a      :     Any        =							PRETRAINED_VOCAB_FILES_MAP
					a      :     Dict        =							PRETRAINED_INIT_CONFIGURATION
					a      :     str        =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					a      :     Optional[Any]        =							SqueezeBertTokenizer



					def __init__(    self			,				UpperCamelCase=None			,				UpperCamelCase=None			,				UpperCamelCase=True			,				UpperCamelCase="[UNK]"			,				UpperCamelCase="[SEP]"			,				UpperCamelCase="[PAD]"			,				UpperCamelCase="[CLS]"			,				UpperCamelCase="[MASK]"			,				UpperCamelCase=True			,				UpperCamelCase=None			,				**UpperCamelCase			,				)     ->			List[Any]:
						super().__init__(
						    UpperCamelCase			,				tokenizer_file=UpperCamelCase			,				do_lower_case=UpperCamelCase			,				unk_token=UpperCamelCase			,				sep_token=UpperCamelCase			,				pad_token=UpperCamelCase			,				cls_token=UpperCamelCase			,				mask_token=UpperCamelCase			,				tokenize_chinese_chars=UpperCamelCase			,				strip_accents=UpperCamelCase			,				**UpperCamelCase			,				)

						__lowerCAmelCase												=  json.loads(self.backend_tokenizer.normalizer.__getstate__()     )
						if (
						    normalizer_state.get("lowercase"			,				UpperCamelCase     ) != do_lower_case
						    or normalizer_state.get("strip_accents"			,				UpperCamelCase     ) != strip_accents
						    or normalizer_state.get("handle_chinese_chars"			,				UpperCamelCase     ) != tokenize_chinese_chars
						):
							__lowerCAmelCase												=  getattr(UpperCamelCase			,				normalizer_state.pop("type"     )     )
							__lowerCAmelCase												=  do_lower_case
							__lowerCAmelCase												=  strip_accents
							__lowerCAmelCase												=  tokenize_chinese_chars
							__lowerCAmelCase												=  normalizer_class(**UpperCamelCase     )

						__lowerCAmelCase												=  do_lower_case



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase=None     )     ->			str:
						__lowerCAmelCase												=  [self.cls_token_id] + token_ids_a + [self.sep_token_id]

						if token_ids_a:
							output += token_ids_a + [self.sep_token_id]

						return output



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase = None     )     ->			List[int]:
						__lowerCAmelCase												=  [self.sep_token_id]
						__lowerCAmelCase												=  [self.cls_token_id]
						if token_ids_a is None:
							return len(cls + token_ids_a + sep     ) * [0]
						return len(cls + token_ids_a + sep     ) * [0] + len(token_ids_a + sep     ) * [1]



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase = None     )     ->			Tuple[str]:
						__lowerCAmelCase												=  self._tokenizer.model.save(UpperCamelCase			,				name=UpperCamelCase     )
						return tuple(UpperCamelCase     )