


'''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 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_						):



     """simple docstring"""






     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Dict       =							self.config_class(**self.inputs_dict )
            self.parent.assertTrue(hasattr(snake_case__   ,						'embed_dim' ) )
            self.parent.assertTrue(hasattr(snake_case__   ,						'num_heads' ) )
class 						UpperCamelCase__       :



     """simple docstring"""






     def __init__(			self   ,						snake_case__   ,						snake_case__=13   ,						snake_case__=64   ,						snake_case__=3   ,						snake_case__=[16, 48, 96]   ,						snake_case__=[1, 3, 6]   ,						snake_case__=[1, 2, 10]   ,						snake_case__=[7, 3, 3]   ,						snake_case__=[4, 2, 2]   ,						snake_case__=[2, 1, 1]   ,						snake_case__=[2, 2, 2]   ,						snake_case__=[False, False, True]   ,						snake_case__=[0.0, 0.0, 0.0]   ,						snake_case__=0.02   ,						snake_case__=1E-12   ,						snake_case__=True   ,						snake_case__=True   ,						snake_case__=2   ,						):

            '''simple docstring'''

            _lowerCAmelCase     :				Union[str, Any]       =							parent
            _lowerCAmelCase     :				Optional[Any]       =							batch_size
            _lowerCAmelCase     :				Tuple       =							image_size
            _lowerCAmelCase     :				Union[str, Any]       =							patch_sizes
            _lowerCAmelCase     :				List[Any]       =							patch_stride
            _lowerCAmelCase     :				List[Any]       =							patch_padding
            _lowerCAmelCase     :				Union[str, Any]       =							is_training
            _lowerCAmelCase     :				List[str]       =							use_labels
            _lowerCAmelCase     :				str       =							num_labels
            _lowerCAmelCase     :				str       =							num_channels
            _lowerCAmelCase     :				Union[str, Any]       =							embed_dim
            _lowerCAmelCase     :				int       =							num_heads
            _lowerCAmelCase     :				Tuple       =							stride_kv
            _lowerCAmelCase     :				List[Any]       =							depth
            _lowerCAmelCase     :				Optional[Any]       =							cls_token
            _lowerCAmelCase     :				str       =							attention_drop_rate
            _lowerCAmelCase     :				List[Any]       =							initializer_range
            _lowerCAmelCase     :				Optional[int]       =							layer_norm_eps







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[str]       =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

            _lowerCAmelCase     :				Tuple       =							None
            if self.use_labels:
                   _lowerCAmelCase     :				int       =							ids_tensor([self.batch_size]   ,						self.num_labels )

            _lowerCAmelCase     :				str       =							self.get_config()
            return config, pixel_values, labels







     def a						(			self ):

            '''simple docstring'''

            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 a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

            '''simple docstring'''

            _lowerCAmelCase     :				Tuple       =							CvtModel(config=snake_case__ )
            model.to(snake_case__ )
            model.eval()
            _lowerCAmelCase     :				List[Any]       =							model(snake_case__ )
            _lowerCAmelCase     :				Union[str, Any]       =							(self.image_size, self.image_size)
            _lowerCAmelCase      ,  _lowerCAmelCase     :				Optional[int]       =							image_size[0], image_size[1]
            for i in range(len(self.depth ) ):
                   _lowerCAmelCase     :				Optional[int]       =							floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
                   _lowerCAmelCase     :				List[Any]       =							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 a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[int]       =							self.num_labels
            _lowerCAmelCase     :				Any       =							CvtForImageClassification(snake_case__ )
            model.to(snake_case__ )
            model.eval()
            _lowerCAmelCase     :				str       =							model(snake_case__   ,						labels=snake_case__ )
            self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.num_labels) )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Tuple       =							self.prepare_config_and_inputs()
            _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase     :				int       =							config_and_inputs
            _lowerCAmelCase     :				Dict       =							{'pixel_values': pixel_values}
            return config, inputs_dict
@require_torch
class 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_     ,       SCREAMING_SNAKE_CASE_     ,       unittest.TestCase						):



     """simple docstring"""






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

     __magic_name__           =     False
     __magic_name__           =     False
     __magic_name__           =     False
     __magic_name__           =     False
     __magic_name__           =     False







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Dict       =							CvtModelTester(self )
            _lowerCAmelCase     :				Dict       =							ConfigTester(self   ,						config_class=snake_case__   ,						has_text_modality=snake_case__   ,						hidden_size=37 )







     def a						(			self ):

            '''simple docstring'''

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

            '''simple docstring'''

            return







     @unittest.skip(reason='Cvt does not output attentions' )
     def a						(			self ):

            '''simple docstring'''

            pass







     @unittest.skip(reason='Cvt does not use inputs_embeds' )
     def a						(			self ):

            '''simple docstring'''

            pass







     @unittest.skip(reason='Cvt does not support input and output embeddings' )
     def a						(			self ):

            '''simple docstring'''

            pass







     def a						(			self ):

            '''simple docstring'''

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

            for model_class in self.all_model_classes:
                   _lowerCAmelCase     :				List[str]       =							model_class(snake_case__ )
                   _lowerCAmelCase     :				Tuple       =							inspect.signature(model.forward )
                   # signature.parameters is an OrderedDict => so arg_names order is deterministic
                   _lowerCAmelCase     :				Any       =							[*signature.parameters.keys()]

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







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				str       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*snake_case__ )







     def a						(			self ):

            '''simple docstring'''

            def check_hidden_states_output(snake_case__   ,						snake_case__   ,						snake_case__ ):
                   _lowerCAmelCase     :				str       =							model_class(snake_case__ )
                   model.to(snake_case__ )
                   model.eval()

                   with torch.no_grad():
                          _lowerCAmelCase     :				Any       =							model(**self._prepare_for_class(snake_case__   ,						snake_case__ ) )

                   _lowerCAmelCase     :				Optional[Any]       =							outputs.hidden_states

                   _lowerCAmelCase     :				Optional[Any]       =							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,
                       ]   ,						)

            _lowerCAmelCase      ,  _lowerCAmelCase     :				Tuple       =							self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   _lowerCAmelCase     :				Any       =							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"]
                   _lowerCAmelCase     :				List[str]       =							True

                   check_hidden_states_output(snake_case__   ,						snake_case__   ,						snake_case__ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[int]       =							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 a						(			self ):

            '''simple docstring'''

            pass







     @slow
     def a						(			self ):

            '''simple docstring'''

            for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _lowerCAmelCase     :				int       =							CvtModel.from_pretrained(snake_case__ )
                   self.assertIsNotNone(snake_case__ )






def       lowercase					():







       """simple docstring"""







       _lowerCAmelCase     :				List[str]       =							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
       return image
@require_torch
@require_vision
class 						UpperCamelCase__       (			unittest.TestCase						):



     """simple docstring"""






     @cached_property
     def a						(			self ):

            '''simple docstring'''

            return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )







     @slow
     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				int       =							CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(snake_case__ )

            _lowerCAmelCase     :				List[str]       =							self.default_image_processor
            _lowerCAmelCase     :				Any       =							prepare_img()
            _lowerCAmelCase     :				Dict       =							image_processor(images=snake_case__   ,						return_tensors='pt' ).to(snake_case__ )

            # forward pass
            with torch.no_grad():
                   _lowerCAmelCase     :				Optional[int]       =							model(**snake_case__ )

            # verify the logits
            _lowerCAmelCase     :				Optional[Any]       =							torch.Size((1, 1000) )
            self.assertEqual(outputs.logits.shape   ,						snake_case__ )

            _lowerCAmelCase     :				int       =							torch.tensor([0.9285, 0.9015, -0.3150] ).to(snake_case__ )

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





'''simple docstring'''







import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_gpta import GPTaTokenizer


if TYPE_CHECKING:
       from transformers.pipelines.conversational import Conversation


lowerCAmelCase     :  Tuple											=    logging.get_logger(__name__)

lowerCAmelCase     :  Union[str, Any]											=    {"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

lowerCAmelCase     :  str											=    {
    """vocab_file""": {
        """gpt2""": """https://huggingface.co/gpt2/resolve/main/vocab.json""",
        """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/vocab.json""",
        """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/vocab.json""",
        """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/vocab.json""",
        """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """gpt2""": """https://huggingface.co/gpt2/resolve/main/merges.txt""",
        """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/merges.txt""",
        """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/merges.txt""",
        """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/merges.txt""",
        """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """gpt2""": """https://huggingface.co/gpt2/resolve/main/tokenizer.json""",
        """gpt2-medium""": """https://huggingface.co/gpt2-medium/resolve/main/tokenizer.json""",
        """gpt2-large""": """https://huggingface.co/gpt2-large/resolve/main/tokenizer.json""",
        """gpt2-xl""": """https://huggingface.co/gpt2-xl/resolve/main/tokenizer.json""",
        """distilgpt2""": """https://huggingface.co/distilgpt2/resolve/main/tokenizer.json""",
    },
}

lowerCAmelCase     :  str											=    {
    """gpt2""": 10_24,
    """gpt2-medium""": 10_24,
    """gpt2-large""": 10_24,
    """gpt2-xl""": 10_24,
    """distilgpt2""": 10_24,
}
class 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_						):



     """simple docstring"""






     __magic_name__           =     VOCAB_FILES_NAMES
     __magic_name__           =     PRETRAINED_VOCAB_FILES_MAP
     __magic_name__           =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     __magic_name__           =     ["input_ids", "attention_mask"]
     __magic_name__           =     GPTaTokenizer







     def __init__(			self   ,						snake_case__=None   ,						snake_case__=None   ,						snake_case__=None   ,						snake_case__="<|endoftext|>"   ,						snake_case__="<|endoftext|>"   ,						snake_case__="<|endoftext|>"   ,						snake_case__=False   ,						**snake_case__   ,						):

            '''simple docstring'''

            super().__init__(
                snake_case__   ,						snake_case__   ,						tokenizer_file=snake_case__   ,						unk_token=snake_case__   ,						bos_token=snake_case__   ,						eos_token=snake_case__   ,						add_prefix_space=snake_case__   ,						**snake_case__   ,						)

            _lowerCAmelCase     :				str       =							kwargs.pop('add_bos_token'   ,						snake_case__ )

            _lowerCAmelCase     :				List[Any]       =							json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__() )
            if pre_tok_state.get('add_prefix_space'   ,						snake_case__ ) != add_prefix_space:
                   _lowerCAmelCase     :				str       =							getattr(snake_case__   ,						pre_tok_state.pop('type' ) )
                   _lowerCAmelCase     :				List[str]       =							add_prefix_space
                   _lowerCAmelCase     :				Dict       =							pre_tok_class(**snake_case__ )

            _lowerCAmelCase     :				Any       =							add_prefix_space







     def a						(			self   ,						*snake_case__   ,						**snake_case__ ):

            '''simple docstring'''

            _lowerCAmelCase     :				int       =							kwargs.get('is_split_into_words'   ,						snake_case__ )
            assert self.add_prefix_space or not is_split_into_words, (
                F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
                "to use it with pretokenized inputs."
            )

            return super()._batch_encode_plus(*snake_case__   ,						**snake_case__ )







     def a						(			self   ,						*snake_case__   ,						**snake_case__ ):

            '''simple docstring'''

            _lowerCAmelCase     :				int       =							kwargs.get('is_split_into_words'   ,						snake_case__ )

            assert self.add_prefix_space or not is_split_into_words, (
                F'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
                "to use it with pretokenized inputs."
            )

            return super()._encode_plus(*snake_case__   ,						**snake_case__ )







     def a						(			self   ,						snake_case__   ,						snake_case__ = None ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[int]       =							self._tokenizer.model.save(snake_case__   ,						name=snake_case__ )
            return tuple(snake_case__ )







     def a						(			self   ,						snake_case__ ):

            '''simple docstring'''

            _lowerCAmelCase     :				Any       =							[]
            for is_user, text in conversation.iter_texts():
                   input_ids.extend(self.encode(snake_case__   ,						add_special_tokens=snake_case__ ) + [self.eos_token_id] )

            if len(snake_case__ ) > self.model_max_length:
                   _lowerCAmelCase     :				str       =							input_ids[-self.model_max_length :]
            return input_ids

