

"""simple docstring"""




import os

from ...utils import logging
from .utils import DataProcessor, InputExample


__A	:       Union[str, Any]        =						logging.get_logger(__name__)





class      __UpperCamelCase					(  _A		):


		def __init__(self					:						Dict	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						Tuple=None):
							A     =   language
							A     =   train_language


		def    SCREAMING_SNAKE_CASE__  (self					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						Tuple):
							A     =   self.language if self.train_language is None else self.train_language
							A     =   self._read_tsv(os.path.join(__SCREAMING_SNAKE_CASE	,	F"""XNLI-MT-1.0/multinli/multinli.train.{lg}.tsv"""))
							A     =   []
							for i, line in enumerate(__SCREAMING_SNAKE_CASE):
												if i == 0:
																	continue
												A     =   F"""train-{i}"""
												A     =   line[0]
												A     =   line[1]
												A     =   "contradiction" if line[2] == "contradictory" else line[2]
												if not isinstance(__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE):
																	raise ValueError(F"""Training input {text_a} is not a string""")
												if not isinstance(__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE):
																	raise ValueError(F"""Training input {text_b} is not a string""")
												if not isinstance(__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE):
																	raise ValueError(F"""Training label {label} is not a string""")
												examples.append(InputExample(guid=__SCREAMING_SNAKE_CASE	,	text_a=__SCREAMING_SNAKE_CASE	,	text_b=__SCREAMING_SNAKE_CASE	,	label=__SCREAMING_SNAKE_CASE))
							return examples


		def    SCREAMING_SNAKE_CASE__  (self					:						Tuple	,	__SCREAMING_SNAKE_CASE					:						List[Any]):
							A     =   self._read_tsv(os.path.join(__SCREAMING_SNAKE_CASE	,	"XNLI-1.0/xnli.test.tsv"))
							A     =   []
							for i, line in enumerate(__SCREAMING_SNAKE_CASE):
												if i == 0:
																	continue
												A     =   line[0]
												if language != self.language:
																	continue
												A     =   F"""test-{i}"""
												A     =   line[6]
												A     =   line[7]
												A     =   line[1]
												if not isinstance(__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE):
																	raise ValueError(F"""Training input {text_a} is not a string""")
												if not isinstance(__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE):
																	raise ValueError(F"""Training input {text_b} is not a string""")
												if not isinstance(__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE):
																	raise ValueError(F"""Training label {label} is not a string""")
												examples.append(InputExample(guid=__SCREAMING_SNAKE_CASE	,	text_a=__SCREAMING_SNAKE_CASE	,	text_b=__SCREAMING_SNAKE_CASE	,	label=__SCREAMING_SNAKE_CASE))
							return examples


		def    SCREAMING_SNAKE_CASE__  (self					:						int):
							return ["contradiction", "entailment", "neutral"]

__A	:       List[Any]        =						{
    'xnli': XnliProcessor,
}

__A	:       Optional[int]        =						{
    'xnli': 'classification',
}

__A	:       List[Any]        =						{
    'xnli': 3,
}









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






    '''simple docstring'''





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





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

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





    def        _lowerCamelCase			(						self					: List[str]						):





       '''simple docstring'''
       UpperCAmelCase_				:   List[str]							= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

       UpperCAmelCase_				:   Tuple							= None
       UpperCAmelCase_				:   Dict							= None
       if self.use_labels:
          UpperCAmelCase_				:   Tuple							= ids_tensor([self.batch_size]							, self.type_sequence_label_size						)
          UpperCAmelCase_				:   Any							= ids_tensor([self.batch_size, self.image_size, self.image_size]							, self.num_labels						)

       UpperCAmelCase_				:   Optional[int]							= 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=__snake_case							, initializer_range=self.initializer_range							, out_indices=self.out_indices							, )





    def        _lowerCamelCase			(						self					: Tuple							, __snake_case					: Dict							, __snake_case					: Union[str, Any]							, __snake_case					: str							, __snake_case					: Optional[int]						):





       '''simple docstring'''
       UpperCAmelCase_				:   Optional[Any]							= DataaVecVisionModel(config=__snake_case						)
       model.to(__snake_case						)
       model.eval()
       UpperCAmelCase_				:   Any							= model(__snake_case						)
       # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
       UpperCAmelCase_				:   int							= (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					: Any							, __snake_case					: str							, __snake_case					: Union[str, Any]							, __snake_case					: Optional[int]							, __snake_case					: Any						):





       '''simple docstring'''
       UpperCAmelCase_				:   Optional[int]							= self.type_sequence_label_size
       UpperCAmelCase_				:   Tuple							= DataaVecVisionForImageClassification(__snake_case						)
       model.to(__snake_case						)
       model.eval()
       UpperCAmelCase_				:   int							= model(__snake_case							, labels=__snake_case						)
       self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.type_sequence_label_size)						)





    def        _lowerCamelCase			(						self					: Union[str, Any]							, __snake_case					: Any							, __snake_case					: Optional[Any]							, __snake_case					: Any							, __snake_case					: Dict						):





       '''simple docstring'''
       UpperCAmelCase_				:   List[Any]							= self.num_labels
       UpperCAmelCase_				:   Dict							= DataaVecVisionForSemanticSegmentation(__snake_case						)
       model.to(__snake_case						)
       model.eval()
       UpperCAmelCase_				:   str							= model(__snake_case						)
       self.parent.assertEqual(
           result.logits.shape							, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)						)
       UpperCAmelCase_				:   Any							= model(__snake_case							, labels=__snake_case						)
       self.parent.assertEqual(
           result.logits.shape							, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)						)





    def        _lowerCamelCase			(						self					: Tuple						):





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





