






'''simple docstring'''

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 	SCREAMING_SNAKE_CASE	:




       def __init__(					self      : Optional[Any]	,    A__      : Dict	,    A__      : Dict=13	,    A__      : Optional[int]=30	,    A__      : Union[str, Any]=2	,    A__      : List[Any]=3	,    A__      : Tuple=True	,    A__      : Any=True	,    A__      : Optional[Any]=32	,    A__      : List[Any]=5	,    A__      : Optional[Any]=4	,    A__      : int=37	,    A__      : Dict="gelu"	,    A__      : List[Any]=0.1	,    A__      : Optional[Any]=0.1	,    A__      : Tuple=10	,    A__      : Optional[Any]=0.02	,    A__      : List[Any]=3	,    A__      : Any=None	,    A__      : Optional[Any]=2	,    ):


            """simple docstring"""







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

            # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
            __lowerCamelCase						:       int				   =				(image_size // patch_size) ** 2
            __lowerCamelCase						:       List[Any]				   =				num_patches + 2




       def 					a_					(					self      : List[str]					):


            """simple docstring"""







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

            __lowerCamelCase						:       Tuple				   =				None
            if self.use_labels:
                 __lowerCamelCase						:       int				   =				ids_tensor([self.batch_size]	,    self.type_sequence_label_size					)

            __lowerCamelCase						:       Union[str, Any]				   =				self.get_config()

            return config, pixel_values, labels




       def 					a_					(					self      : List[str]					):


            """simple docstring"""







            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=A__	,    initializer_range=self.initializer_range	,    encoder_stride=self.encoder_stride	,    )




       def 					a_					(					self      : Union[str, Any]	,    A__      : Optional[Any]	,    A__      : Dict	,    A__      : Union[str, Any]					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[int]				   =				DeiTModel(config=A__					)
            model.to(A__					)
            model.eval()
            __lowerCamelCase						:       str				   =				model(A__					)
            self.parent.assertEqual(result.last_hidden_state.shape	,    (self.batch_size, self.seq_length, self.hidden_size)					)




       def 					a_					(					self      : Union[str, Any]	,    A__      : Optional[Any]	,    A__      : Any	,    A__      : int					):


            """simple docstring"""







            __lowerCamelCase						:       List[str]				   =				DeiTForMaskedImageModeling(config=A__					)
            model.to(A__					)
            model.eval()
            __lowerCamelCase						:       Any				   =				model(A__					)
            self.parent.assertEqual(
                result.reconstruction.shape	,    (self.batch_size, self.num_channels, self.image_size, self.image_size)					)

            # test greyscale images
            __lowerCamelCase						:       Optional[int]				   =				1
            __lowerCamelCase						:       Dict				   =				DeiTForMaskedImageModeling(A__					)
            model.to(A__					)
            model.eval()

            __lowerCamelCase						:       Optional[Any]				   =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
            __lowerCamelCase						:       Dict				   =				model(A__					)
            self.parent.assertEqual(result.reconstruction.shape	,    (self.batch_size, 1, self.image_size, self.image_size)					)




       def 					a_					(					self      : str	,    A__      : int	,    A__      : str	,    A__      : Optional[Any]					):


            """simple docstring"""







            __lowerCamelCase						:       Union[str, Any]				   =				self.type_sequence_label_size
            __lowerCamelCase						:       List[Any]				   =				DeiTForImageClassification(A__					)
            model.to(A__					)
            model.eval()
            __lowerCamelCase						:       str				   =				model(A__	,    labels=A__					)
            self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.type_sequence_label_size)					)

            # test greyscale images
            __lowerCamelCase						:       str				   =				1
            __lowerCamelCase						:       int				   =				DeiTForImageClassification(A__					)
            model.to(A__					)
            model.eval()

            __lowerCamelCase						:       List[str]				   =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
            __lowerCamelCase						:       List[str]				   =				model(A__	,    labels=A__					)
            self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.type_sequence_label_size)					)




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


            """simple docstring"""







            __lowerCamelCase						:       Optional[Any]				   =				self.prepare_config_and_inputs()
            (
                (
                __lowerCamelCase
            )   ,      (
                __lowerCamelCase
            )   ,      (
                __lowerCamelCase
            )   ,      
            )						:       Tuple				   =				config_and_inputs
            __lowerCamelCase						:       int				   =				{"""pixel_values""": pixel_values}
            return config, inputs_dict







