


'''simple docstring'''
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   ,       UpperCamelCase_   ,       UpperCamelCase_=13   ,       UpperCamelCase_=64   ,       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_=[1, 16, 4, 4]   ,       UpperCamelCase_=None   ,       ):

							'''simple docstring'''


							UpperCamelCase__       :Union[str, Any]							 =			parent
							UpperCamelCase__       :Optional[int]							 =			batch_size
							UpperCamelCase__       :Tuple							 =			image_size
							UpperCamelCase__       :int							 =			patch_size
							UpperCamelCase__       :str							 =			num_channels
							UpperCamelCase__       :Any							 =			is_training
							UpperCamelCase__       :Dict							 =			use_labels
							UpperCamelCase__       :List[Any]							 =			hidden_size
							UpperCamelCase__       :Optional[Any]							 =			num_hidden_layers
							UpperCamelCase__       :Union[str, Any]							 =			num_attention_heads
							UpperCamelCase__       :Dict							 =			intermediate_size
							UpperCamelCase__       :Any							 =			hidden_act
							UpperCamelCase__       :List[Any]							 =			hidden_dropout_prob
							UpperCamelCase__       :Dict							 =			attention_probs_dropout_prob
							UpperCamelCase__       :Optional[int]							 =			type_sequence_label_size
							UpperCamelCase__       :Dict							 =			initializer_range
							UpperCamelCase__       :Optional[int]							 =			scope
							UpperCamelCase__       :Union[str, 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
							UpperCamelCase__       :str							 =			(self.image_size // 32) ** 2
							UpperCamelCase__       :Dict							 =			num_patches + 1




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Tuple							 =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

							UpperCamelCase__       :List[Any]							 =			None
							if self.use_labels:
												UpperCamelCase__       :List[Any]							 =			ids_tensor([self.batch_size]   ,       self.type_sequence_label_size							)

							UpperCamelCase__       :str							 =			self.get_config()

							return config, pixel_values, labels




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :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=UpperCamelCase_   ,       initializer_range=self.initializer_range   ,       backbone_featmap_shape=self.backbone_featmap_shape   ,       backbone_config=UpperCamelCase_   ,       )




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[int]							 =			ViTHybridModel(config=UpperCamelCase_							)
							model.to(UpperCamelCase_							)
							model.eval()
							UpperCamelCase__       :List[str]							 =			model(UpperCamelCase_							)
							self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.seq_length, self.hidden_size)							)




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__       :List[str]							 =			self.type_sequence_label_size
							UpperCamelCase__       :Dict							 =			ViTHybridForImageClassification(UpperCamelCase_							)
							model.to(UpperCamelCase_							)
							model.eval()
							UpperCamelCase__       :str							 =			model(UpperCamelCase_   ,       labels=UpperCamelCase_							)
							self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.type_sequence_label_size)							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[Any]							 =			self.prepare_config_and_inputs()
							UpperCamelCase__       :Optional[Any]							 =			config_and_inputs
							UpperCamelCase__       :List[str]							 =			{'''pixel_values''': pixel_values}
							return config, inputs_dict






@require_torch
class        lowercase     (							A__     ,		A__     ,		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    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Union[str, Any]							 =			ViTHybridModelTester(self							)
							UpperCamelCase__       :str							 =			ConfigTester(self   ,       config_class=UpperCamelCase_   ,       has_text_modality=UpperCamelCase_   ,       hidden_size=37							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							self.config_tester.run_common_tests()




		@unittest.skip(reason='''ViT does not use inputs_embeds'''							)
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							pass




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :int							 =			self.model_tester.prepare_config_and_inputs_for_common()

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




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :int							 =			self.model_tester.prepare_config_and_inputs_for_common()

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

												UpperCamelCase__       :int							 =			['''pixel_values''']
												self.assertListEqual(arg_names[:1]   ,       UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Tuple							 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :int							 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[int]							 =			self.model_tester.prepare_config_and_inputs_for_common()

							UpperCamelCase__       :Dict							 =			_config_zero_init(UpperCamelCase_							)
							for model_class in self.all_model_classes:
												UpperCamelCase__       :Optional[Any]							 =			model_class(config=UpperCamelCase_							)
												# Skip the check for the backbone
												for name, module in model.named_modules():
																	if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
																						UpperCamelCase__       :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    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												UpperCamelCase__       :List[str]							 =			ViTHybridModel.from_pretrained(UpperCamelCase_							)
												self.assertIsNotNone(UpperCamelCase_							)



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



					'''simple docstring'''






					UpperCamelCase__       :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    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							return (
							    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]							)
							    if is_vision_available()
							    else None
							)




		@slow
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Union[str, Any]							 =			ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]							).to(
							    UpperCamelCase_							)

							UpperCamelCase__       :Any							 =			self.default_image_processor
							UpperCamelCase__       :Tuple							 =			prepare_img()
							UpperCamelCase__       :List[Any]							 =			image_processor(images=UpperCamelCase_   ,       return_tensors='''pt'''							).to(UpperCamelCase_							)

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

							# verify the logits
							UpperCamelCase__       :Tuple							 =			torch.Size((1, 1000)							)
							self.assertEqual(outputs.logits.shape   ,       UpperCamelCase_							)

							UpperCamelCase__       :Union[str, Any]							 =			torch.tensor([-1.9090, -0.4993, -0.2389]							).to(UpperCamelCase_							)

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




		@slow
		@require_accelerate
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Any							 =			ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384'''							)
							UpperCamelCase__       :str							 =			ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384'''   ,       device_map='''auto'''							)

							UpperCamelCase__       :Tuple							 =			prepare_img()

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

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