@require_torch
class 			lowerCAmelCase__(     snake_case__     ,     snake_case__     ,     unittest.TestCase    ):






    '''simple docstring'''





    A_	:  str								=			(
        (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
        if is_torch_available()
        else ()
    )
    A_	:  int								=			(
        {
            'feature-extraction': DataaVecVisionModel,
            'image-classification': DataaVecVisionForImageClassification,
            'image-segmentation': DataaVecVisionForSemanticSegmentation,
        }
        if is_torch_available()
        else {}
    )

    A_	:  Tuple								=			False
    A_	:  int								=			False
    A_	:  List[str]								=			False





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





       '''simple docstring'''
       UpperCAmelCase_				:   List[Any]							= DataaVecVisionModelTester(self						)
       UpperCAmelCase_				:   Dict							= ConfigTester(
           self							, config_class=__snake_case							, has_text_modality=__snake_case							, hidden_size=37						)





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





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





    def        _lowerCamelCase			(						self					: Optional[Any]						):





       '''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					: Any						):





       '''simple docstring'''
       UpperCAmelCase_		,   UpperCAmelCase_				:   int							= self.model_tester.prepare_config_and_inputs_for_common()

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





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





       '''simple docstring'''
       UpperCAmelCase_		,   UpperCAmelCase_				:   Dict							= self.model_tester.prepare_config_and_inputs_for_common()

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

          UpperCAmelCase_				:   List[Any]							= ['''pixel_values''']
          self.assertListEqual(arg_names[:1]							, __snake_case						)





    def        _lowerCamelCase			(						self					: Optional[Any]						):





       '''simple docstring'''
       UpperCAmelCase_				:   Dict							= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__snake_case						)





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





       '''simple docstring'''
       UpperCAmelCase_				:   Dict							= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_segmentation(*__snake_case						)





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





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

       UpperCAmelCase_		,   UpperCAmelCase_				:   Tuple							= self.model_tester.prepare_config_and_inputs_for_common()
       UpperCAmelCase_				:   List[str]							= True

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

          UpperCAmelCase_				:   Union[str, Any]							= model_class(__snake_case						)
          model.to(__snake_case						)
          model.train()
          UpperCAmelCase_				:   List[Any]							= self._prepare_for_class(__snake_case							, __snake_case							, return_labels=__snake_case						)
          UpperCAmelCase_				:   Any							= model(**__snake_case						).loss
          loss.backward()





    def        _lowerCamelCase			(						self					: int						):





       '''simple docstring'''
       UpperCAmelCase_		,   UpperCAmelCase_				:   Dict							= self.model_tester.prepare_config_and_inputs_for_common()
       if not self.model_tester.is_training:
          return

       UpperCAmelCase_				:   str							= False
       UpperCAmelCase_				:   Union[str, Any]							= True

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





    def        _lowerCamelCase			(						self					: List[Any]						):





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

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





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





    def        _lowerCamelCase			(						self					: Tuple						):





       '''simple docstring'''
       UpperCAmelCase_				:   List[str]							= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__snake_case						)





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





       '''simple docstring'''
       for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          UpperCAmelCase_				:   Optional[int]							= DataaVecVisionModel.from_pretrained(__snake_case						)
          self.assertIsNotNone(__snake_case						)





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





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






    '''simple docstring'''





    @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					: int						):





       '''simple docstring'''
       UpperCAmelCase_				:   int							= DataaVecVisionForImageClassification.from_pretrained('''facebook/data2vec-vision-base-ft1k'''						).to(
           __snake_case						)

       UpperCAmelCase_				:   Optional[Any]							= self.default_image_processor
       UpperCAmelCase_				:   int							= prepare_img()
       UpperCAmelCase_				:   Optional[int]							= image_processor(images=__snake_case							, return_tensors='''pt'''						).to(__snake_case						)

       # forward pass
       with torch.no_grad():
          UpperCAmelCase_				:   int							= model(**__snake_case						)
       UpperCAmelCase_				:   Optional[int]							= outputs.logits

       # verify the logits
       UpperCAmelCase_				:   List[str]							= torch.Size((1, 1_000)						)
       self.assertEqual(logits.shape							, __snake_case						)

       UpperCAmelCase_				:   Optional[Any]							= torch.tensor([0.3_277, -0.1_395, 0.0_911]						).to(__snake_case						)

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

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