

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






      """simple docstring"""




      def __init__(							self					:      Any			,			snake_case_					:      Optional[int]			,			snake_case_					:      int=1_3			,			snake_case_					:      Any=3_0			,			snake_case_					:      Dict=2			,			snake_case_					:      Union[str, Any]=3			,			snake_case_					:      Optional[int]=True			,			snake_case_					:      Dict=True			,			snake_case_					:      List[str]=3_2			,			snake_case_					:      str=2			,			snake_case_					:      Tuple=4			,			snake_case_					:      int=3_7			,			snake_case_					:      Union[str, Any]="gelu"			,			snake_case_					:      Optional[int]=0.1			,			snake_case_					:      int=0.1			,			snake_case_					:      Tuple=1_0			,			snake_case_					:      Optional[Any]=0.0_2			,			snake_case_					:      Optional[Any]=3			,			snake_case_					:      Union[str, Any]=None			,			snake_case_					:      List[Any]=2			,			):



        '''simple docstring'''



        snake_case__						:       Optional[int]       						=  parent
        snake_case__						:       List[str]       						=  batch_size
        snake_case__						:       Tuple       						=  image_size
        snake_case__						:       Optional[int]       						=  patch_size
        snake_case__						:       Optional[int]       						=  num_channels
        snake_case__						:       Union[str, Any]       						=  is_training
        snake_case__						:       Tuple       						=  use_labels
        snake_case__						:       Tuple       						=  hidden_size
        snake_case__						:       str       						=  num_hidden_layers
        snake_case__						:       int       						=  num_attention_heads
        snake_case__						:       Tuple       						=  intermediate_size
        snake_case__						:       List[str]       						=  hidden_act
        snake_case__						:       Tuple       						=  hidden_dropout_prob
        snake_case__						:       Any       						=  attention_probs_dropout_prob
        snake_case__						:       Dict       						=  type_sequence_label_size
        snake_case__						:       Optional[int]       						=  initializer_range
        snake_case__						:       Tuple       						=  scope
        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)
        snake_case__						:       Union[str, Any]       						=  (image_size // patch_size) ** 2
        snake_case__						:       Dict       						=  num_patches + 2







      def       __magic_name__							(							self					:      Optional[Any]  ):



        '''simple docstring'''



        snake_case__						:       Optional[int]       						=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

        snake_case__						:       Union[str, Any]       						=  None
        if self.use_labels:
          snake_case__						:       Tuple       						=  ids_tensor([self.batch_size]			,			self.type_sequence_label_size  )

        snake_case__						:       str       						=  self.get_config()

        return config, pixel_values, labels







      def       __magic_name__							(							self					:      Optional[int]  ):



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







      def       __magic_name__							(							self					:      Optional[Any]			,			snake_case_					:      Any			,			snake_case_					:      Optional[Any]			,			snake_case_					:      Optional[int]  ):



        '''simple docstring'''



        snake_case__						:       List[str]       						=  TFDeiTModel(config=snake_case_  )
        snake_case__						:       int       						=  model(snake_case_  )
        self.parent.assertEqual(result.last_hidden_state.shape			,			(self.batch_size, self.seq_length, self.hidden_size)  )







      def       __magic_name__							(							self					:      str			,			snake_case_					:      List[str]			,			snake_case_					:      Any			,			snake_case_					:      Tuple  ):



        '''simple docstring'''



        snake_case__						:       str       						=  TFDeiTForMaskedImageModeling(config=snake_case_  )
        snake_case__						:       str       						=  model(snake_case_  )
        self.parent.assertEqual(
            result.reconstruction.shape			,			(self.batch_size, self.num_channels, self.image_size, self.image_size)  )

        # test greyscale images
        snake_case__						:       int       						=  1
        snake_case__						:       Tuple       						=  TFDeiTForMaskedImageModeling(snake_case_  )

        snake_case__						:       Any       						=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
        snake_case__						:       int       						=  model(snake_case_  )
        self.parent.assertEqual(result.reconstruction.shape			,			(self.batch_size, 1, self.image_size, self.image_size)  )







      def       __magic_name__							(							self					:      List[Any]			,			snake_case_					:      Any			,			snake_case_					:      List[Any]			,			snake_case_					:      Tuple  ):



        '''simple docstring'''



        snake_case__						:       Tuple       						=  self.type_sequence_label_size
        snake_case__						:       Dict       						=  TFDeiTForImageClassification(snake_case_  )
        snake_case__						:       int       						=  model(snake_case_			,			labels=snake_case_  )
        self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.type_sequence_label_size)  )

        # test greyscale images
        snake_case__						:       int       						=  1
        snake_case__						:       Any       						=  TFDeiTForImageClassification(snake_case_  )

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







      def       __magic_name__							(							self					:      Optional[int]  ):



        '''simple docstring'''



        snake_case__						:       Any       						=  self.prepare_config_and_inputs()
        snake_case__						:       List[Any]       						=  config_and_inputs
        snake_case__						:       List[str]       						=  {'''pixel_values''': pixel_values}
        return config, inputs_dict





