





'''simple docstring'''




import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import CvtForImageClassification, CvtModel
 from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor
class 			_lowerCAmelCase		(			A__	):






     """simple docstring"""






     def      lowerCAmelCase			(   self							:			Optional[Any]						)->	Optional[Any]:
        snake_case 						=   self.config_class(**self.inputs_dict						)
        self.parent.assertTrue(hasattr(__snake_case   ,       """embed_dim"""						)						)
        self.parent.assertTrue(hasattr(__snake_case   ,       """num_heads"""						)						)




class 			_lowerCAmelCase		:






     """simple docstring"""






     def __init__(   self							:			Union[str, Any]   ,       __snake_case							:			List[str]   ,       __snake_case							:			int=13   ,       __snake_case							:			Union[str, Any]=64   ,       __snake_case							:			str=3   ,       __snake_case							:			Any=[16, 48, 96]   ,       __snake_case							:			str=[1, 3, 6]   ,       __snake_case							:			Tuple=[1, 2, 10]   ,       __snake_case							:			Dict=[7, 3, 3]   ,       __snake_case							:			Union[str, Any]=[4, 2, 2]   ,       __snake_case							:			Optional[int]=[2, 1, 1]   ,       __snake_case							:			Dict=[2, 2, 2]   ,       __snake_case							:			int=[False, False, True]   ,       __snake_case							:			Any=[0.0, 0.0, 0.0]   ,       __snake_case							:			List[Any]=0.02   ,       __snake_case							:			Optional[int]=1e-12   ,       __snake_case							:			Optional[int]=True   ,       __snake_case							:			str=True   ,       __snake_case							:			Tuple=2   ,       )->	Optional[Any]:
        snake_case 						=   parent
        snake_case 						=   batch_size
        snake_case 						=   image_size
        snake_case 						=   patch_sizes
        snake_case 						=   patch_stride
        snake_case 						=   patch_padding
        snake_case 						=   is_training
        snake_case 						=   use_labels
        snake_case 						=   num_labels
        snake_case 						=   num_channels
        snake_case 						=   embed_dim
        snake_case 						=   num_heads
        snake_case 						=   stride_kv
        snake_case 						=   depth
        snake_case 						=   cls_token
        snake_case 						=   attention_drop_rate
        snake_case 						=   initializer_range
        snake_case 						=   layer_norm_eps



     def      lowerCAmelCase			(   self							:			Union[str, Any]						)->	str:
        snake_case 						=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

        snake_case 						=   None
        if self.use_labels:
           snake_case 						=   ids_tensor([self.batch_size]   ,       self.num_labels						)

        snake_case 						=   self.get_config()
        return config, pixel_values, labels



     def      lowerCAmelCase			(   self							:			Optional[Any]						)->	List[str]:
        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      lowerCAmelCase			(   self							:			Optional[int]   ,       __snake_case							:			Tuple   ,       __snake_case							:			Dict   ,       __snake_case							:			Optional[Any]						)->	Optional[Any]:
        snake_case 						=   CvtModel(config=__snake_case						)
        model.to(__snake_case						)
        model.eval()
        snake_case 						=   model(__snake_case						)
        snake_case 						=   (self.image_size, self.image_size)
        snake_case						,    snake_case 						=   image_size[0], image_size[1]
        for i in range(len(self.depth						)						):
           snake_case 						=   floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1						)
           snake_case 						=   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      lowerCAmelCase			(   self							:			Union[str, Any]   ,       __snake_case							:			int   ,       __snake_case							:			Tuple   ,       __snake_case							:			List[Any]						)->	int:
        snake_case 						=   self.num_labels
        snake_case 						=   CvtForImageClassification(__snake_case						)
        model.to(__snake_case						)
        model.eval()
        snake_case 						=   model(__snake_case   ,       labels=__snake_case						)
        self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.num_labels)						)



     def      lowerCAmelCase			(   self							:			Any						)->	Dict:
        snake_case 						=   self.prepare_config_and_inputs()
        snake_case						,    snake_case						,    snake_case 						=   config_and_inputs
        snake_case 						=   {"""pixel_values""": pixel_values}
        return config, inputs_dict




