



import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
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 (
				    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
				    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
				    MODEL_MAPPING,
				    DeiTForImageClassification,
				    DeiTForImageClassificationWithTeacher,
				    DeiTForMaskedImageModeling,
				    DeiTModel,
				)
				from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import DeiTImageProcessor






class     __a			:



	def __init__(    self				:     List[str]					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Any=13					,						snake_case_				:     str=30					,						snake_case_				:     List[Any]=2					,						snake_case_				:     Any=3					,						snake_case_				:     Tuple=True					,						snake_case_				:     Union[str, Any]=True					,						snake_case_				:     List[str]=32					,						snake_case_				:     Optional[int]=5					,						snake_case_				:     Any=4					,						snake_case_				:     Union[str, Any]=37					,						snake_case_				:     Optional[int]="gelu"					,						snake_case_				:     int=0.1					,						snake_case_				:     Optional[Any]=0.1					,						snake_case_				:     List[str]=10					,						snake_case_				:     Union[str, Any]=0.0_2					,						snake_case_				:     List[str]=3					,						snake_case_				:     List[str]=None					,						snake_case_				:     List[str]=2					,						)->				Tuple:
						__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 DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
						__lowerCAmelCase						 =(image_size // patch_size) ** 2
						__lowerCAmelCase						 =num_patches + 2



	def 		UpperCamelCase				(    self				:     Tuple)->				Tuple:
						__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				:     Dict)->				List[str]:
						return DeiTConfig(
						    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=snake_case_					,						initializer_range=self.initializer_range					,						encoder_stride=self.encoder_stride					,						)



	def 		UpperCamelCase				(    self				:     Tuple					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     List[Any]					,						snake_case_				:     List[Any])->				Dict:
						__lowerCAmelCase						 =DeiTModel(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =model(snake_case_)
						self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.hidden_size))



	def 		UpperCamelCase				(    self				:     Dict					,						snake_case_				:     int					,						snake_case_				:     Any					,						snake_case_				:     Any)->				Dict:
						__lowerCAmelCase						 =DeiTForMaskedImageModeling(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =model(snake_case_)
						self.parent.assertEqual(
						    result.reconstruction.shape					,						(self.batch_size, self.num_channels, self.image_size, self.image_size))

						# test greyscale images
						__lowerCAmelCase						 =1
						__lowerCAmelCase						 =DeiTForMaskedImageModeling(snake_case_)
						model.to(snake_case_)
						model.eval()

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



	def 		UpperCamelCase				(    self				:     Tuple					,						snake_case_				:     Optional[int]					,						snake_case_				:     List[Any]					,						snake_case_				:     int)->				Tuple:
						__lowerCAmelCase						 =self.type_sequence_label_size
						__lowerCAmelCase						 =DeiTForImageClassification(snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =model(snake_case_					,						labels=snake_case_)
						self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.type_sequence_label_size))

						# test greyscale images
						__lowerCAmelCase						 =1
						__lowerCAmelCase						 =DeiTForImageClassification(snake_case_)
						model.to(snake_case_)
						model.eval()

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



	def 		UpperCamelCase				(    self				:     Optional[int])->				Any:
						__lowerCAmelCase						 =self.prepare_config_and_inputs()
						(
						    (
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						(
						    __lowerCAmelCase
						) ,						
						)						 =config_and_inputs
						__lowerCAmelCase						 ={"""pixel_values""": pixel_values}
						return config, inputs_dict






@require_torch
class     __a			(  SCREAMING_SNAKE_CASE						,     SCREAMING_SNAKE_CASE						,     unittest.TestCase					):
	SCREAMING_SNAKE_CASE  	=							(
	    (
	        DeiTModel,
	        DeiTForImageClassification,
	        DeiTForImageClassificationWithTeacher,
	        DeiTForMaskedImageModeling,
	    )
	    if is_torch_available()
	    else ()
	)
	SCREAMING_SNAKE_CASE  	=							(
	    {
	        "feature-extraction": DeiTModel,
	        "image-classification": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
	    }
	    if is_torch_available()
	    else {}
	)

	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False



	def 		UpperCamelCase				(    self				:     Any)->				Tuple:
						__lowerCAmelCase						 =DeiTModelTester(self)
						__lowerCAmelCase						 =ConfigTester(self					,						config_class=snake_case_					,						has_text_modality=snake_case_					,						hidden_size=37)



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



	@unittest.skip(reason="""DeiT does not use inputs_embeds""")
	def 		UpperCamelCase				(    self				:     Union[str, Any])->				str:
						pass



	def 		UpperCamelCase				(    self				:     List[str])->				Tuple:
						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()

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



	def 		UpperCamelCase				(    self				:     str)->				Dict:
						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()

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



	def 		UpperCamelCase				(    self				:     Dict)->				Optional[Any]:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*snake_case_)



	def 		UpperCamelCase				(    self				:     Any)->				Any:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*snake_case_)



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



	def 		UpperCamelCase				(    self				:     Optional[Any]					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Optional[int]					,						snake_case_				:     Tuple=False)->				List[Any]:
						__lowerCAmelCase						 =super()._prepare_for_class(snake_case_					,						snake_case_					,						return_labels=snake_case_)

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

						return inputs_dict



	def 		UpperCamelCase				(    self				:     List[str])->				int:
						if not self.model_tester.is_training:
											return

						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()
						__lowerCAmelCase						 =True

						for model_class in self.all_model_classes:
											# DeiTForImageClassificationWithTeacher supports inference-only
											if (
											    model_class in get_values(snake_case_)
											    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
											):
																continue
											__lowerCAmelCase						 =model_class(snake_case_)
											model.to(snake_case_)
											model.train()
											__lowerCAmelCase						 =self._prepare_for_class(snake_case_					,						snake_case_					,						return_labels=snake_case_)
											__lowerCAmelCase						 =model(**snake_case_).loss
											loss.backward()



	def 		UpperCamelCase				(    self				:     Any)->				str:
						__lowerCAmelCase ,						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs_for_common()
						if not self.model_tester.is_training:
											return

						__lowerCAmelCase						 =False
						__lowerCAmelCase						 =True

						for model_class in self.all_model_classes:
											if model_class in get_values(snake_case_) or not model_class.supports_gradient_checkpointing:
																continue
											# DeiTForImageClassificationWithTeacher supports inference-only
											if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
																continue
											__lowerCAmelCase						 =model_class(snake_case_)
											model.gradient_checkpointing_enable()
											model.to(snake_case_)
											model.train()
											__lowerCAmelCase						 =self._prepare_for_class(snake_case_					,						snake_case_					,						return_labels=snake_case_)
											__lowerCAmelCase						 =model(**snake_case_).loss
											loss.backward()



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

						__lowerCAmelCase						 =[
						    {"""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(snake_case_),
											        *get_values(snake_case_),
											    ]
											    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
											):
																continue

											for problem_type in problem_types:
																with self.subTest(msg=F"""Testing {model_class} with {problem_type['title']}"""):
																					__lowerCAmelCase						 =problem_type["""title"""]
																					__lowerCAmelCase						 =problem_type["""num_labels"""]

																					__lowerCAmelCase						 =model_class(snake_case_)
																					model.to(snake_case_)
																					model.train()

																					__lowerCAmelCase						 =self._prepare_for_class(snake_case_					,						snake_case_					,						return_labels=snake_case_)

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

																					__lowerCAmelCase						 =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=snake_case_) as warning_list:
																										__lowerCAmelCase						 =model(**snake_case_).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 		UpperCamelCase				(    self				:     Tuple)->				Optional[int]:
						for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCAmelCase						 =DeiTModel.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)






def        __lowerCAmelCase		(	)	->    List[Any]:
					__lowerCAmelCase						 =Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
					return image






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



	@cached_property
	def 		UpperCamelCase				(    self				:     Union[str, Any])->				Union[str, Any]:
						return (
						    DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224""")
						    if is_vision_available()
						    else None
						)



	@slow
	def 		UpperCamelCase				(    self				:     Tuple)->				List[Any]:
						__lowerCAmelCase						 =DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224""").to(
						    snake_case_)

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

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

						# verify the logits
						__lowerCAmelCase						 =torch.Size((1, 10_00))
						self.assertEqual(outputs.logits.shape					,						snake_case_)

						__lowerCAmelCase						 =torch.tensor([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1]).to(snake_case_)

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



	@slow
	@require_accelerate
	@require_torch_gpu
	def 		UpperCamelCase				(    self				:     Dict)->				Tuple:
						__lowerCAmelCase						 =DeiTModel.from_pretrained(
						    """facebook/deit-base-distilled-patch16-224"""					,						torch_dtype=torch.floataa					,						device_map="""auto""")
						__lowerCAmelCase						 =self.default_image_processor

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

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









from __future__ import annotations






class     __a			:



	def __init__(    self				:     List[Any]					,						snake_case_				:     str					,						snake_case_				:     str)->				Optional[int]:
						__lowerCAmelCase ,						__lowerCAmelCase						 =text, pattern
						__lowerCAmelCase ,						__lowerCAmelCase						 =len(snake_case_), len(snake_case_)



	def 		UpperCamelCase				(    self				:     List[Any]					,						snake_case_				:     str)->				int:

						for i in range(self.patLen - 1					,						-1					,						-1):
											if char == self.pattern[i]:
																return i
						return -1



	def 		UpperCamelCase				(    self				:     List[str]					,						snake_case_				:     int)->				int:

						for i in range(self.patLen - 1					,						-1					,						-1):
											if self.pattern[i] != self.text[current_pos + i]:
																return current_pos + i
						return -1



	def 		UpperCamelCase				(    self				:     Dict)->				list[int]:
						# searches pattern in text and returns index positions
						__lowerCAmelCase						 =[]
						for i in range(self.textLen - self.patLen + 1):
											__lowerCAmelCase						 =self.mismatch_in_text(snake_case_)
											if mismatch_index == -1:
																positions.append(snake_case_)
											else:
																__lowerCAmelCase						 =self.match_in_pattern(self.text[mismatch_index])
																__lowerCAmelCase						 =(
																    mismatch_index - match_index
																)  # shifting index lgtm [py/multiple-definition]
						return positions


lowercase_													=      '''ABAABA'''
lowercase_													=      '''AB'''
lowercase_													=      BoyerMooreSearch(text, pattern)
lowercase_													=      bms.bad_character_heuristic()

if len(positions) == 0:
				print('''No match found''')
else:
				print('''Pattern found in following positions: ''')
				print(positions)