@require_torch
class 	SCREAMING_SNAKE_CASE	(      lowerCAmelCase_    ,   lowerCAmelCase_    ,   unittest.TestCase  ):
       snake_case__    :     Any   		=			(
           (
               DeiTModel,
               DeiTForImageClassification,
               DeiTForImageClassificationWithTeacher,
               DeiTForMaskedImageModeling,
           )
           if is_torch_available()
           else ()
       )
       snake_case__    :     Union[str, Any]   		=			(
           {
               'feature-extraction': DeiTModel,
               'image-classification': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
           }
           if is_torch_available()
           else {}
       )

       snake_case__    :     List[Any]   		=			False
       snake_case__    :     List[Any]   		=			False
       snake_case__    :     Dict   		=			False




       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       List[Any]				   =				DeiTModelTester(self					)
            __lowerCamelCase						:       List[Any]				   =				ConfigTester(self	,    config_class=A__	,    has_text_modality=A__	,    hidden_size=37					)




       def 					a_					(					self      : str					):


            """simple docstring"""







            self.config_tester.run_common_tests()




       @unittest.skip(reason="""DeiT does not use inputs_embeds"""					)
       def 					a_					(					self      : str					):


            """simple docstring"""







            pass




       def 					a_					(					self      : int					):


            """simple docstring"""







            __lowerCamelCase   ,      __lowerCamelCase						:       List[Any]				   =				self.model_tester.prepare_config_and_inputs_for_common()

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




       def 					a_					(					self      : str					):


            """simple docstring"""







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

            for model_class in self.all_model_classes:
                 __lowerCamelCase						:       Union[str, Any]				   =				model_class(A__					)
                 __lowerCamelCase						:       Any				   =				inspect.signature(model.forward					)
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 __lowerCamelCase						:       int				   =				[*signature.parameters.keys()]

                 __lowerCamelCase						:       int				   =				["""pixel_values"""]
                 self.assertListEqual(arg_names[:1]	,    A__					)




       def 					a_					(					self      : Optional[Any]					):


            """simple docstring"""







            __lowerCamelCase						:       Dict				   =				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*A__					)




       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       List[Any]				   =				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_image_modeling(*A__					)




       def 					a_					(					self      : List[Any]					):


            """simple docstring"""







            __lowerCamelCase						:       Dict				   =				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*A__					)




       def 					a_					(					self      : List[Any]	,    A__      : Optional[int]	,    A__      : str	,    A__      : str=False					):


            """simple docstring"""







            __lowerCamelCase						:       int				   =				super()._prepare_for_class(A__	,    A__	,    return_labels=A__					)

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

            return inputs_dict




       def 					a_					(					self      : Dict					):


            """simple docstring"""







            if not self.model_tester.is_training:
                 return

            __lowerCamelCase   ,      __lowerCamelCase						:       List[str]				   =				self.model_tester.prepare_config_and_inputs_for_common()
            __lowerCamelCase						:       Tuple				   =				True

            for model_class in self.all_model_classes:
                 # DeiTForImageClassificationWithTeacher supports inference-only
                 if (
                     model_class in get_values(A__					)
                     or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
                 ):
                      continue
                 __lowerCamelCase						:       Union[str, Any]				   =				model_class(A__					)
                 model.to(A__					)
                 model.train()
                 __lowerCamelCase						:       Union[str, Any]				   =				self._prepare_for_class(A__	,    A__	,    return_labels=A__					)
                 __lowerCamelCase						:       List[Any]				   =				model(**A__					).loss
                 loss.backward()




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


            """simple docstring"""







            __lowerCamelCase   ,      __lowerCamelCase						:       List[Any]				   =				self.model_tester.prepare_config_and_inputs_for_common()
            if not self.model_tester.is_training:
                 return

            __lowerCamelCase						:       Optional[Any]				   =				False
            __lowerCamelCase						:       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
                 # DeiTForImageClassificationWithTeacher supports inference-only
                 if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
                      continue
                 __lowerCamelCase						:       Tuple				   =				model_class(A__					)
                 model.gradient_checkpointing_enable()
                 model.to(A__					)
                 model.train()
                 __lowerCamelCase						:       Optional[Any]				   =				self._prepare_for_class(A__	,    A__	,    return_labels=A__					)
                 __lowerCamelCase						:       List[Any]				   =				model(**A__					).loss
                 loss.backward()




       def 					a_					(					self      : Dict					):


            """simple docstring"""







            __lowerCamelCase   ,      __lowerCamelCase						:       Tuple				   =				self.model_tester.prepare_config_and_inputs_for_common()

            __lowerCamelCase						:       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__					),
                         *get_values(A__					),
                     ]
                     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						:       Dict				   =				problem_type["""title"""]
                           __lowerCamelCase						:       Tuple				   =				problem_type["""num_labels"""]

                           __lowerCamelCase						:       Tuple				   =				model_class(A__					)
                           model.to(A__					)
                           model.train()

                           __lowerCamelCase						:       Tuple				   =				self._prepare_for_class(A__	,    A__	,    return_labels=A__					)

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

                           __lowerCamelCase						:       Dict				   =				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:
                                __lowerCamelCase						:       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 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







            for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 __lowerCamelCase						:       Tuple				   =				DeiTModel.from_pretrained(A__					)
                 self.assertIsNotNone(A__					)

