






'''simple docstring'''
from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import (
      TFDeiTForImageClassification,
      TFDeiTForImageClassificationWithTeacher,
      TFDeiTForMaskedImageModeling,
      TFDeiTModel,
  )
  from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import DeiTImageProcessor






class        __SCREAMING_SNAKE_CASE :




 def __init__( self		:							List[Any]   , __lowercase		:							List[Any]   , __lowercase		:							Optional[int]=13   , __lowercase		:							Optional[int]=30   , __lowercase		:							Tuple=2   , __lowercase		:							str=3   , __lowercase		:							List[Any]=True   , __lowercase		:							Tuple=True   , __lowercase		:							List[str]=32   , __lowercase		:							List[Any]=2   , __lowercase		:							str=4   , __lowercase		:							Optional[int]=37   , __lowercase		:							Tuple="gelu"   , __lowercase		:							List[str]=0.1   , __lowercase		:							Union[str, Any]=0.1   , __lowercase		:							Dict=10   , __lowercase		:							Optional[int]=0.02   , __lowercase		:							List[str]=3   , __lowercase		:							Optional[Any]=None   , __lowercase		:							str=2   , )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			List[Any]													=parent
       SCREAMING_SNAKE_CASE__       :			str													=batch_size
       SCREAMING_SNAKE_CASE__       :			Dict													=image_size
       SCREAMING_SNAKE_CASE__       :			int													=patch_size
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=num_channels
       SCREAMING_SNAKE_CASE__       :			Any													=is_training
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=use_labels
       SCREAMING_SNAKE_CASE__       :			List[str]													=hidden_size
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=num_hidden_layers
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=num_attention_heads
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=intermediate_size
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=hidden_act
       SCREAMING_SNAKE_CASE__       :			Any													=hidden_dropout_prob
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__       :			Tuple													=type_sequence_label_size
       SCREAMING_SNAKE_CASE__       :			Dict													=initializer_range
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=scope
       SCREAMING_SNAKE_CASE__       :			Dict													=encoder_stride

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




 def        __magic_name__		( self		:							Union[str, Any] )		->  Any:
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

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

       return config, pixel_values, labels




 def        __magic_name__		( self		:							Optional[int] )		->  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=__lowercase   , initializer_range=self.initializer_range   , encoder_stride=self.encoder_stride   , )




 def        __magic_name__		( self		:							List[str]   , __lowercase		:							List[str]   , __lowercase		:							Dict   , __lowercase		:							Optional[Any] )		->  Tuple:
       SCREAMING_SNAKE_CASE__       :			List[str]													=TFDeiTModel(config=__lowercase )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=model(__lowercase )
       self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.hidden_size) )




 def        __magic_name__		( self		:							int   , __lowercase		:							List[str]   , __lowercase		:							List[Any]   , __lowercase		:							Any )		->  Optional[Any]:
       SCREAMING_SNAKE_CASE__       :			int													=TFDeiTForMaskedImageModeling(config=__lowercase )
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=model(__lowercase )
       self.parent.assertEqual(
           result.reconstruction.shape   , (self.batch_size, self.num_channels, self.image_size, self.image_size) )

       # test greyscale images
       SCREAMING_SNAKE_CASE__       :			Tuple													=1
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=TFDeiTForMaskedImageModeling(__lowercase )

       SCREAMING_SNAKE_CASE__       :			List[Any]													=floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
       SCREAMING_SNAKE_CASE__       :			str													=model(__lowercase )
       self.parent.assertEqual(result.reconstruction.shape   , (self.batch_size, 1, self.image_size, self.image_size) )




 def        __magic_name__		( self		:							Optional[int]   , __lowercase		:							str   , __lowercase		:							Union[str, Any]   , __lowercase		:							Optional[int] )		->  Dict:
       SCREAMING_SNAKE_CASE__       :			int													=self.type_sequence_label_size
       SCREAMING_SNAKE_CASE__       :			str													=TFDeiTForImageClassification(__lowercase )
       SCREAMING_SNAKE_CASE__       :			Dict													=model(__lowercase   , labels=__lowercase )
       self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.type_sequence_label_size) )

       # test greyscale images
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=1
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=TFDeiTForImageClassification(__lowercase )

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




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






