


'''simple docstring'''
from math import factorial, radians





def 					a__       (					lowerCAmelCase__  ,					lowerCAmelCase__ = 18  ,					lowerCAmelCase__ = 10						)    ->	float:
    UpperCAmelCase__			:		int													=				angle_in_degrees - ((angle_in_degrees // 3_6_0.0) * 3_6_0.0)

    # Converting from degrees to radians
    UpperCAmelCase__			:		int													=				radians(lowerCAmelCase__						)

    UpperCAmelCase__			:		str													=				angle_in_radians
    UpperCAmelCase__			:		Dict													=				3
    UpperCAmelCase__			:		Dict													=				-1

    for _ in range(lowerCAmelCase__						):
        result += (b * (angle_in_radians**a)) / factorial(lowerCAmelCase__						)

        UpperCAmelCase__			:		Dict													=				-b  # One positive term and the next will be negative and so on...
        a += 2  # Increased by 2 for every term.

    return round(lowerCAmelCase__  ,					lowerCAmelCase__						)


if __name__ == "__main__":
  __import__('''doctest''').testmod()




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

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
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 TFCvtForImageClassification, TFCvtModel
  from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor




class 			lowerCamelCase_   (	__a      ):






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


          '''simple docstring'''


          UpperCAmelCase__			:		str													=				self.config_class(**self.inputs_dict )
          self.parent.assertTrue(hasattr(_A						,						'''embed_dim''' ) )
          self.parent.assertTrue(hasattr(_A						,						'''num_heads''' ) )




class 			lowerCamelCase_   :






      def __init__(					self				:    int						,						_A				:    Tuple						,						_A				:    Any=13						,						_A				:    Optional[int]=64						,						_A				:    Optional[Any]=3						,						_A				:    List[str]=[16, 48, 96]						,						_A				:    int=[1, 3, 6]						,						_A				:    Optional[int]=[1, 2, 10]						,						_A				:    int=[7, 3, 3]						,						_A				:    Union[str, Any]=[4, 2, 2]						,						_A				:    Dict=[2, 1, 1]						,						_A				:    Optional[Any]=[2, 2, 2]						,						_A				:    Optional[Any]=[False, False, True]						,						_A				:    List[Any]=[0.0, 0.0, 0.0]						,						_A				:    str=0.0_2						,						_A				:    Tuple=1e-12						,						_A				:    Union[str, Any]=True						,						_A				:    Optional[Any]=True						,						_A				:    Optional[int]=2						,						):


          '''simple docstring'''


          UpperCAmelCase__			:		Dict													=				parent
          UpperCAmelCase__			:		List[str]													=				batch_size
          UpperCAmelCase__			:		Optional[int]													=				image_size
          UpperCAmelCase__			:		List[str]													=				patch_sizes
          UpperCAmelCase__			:		Any													=				patch_stride
          UpperCAmelCase__			:		Tuple													=				patch_padding
          UpperCAmelCase__			:		int													=				is_training
          UpperCAmelCase__			:		Dict													=				use_labels
          UpperCAmelCase__			:		List[Any]													=				num_labels
          UpperCAmelCase__			:		Optional[Any]													=				num_channels
          UpperCAmelCase__			:		Optional[int]													=				embed_dim
          UpperCAmelCase__			:		int													=				num_heads
          UpperCAmelCase__			:		Any													=				stride_kv
          UpperCAmelCase__			:		str													=				depth
          UpperCAmelCase__			:		List[Any]													=				cls_token
          UpperCAmelCase__			:		List[Any]													=				attention_drop_rate
          UpperCAmelCase__			:		Optional[int]													=				initializer_range
          UpperCAmelCase__			:		Optional[int]													=				layer_norm_eps






      def lowercase_							(					self				:    Optional[int] ):


          '''simple docstring'''


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

          UpperCAmelCase__			:		Any													=				None
          if self.use_labels:
              # create a random int32 tensor of given shape
              UpperCAmelCase__			:		List[Any]													=				ids_tensor([self.batch_size]						,						self.num_labels )

          UpperCAmelCase__			:		List[Any]													=				self.get_config()
          return config, pixel_values, labels






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          return CvtConfig(
              image_size=self.image_size						,						num_labels=self.num_labels						,						num_channels=self.num_channels						,						embed_dim=self.embed_dim						,						num_heads=self.num_heads						,						patch_sizes=self.patch_sizes						,						patch_padding=self.patch_padding						,						patch_stride=self.patch_stride						,						stride_kv=self.stride_kv						,						depth=self.depth						,						cls_token=self.cls_token						,						attention_drop_rate=self.attention_drop_rate						,						initializer_range=self.initializer_range						,						)






      def lowercase_							(					self				:    Optional[int]						,						_A				:    List[Any]						,						_A				:    Tuple						,						_A				:    Dict ):


          '''simple docstring'''


          UpperCAmelCase__			:		Optional[int]													=				TFCvtModel(config=_A )
          UpperCAmelCase__			:		List[str]													=				model(_A						,						training=_A )
          UpperCAmelCase__			:		int													=				(self.image_size, self.image_size)
          UpperCAmelCase__						,						UpperCAmelCase__			:		Optional[int]													=				image_size[0], image_size[1]
          for i in range(len(self.depth ) ):
              UpperCAmelCase__			:		Union[str, Any]													=				floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
              UpperCAmelCase__			:		Optional[Any]													=				floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
          self.parent.assertEqual(result.last_hidden_state.shape						,						(self.batch_size, self.embed_dim[-1], height, width) )






      def lowercase_							(					self				:    Optional[Any]						,						_A				:    Optional[Any]						,						_A				:    List[Any]						,						_A				:    int ):


          '''simple docstring'''


          UpperCAmelCase__			:		str													=				self.num_labels
          UpperCAmelCase__			:		Union[str, Any]													=				TFCvtForImageClassification(_A )
          UpperCAmelCase__			:		Any													=				model(_A						,						labels=_A						,						training=_A )
          self.parent.assertEqual(result.logits.shape						,						(self.batch_size, self.num_labels) )






      def lowercase_							(					self				:    Optional[int] ):


          '''simple docstring'''


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




@require_tf
class 			lowerCamelCase_   (	__a     ,       __a     ,       unittest.TestCase      ):
      lowerCAmelCase__		     =     (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
      lowerCAmelCase__		     =     (
          {'feature-extraction': TFCvtModel, 'image-classification': TFCvtForImageClassification}
          if is_tf_available()
          else {}
      )
      lowerCAmelCase__		     =     False
      lowerCAmelCase__		     =     False
      lowerCAmelCase__		     =     False
      lowerCAmelCase__		     =     False
      lowerCAmelCase__		     =     False






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          UpperCAmelCase__			:		Union[str, Any]													=				TFCvtModelTester(self )
          UpperCAmelCase__			:		Tuple													=				TFCvtConfigTester(self						,						config_class=_A						,						has_text_modality=_A						,						hidden_size=37 )






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          self.config_tester.create_and_test_config_common_properties()
          self.config_tester.create_and_test_config_to_json_string()
          self.config_tester.create_and_test_config_to_json_file()
          self.config_tester.create_and_test_config_from_and_save_pretrained()
          self.config_tester.create_and_test_config_with_num_labels()
          self.config_tester.check_config_can_be_init_without_params()
          self.config_tester.check_config_arguments_init()






      @unittest.skip(reason='''Cvt does not output attentions''' )
      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          pass






      @unittest.skip(reason='''Cvt does not use inputs_embeds''' )
      def lowercase_							(					self				:    str ):


          '''simple docstring'''


          pass






      @unittest.skip(reason='''Cvt does not support input and output embeddings''' )
      def lowercase_							(					self				:    Optional[Any] ):


          '''simple docstring'''


          pass






      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('''GPU''' ) ) == 0						,						reason='''TF does not support backprop for grouped convolutions on CPU.'''						,						)
      def lowercase_							(					self				:    List[str] ):


          '''simple docstring'''


          super().test_dataset_conversion()






      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('''GPU''' ) ) == 0						,						reason='''TF does not support backprop for grouped convolutions on CPU.'''						,						)
      @slow
      def lowercase_							(					self				:    Optional[int] ):


          '''simple docstring'''


          super().test_keras_fit()






      @unittest.skip(reason='''Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8''' )
      def lowercase_							(					self				:    Union[str, Any] ):


          '''simple docstring'''


          UpperCAmelCase__			:		List[Any]													=				tf.keras.mixed_precision.Policy('''mixed_float16''' )
          tf.keras.mixed_precision.set_global_policy(_A )
          super().test_keras_fit()
          tf.keras.mixed_precision.set_global_policy('''float32''' )






      def lowercase_							(					self				:    Optional[Any] ):


          '''simple docstring'''


          UpperCAmelCase__						,						UpperCAmelCase__			:		str													=				self.model_tester.prepare_config_and_inputs_for_common()

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

              UpperCAmelCase__			:		Any													=				['''pixel_values''']
              self.assertListEqual(arg_names[:1]						,						_A )






      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          def check_hidden_states_output(_A				:    Dict						,						_A				:    Optional[Any]						,						_A				:    Dict ):
              UpperCAmelCase__			:		str													=				model_class(_A )

              UpperCAmelCase__			:		List[str]													=				model(**self._prepare_for_class(_A						,						_A ) )
              UpperCAmelCase__			:		Tuple													=				outputs.hidden_states

              UpperCAmelCase__			:		int													=				len(self.model_tester.depth )
              self.assertEqual(len(_A )						,						_A )

              # verify the first hidden states (first block)
              self.assertListEqual(
                  list(hidden_states[0].shape[-3:] )						,						[
                      self.model_tester.embed_dim[0],
                      self.model_tester.image_size // 4,
                      self.model_tester.image_size // 4,
                  ]						,						)

          UpperCAmelCase__						,						UpperCAmelCase__			:		int													=				self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCAmelCase__			:		Tuple													=				True
              check_hidden_states_output(_A						,						_A						,						_A )

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              UpperCAmelCase__			:		List[str]													=				True

              check_hidden_states_output(_A						,						_A						,						_A )






      def lowercase_							(					self				:    Tuple ):


          '''simple docstring'''


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






      def lowercase_							(					self				:    str ):


          '''simple docstring'''


          UpperCAmelCase__			:		Optional[Any]													=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*_A )






      @slow
      def lowercase_							(					self				:    Optional[Any] ):


          '''simple docstring'''


          for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase__			:		Optional[int]													=				TFCvtModel.from_pretrained(_A )
              self.assertIsNotNone(_A )





def 					a__       (					)    ->	Any:
    UpperCAmelCase__			:		str													=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
    return image







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






      @cached_property
      def lowercase_							(					self				:    Union[str, Any] ):


          '''simple docstring'''


          return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )






      @slow
      def lowercase_							(					self				:    Any ):


          '''simple docstring'''


          UpperCAmelCase__			:		Dict													=				TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

          UpperCAmelCase__			:		Union[str, Any]													=				self.default_image_processor
          UpperCAmelCase__			:		Optional[Any]													=				prepare_img()
          UpperCAmelCase__			:		Tuple													=				image_processor(images=_A						,						return_tensors='''tf''' )

          # forward pass
          UpperCAmelCase__			:		Optional[Any]													=				model(**_A )

          # verify the logits
          UpperCAmelCase__			:		Union[str, Any]													=				tf.TensorShape((1, 1_000) )
          self.assertEqual(outputs.logits.shape						,						_A )

          UpperCAmelCase__			:		Union[str, Any]													=				tf.constant([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0] )
          self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()						,						_A						,						atol=1e-4 ) )
