


'''simple docstring'''






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 					__magic_name__							:






       def __init__(			self  ,							snake_case  ,							snake_case=1_0_0  ,							snake_case=1_3  ,							snake_case=3_0  ,							snake_case=2  ,							snake_case=3  ,							snake_case=True  ,							snake_case=True  ,							snake_case=3_2  ,							snake_case=4  ,							snake_case=4  ,							snake_case=3_7  ,							snake_case="gelu"  ,							snake_case=0.1  ,							snake_case=0.1  ,							snake_case=1_0  ,							snake_case=0.02  ,							snake_case=3  ,							snake_case=None  ,							snake_case=[0, 1, 2, 3]  ,							)						->  Tuple:




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

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






       def 			lowerCAmelCase		(			self)						->  Dict:




          '''simple docstring'''
          _UpperCAmelCase   :   int							    =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

          _UpperCAmelCase   :   List[str]							    =self.get_config()

          return config, pixel_values, labels, pixel_labels






       def 			lowerCAmelCase		(			self)						->  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=__UpperCAmelCase  ,							initializer_range=self.initializer_range  ,							out_indices=self.out_indices  ,							)






       def 			lowerCAmelCase		(			self  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case)						->  Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase   :   Tuple							    =DataaVecVisionModel(config=__UpperCAmelCase)
          model.to(__UpperCAmelCase)
          model.eval()
          _UpperCAmelCase   :   str							    =model(__UpperCAmelCase)
          # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
          _UpperCAmelCase   :   Optional[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  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case)						->  Dict:




          '''simple docstring'''
          _UpperCAmelCase   :   Optional[Any]							    =self.type_sequence_label_size
          _UpperCAmelCase   :   Dict							    =DataaVecVisionForImageClassification(__UpperCAmelCase)
          model.to(__UpperCAmelCase)
          model.eval()
          _UpperCAmelCase   :   Optional[int]							    =model(__UpperCAmelCase  ,							labels=__UpperCAmelCase)
          self.parent.assertEqual(result.logits.shape  ,							(self.batch_size, self.type_sequence_label_size))






       def 			lowerCAmelCase		(			self  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case)						->  Any:




          '''simple docstring'''
          _UpperCAmelCase   :   List[Any]							    =self.num_labels
          _UpperCAmelCase   :   Optional[Any]							    =DataaVecVisionForSemanticSegmentation(__UpperCAmelCase)
          model.to(__UpperCAmelCase)
          model.eval()
          _UpperCAmelCase   :   Tuple							    =model(__UpperCAmelCase)
          self.parent.assertEqual(
              result.logits.shape  ,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2))
          _UpperCAmelCase   :   int							    =model(__UpperCAmelCase  ,							labels=__UpperCAmelCase)
          self.parent.assertEqual(
              result.logits.shape  ,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2))






       def 			lowerCAmelCase		(			self)						->  Union[str, Any]:




          '''simple docstring'''
          _UpperCAmelCase   :   List[Any]							    =self.prepare_config_and_inputs()
          _UpperCAmelCase    ,       _UpperCAmelCase    ,       _UpperCAmelCase    ,       _UpperCAmelCase   :   Union[str, Any]							    =config_and_inputs
          _UpperCAmelCase   :   Optional[Any]							    ={'pixel_values': pixel_values}
          return config, inputs_dict






