

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




  """simple docstring"""




  def __init__(   self     :  Tuple      ,lowercase__     :  Union[str, Any]      ,lowercase__     :  str=1_3      ,lowercase__     :  Optional[int]=3_0      ,lowercase__     :  List[Any]=2      ,lowercase__     :  Optional[int]=3      ,lowercase__     :  int=True      ,lowercase__     :  List[Any]=True      ,lowercase__     :  int=3_2      ,lowercase__     :  Optional[Any]=5      ,lowercase__     :  str=4      ,lowercase__     :  Tuple=3_7      ,lowercase__     :  Dict="gelu"      ,lowercase__     :  str=0.1      ,lowercase__     :  str=0.1      ,lowercase__     :  Optional[Any]=1_0      ,lowercase__     :  Tuple=0.0_2      ,lowercase__     :  Optional[int]=3      ,lowercase__     :  Optional[int]=None      ,lowercase__     :  Tuple=2      ,):
   __lowercase													=				parent
   __lowercase													=				batch_size
   __lowercase													=				image_size
   __lowercase													=				patch_size
   __lowercase													=				num_channels
   __lowercase													=				is_training
   __lowercase													=				use_labels
   __lowercase													=				hidden_size
   __lowercase													=				num_hidden_layers
   __lowercase													=				num_attention_heads
   __lowercase													=				intermediate_size
   __lowercase													=				hidden_act
   __lowercase													=				hidden_dropout_prob
   __lowercase													=				attention_probs_dropout_prob
   __lowercase													=				type_sequence_label_size
   __lowercase													=				initializer_range
   __lowercase													=				scope
   __lowercase													=				encoder_stride

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






  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   __lowercase													=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

   __lowercase													=				self.get_config()

   return config, pixel_values, labels






  def       SCREAMING_SNAKE_CASE   (   self     :  Union[str, Any]    ):
   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=lowercase__      ,initializer_range=self.initializer_range      ,encoder_stride=self.encoder_stride      ,)






  def       SCREAMING_SNAKE_CASE   (   self     :  Dict      ,lowercase__     :  Tuple      ,lowercase__     :  Dict      ,lowercase__     :  Union[str, Any]    ):
   __lowercase													=				DeiTModel(config=lowercase__    )
   model.to(lowercase__    )
   model.eval()
   __lowercase													=				model(lowercase__    )
   self.parent.assertEqual(result.last_hidden_state.shape      ,(self.batch_size, self.seq_length, self.hidden_size)    )






  def       SCREAMING_SNAKE_CASE   (   self     :  Union[str, Any]      ,lowercase__     :  Any      ,lowercase__     :  Union[str, Any]      ,lowercase__     :  int    ):
   __lowercase													=				DeiTForMaskedImageModeling(config=lowercase__    )
   model.to(lowercase__    )
   model.eval()
   __lowercase													=				model(lowercase__    )
   self.parent.assertEqual(
       result.reconstruction.shape      ,(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

   # test greyscale images
   __lowercase													=				1
   __lowercase													=				DeiTForMaskedImageModeling(lowercase__    )
   model.to(lowercase__    )
   model.eval()

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






  def       SCREAMING_SNAKE_CASE   (   self     :  List[Any]      ,lowercase__     :  Tuple      ,lowercase__     :  Union[str, Any]      ,lowercase__     :  str    ):
   __lowercase													=				self.type_sequence_label_size
   __lowercase													=				DeiTForImageClassification(lowercase__    )
   model.to(lowercase__    )
   model.eval()
   __lowercase													=				model(lowercase__      ,labels=lowercase__    )
   self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.type_sequence_label_size)    )

   # test greyscale images
   __lowercase													=				1
   __lowercase													=				DeiTForImageClassification(lowercase__    )
   model.to(lowercase__    )
   model.eval()

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






  def       SCREAMING_SNAKE_CASE   (   self     :  Any    ):
   __lowercase													=				self.prepare_config_and_inputs()
   (
       (
       __lowercase
   )						,			(
       __lowercase
   )						,			(
       __lowercase
   )						,			
   )													=				config_and_inputs
   __lowercase													=				{'''pixel_values''': pixel_values}
   return config, inputs_dict






