'''simple docstring'''




from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
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 TFViTForImageClassification, TFViTModel


if is_vision_available():
   from PIL import Image

   from transformers import ViTImageProcessor






class 		__A       :






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

              # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
              __UpperCAmelCase  :				Tuple       				=		(image_size // patch_size) ** 2
              __UpperCAmelCase  :				Any       				=		num_patches + 1






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Any       				=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

              __UpperCAmelCase  :				Optional[Any]       				=		None
              if self.use_labels:
                     __UpperCAmelCase  :				Optional[Any]       				=		ids_tensor([self.batch_size]				,     self.type_sequence_label_size      )

              __UpperCAmelCase  :				Dict       				=		self.get_config()

              return config, pixel_values, labels






       def 			_snake_case						(      self      ):
              return ViTConfig(
                  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=UpperCamelCase_				,     initializer_range=self.initializer_range				,     )






       def 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				Dict       				=		TFViTModel(config=UpperCamelCase_      )
              __UpperCAmelCase  :				Optional[int]       				=		model(UpperCamelCase_				,     training=UpperCamelCase_      )
              self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)      )

              # Test with an image with different size than the one specified in config.
              __UpperCAmelCase  :				List[str]       				=		self.image_size // 2
              __UpperCAmelCase  :				Dict       				=		pixel_values[:, :, :image_size, :image_size]
              __UpperCAmelCase  :				Union[str, Any]       				=		model(UpperCamelCase_				,     interpolate_pos_encoding=UpperCamelCase_				,     training=UpperCamelCase_      )
              __UpperCAmelCase  :				str       				=		(image_size // self.patch_size) ** 2 + 1
              self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, seq_length, self.hidden_size)      )






       def 			_snake_case						(      self				,     UpperCamelCase_				,     UpperCamelCase_				,     UpperCamelCase_      ):
              __UpperCAmelCase  :				List[str]       				=		self.type_sequence_label_size
              __UpperCAmelCase  :				Dict       				=		TFViTForImageClassification(UpperCamelCase_      )
              __UpperCAmelCase  :				List[Any]       				=		model(UpperCamelCase_				,     labels=UpperCamelCase_				,     training=UpperCamelCase_      )
              self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.type_sequence_label_size)      )

              # Test with an image with different size than the one specified in config.
              __UpperCAmelCase  :				Optional[Any]       				=		self.image_size // 2
              __UpperCAmelCase  :				Optional[int]       				=		pixel_values[:, :, :image_size, :image_size]
              __UpperCAmelCase  :				Optional[int]       				=		model(UpperCamelCase_				,     interpolate_pos_encoding=UpperCamelCase_				,     training=UpperCamelCase_      )
              self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.type_sequence_label_size)      )

              # test greyscale images
              __UpperCAmelCase  :				str       				=		1
              __UpperCAmelCase  :				Tuple       				=		TFViTForImageClassification(UpperCamelCase_      )
              __UpperCAmelCase  :				Optional[int]       				=		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
              __UpperCAmelCase  :				Optional[Any]       				=		model(UpperCamelCase_      )
              self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.type_sequence_label_size)      )







       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				str       				=		self.prepare_config_and_inputs()
              __UpperCAmelCase				, __UpperCAmelCase				, __UpperCAmelCase  :				Optional[int]       				=		config_and_inputs
              __UpperCAmelCase  :				str       				=		{"pixel_values": pixel_values}
              return config, inputs_dict






@require_tf
class 		__A       (__magic_name__ ,					__magic_name__ ,					unittest.TestCase   ):
       snake_case       :Any				    =					(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
       snake_case       :str				    =					(
           {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
           if is_tf_available()
           else {}
       )

       snake_case       :Union[str, Any]				    =					False
       snake_case       :Tuple				    =					False
       snake_case       :int				    =					False






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Optional[Any]       				=		TFViTModelTester(self      )
              __UpperCAmelCase  :				int       				=		ConfigTester(self				,     config_class=UpperCamelCase_				,     has_text_modality=UpperCamelCase_				,     hidden_size=37      )






       def 			_snake_case						(      self      ):
              self.config_tester.run_common_tests()






       @unittest.skip(reason="ViT does not use inputs_embeds"      )
       def 			_snake_case						(      self      ):
              pass






       @unittest.skip(reason="ViT does not use inputs_embeds"      )
       def 			_snake_case						(      self      ):
              pass






       def 			_snake_case						(      self      ):
              __UpperCAmelCase				, __UpperCAmelCase  :				List[str]       				=		self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     __UpperCAmelCase  :				List[Any]       				=		model_class(UpperCamelCase_      )
                     self.assertIsInstance(model.get_input_embeddings()				,     (tf.keras.layers.Layer)      )
                     __UpperCAmelCase  :				Optional[int]       				=		model.get_output_embeddings()
                     self.assertTrue(x is None or isinstance(UpperCamelCase_				,     tf.keras.layers.Layer      )      )






       def 			_snake_case						(      self      ):
              __UpperCAmelCase				, __UpperCAmelCase  :				List[Any]       				=		self.model_tester.prepare_config_and_inputs_for_common()

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

                     __UpperCAmelCase  :				Tuple       				=		["pixel_values"]
                     self.assertListEqual(arg_names[:1]				,     UpperCamelCase_      )






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				List[str]       				=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*UpperCamelCase_      )






       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				int       				=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_      )







       @slow
       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Union[str, Any]       				=		TFViTModel.from_pretrained("google/vit-base-patch16-224"      )
              self.assertIsNotNone(UpperCamelCase_      )





def _lowercase	(      )					->       Dict:





       """simple docstring"""


       __UpperCAmelCase  :				int       				=		Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
       return image






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






       @cached_property
       def 			_snake_case						(      self      ):
              return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"      ) if is_vision_available() else None







       @slow
       def 			_snake_case						(      self      ):
              __UpperCAmelCase  :				Any       				=		TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224"      )

              __UpperCAmelCase  :				str       				=		self.default_image_processor
              __UpperCAmelCase  :				Optional[int]       				=		prepare_img()
              __UpperCAmelCase  :				Optional[int]       				=		image_processor(images=UpperCamelCase_				,     return_tensors="tf"      )

              # forward pass
              __UpperCAmelCase  :				Optional[Any]       				=		model(**UpperCamelCase_      )

              # verify the logits
              __UpperCAmelCase  :				List[Any]       				=		tf.TensorShape((1, 10_00)      )
              self.assertEqual(outputs.logits.shape				,     UpperCamelCase_      )

              __UpperCAmelCase  :				List[str]       				=		tf.constant([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]      )

              tf.debugging.assert_near(outputs.logits[0, :3]				,     UpperCamelCase_				,     atol=1E-4      )







'''simple docstring'''




def _lowercase	(      lowerCamelCase__							,    lowerCamelCase__						)					->       float:





       """simple docstring"""


       if discount_rate < 0:
              raise ValueError("Discount rate cannot be negative"						)
       if not cash_flows:
              raise ValueError("Cash flows list cannot be empty"						)
       __UpperCAmelCase  :				Tuple       				=		sum(
           cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(lowerCamelCase__						)						)
       return round(lowerCamelCase__							,    ndigits=2						)


if __name__ == "__main__":
   import doctest

   doctest.testmod()






