"""simple docstring"""







import os
from typing import List, Optional, Union

from ...tokenization_utils import PreTrainedTokenizer
from ...tokenization_utils_base import AddedToken
from ...utils import logging


_a  :  List[str]=				logging.get_logger(__name__)

_a  :  Tuple=				{"vocab_file": "vocab.txt"}

_a  :  str=				{
    "vocab_file": {
        "facebook/esm2_t6_8M_UR50D": "https://huggingface.co/facebook/esm2_t6_8M_UR50D/resolve/main/vocab.txt",
        "facebook/esm2_t12_35M_UR50D": "https://huggingface.co/facebook/esm2_t12_35M_UR50D/resolve/main/vocab.txt",
    },
}

_a  :  int=				{
    "facebook/esm2_t6_8M_UR50D": 1_024,
    "facebook/esm2_t12_35M_UR50D": 1_024,
}
def 						__UpperCAmelCase       (					UpperCAmelCase_					:       List[Any]						)							->    Optional[int]:

	'''simple docstring'''





	with open(UpperCAmelCase_    ,							'r'						) as f:
		__snake_case    :							Optional[Any]   	=				f.read().splitlines()
		return [l.strip() for l in lines]

class 	UpperCamelCase    (					lowercase		):
				UpperCAmelCase       :   Any					=	VOCAB_FILES_NAMES
				UpperCAmelCase       :   List[Any]					=	PRETRAINED_VOCAB_FILES_MAP
				UpperCAmelCase       :   Tuple					=	PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				UpperCAmelCase       :   int					=	["""input_ids""", """attention_mask"""]



				def __init__(self       :   Optional[Any]			,						_A       :   Optional[Any]			,						_A       :   List[str]="<unk>"			,						_A       :   Union[str, Any]="<cls>"			,						_A       :   str="<pad>"			,						_A       :   List[str]="<mask>"			,						_A       :   Optional[int]="<eos>"			,						**_A       :   List[str]			,						)					->			Any:
					super().__init__(**_A)
					__snake_case    :							Union[str, Any]   	=				load_vocab_file(_A)
					__snake_case    :							Tuple   	=				dict(enumerate(self.all_tokens))
					__snake_case    :							Dict   	=				{tok: ind for ind, tok in enumerate(self.all_tokens)}
					__snake_case    :							Tuple   	=				unk_token
					__snake_case    :							str   	=				cls_token
					__snake_case    :							Union[str, Any]   	=				pad_token
					__snake_case    :							str   	=				mask_token
					__snake_case    :							Union[str, Any]   	=				eos_token
					__snake_case    :							Union[str, Any]   	=				self.all_tokens
					self._create_trie(self.unique_no_split_tokens)



				def       _lowercase  (self       :   Optional[Any]			,						_A       :   int)					->			str:
					return self._id_to_token.get(_A			,						self.unk_token)



				def       _lowercase  (self       :   List[str]			,						_A       :   str)					->			int:
					return self._token_to_id.get(_A			,						self._token_to_id.get(self.unk_token))



				def       _lowercase  (self       :   Tuple			,						_A       :   Tuple			,						**_A       :   Union[str, Any])					->			Optional[int]:
					return text.split()



				def       _lowercase  (self       :   str			,						_A       :   str=False)					->			Any:
					return len(self._id_to_token)



				def       _lowercase  (self       :   Dict)					->			Optional[Any]:
					return {token: i for i, token in enumerate(self.all_tokens)}



				def       _lowercase  (self       :   str			,						_A       :   str)					->			int:
					return self._token_to_id.get(_A			,						self._token_to_id.get(self.unk_token))



				def       _lowercase  (self       :   str			,						_A       :   int)					->			str:
					return self._id_to_token.get(_A			,						self.unk_token)



				def       _lowercase  (self       :   Tuple			,						_A       :   List[int]			,						_A       :   Optional[List[int]] = None)					->			List[int]:
					__snake_case    :							Union[str, Any]   	=				[self.cls_token_id]
					__snake_case    :							int   	=				[self.eos_token_id]  # No sep token in ESM vocabulary
					if token_ids_a is None:
						if self.eos_token_id is None:
							return cls + token_ids_a
						else:
							return cls + token_ids_a + sep
					elif self.eos_token_id is None:
						raise ValueError('Cannot tokenize multiple sequences when EOS token is not set!')
					return cls + token_ids_a + sep + token_ids_a + sep  # Multiple inputs always have an EOS token



				def       _lowercase  (self       :   Any			,						_A       :   List			,						_A       :   Optional[List] = None			,						_A       :   bool = False)					->			List[int]:
					if already_has_special_tokens:
						if token_ids_a is not None:
							raise ValueError(
							    'You should not supply a second sequence if the provided sequence of '
							    'ids is already formatted with special tokens for the model.')

						return [1 if token in self.all_special_ids else 0 for token in token_ids_a]
					__snake_case    :							Any   	=				[1] + ([0] * len(_A)) + [1]
					if token_ids_a is not None:
						mask += [0] * len(_A) + [1]
					return mask



				def       _lowercase  (self       :   List[str]			,						_A       :   List[Any]			,						_A       :   Optional[Any])					->			List[str]:
					__snake_case    :							str   	=				os.path.join(_A			,						(filename_prefix + '-' if filename_prefix else '') + 'vocab.txt')
					with open(_A			,						'w') as f:
						f.write('\n'.join(self.all_tokens))
					return (vocab_file,)



				@property
				def       _lowercase  (self       :   str)					->			int:
					return self.get_vocab_size(with_added_tokens=_A)






				def       _lowercase  (self       :   Optional[Any]			,						_A       :   Union[List[str], List[AddedToken]]			,						_A       :   bool = False)					->			int:
					return super()._add_tokens(_A			,						special_tokens=_A)