@require_torch
class 			_lowerCAmelCase		(			A__							,     A__							,     unittest.TestCase	):






     """simple docstring"""






     snake_case_						      =   (CvtModel, CvtForImageClassification) if is_torch_available() else ()
     snake_case_						      =   (
         {"feature-extraction": CvtModel, "image-classification": CvtForImageClassification}
         if is_torch_available()
         else {}
     )

     snake_case_						      =   False
     snake_case_						      =   False
     snake_case_						      =   False
     snake_case_						      =   False
     snake_case_						      =   False



     def      lowerCAmelCase			(   self							:			List[str]						)->	Optional[int]:
        snake_case 						=   CvtModelTester(self						)
        snake_case 						=   ConfigTester(self   ,       config_class=__snake_case   ,       has_text_modality=__snake_case   ,       hidden_size=37						)



     def      lowerCAmelCase			(   self							:			int						)->	Optional[int]:
        self.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()



     def      lowerCAmelCase			(   self							:			Optional[int]						)->	Dict:
        return



     @unittest.skip(reason="""Cvt does not output attentions"""						)
     def      lowerCAmelCase			(   self							:			Optional[int]						)->	Union[str, Any]:
        pass



     @unittest.skip(reason="""Cvt does not use inputs_embeds"""						)
     def      lowerCAmelCase			(   self							:			str						)->	Optional[Any]:
        pass



     @unittest.skip(reason="""Cvt does not support input and output embeddings"""						)
     def      lowerCAmelCase			(   self							:			List[Any]						)->	List[str]:
        pass



     def      lowerCAmelCase			(   self							:			Any						)->	Any:
        snake_case						,    snake_case 						=   self.model_tester.prepare_config_and_inputs_for_common()

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

           snake_case 						=   ["""pixel_values"""]
           self.assertListEqual(arg_names[:1]   ,       __snake_case						)



     def      lowerCAmelCase			(   self							:			Tuple						)->	Tuple:
        snake_case 						=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__snake_case						)



     def      lowerCAmelCase			(   self							:			Dict						)->	str:
        def check_hidden_states_output(__snake_case							:			Optional[Any]   ,       __snake_case							:			int   ,       __snake_case							:			Optional[int]						):
           snake_case 						=   model_class(__snake_case						)
           model.to(__snake_case						)
           model.eval()

           with torch.no_grad():
              snake_case 						=   model(**self._prepare_for_class(__snake_case   ,       __snake_case						)						)

           snake_case 						=   outputs.hidden_states

           snake_case 						=   len(self.model_tester.depth						)
           self.assertEqual(len(__snake_case						)   ,       __snake_case						)

           # 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,
               ]   ,       )

        snake_case						,    snake_case 						=   self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           snake_case 						=   True
           check_hidden_states_output(__snake_case   ,       __snake_case   ,       __snake_case						)

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           snake_case 						=   True

           check_hidden_states_output(__snake_case   ,       __snake_case   ,       __snake_case						)



     def      lowerCAmelCase			(   self							:			Dict						)->	Optional[int]:
        snake_case 						=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__snake_case						)



     @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""						)
     def      lowerCAmelCase			(   self							:			List[Any]						)->	Union[str, Any]:
        pass



     @slow
     def      lowerCAmelCase			(   self							:			List[Any]						)->	List[Any]:
        for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case 						=   CvtModel.from_pretrained(__snake_case						)
           self.assertIsNotNone(__snake_case						)





def 	__lowerCamelCase			(      )		->  Optional[Any]:
   snake_case 						=   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							:			List[Any]						)->	int:
        return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)



     @slow
     def      lowerCAmelCase			(   self							:			Any						)->	Any:
        snake_case 						=   CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						).to(__snake_case						)

        snake_case 						=   self.default_image_processor
        snake_case 						=   prepare_img()
        snake_case 						=   image_processor(images=__snake_case   ,       return_tensors="""pt"""						).to(__snake_case						)

        # forward pass
        with torch.no_grad():
           snake_case 						=   model(**__snake_case						)

        # verify the logits
        snake_case 						=   torch.Size((1, 10_00)						)
        self.assertEqual(outputs.logits.shape   ,       __snake_case						)

        snake_case 						=   torch.tensor([0.92_85, 0.90_15, -0.31_50]						).to(__snake_case						)

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








'''simple docstring'''




import json
import os
import unittest

