


import inspect
import unittest

from transformers import DataaVecVisionConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch
 from torch import nn

 from transformers import (
     MODEL_MAPPING,
     DataaVecVisionForImageClassification,
     DataaVecVisionForSemanticSegmentation,
     DataaVecVisionModel,
 )
 from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import BeitImageProcessor




class A						:






      def __init__(      self		:  Dict    ,     lowercase_		:  Optional[int]    ,     lowercase_		:  Any=100    ,     lowercase_		:  int=13    ,     lowercase_		:  Optional[int]=30    ,     lowercase_		:  Dict=2    ,     lowercase_		:  List[str]=3    ,     lowercase_		:  str=True    ,     lowercase_		:  Optional[Any]=True    ,     lowercase_		:  Optional[Any]=32    ,     lowercase_		:  List[Any]=4    ,     lowercase_		:  str=4    ,     lowercase_		:  Dict=37    ,     lowercase_		:  List[Any]="gelu"    ,     lowercase_		:  Union[str, Any]=0.1    ,     lowercase_		:  str=0.1    ,     lowercase_		:  Optional[Any]=10    ,     lowercase_		:  int=0.02    ,     lowercase_		:  List[str]=3    ,     lowercase_		:  Dict=None    ,     lowercase_		:  str=[0, 1, 2, 3]    ,     )      ->		Dict:
             """simple docstring"""




             _lowerCamelCase :	Dict						=parent
             _lowerCamelCase :	Tuple						=100
             _lowerCamelCase :	Union[str, Any]						=batch_size
             _lowerCamelCase :	List[Any]						=image_size
             _lowerCamelCase :	List[str]						=patch_size
             _lowerCamelCase :	List[Any]						=num_channels
             _lowerCamelCase :	List[str]						=is_training
             _lowerCamelCase :	Dict						=use_labels
             _lowerCamelCase :	Any						=hidden_size
             _lowerCamelCase :	Optional[Any]						=num_hidden_layers
             _lowerCamelCase :	Dict						=num_attention_heads
             _lowerCamelCase :	Optional[int]						=intermediate_size
             _lowerCamelCase :	Tuple						=hidden_act
             _lowerCamelCase :	int						=hidden_dropout_prob
             _lowerCamelCase :	List[Any]						=attention_probs_dropout_prob
             _lowerCamelCase :	List[str]						=type_sequence_label_size
             _lowerCamelCase :	Any						=initializer_range
             _lowerCamelCase :	Union[str, Any]						=scope
             _lowerCamelCase :	Tuple						=out_indices
             _lowerCamelCase :	str						=num_labels

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






      def  lowerCamelCase (      self		:  Dict				)      ->		Any:
             """simple docstring"""




             _lowerCamelCase :	Dict						=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

             _lowerCamelCase :	Tuple						=None
             _lowerCamelCase :	Optional[int]						=None
             if self.use_labels:
                    _lowerCamelCase :	List[str]						=ids_tensor([self.batch_size]    ,     self.type_sequence_label_size				)
                    _lowerCamelCase :	Optional[Any]						=ids_tensor([self.batch_size, self.image_size, self.image_size]    ,     self.num_labels				)

             _lowerCamelCase :	Tuple						=self.get_config()

             return config, pixel_values, labels, pixel_labels






      def  lowerCamelCase (      self		:  Union[str, Any]				)      ->		Any:
             """simple docstring"""




             return DataaVecVisionConfig(
                 vocab_size=self.vocab_size    ,     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    ,     out_indices=self.out_indices    ,     )






      def  lowerCamelCase (      self		:  List[str]    ,     lowercase_		:  Dict    ,     lowercase_		:  int    ,     lowercase_		:  Tuple    ,     lowercase_		:  Union[str, Any]				)      ->		str:
             """simple docstring"""




             _lowerCamelCase :	Optional[Any]						=DataaVecVisionModel(config=lowercase_				)
             model.to(lowercase_				)
             model.eval()
             _lowerCamelCase :	Union[str, Any]						=model(lowercase_				)
             # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
             _lowerCamelCase :	Union[str, Any]						=(self.image_size // self.patch_size) ** 2
             self.parent.assertEqual(result.last_hidden_state.shape    ,     (self.batch_size, num_patches + 1, self.hidden_size)				)






      def  lowerCamelCase (      self		:  int    ,     lowercase_		:  int    ,     lowercase_		:  Optional[int]    ,     lowercase_		:  int    ,     lowercase_		:  Optional[int]				)      ->		List[Any]:
             """simple docstring"""




             _lowerCamelCase :	Tuple						=self.type_sequence_label_size
             _lowerCamelCase :	Tuple						=DataaVecVisionForImageClassification(lowercase_				)
             model.to(lowercase_				)
             model.eval()
             _lowerCamelCase :	str						=model(lowercase_    ,     labels=lowercase_				)
             self.parent.assertEqual(result.logits.shape    ,     (self.batch_size, self.type_sequence_label_size)				)






      def  lowerCamelCase (      self		:  int    ,     lowercase_		:  str    ,     lowercase_		:  str    ,     lowercase_		:  Optional[int]    ,     lowercase_		:  Union[str, Any]				)      ->		Optional[Any]:
             """simple docstring"""




             _lowerCamelCase :	Optional[Any]						=self.num_labels
             _lowerCamelCase :	Tuple						=DataaVecVisionForSemanticSegmentation(lowercase_				)
             model.to(lowercase_				)
             model.eval()
             _lowerCamelCase :	List[Any]						=model(lowercase_				)
             self.parent.assertEqual(
                 result.logits.shape    ,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)				)
             _lowerCamelCase :	Dict						=model(lowercase_    ,     labels=lowercase_				)
             self.parent.assertEqual(
                 result.logits.shape    ,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)				)






      def  lowerCamelCase (      self		:  int				)      ->		Optional[Any]:
             """simple docstring"""




             _lowerCamelCase :	Any						=self.prepare_config_and_inputs()
             _lowerCamelCase			,  _lowerCamelCase			,  _lowerCamelCase			,  _lowerCamelCase :	int						=config_and_inputs
             _lowerCamelCase :	Any						={'pixel_values': pixel_values}
             return config, inputs_dict