"""simple docstring"""







import inspect
import unittest
import warnings
from math import ceil, floor

from transformers import LevitConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 transformers import (
	    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
	    MODEL_MAPPING,
	    LevitForImageClassification,
	    LevitForImageClassificationWithTeacher,
	    LevitModel,
	)
	from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import LevitImageProcessor

class 	UpperCamelCase    (					lowercase		):



				def       _lowercase  (self       :   List[str])					->			Dict:
					__snake_case    :							Any   	=				self.config_class(**self.inputs_dict)
					self.parent.assertTrue(hasattr(_A			,						'hidden_sizes'))
					self.parent.assertTrue(hasattr(_A			,						'num_attention_heads'))







class 	UpperCamelCase    :



				def __init__(self       :   str			,						_A       :   Dict			,						_A       :   Dict=13			,						_A       :   int=64			,						_A       :   Tuple=3			,						_A       :   List[str]=3			,						_A       :   Tuple=2			,						_A       :   int=1			,						_A       :   Optional[int]=16			,						_A       :   Tuple=[1_28, 2_56, 3_84]			,						_A       :   List[Any]=[4, 6, 8]			,						_A       :   int=[2, 3, 4]			,						_A       :   Union[str, Any]=[16, 16, 16]			,						_A       :   List[Any]=0			,						_A       :   Optional[int]=[2, 2, 2]			,						_A       :   List[Any]=[2, 2, 2]			,						_A       :   Dict=0.02			,						_A       :   str=True			,						_A       :   List[str]=True			,						_A       :   Tuple=2			,						)					->			Tuple:
					__snake_case    :							Optional[int]   	=				parent
					__snake_case    :							int   	=				batch_size
					__snake_case    :							Tuple   	=				image_size
					__snake_case    :							Dict   	=				num_channels
					__snake_case    :							Optional[int]   	=				kernel_size
					__snake_case    :							Union[str, Any]   	=				stride
					__snake_case    :							Dict   	=				padding
					__snake_case    :							Optional[int]   	=				hidden_sizes
					__snake_case    :							Any   	=				num_attention_heads
					__snake_case    :							Optional[Any]   	=				depths
					__snake_case    :							Dict   	=				key_dim
					__snake_case    :							Dict   	=				drop_path_rate
					__snake_case    :							Optional[int]   	=				patch_size
					__snake_case    :							List[Any]   	=				attention_ratio
					__snake_case    :							List[str]   	=				mlp_ratio
					__snake_case    :							str   	=				initializer_range
					__snake_case    :							str   	=				[
					    ['Subsample', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
					    ['Subsample', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
					]
					__snake_case    :							List[str]   	=				is_training
					__snake_case    :							Dict   	=				use_labels
					__snake_case    :							Optional[int]   	=				num_labels
					__snake_case    :							Optional[int]   	=				initializer_range



				def       _lowercase  (self       :   Optional[Any])					->			Dict:
					__snake_case    :							List[Any]   	=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

					__snake_case    :							Tuple   	=				None
					if self.use_labels:
						__snake_case    :							Tuple   	=				ids_tensor([self.batch_size]			,						self.num_labels)

					__snake_case    :							Tuple   	=				self.get_config()
					return config, pixel_values, labels



				def       _lowercase  (self       :   List[str])					->			Optional[Any]:
					return LevitConfig(
					    image_size=self.image_size			,						num_channels=self.num_channels			,						kernel_size=self.kernel_size			,						stride=self.stride			,						padding=self.padding			,						patch_size=self.patch_size			,						hidden_sizes=self.hidden_sizes			,						num_attention_heads=self.num_attention_heads			,						depths=self.depths			,						key_dim=self.key_dim			,						drop_path_rate=self.drop_path_rate			,						mlp_ratio=self.mlp_ratio			,						attention_ratio=self.attention_ratio			,						initializer_range=self.initializer_range			,						down_ops=self.down_ops			,						)



				def       _lowercase  (self       :   List[Any]			,						_A       :   int			,						_A       :   Optional[Any]			,						_A       :   List[str])					->			str:
					__snake_case    :							Tuple   	=				LevitModel(config=_A)
					model.to(_A)
					model.eval()
					__snake_case    :							List[str]   	=				model(_A)
					__snake_case    :							List[Any]   	=				(self.image_size, self.image_size)
					__snake_case	,       __snake_case    :							List[Any]   	=				image_size[0], image_size[1]
					for _ in range(4):
						__snake_case    :							str   	=				floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1)
						__snake_case    :							Optional[int]   	=				floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1)
					self.parent.assertEqual(
					    result.last_hidden_state.shape			,						(self.batch_size, ceil(height / 4) * ceil(width / 4), self.hidden_sizes[-1])			,						)



				def       _lowercase  (self       :   str			,						_A       :   Optional[Any]			,						_A       :   List[str]			,						_A       :   int)					->			Optional[Any]:
					__snake_case    :							int   	=				self.num_labels
					__snake_case    :							Any   	=				LevitForImageClassification(_A)
					model.to(_A)
					model.eval()
					__snake_case    :							Tuple   	=				model(_A			,						labels=_A)
					self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_labels))






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