from transformers import DebertaTokenizer, DebertaTokenizerFast
from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin
class 			_lowerCAmelCase		(			A__							,     unittest.TestCase	):






     """simple docstring"""






     snake_case_						      =   DebertaTokenizer
     snake_case_						      =   True
     snake_case_						      =   DebertaTokenizerFast



     def      lowerCAmelCase			(   self							:			Any						)->	Optional[int]:
        super().setUp()

        # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
        snake_case 						=   [
            """l""",
            """o""",
            """w""",
            """e""",
            """r""",
            """s""",
            """t""",
            """i""",
            """d""",
            """n""",
            """\u0120""",
            """\u0120l""",
            """\u0120n""",
            """\u0120lo""",
            """\u0120low""",
            """er""",
            """\u0120lowest""",
            """\u0120newer""",
            """\u0120wider""",
            """[UNK]""",
        ]
        snake_case 						=   dict(zip(__snake_case   ,       range(len(__snake_case						)						)						)						)
        snake_case 						=   ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""]
        snake_case 						=   {"""unk_token""": """[UNK]"""}

        snake_case 						=   os.path.join(self.tmpdirname   ,       VOCAB_FILES_NAMES["""vocab_file"""]						)
        snake_case 						=   os.path.join(self.tmpdirname   ,       VOCAB_FILES_NAMES["""merges_file"""]						)
        with open(self.vocab_file   ,       """w"""   ,       encoding="""utf-8"""						) as fp:
           fp.write(json.dumps(__snake_case						) + """\n"""						)
        with open(self.merges_file   ,       """w"""   ,       encoding="""utf-8"""						) as fp:
           fp.write("""\n""".join(__snake_case						)						)



     def      lowerCAmelCase			(   self							:			Optional[Any]   ,       **__snake_case							:			Union[str, Any]						)->	int:
        kwargs.update(self.special_tokens_map						)
        return self.tokenizer_class.from_pretrained(self.tmpdirname   ,       **__snake_case						)



     def      lowerCAmelCase			(   self							:			Any   ,       __snake_case							:			int						)->	Any:
        snake_case 						=   """lower newer"""
        snake_case 						=   """lower newer"""
        return input_text, output_text



     def      lowerCAmelCase			(   self							:			str						)->	Any:
        snake_case 						=   self.get_tokenizer()
        snake_case 						=   """lower newer"""
        snake_case 						=   ["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""]
        snake_case 						=   tokenizer.tokenize(__snake_case						)
        self.assertListEqual(__snake_case   ,       __snake_case						)

        snake_case 						=   tokens + [tokenizer.unk_token]
        snake_case 						=   [0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case						)   ,       __snake_case						)



     def      lowerCAmelCase			(   self							:			Any						)->	List[str]:
        snake_case 						=   self.get_tokenizer()
        snake_case 						=   tokenizer("""Hello"""   ,       """World"""						)
        snake_case 						=   [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
        self.assertListEqual(tokd["""token_type_ids"""]   ,       __snake_case						)



     @slow
     def      lowerCAmelCase			(   self							:			Optional[int]						)->	Union[str, Any]:
        snake_case 						=   self.tokenizer_class.from_pretrained("""microsoft/deberta-base"""						)

        snake_case 						=   tokenizer.encode("""sequence builders"""   ,       add_special_tokens=__snake_case						)
        snake_case 						=   tokenizer.encode("""multi-sequence build"""   ,       add_special_tokens=__snake_case						)

        snake_case 						=   tokenizer.encode(
            """sequence builders"""   ,       add_special_tokens=__snake_case   ,       add_prefix_space=__snake_case						)
        snake_case 						=   tokenizer.encode(
            """sequence builders"""   ,       """multi-sequence build"""   ,       add_special_tokens=__snake_case   ,       add_prefix_space=__snake_case						)

        snake_case 						=   tokenizer.build_inputs_with_special_tokens(__snake_case						)
        snake_case 						=   tokenizer.build_inputs_with_special_tokens(__snake_case   ,       __snake_case						)

        assert encoded_sentence == encoded_text_from_decode
        assert encoded_pair == encoded_pair_from_decode



     @slow
     def      lowerCAmelCase			(   self							:			Any						)->	Union[str, Any]:
        snake_case 						=   [self.tokenizer_class]
        if self.test_rust_tokenizer:
           tokenizer_classes.append(self.rust_tokenizer_class						)

        for tokenizer_class in tokenizer_classes:
           snake_case 						=   tokenizer_class.from_pretrained("""microsoft/deberta-base"""						)

           snake_case 						=   [
               """ALBERT: A Lite BERT for Self-supervised Learning of Language Representations""",
               """ALBERT incorporates two parameter reduction techniques""",
               """The first one is a factorized embedding parameterization. By decomposing the large vocabulary"""
               """ embedding matrix into two small matrices, we separate the size of the hidden layers from the size of"""
               """ vocabulary embedding.""",
           ]

           snake_case 						=   tokenizer(__snake_case   ,       padding=__snake_case						)
           snake_case 						=   [tokenizer.decode(__snake_case   ,       skip_special_tokens=__snake_case						) for seq in encoding["""input_ids"""]]

           # fmt: off
           snake_case 						=   {
               """input_ids""": [
                   [1, 21_18, 1_11_26, 5_65, 35, 83, 2_51_91, 1_63, 1_88_54, 13, 1_21_56, 12, 1_61_01, 2_53_76, 1_38_07, 9, 2_22_05, 2_78_93, 16_35, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 21_18, 1_11_26, 5_65, 2_45_36, 80, 4_37_97, 48_78, 73_73, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 1_33, 78, 65, 16, 10, 37_24, 15_38, 3_31_83, 1_13_03, 4_37_97, 19_38, 4, 8_70, 2_41_65, 2_91_05, 5, 7_39, 3_26_44, 3_31_83, 1_13_03, 3_61_73, 88, 80, 6_50, 78_21, 4_59_40, 6, 52, 25_59, 5, 18_36, 9, 5, 73_97, 1_31_71, 31, 5, 18_36, 9, 3_26_44, 3_31_83, 1_13_03, 4, 2]
               ],
               """token_type_ids""": [
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
               ],
               """attention_mask""": [
                   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
               ]
           }
           # fmt: on

           snake_case 						=   [
               """ALBERT: A Lite BERT for Self-supervised Learning of Language Representations""",
               """ALBERT incorporates two parameter reduction techniques""",
               """The first one is a factorized embedding parameterization. By decomposing the large vocabulary"""
               """ embedding matrix into two small matrices, we separate the size of the hidden layers from the size of"""
               """ vocabulary embedding.""",
           ]

           self.assertDictEqual(encoding.data   ,       __snake_case						)

           for expected, decoded in zip(__snake_case   ,       __snake_case						):
              self.assertEqual(__snake_case   ,       __snake_case						)

