




'''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 						__UpperCamelCase :






       def __init__(							self,	lowerCAmelCase,	lowerCAmelCase=100,	lowerCAmelCase=13,	lowerCAmelCase=30,	lowerCAmelCase=2,	lowerCAmelCase=3,	lowerCAmelCase=True,	lowerCAmelCase=True,	lowerCAmelCase=32,	lowerCAmelCase=4,	lowerCAmelCase=4,	lowerCAmelCase=37,	lowerCAmelCase="gelu",	lowerCAmelCase=0.1,	lowerCAmelCase=0.1,	lowerCAmelCase=10,	lowerCAmelCase=0.0_2,	lowerCAmelCase=3,	lowerCAmelCase=None,	lowerCAmelCase=[0, 1, 2, 3],	):



              """simple docstring"""







              lowerCamelCase_							=parent
              lowerCamelCase_							=100
              lowerCamelCase_							=batch_size
              lowerCamelCase_							=image_size
              lowerCamelCase_							=patch_size
              lowerCamelCase_							=num_channels
              lowerCamelCase_							=is_training
              lowerCamelCase_							=use_labels
              lowerCamelCase_							=hidden_size
              lowerCamelCase_							=num_hidden_layers
              lowerCamelCase_							=num_attention_heads
              lowerCamelCase_							=intermediate_size
              lowerCamelCase_							=hidden_act
              lowerCamelCase_							=hidden_dropout_prob
              lowerCamelCase_							=attention_probs_dropout_prob
              lowerCamelCase_							=type_sequence_label_size
              lowerCamelCase_							=initializer_range
              lowerCamelCase_							=scope
              lowerCamelCase_							=out_indices
              lowerCamelCase_							=num_labels

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






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

              lowerCamelCase_							=None
              lowerCamelCase_							=None
              if self.use_labels:
                     lowerCamelCase_							=ids_tensor([self.batch_size],	self.type_sequence_label_size						)
                     lowerCamelCase_							=ids_tensor([self.batch_size, self.image_size, self.image_size],	self.num_labels						)

              lowerCamelCase_							=self.get_config()

              return config, pixel_values, labels, pixel_labels






       def   lowercase__  (							self						):



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






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=DataaVecVisionModel(config=lowerCAmelCase						)
              model.to(lowerCAmelCase						)
              model.eval()
              lowerCamelCase_							=model(lowerCAmelCase						)
              # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
              lowerCamelCase_							=(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   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=self.type_sequence_label_size
              lowerCamelCase_							=DataaVecVisionForImageClassification(lowerCAmelCase						)
              model.to(lowerCAmelCase						)
              model.eval()
              lowerCamelCase_							=model(lowerCAmelCase,	labels=lowerCAmelCase						)
              self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.type_sequence_label_size)						)






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						):



              """simple docstring"""







              lowerCamelCase_							=self.num_labels
              lowerCamelCase_							=DataaVecVisionForSemanticSegmentation(lowerCAmelCase						)
              model.to(lowerCAmelCase						)
              model.eval()
              lowerCamelCase_							=model(lowerCAmelCase						)
              self.parent.assertEqual(
                  result.logits.shape,	(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)						)
              lowerCamelCase_							=model(lowerCAmelCase,	labels=lowerCAmelCase						)
              self.parent.assertEqual(
                  result.logits.shape,	(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)						)






       def   lowercase__  (							self						):



              """simple docstring"""







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





@require_torch
class 						__UpperCamelCase (				lowerCamelCase__    ,      lowerCamelCase__    ,      unittest.TestCase       ):
       lowercase			:					Dict   		=(
           (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
           if is_torch_available()
           else ()
       )
       lowercase			:					Optional[int]   		=(
           {
               'feature-extraction': DataaVecVisionModel,
               'image-classification': DataaVecVisionForImageClassification,
               'image-segmentation': DataaVecVisionForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       lowercase			:					List[Any]   		=False
       lowercase			:					Optional[int]   		=False
       lowercase			:					int   		=False






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=DataaVecVisionModelTester(self						)
              lowerCamelCase_							=ConfigTester(
                  self,	config_class=lowerCAmelCase,	has_text_modality=lowerCAmelCase,	hidden_size=37						)






       def   lowercase__  (							self						):



              """simple docstring"""







              self.config_tester.run_common_tests()






       def   lowercase__  (							self						):



              """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   lowercase__  (							self						):



              """simple docstring"""







              pass






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_,   lowerCamelCase_							=self.model_tester.prepare_config_and_inputs_for_common()

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






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_,   lowerCamelCase_							=self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     lowerCamelCase_							=model_class(lowerCAmelCase						)
                     lowerCamelCase_							=inspect.signature(model.forward						)
                     # signature.parameters is an OrderedDict => so arg_names order is deterministic
                     lowerCamelCase_							=[*signature.parameters.keys()]

                     lowerCamelCase_							=['''pixel_values''']
                     self.assertListEqual(arg_names[:1],	lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_image_segmentation(*lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              if not self.model_tester.is_training:
                     return

              lowerCamelCase_,   lowerCamelCase_							=self.model_tester.prepare_config_and_inputs_for_common()
              lowerCamelCase_							=True

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

                     lowerCamelCase_							=model_class(lowerCAmelCase						)
                     model.to(lowerCAmelCase						)
                     model.train()
                     lowerCamelCase_							=self._prepare_for_class(lowerCAmelCase,	lowerCAmelCase,	return_labels=lowerCAmelCase						)
                     lowerCamelCase_							=model(**lowerCAmelCase						).loss
                     loss.backward()






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_,   lowerCamelCase_							=self.model_tester.prepare_config_and_inputs_for_common()
              if not self.model_tester.is_training:
                     return

              lowerCamelCase_							=False
              lowerCamelCase_							=True

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






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_,   lowerCamelCase_							=self.model_tester.prepare_config_and_inputs_for_common()

              lowerCamelCase_							=_config_zero_init(lowerCAmelCase						)
              for model_class in self.all_model_classes:
                     lowerCamelCase_							=model_class(config=lowerCAmelCase						)
                     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   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase=2e-4,	lowerCAmelCase="outputs",	lowerCAmelCase=None						):



              """simple docstring"""







              super().check_pt_tf_outputs(lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase,	lowerCAmelCase						)






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase						)






       @slow
       def   lowercase__  (							self						):



              """simple docstring"""







              for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     lowerCamelCase_							=DataaVecVisionModel.from_pretrained(lowerCAmelCase						)
                     self.assertIsNotNone(lowerCAmelCase						)





def   a_							(   )							->		str:



       """simple docstring"""
       lowerCamelCase_							=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
       return image


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






       @cached_property
       def   lowercase__  (							self						):



              """simple docstring"""







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






       @slow
       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=DataaVecVisionForImageClassification.from_pretrained('''facebook/data2vec-vision-base-ft1k'''						).to(
                  lowerCAmelCase						)

              lowerCamelCase_							=self.default_image_processor
              lowerCamelCase_							=prepare_img()
              lowerCamelCase_							=image_processor(images=lowerCAmelCase,	return_tensors='''pt'''						).to(lowerCAmelCase						)

              # forward pass
              with torch.no_grad():
                     lowerCamelCase_							=model(**lowerCAmelCase						)
              lowerCamelCase_							=outputs.logits

              # verify the logits
              lowerCamelCase_							=torch.Size((1, 1_000)						)
              self.assertEqual(logits.shape,	lowerCAmelCase						)

              lowerCamelCase_							=torch.tensor([0.3_2_7_7, -0.1_3_9_5, 0.0_9_1_1]						).to(lowerCAmelCase						)

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

              lowerCamelCase_							=[model.config.labelaid[i] for i in ['''remote control, remote''', '''tabby, tabby cat''']]
              self.assertEqual(logits[0].topk(2						).indices.cpu().tolist(),	lowerCAmelCase						)







'''simple docstring'''




from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_clip import CLIPTokenizer


a_   :	Tuple							   = logging.get_logger(__name__)

a_   :	str							   = {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

a_   :	Tuple							   = {
    """vocab_file""": {
        """openai/clip-vit-base-patch32""": """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """openai/clip-vit-base-patch32""": """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """openai/clip-vit-base-patch32""": (
            """https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/tokenizer.json"""
        ),
    },
}

a_   :	str							   = {
    """openai/clip-vit-base-patch32""": 77,
}


class 						__UpperCamelCase (				lowerCamelCase__       ):
       lowercase			:					Union[str, Any]   		=VOCAB_FILES_NAMES
       lowercase			:					Optional[int]   		=PRETRAINED_VOCAB_FILES_MAP
       lowercase			:					Any   		=PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       lowercase			:					str   		=['input_ids', 'attention_mask']
       lowercase			:					List[Any]   		=CLIPTokenizer






       def __init__(							self,	lowerCAmelCase=None,	lowerCAmelCase=None,	lowerCAmelCase=None,	lowerCAmelCase="<|endoftext|>",	lowerCAmelCase="<|startoftext|>",	lowerCAmelCase="<|endoftext|>",	lowerCAmelCase="<|endoftext|>",	**lowerCAmelCase,	):



              """simple docstring"""







              super().__init__(
                  lowerCAmelCase,	lowerCAmelCase,	tokenizer_file=lowerCAmelCase,	unk_token=lowerCAmelCase,	bos_token=lowerCAmelCase,	eos_token=lowerCAmelCase,	pad_token=lowerCAmelCase,	**lowerCAmelCase,	)

              if not isinstance(self.backend_tokenizer.pre_tokenizer,	pre_tokenizers.Sequence						):
                     raise ValueError(
                         '''The `backend_tokenizer` provided does not match the expected format. The CLIP tokenizer has been'''
                         ''' heavily modified from transformers version 4.17.0. You need to convert the tokenizer you are using'''
                         ''' to be compatible with this version.The easiest way to do so is'''
                         ''' `CLIPTokenizerFast.from_pretrained("path_to_local_folder_or_hub_repo, from_slow=True)`. If you want'''
                         ''' to use your existing tokenizer, you will have to revert to a version prior to 4.17.0 of'''
                         ''' transformers.'''						)

              self._wrap_decode_method_backend_tokenizer()






       def   lowercase__  (							self						):



              """simple docstring"""







              lowerCamelCase_							=self.backend_tokenizer.decode

              def new_decode_method(*lowerCAmelCase,	**lowerCAmelCase						):
                     lowerCamelCase_							=orig_decode_method(*lowerCAmelCase,	**lowerCAmelCase						)
                     lowerCamelCase_							=text.replace(self.backend_tokenizer.model.end_of_word_suffix,	''' '''						).strip()
                     return text

              lowerCamelCase_							=new_decode_method






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase = None						):



              """simple docstring"""







              lowerCamelCase_							=[self.bos_token_id]
              lowerCamelCase_							=[self.eos_token_id]

              if token_ids_a is None:
                     return bos_token + token_ids_a + eos_token
              return bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase = None						):



              """simple docstring"""







              lowerCamelCase_							=[self.bos_token_id]
              lowerCamelCase_							=[self.eos_token_id]

              if token_ids_a is None:
                     return len(bos_token + token_ids_a + eos_token						) * [0]
              return len(bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token						) * [0]






       def   lowercase__  (							self,	lowerCAmelCase,	lowerCAmelCase = None						):



              """simple docstring"""







              lowerCamelCase_							=self._tokenizer.model.save(lowerCAmelCase,	name=lowerCAmelCase						)
              return tuple(lowerCAmelCase						)