@require_torch
class 	UpperCamelCase    (					lowercase   ,			lowercase   ,			unittest.TestCase		):
				UpperCAmelCase       :   Union[str, Any]					=	(
				    (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher)
				    if is_torch_available()
				    else ()
				)
				UpperCAmelCase       :   Optional[Any]					=	(
				    {
				        """feature-extraction""": LevitModel,
				        """image-classification""": (LevitForImageClassification, LevitForImageClassificationWithTeacher),
				    }
				    if is_torch_available()
				    else {}
				)

				UpperCAmelCase       :   str					=	False
				UpperCAmelCase       :   Optional[Any]					=	False
				UpperCAmelCase       :   Optional[int]					=	False
				UpperCAmelCase       :   Any					=	False
				UpperCAmelCase       :   str					=	False



				def       _lowercase  (self       :   Any)					->			List[Any]:
					__snake_case    :							Any   	=				LevitModelTester(self)
					__snake_case    :							List[Any]   	=				ConfigTester(self			,						config_class=_A			,						has_text_modality=_A			,						hidden_size=37)



				def       _lowercase  (self       :   int)					->			int:
					self.create_and_test_config_common_properties()
					self.config_tester.create_and_test_config_to_json_string()
					self.config_tester.create_and_test_config_to_json_file()
					self.config_tester.create_and_test_config_from_and_save_pretrained()
					self.config_tester.create_and_test_config_with_num_labels()
					self.config_tester.check_config_can_be_init_without_params()
					self.config_tester.check_config_arguments_init()



				def       _lowercase  (self       :   Dict)					->			Tuple:
					return



				@unittest.skip(reason='Levit does not use inputs_embeds')
				def       _lowercase  (self       :   int)					->			Dict:
					pass



				@unittest.skip(reason='Levit does not support input and output embeddings')
				def       _lowercase  (self       :   Tuple)					->			Optional[int]:
					pass



				@unittest.skip(reason='Levit does not output attentions')
				def       _lowercase  (self       :   List[str])					->			Dict:
					pass



				def       _lowercase  (self       :   str)					->			List[str]:
					__snake_case	,       __snake_case    :							Optional[int]   	=				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    :							Tuple   	=				[*signature.parameters.keys()]

						__snake_case    :							str   	=				['pixel_values']
						self.assertListEqual(arg_names[:1]			,						_A)



				def       _lowercase  (self       :   Dict)					->			Optional[int]:
					def check_hidden_states_output(_A       :   Any			,						_A       :   List[Any]			,						_A       :   int):
						__snake_case    :							int   	=				model_class(_A)
						model.to(_A)
						model.eval()

						with torch.no_grad():
							__snake_case    :							Optional[Any]   	=				model(**self._prepare_for_class(_A			,						_A))

						__snake_case    :							Union[str, Any]   	=				outputs.hidden_states

						__snake_case    :							List[str]   	=				len(self.model_tester.depths) + 1
						self.assertEqual(len(_A)			,						_A)

						__snake_case    :							Any   	=				(self.model_tester.image_size, self.model_tester.image_size)
						__snake_case	,       __snake_case    :							str   	=				image_size[0], image_size[1]
						for _ in range(4):
							__snake_case    :							Tuple   	=				floor(
							    (
							        (height + 2 * self.model_tester.padding - self.model_tester.kernel_size)
							        / self.model_tester.stride
							    )
							    + 1)
							__snake_case    :							List[str]   	=				floor(
							    (
							        (width + 2 * self.model_tester.padding - self.model_tester.kernel_size)
							        / self.model_tester.stride
							    )
							    + 1)
						# verify the first hidden states (first block)
						self.assertListEqual(
						    list(hidden_states[0].shape[-2:])			,						[
						        height * width,
						        self.model_tester.hidden_sizes[0],
						    ]			,						)

					__snake_case	,       __snake_case    :							str   	=				self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						__snake_case    :							Optional[Any]   	=				True
						check_hidden_states_output(_A			,						_A			,						_A)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						__snake_case    :							List[Any]   	=				True

						check_hidden_states_output(_A			,						_A			,						_A)



				@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
				def       _lowercase  (self       :   List[Any])					->			Optional[int]:
					pass



				def       _lowercase  (self       :   List[Any]			,						_A       :   Optional[Any]			,						_A       :   List[Any]			,						_A       :   Dict=False)					->			Tuple:
					__snake_case    :							Any   	=				super()._prepare_for_class(_A			,						_A			,						return_labels=_A)

					if return_labels:
						if model_class.__name__ == "LevitForImageClassificationWithTeacher":
							del inputs_dict["labels"]

					return inputs_dict



				def       _lowercase  (self       :   Union[str, Any])					->			Any:
					__snake_case    :							Optional[int]   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_A)



				def       _lowercase  (self       :   int)					->			Optional[Any]:
					__snake_case    :							int   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*_A)



				def       _lowercase  (self       :   Dict)					->			List[Any]:
					if not self.model_tester.is_training:
						return

					__snake_case	,       __snake_case    :							Union[str, Any]   	=				self.model_tester.prepare_config_and_inputs_for_common()
					__snake_case    :							Union[str, Any]   	=				True

					for model_class in self.all_model_classes:
						# LevitForImageClassificationWithTeacher supports inference-only
						if (
						    model_class in get_values(_A)
						    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
						):
							continue
						__snake_case    :							Any   	=				model_class(_A)
						model.to(_A)
						model.train()
						__snake_case    :							Optional[Any]   	=				self._prepare_for_class(_A			,						_A			,						return_labels=_A)
						__snake_case    :							Dict   	=				model(**_A).loss
						loss.backward()



				def       _lowercase  (self       :   Union[str, Any])					->			int:
					__snake_case	,       __snake_case    :							Dict   	=				self.model_tester.prepare_config_and_inputs_for_common()
					if not self.model_tester.is_training:
						return

					__snake_case    :							str   	=				False
					__snake_case    :							List[str]   	=				True

					for model_class in self.all_model_classes:
						if model_class in get_values(_A) or not model_class.supports_gradient_checkpointing:
							continue
						# LevitForImageClassificationWithTeacher supports inference-only
						if model_class.__name__ == "LevitForImageClassificationWithTeacher":
							continue
						__snake_case    :							Optional[int]   	=				model_class(_A)
						model.gradient_checkpointing_enable()
						model.to(_A)
						model.train()
						__snake_case    :							Optional[int]   	=				self._prepare_for_class(_A			,						_A			,						return_labels=_A)
						__snake_case    :							Optional[int]   	=				model(**_A).loss
						loss.backward()



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

					__snake_case    :							List[Any]   	=				[
					    {'title': 'multi_label_classification', 'num_labels': 2, 'dtype': torch.float},
					    {'title': 'single_label_classification', 'num_labels': 1, 'dtype': torch.long},
					    {'title': 'regression', 'num_labels': 1, 'dtype': torch.float},
					]

					for model_class in self.all_model_classes:
						if (
						    model_class
						    not in [
						        *get_values(_A),
						    ]
						    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
						):
							continue

						for problem_type in problem_types:
							with self.subTest(msg=f"Testing {model_class} with {problem_type['title']}"):
								__snake_case    :							int   	=				problem_type['title']
								__snake_case    :							List[Any]   	=				problem_type['num_labels']

								__snake_case    :							List[str]   	=				model_class(_A)
								model.to(_A)
								model.train()

								__snake_case    :							Optional[Any]   	=				self._prepare_for_class(_A			,						_A			,						return_labels=_A)

								if problem_type["num_labels"] > 1:
									__snake_case    :							Dict   	=				inputs['labels'].unsqueeze(1).repeat(1			,						problem_type['num_labels'])

								__snake_case    :							List[Any]   	=				inputs['labels'].to(problem_type['dtype'])

								# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
								# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
								# they have the same size." which is a symptom something in wrong for the regression problem.
								# See https://github.com/huggingface/transformers/issues/11780
								with warnings.catch_warnings(record=_A) as warning_list:
									__snake_case    :							Optional[Any]   	=				model(**_A).loss
								for w in warning_list:
									if "Using a target size that is different to the input size" in str(w.message):
										raise ValueError(
										    f"Something is going wrong in the regression problem: intercepted {w.message}")

								loss.backward()






				@slow
				def       _lowercase  (self       :   Dict)					->			Tuple:
					for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case    :							List[str]   	=				LevitModel.from_pretrained(_A)
						self.assertIsNotNone(_A)







def 						__UpperCAmelCase       (					)							->    List[str]:

	'''simple docstring'''





	__snake_case    :							Dict   	=				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
	return image

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



				@cached_property
				def       _lowercase  (self       :   Tuple)					->			Union[str, Any]:
					return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0])






				@slow
				def       _lowercase  (self       :   Optional[int])					->			str:
					__snake_case    :							int   	=				LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(
					    _A)

					__snake_case    :							Any   	=				self.default_image_processor
					__snake_case    :							Optional[Any]   	=				prepare_img()
					__snake_case    :							Union[str, Any]   	=				image_processor(images=_A			,						return_tensors='pt').to(_A)

					# forward pass
					with torch.no_grad():
						__snake_case    :							List[str]   	=				model(**_A)

					# verify the logits
					__snake_case    :							List[str]   	=				torch.Size((1, 10_00))
					self.assertEqual(outputs.logits.shape			,						_A)

					__snake_case    :							List[str]   	=				torch.tensor([1.0_448, -0.3_745, -1.8_317]).to(_A)

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