@require_torch
class A						(     UpperCamelCase_					,							UpperCamelCase_					,							unittest.TestCase  ):
      UpperCamelCase__							:						Any   =(
          (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
          if is_torch_available()
          else ()
      )
      UpperCamelCase__							:						Tuple   =(
          {
              'feature-extraction': DataaVecVisionModel,
              'image-classification': DataaVecVisionForImageClassification,
              'image-segmentation': DataaVecVisionForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      UpperCamelCase__							:						int   =False
      UpperCamelCase__							:						str   =False
      UpperCamelCase__							:						Optional[Any]   =False






      def  lowerCamelCase (      self		:  str				)      ->		Optional[int]:
             """simple docstring"""




             _lowerCamelCase :	List[Any]						=DataaVecVisionModelTester(self				)
             _lowerCamelCase :	Dict						=ConfigTester(
                 self    ,     config_class=lowercase_    ,     has_text_modality=lowercase_    ,     hidden_size=37				)






      def  lowerCamelCase (      self		:  Any				)      ->		Optional[Any]:
             """simple docstring"""




             self.config_tester.run_common_tests()






      def  lowerCamelCase (      self		:  Optional[int]				)      ->		Optional[int]:
             """simple docstring"""




             pass






      @require_torch_multi_gpu
      @unittest.skip(
          reason='Data2VecVision has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'				)
      def  lowerCamelCase (      self		:  List[Any]				)      ->		Any:
             """simple docstring"""




             pass






      def  lowerCamelCase (      self		:  int				)      ->		Dict:
             """simple docstring"""




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

             for model_class in self.all_model_classes:
                    _lowerCamelCase :	Optional[int]						=model_class(lowercase_				)
                    self.assertIsInstance(model.get_input_embeddings()    ,     (nn.Module)				)
                    _lowerCamelCase :	Dict						=model.get_output_embeddings()
                    self.assertTrue(x is None or isinstance(lowercase_    ,     nn.Linear				)				)






      def  lowerCamelCase (      self		:  Optional[Any]				)      ->		Dict:
             """simple docstring"""




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

             for model_class in self.all_model_classes:
                    _lowerCamelCase :	Dict						=model_class(lowercase_				)
                    _lowerCamelCase :	Any						=inspect.signature(model.forward				)
                    # signature.parameters is an OrderedDict => so arg_names order is deterministic
                    _lowerCamelCase :	Union[str, Any]						=[*signature.parameters.keys()]

                    _lowerCamelCase :	Optional[Any]						=['pixel_values']
                    self.assertListEqual(arg_names[:1]    ,     lowercase_				)






      def  lowerCamelCase (      self		:  Optional[Any]				)      ->		List[Any]:
             """simple docstring"""




             _lowerCamelCase :	List[str]						=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*lowercase_				)






      def  lowerCamelCase (      self		:  Tuple				)      ->		List[Any]:
             """simple docstring"""




             _lowerCamelCase :	Dict						=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_segmentation(*lowercase_				)






      def  lowerCamelCase (      self		:  Any				)      ->		Tuple:
             """simple docstring"""




             if not self.model_tester.is_training:
                    return

             _lowerCamelCase			,  _lowerCamelCase :	Union[str, Any]						=self.model_tester.prepare_config_and_inputs_for_common()
             _lowerCamelCase :	Optional[int]						=True

             for model_class in self.all_model_classes:
                    if model_class in [*get_values(lowercase_				)]:
                           continue

                    _lowerCamelCase :	int						=model_class(lowercase_				)
                    model.to(lowercase_				)
                    model.train()
                    _lowerCamelCase :	Optional[Any]						=self._prepare_for_class(lowercase_    ,     lowercase_    ,     return_labels=lowercase_				)
                    _lowerCamelCase :	Dict						=model(**lowercase_				).loss
                    loss.backward()






      def  lowerCamelCase (      self		:  List[Any]				)      ->		Optional[int]:
             """simple docstring"""




             _lowerCamelCase			,  _lowerCamelCase :	Union[str, Any]						=self.model_tester.prepare_config_and_inputs_for_common()
             if not self.model_tester.is_training:
                    return

             _lowerCamelCase :	str						=False
             _lowerCamelCase :	Optional[int]						=True

             for model_class in self.all_model_classes:
                    if model_class in [*get_values(lowercase_				)] or not model_class.supports_gradient_checkpointing:
                           continue
                    # TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
                    # this can then be incorporated into _prepare_for_class in test_modeling_common.py
                    elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
                           _lowerCamelCase			,  _lowerCamelCase			,  _lowerCamelCase			,  _lowerCamelCase :	Any						=inputs_dict['pixel_values'].shape
                           _lowerCamelCase :	Any						=torch.zeros(
                               [self.model_tester.batch_size, height, width]    ,     device=lowercase_				).long()
                    _lowerCamelCase :	Union[str, Any]						=model_class(lowercase_				)
                    model.gradient_checkpointing_enable()
                    model.to(lowercase_				)
                    model.train()
                    _lowerCamelCase :	Optional[int]						=self._prepare_for_class(lowercase_    ,     lowercase_    ,     return_labels=lowercase_				)
                    _lowerCamelCase :	Dict						=model(**lowercase_				).loss
                    loss.backward()






      def  lowerCamelCase (      self		:  List[str]				)      ->		List[Any]:
             """simple docstring"""




             _lowerCamelCase			,  _lowerCamelCase :	Dict						=self.model_tester.prepare_config_and_inputs_for_common()

             _lowerCamelCase :	str						=_config_zero_init(lowercase_				)
             for model_class in self.all_model_classes:
                    _lowerCamelCase :	Dict						=model_class(config=lowercase_				)
                    for name, param in model.named_parameters():
                           # we skip lambda parameters as these require special initial values
                           # determined by config.layer_scale_init_value
                           if "lambda" in name:
                                  continue
                           if param.requires_grad:
                                  self.assertIn(
                                      ((param.data.mean() * 1E9).round() / 1E9).item()    ,     [0.0, 1.0]    ,     msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''    ,     )






      def  lowerCamelCase (      self		:  List[str]    ,     lowercase_		:  Any    ,     lowercase_		:  str    ,     lowercase_		:  Optional[int]    ,     lowercase_		:  int=2E-4    ,     lowercase_		:  Union[str, Any]="outputs"    ,     lowercase_		:  Optional[Any]=None				)      ->		Dict:
             """simple docstring"""




             super().check_pt_tf_outputs(lowercase_    ,     lowercase_    ,     lowercase_    ,     lowercase_    ,     lowercase_    ,     lowercase_				)






      def  lowerCamelCase (      self		:  Union[str, Any]				)      ->		Dict:
             """simple docstring"""




             _lowerCamelCase :	Optional[int]						=self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*lowercase_				)






      @slow
      def  lowerCamelCase (      self		:  Dict				)      ->		Tuple:
             """simple docstring"""




             for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _lowerCamelCase :	Optional[Any]						=DataaVecVisionModel.from_pretrained(lowercase_				)
                    self.assertIsNotNone(lowercase_				)




def 			a_   (					):
       '''simple docstring'''





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




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






      @cached_property
      def  lowerCamelCase (      self		:  str				)      ->		Optional[Any]:
             """simple docstring"""




             return (
                 BeitImageProcessor.from_pretrained('facebook/data2vec-vision-base-ft1k'				) if is_vision_available() else None
             )






      @slow
      def  lowerCamelCase (      self		:  Optional[Any]				)      ->		Any:
             """simple docstring"""




             _lowerCamelCase :	Any						=DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k'				).to(
                 lowercase_				)

             _lowerCamelCase :	Union[str, Any]						=self.default_image_processor
             _lowerCamelCase :	Any						=prepare_img()
             _lowerCamelCase :	Dict						=image_processor(images=lowercase_    ,     return_tensors='pt'				).to(lowercase_				)

             # forward pass
             with torch.no_grad():
                    _lowerCamelCase :	Dict						=model(**lowercase_				)
             _lowerCamelCase :	Tuple						=outputs.logits

             # verify the logits
             _lowerCamelCase :	Union[str, Any]						=torch.Size((1, 1000)				)
             self.assertEqual(logits.shape    ,     lowercase_				)

             _lowerCamelCase :	List[str]						=torch.tensor([0.3277, -0.1395, 0.0911]				).to(lowercase_				)

             self.assertTrue(torch.allclose(logits[0, :3]    ,     lowercase_    ,     atol=1E-4				)				)

             _lowerCamelCase :	Optional[Any]						=[model.config.labelaid[i] for i in ['remote control, remote', 'tabby, tabby cat']]
             self.assertEqual(logits[0].topk(2				).indices.cpu().tolist()    ,     lowercase_				)