'''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_distilbert import DistilBertTokenizer


__snake_case											=					logging.get_logger(__name__)

__snake_case											=					{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

__snake_case											=					{
    '''vocab_file''': {
        '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt''',
        '''distilbert-base-uncased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt'''
        ),
        '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt''',
        '''distilbert-base-cased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt'''
        ),
        '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt''',
        '''distilbert-base-multilingual-cased''': (
            '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json''',
        '''distilbert-base-uncased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json''',
        '''distilbert-base-cased-distilled-squad''': (
            '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-german-cased''': (
            '''https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json'''
        ),
        '''distilbert-base-multilingual-cased''': (
            '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json'''
        ),
    },
}

__snake_case											=					{
    '''distilbert-base-uncased''': 512,
    '''distilbert-base-uncased-distilled-squad''': 512,
    '''distilbert-base-cased''': 512,
    '''distilbert-base-cased-distilled-squad''': 512,
    '''distilbert-base-german-cased''': 512,
    '''distilbert-base-multilingual-cased''': 512,
}


__snake_case											=					{
    '''distilbert-base-uncased''': {'''do_lower_case''': True},
    '''distilbert-base-uncased-distilled-squad''': {'''do_lower_case''': True},
    '''distilbert-base-cased''': {'''do_lower_case''': False},
    '''distilbert-base-cased-distilled-squad''': {'''do_lower_case''': False},
    '''distilbert-base-german-cased''': {'''do_lower_case''': False},
    '''distilbert-base-multilingual-cased''': {'''do_lower_case''': False},
}
class        lowercase     (							A__ ):

		"""simple docstring"""

		_a   			=    VOCAB_FILES_NAMES
		_a   			=    PRETRAINED_VOCAB_FILES_MAP
		_a   			=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		_a   			=    PRETRAINED_INIT_CONFIGURATION
		_a   			=    ['input_ids', 'attention_mask']
		_a   			=    DistilBertTokenizer




		def __init__(		self   ,       UpperCamelCase_=None   ,       UpperCamelCase_=None   ,       UpperCamelCase_=True   ,       UpperCamelCase_="[UNK]"   ,       UpperCamelCase_="[SEP]"   ,       UpperCamelCase_="[PAD]"   ,       UpperCamelCase_="[CLS]"   ,       UpperCamelCase_="[MASK]"   ,       UpperCamelCase_=True   ,       UpperCamelCase_=None   ,       **UpperCamelCase_   ,       ):

							'''simple docstring'''


							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_   ,       )

							UpperCamelCase__       :int							 =			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
							):
												UpperCamelCase__       :int							 =			getattr(UpperCamelCase_   ,       normalizer_state.pop('''type'''							)							)
												UpperCamelCase__       :Optional[Any]							 =			do_lower_case
												UpperCamelCase__       :Optional[Any]							 =			strip_accents
												UpperCamelCase__       :List[Any]							 =			tokenize_chinese_chars
												UpperCamelCase__       :Any							 =			normalizer_class(**UpperCamelCase_							)

							UpperCamelCase__       :int							 =			do_lower_case




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_=None							):

							'''simple docstring'''


							UpperCamelCase__       :Tuple							 =			[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    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = None							):

							'''simple docstring'''


							UpperCamelCase__       :List[str]							 =			[self.sep_token_id]
							UpperCamelCase__       :List[str]							 =			[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    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = None							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			self._tokenizer.model.save(UpperCamelCase_   ,       name=UpperCamelCase_							)
							return tuple(UpperCamelCase_							)