@require_torch
class     lowercase_			(lowerCamelCase__       , lowerCamelCase__       , unittest.TestCase   ):




  """simple docstring"""




  SCREAMING_SNAKE_CASE					:						List[Any]           =    (
      (
          DeiTModel,
          DeiTForImageClassification,
          DeiTForImageClassificationWithTeacher,
          DeiTForMaskedImageModeling,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE					:						Any           =    (
      {
          'feature-extraction': DeiTModel,
          'image-classification': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
      }
      if is_torch_available()
      else {}
  )

  SCREAMING_SNAKE_CASE					:						Dict           =    False
  SCREAMING_SNAKE_CASE					:						int           =    False
  SCREAMING_SNAKE_CASE					:						Union[str, Any]           =    False






  def       SCREAMING_SNAKE_CASE   (   self     :  Tuple    ):
   __lowercase													=				DeiTModelTester(self    )
   __lowercase													=				ConfigTester(self      ,config_class=lowercase__      ,has_text_modality=lowercase__      ,hidden_size=3_7    )






  def       SCREAMING_SNAKE_CASE   (   self     :  int    ):
   self.config_tester.run_common_tests()






  @unittest.skip(reason='''DeiT does not use inputs_embeds'''    )
  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   pass






  def       SCREAMING_SNAKE_CASE   (   self     :  Dict    ):
   __lowercase						,			__lowercase													=				self.model_tester.prepare_config_and_inputs_for_common()

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






  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[Any]    ):
   __lowercase						,			__lowercase													=				self.model_tester.prepare_config_and_inputs_for_common()

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

    __lowercase													=				['''pixel_values''']
    self.assertListEqual(arg_names[:1]      ,lowercase__    )






  def       SCREAMING_SNAKE_CASE   (   self     :  Union[str, Any]    ):
   __lowercase													=				self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*lowercase__    )






  def       SCREAMING_SNAKE_CASE   (   self     :  Optional[int]    ):
   __lowercase													=				self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_image_modeling(*lowercase__    )






  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   __lowercase													=				self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*lowercase__    )






  def       SCREAMING_SNAKE_CASE   (   self     :  Any      ,lowercase__     :  Union[str, Any]      ,lowercase__     :  int      ,lowercase__     :  Optional[Any]=False    ):
   __lowercase													=				super()._prepare_for_class(lowercase__      ,lowercase__      ,return_labels=lowercase__    )

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

   return inputs_dict






  def       SCREAMING_SNAKE_CASE   (   self     :  List[str]    ):
   if not self.model_tester.is_training:
    return

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

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






  def       SCREAMING_SNAKE_CASE   (   self     :  str    ):
   __lowercase						,			__lowercase													=				self.model_tester.prepare_config_and_inputs_for_common()
   if not self.model_tester.is_training:
    return

   __lowercase													=				False
   __lowercase													=				True

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






  def       SCREAMING_SNAKE_CASE   (   self     :  List[Any]    ):
   __lowercase						,			__lowercase													=				self.model_tester.prepare_config_and_inputs_for_common()

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

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

      __lowercase													=				model_class(lowercase__    )
      model.to(lowercase__    )
      model.train()

      __lowercase													=				self._prepare_for_class(lowercase__      ,lowercase__      ,return_labels=lowercase__    )

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

      __lowercase													=				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=lowercase__    ) as warning_list:
       __lowercase													=				model(**lowercase__    ).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       SCREAMING_SNAKE_CASE   (   self     :  Any    ):
   for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    __lowercase													=				DeiTModel.from_pretrained(lowercase__    )
    self.assertIsNotNone(lowercase__    )


def 	_A  (					):






 """simple docstring"""



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






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




  """simple docstring"""




  @cached_property
  def       SCREAMING_SNAKE_CASE   (   self     :  Tuple    ):
   return (
       DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224'''    )
       if is_vision_available()
       else None
   )






  @slow
  def       SCREAMING_SNAKE_CASE   (   self     :  Dict    ):
   __lowercase													=				DeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''    ).to(
       lowercase__    )

   __lowercase													=				self.default_image_processor
   __lowercase													=				prepare_img()
   __lowercase													=				image_processor(images=lowercase__      ,return_tensors='''pt'''    ).to(lowercase__    )

   # forward pass
   with torch.no_grad():
    __lowercase													=				model(**lowercase__    )

   # verify the logits
   __lowercase													=				torch.Size((1, 1_0_0_0)    )
   self.assertEqual(outputs.logits.shape      ,lowercase__    )

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

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






  @slow
  @require_accelerate
  @require_torch_gpu
  def       SCREAMING_SNAKE_CASE   (   self     :  Union[str, Any]    ):
   __lowercase													=				DeiTModel.from_pretrained(
       '''facebook/deit-base-distilled-patch16-224'''      ,torch_dtype=torch.floataa      ,device_map='''auto'''    )
   __lowercase													=				self.default_image_processor

   __lowercase													=				prepare_img()
   __lowercase													=				image_processor(images=lowercase__      ,return_tensors='''pt'''    )
   __lowercase													=				inputs.pixel_values.to(lowercase__    )

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




'''simple docstring'''






import re


def 	_A  (					A__   ):






 """simple docstring"""



 __lowercase													=				re.compile(
     R'''^(?:0|94|\+94|0{2}94)''' R'''7(0|1|2|4|5|6|7|8)''' R'''(-| |)''' R'''\d{7}$'''   )

 return bool(re.search(A__  ,       A__   )   )


if __name__ == "__main__":
 lowerCAmelCase__      		=			'''0094702343221'''

 print(is_sri_lankan_phone_number(phone))