@require_tf
class        __SCREAMING_SNAKE_CASE (     lowerCamelCase		,			lowerCamelCase		,			unittest.TestCase			):
 snake_case_												=							(
     (
         TFDeiTModel,
         TFDeiTForImageClassification,
         TFDeiTForImageClassificationWithTeacher,
         TFDeiTForMaskedImageModeling,
     )
     if is_tf_available()
     else ()
 )
 snake_case_												=							(
     {
         """feature-extraction""": TFDeiTModel,
         """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
     }
     if is_tf_available()
     else {}
 )

 snake_case_												=							False
 snake_case_												=							False
 snake_case_												=							False
 snake_case_												=							False




 def        __magic_name__		( self		:							Tuple )		->  str:
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=TFDeiTModelTester(self )
       SCREAMING_SNAKE_CASE__       :			Any													=ConfigTester(self   , config_class=__lowercase   , has_text_modality=__lowercase   , hidden_size=37 )




 def        __magic_name__		( self		:							Dict )		->  int:
       self.config_tester.run_common_tests()




 @unittest.skip(reason='''DeiT does not use inputs_embeds''' )
 def        __magic_name__		( self		:							Dict )		->  Tuple:
       pass




 def        __magic_name__		( self		:							List[Any] )		->  List[str]:
       SCREAMING_SNAKE_CASE__       :			List[str]													=self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__       :			Tuple													=model_class(__lowercase )
             self.assertIsInstance(model.get_input_embeddings()   , (tf.keras.layers.Layer) )
             SCREAMING_SNAKE_CASE__       :			List[Any]													=model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(__lowercase   , tf.keras.layers.Dense ) )




 def        __magic_name__		( self		:							Optional[Any] )		->  Any:
       SCREAMING_SNAKE_CASE__       :			Dict													=self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=model_class(__lowercase )
             SCREAMING_SNAKE_CASE__       :			List[str]													=inspect.signature(model.call )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             SCREAMING_SNAKE_CASE__       :			Optional[Any]													=[*signature.parameters.keys()]

             SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=['''pixel_values''']
             self.assertListEqual(arg_names[:1]   , __lowercase )




 def        __magic_name__		( self		:							Tuple )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			List[str]													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowercase )




 def        __magic_name__		( self		:							Any )		->  Tuple:
       SCREAMING_SNAKE_CASE__       :			Dict													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*__lowercase )




 def        __magic_name__		( self		:							Tuple )		->  str:
       SCREAMING_SNAKE_CASE__       :			str													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__lowercase )




 def        __magic_name__		( self		:							List[str]   , __lowercase		:							Tuple   , __lowercase		:							List[Any]   , __lowercase		:							Union[str, Any]=False )		->  str:
       SCREAMING_SNAKE_CASE__       :			int													=super()._prepare_for_class(__lowercase   , __lowercase   , return_labels=__lowercase )

       if return_labels:
             if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call ).parameters:
                   del inputs_dict["labels"]

       return inputs_dict




 @slow
 def        __magic_name__		( self		:							Any )		->  Any:
       for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE__       :			str													=TFDeiTModel.from_pretrained(__lowercase )
             self.assertIsNotNone(__lowercase )





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







@require_tf
@require_vision
class        __SCREAMING_SNAKE_CASE (     unittest.TestCase			):




 @cached_property
 def        __magic_name__		( self		:							int )		->  List[Any]:
       return (
           DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224''' )
           if is_vision_available()
           else None
       )




 @slow
 def        __magic_name__		( self		:							List[str] )		->  str:
       SCREAMING_SNAKE_CASE__       :			Tuple													=TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224''' )

       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=self.default_image_processor
       SCREAMING_SNAKE_CASE__       :			List[str]													=prepare_img()
       SCREAMING_SNAKE_CASE__       :			int													=image_processor(images=__lowercase   , return_tensors='''tf''' )

       # forward pass
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=model(**__lowercase )

       # verify the logits
       SCREAMING_SNAKE_CASE__       :			int													=tf.TensorShape((1, 10_00) )
       self.assertEqual(outputs.logits.shape   , __lowercase )

       SCREAMING_SNAKE_CASE__       :			Tuple													=tf.constant([-1.0266, 0.1912, -1.2861] )

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







'''simple docstring'''
import math





def 	_a(					UpperCamelCase__			:  int							):




      '''simple docstring'''






      return math.sqrt(UpperCamelCase__							) * math.sqrt(UpperCamelCase__							) == num





def 	_a(					UpperCamelCase__			:  int							):




      '''simple docstring'''






      SCREAMING_SNAKE_CASE__       :			List[Any]													=0
      SCREAMING_SNAKE_CASE__       :			Any													=n
      while left <= right:
            SCREAMING_SNAKE_CASE__       :			str													=(left + right) // 2
            if mid**2 == n:
                  return True
            elif mid**2 > n:
                  SCREAMING_SNAKE_CASE__       :			Optional[Any]													=mid - 1
            else:
                  SCREAMING_SNAKE_CASE__       :			Tuple													=mid + 1
      return False


if __name__ == "__main__":
  import doctest

  doctest.testmod()