from collections.abc import Callable

lowerCamelCase		      = Callable[[float], float]  # type alias for a real -> real function




def 			a_   (					SCREAMING_SNAKE_CASE__      :   RealFunc	,    SCREAMING_SNAKE_CASE__      :   RealFunc	,    SCREAMING_SNAKE_CASE__      :   int	,    ):
       '''simple docstring'''





       _lowerCamelCase :	Tuple						=float(SCREAMING_SNAKE_CASE__  )
       while True:
              try:
                     _lowerCamelCase :	int						=prev_guess - function(SCREAMING_SNAKE_CASE__  ) / derivative(SCREAMING_SNAKE_CASE__  )
              except ZeroDivisionError:
                     raise ZeroDivisionError('Could not find root'  ) from None
              if abs(prev_guess - next_guess  ) < 10**-5:
                     return next_guess
              _lowerCamelCase :	Union[str, Any]						=next_guess




def 			a_   (					SCREAMING_SNAKE_CASE__      :   float  ):
       '''simple docstring'''





       return (x**3) - (2 * x) - 5




def 			a_   (					SCREAMING_SNAKE_CASE__      :   float  ):
       '''simple docstring'''





       return 3 * (x**2) - 2


if __name__ == "__main__":
 print(newton(f, fa, 3))