def     __lowercase  ()   ->     Tuple:


     """simple docstring"""
     __lowerCamelCase						:       List[str]				   =				Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
     return image







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




       @cached_property
       def 					a_					(					self      : Optional[Any]					):


            """simple docstring"""







            return (
                DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""					)
                if is_vision_available()
                else None
            )




       @slow
       def 					a_					(					self      : Tuple					):


            """simple docstring"""







            __lowerCamelCase						:       int				   =				DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""					).to(
                A__					)

            __lowerCamelCase						:       Dict				   =				self.default_image_processor
            __lowerCamelCase						:       Optional[Any]				   =				prepare_img()
            __lowerCamelCase						:       int				   =				image_processor(images=A__	,    return_tensors="""pt"""					).to(A__					)

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

            # verify the logits
            __lowerCamelCase						:       Dict				   =				torch.Size((1, 1000)					)
            self.assertEqual(outputs.logits.shape	,    A__					)

            __lowerCamelCase						:       List[str]				   =				torch.tensor([-1.0266, 0.1912, -1.2861]					).to(A__					)

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




       @slow
       @require_accelerate
       @require_torch_gpu
       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       Union[str, Any]				   =				DeiTModel.from_pretrained(
                """facebook/deit-base-distilled-patch16-224"""	,    torch_dtype=torch.floataa	,    device_map="""auto"""					)
            __lowerCamelCase						:       int				   =				self.default_image_processor

            __lowerCamelCase						:       Union[str, Any]				   =				prepare_img()
            __lowerCamelCase						:       int				   =				image_processor(images=A__	,    return_tensors="""pt"""					)
            __lowerCamelCase						:       Union[str, Any]				   =				inputs.pixel_values.to(A__					)

            # forward pass to make sure inference works in fp16
            with torch.no_grad():
                 __lowerCamelCase						:       Optional[Any]				   =				model(A__					)











'''simple docstring'''

UpperCAmelCase__						:List[Any]          =			256
# Modulus to hash a string
UpperCAmelCase__						:str          =			1_000_003
def     __lowercase  (_lowercase,						_lowercase     )   ->     bool:


     """simple docstring"""
     __lowerCamelCase						:       str				   =				len(_lowercase     )
     __lowerCamelCase						:       List[str]				   =				len(_lowercase     )
     if p_len > t_len:
          return False

     __lowerCamelCase						:       Optional[Any]				   =				0
     __lowerCamelCase						:       Tuple				   =				0
     __lowerCamelCase						:       Dict				   =				1

     # Calculating the hash of pattern and substring of text
     for i in range(_lowercase     ):
          __lowerCamelCase						:       Optional[int]				   =				(ord(pattern[i]     ) + p_hash * alphabet_size) % modulus
          __lowerCamelCase						:       Dict				   =				(ord(text[i]     ) + text_hash * alphabet_size) % modulus
          if i == p_len - 1:
               continue
          __lowerCamelCase						:       Dict				   =				(modulus_power * alphabet_size) % modulus

     for i in range(0,						t_len - p_len + 1     ):
          if text_hash == p_hash and text[i : i + p_len] == pattern:
               return True
          if i == t_len - p_len:
               continue
          # Calculate the https://en.wikipedia.org/wiki/Rolling_hash
          __lowerCamelCase						:       Dict				   =				(
              (text_hash - ord(text[i]     ) * modulus_power) * alphabet_size
              + ord(text[i + p_len]     )
          ) % modulus
     return False
def     __lowercase  ()   ->     None:


     """simple docstring"""
     __lowerCamelCase						:       List[Any]				   =				"""abc1abc12"""
     __lowerCamelCase						:       Optional[Any]				   =				"""alskfjaldsabc1abc1abc12k23adsfabcabc"""
     __lowerCamelCase						:       List[Any]				   =				"""alskfjaldsk23adsfabcabc"""
     assert rabin_karp(_lowercase,						_lowercase     ) and not rabin_karp(_lowercase,						_lowercase     )

     # Test 2)
     __lowerCamelCase						:       Optional[int]				   =				"""ABABX"""
     __lowerCamelCase						:       Dict				   =				"""ABABZABABYABABX"""
     assert rabin_karp(_lowercase,						_lowercase     )

     # Test 3)
     __lowerCamelCase						:       Any				   =				"""AAAB"""
     __lowerCamelCase						:       int				   =				"""ABAAAAAB"""
     assert rabin_karp(_lowercase,						_lowercase     )

     # Test 4)
     __lowerCamelCase						:       Any				   =				"""abcdabcy"""
     __lowerCamelCase						:       Dict				   =				"""abcxabcdabxabcdabcdabcy"""
     assert rabin_karp(_lowercase,						_lowercase     )

     # Test 5)
     __lowerCamelCase						:       str				   =				"""Lü"""
     __lowerCamelCase						:       str				   =				"""Lüsai"""
     assert rabin_karp(_lowercase,						_lowercase     )
     __lowerCamelCase						:       Tuple				   =				"""Lue"""
     assert not rabin_karp(_lowercase,						_lowercase     )
     print("""Success."""     )


if __name__ == "__main__":
   test_rabin_karp()