@require_torch
class 					__magic_name__							(		lowerCAmelCase					,lowerCAmelCase					,unittest.TestCase				):
       UpperCAmelCase						      =(
           (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
           if is_torch_available()
           else ()
       )
       UpperCAmelCase						      =(
           {
               "feature-extraction": DataaVecVisionModel,
               "image-classification": DataaVecVisionForImageClassification,
               "image-segmentation": DataaVecVisionForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       UpperCAmelCase						      =False
       UpperCAmelCase						      =False
       UpperCAmelCase						      =False






       def 			lowerCAmelCase		(			self)						->  Dict:




          '''simple docstring'''
          _UpperCAmelCase   :   int							    =DataaVecVisionModelTester(self)
          _UpperCAmelCase   :   List[str]							    =ConfigTester(
              self  ,							config_class=__UpperCAmelCase  ,							has_text_modality=__UpperCAmelCase  ,							hidden_size=3_7)






       def 			lowerCAmelCase		(			self)						->  Any:




          '''simple docstring'''
          self.config_tester.run_common_tests()






       def 			lowerCAmelCase		(			self)						->  int:




          '''simple docstring'''
          # Data2VecVision does not use inputs_embeds
          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)						->  Optional[Any]:




          '''simple docstring'''
          pass






       def 			lowerCAmelCase		(			self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase    ,       _UpperCAmelCase   :   List[str]							    =self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             _UpperCAmelCase   :   str							    =model_class(__UpperCAmelCase)
             self.assertIsInstance(model.get_input_embeddings()  ,							(nn.Module))
             _UpperCAmelCase   :   Tuple							    =model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(__UpperCAmelCase  ,							nn.Linear))






       def 			lowerCAmelCase		(			self)						->  str:




          '''simple docstring'''
          _UpperCAmelCase    ,       _UpperCAmelCase   :   Any							    =self.model_tester.prepare_config_and_inputs_for_common()

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

             _UpperCAmelCase   :   int							    =['pixel_values']
             self.assertListEqual(arg_names[:1]  ,							__UpperCAmelCase)






       def 			lowerCAmelCase		(			self)						->  int:




          '''simple docstring'''
          _UpperCAmelCase   :   Tuple							    =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__UpperCAmelCase)






       def 			lowerCAmelCase		(			self)						->  Dict:




          '''simple docstring'''
          _UpperCAmelCase   :   List[Any]							    =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_segmentation(*__UpperCAmelCase)






       def 			lowerCAmelCase		(			self)						->  Union[str, Any]:




          '''simple docstring'''
          if not self.model_tester.is_training:
             return

          _UpperCAmelCase    ,       _UpperCAmelCase   :   int							    =self.model_tester.prepare_config_and_inputs_for_common()
          _UpperCAmelCase   :   List[Any]							    =True

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

             _UpperCAmelCase   :   Optional[int]							    =model_class(__UpperCAmelCase)
             model.to(__UpperCAmelCase)
             model.train()
             _UpperCAmelCase   :   List[Any]							    =self._prepare_for_class(__UpperCAmelCase  ,							__UpperCAmelCase  ,							return_labels=__UpperCAmelCase)
             _UpperCAmelCase   :   List[Any]							    =model(**__UpperCAmelCase).loss
             loss.backward()






       def 			lowerCAmelCase		(			self)						->  List[Any]:




          '''simple docstring'''
          _UpperCAmelCase    ,       _UpperCAmelCase   :   Optional[Any]							    =self.model_tester.prepare_config_and_inputs_for_common()
          if not self.model_tester.is_training:
             return

          _UpperCAmelCase   :   str							    =False
          _UpperCAmelCase   :   Any							    =True

          for model_class in self.all_model_classes:
             if model_class in [*get_values(__UpperCAmelCase)] 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":
                _UpperCAmelCase    ,       _UpperCAmelCase    ,       _UpperCAmelCase    ,       _UpperCAmelCase   :   str							    =inputs_dict['pixel_values'].shape
                _UpperCAmelCase   :   Dict							    =torch.zeros(
                    [self.model_tester.batch_size, height, width]  ,							device=__UpperCAmelCase).long()
             _UpperCAmelCase   :   List[str]							    =model_class(__UpperCAmelCase)
             model.gradient_checkpointing_enable()
             model.to(__UpperCAmelCase)
             model.train()
             _UpperCAmelCase   :   Union[str, Any]							    =self._prepare_for_class(__UpperCAmelCase  ,							__UpperCAmelCase  ,							return_labels=__UpperCAmelCase)
             _UpperCAmelCase   :   Dict							    =model(**__UpperCAmelCase).loss
             loss.backward()






       def 			lowerCAmelCase		(			self)						->  Dict:




          '''simple docstring'''
          _UpperCAmelCase    ,       _UpperCAmelCase   :   Tuple							    =self.model_tester.prepare_config_and_inputs_for_common()

          _UpperCAmelCase   :   Optional[Any]							    =_config_zero_init(__UpperCAmelCase)
          for model_class in self.all_model_classes:
             _UpperCAmelCase   :   int							    =model_class(config=__UpperCAmelCase)
             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  ,							snake_case  ,							snake_case  ,							snake_case  ,							snake_case=2E-4  ,							snake_case="outputs"  ,							snake_case=None)						->  Dict:




          '''simple docstring'''
          # We override with a slightly higher tol value, as semseg models tend to diverge a bit more
          super().check_pt_tf_outputs(__UpperCAmelCase  ,							__UpperCAmelCase  ,							__UpperCAmelCase  ,							__UpperCAmelCase  ,							__UpperCAmelCase  ,							__UpperCAmelCase)






       def 			lowerCAmelCase		(			self)						->  Dict:




          '''simple docstring'''
          _UpperCAmelCase   :   Optional[int]							    =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase)






       @slow
       def 			lowerCAmelCase		(			self)						->  Tuple:




          '''simple docstring'''
          for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase   :   Any							    =DataaVecVisionModel.from_pretrained(__UpperCAmelCase)
             self.assertIsNotNone(__UpperCAmelCase)






def lowerCamelCase__			(						):







   '''simple docstring'''




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






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






       @cached_property
       def 			lowerCAmelCase		(			self)						->  Optional[Any]:




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






       @slow
       def 			lowerCAmelCase		(			self)						->  List[Any]:




          '''simple docstring'''
          _UpperCAmelCase   :   Any							    =DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k').to(
              __UpperCAmelCase)

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

          # forward pass
          with torch.no_grad():
             _UpperCAmelCase   :   Any							    =model(**__UpperCAmelCase)
          _UpperCAmelCase   :   Tuple							    =outputs.logits

          # verify the logits
          _UpperCAmelCase   :   List[str]							    =torch.Size((1, 1_0_0_0))
          self.assertEqual(logits.shape  ,							__UpperCAmelCase)

          _UpperCAmelCase   :   List[Any]							    =torch.tensor([0.32_77, -0.13_95, 0.09_11]).to(__UpperCAmelCase)

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

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



"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


__A       						=       {'configuration_bert_generation': ['BertGenerationConfig']}

try:
			if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__A       						=       ['BertGenerationTokenizer']

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__A       						=       [
			    'BertGenerationDecoder',
			    'BertGenerationEncoder',
			    'BertGenerationPreTrainedModel',
			    'load_tf_weights_in_bert_generation',
			]


if TYPE_CHECKING:
			from .configuration_bert_generation import BertGenerationConfig

			try:
						if not is_sentencepiece_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_bert_generation import BertGenerationTokenizer

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_bert_generation import (
						    BertGenerationDecoder,
						    BertGenerationEncoder,
						    BertGenerationPreTrainedModel,
						    load_tf_weights_in_bert_generation,
						)

else:
			import sys

			__A       						=       _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