@require_tf
class     a			(				SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE			,			unittest.TestCase       ):






      """simple docstring"""




      __UpperCAmelCase			     =    (
          (
              TFDeiTModel,
              TFDeiTForImageClassification,
              TFDeiTForImageClassificationWithTeacher,
              TFDeiTForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      __UpperCAmelCase			     =    (
          {
              """feature-extraction""": TFDeiTModel,
              """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
          }
          if is_tf_available()
          else {}
      )

      __UpperCAmelCase			     =    False
      __UpperCAmelCase			     =    False
      __UpperCAmelCase			     =    False
      __UpperCAmelCase			     =    False







      def       __magic_name__							(							self					:      Any  ):



        '''simple docstring'''



        snake_case__						:       Union[str, Any]       						=  TFDeiTModelTester(self  )
        snake_case__						:       Optional[int]       						=  ConfigTester(self			,			config_class=snake_case_			,			has_text_modality=snake_case_			,			hidden_size=3_7  )







      def       __magic_name__							(							self					:      Optional[Any]  ):



        '''simple docstring'''



        self.config_tester.run_common_tests()







      @unittest.skip(reason='''DeiT does not use inputs_embeds'''  )
      def       __magic_name__							(							self					:      Optional[Any]  ):



        '''simple docstring'''



        pass







      def       __magic_name__							(							self					:      Tuple  ):



        '''simple docstring'''



        snake_case__						:       str       						=  self.model_tester.prepare_config_and_inputs_for_common()

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







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



        '''simple docstring'''



        snake_case__						:       Optional[Any]       						=  self.model_tester.prepare_config_and_inputs_for_common()

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

          snake_case__						:       Dict       						=  ['''pixel_values''']
          self.assertListEqual(arg_names[:1]			,			snake_case_  )







      def       __magic_name__							(							self					:      Any  ):



        '''simple docstring'''



        snake_case__						:       Union[str, Any]       						=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case_  )







      def       __magic_name__							(							self					:      List[Any]  ):



        '''simple docstring'''



        snake_case__						:       Union[str, Any]       						=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*snake_case_  )







      def       __magic_name__							(							self					:      str  ):



        '''simple docstring'''



        snake_case__						:       Optional[int]       						=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*snake_case_  )







      def       __magic_name__							(							self					:      List[Any]			,			snake_case_					:      Optional[int]			,			snake_case_					:      Optional[Any]			,			snake_case_					:      str=False  ):



        '''simple docstring'''



        snake_case__						:       List[str]       						=  super()._prepare_for_class(snake_case_			,			snake_case_			,			return_labels=snake_case_  )

        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					:      Optional[int]  ):



        '''simple docstring'''



        for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          snake_case__						:       int       						=  TFDeiTModel.from_pretrained(snake_case_  )
          self.assertIsNotNone(snake_case_  )





def 	_a       (				):

  """simple docstring"""

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





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






      """simple docstring"""




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



        '''simple docstring'''



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







      @slow
      def       __magic_name__							(							self					:      int  ):



        '''simple docstring'''



        snake_case__						:       Any       						=  TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''  )

        snake_case__						:       List[str]       						=  self.default_image_processor
        snake_case__						:       Optional[Any]       						=  prepare_img()
        snake_case__						:       List[str]       						=  image_processor(images=snake_case_			,			return_tensors='''tf'''  )

        # forward pass
        snake_case__						:       Optional[int]       						=  model(**snake_case_  )

        # verify the logits
        snake_case__						:       List[Any]       						=  tf.TensorShape((1, 1_0_0_0)  )
        self.assertEqual(outputs.logits.shape			,			snake_case_  )

        snake_case__						:       Optional[int]       						=  tf.constant([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1]  )

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






'''simple docstring'''


def 	_a       (				__lowerCAmelCase		:      float   ,							__lowerCAmelCase		:      float   ,							__lowerCAmelCase		:      int     ):

		"""simple docstring"""

		if principal <= 0:
				raise Exception('''Principal borrowed must be > 0'''     )
		if rate_per_annum < 0:
				raise Exception('''Rate of interest must be >= 0'''     )
		if years_to_repay <= 0 or not isinstance(__lowerCAmelCase   ,							__lowerCAmelCase     ):
				raise Exception('''Years to repay must be an integer > 0'''     )

		# Yearly rate is divided by 12 to get monthly rate
		snake_case__						:       int       						=  rate_per_annum / 12

		# Years to repay is multiplied by 12 to get number of payments as payment is monthly
		snake_case__						:       Tuple       						=  years_to_repay * 12

		return (
		    principal
		    * rate_per_month
		    * (1 + rate_per_month) ** number_of_payments
		    / ((1 + rate_per_month) ** number_of_payments - 1)
		)


if __name__ == "__main__":
					import doctest

					doctest.testmod()



