






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 					A							(    UpperCAmelCase_					):


   def       lowercase_				(self					:						Optional[Any]		)						->    Any:
         """simple docstring"""



         UpperCAmelCase__								=  self.config_class(**self.inputs_dict		)
         self.parent.assertTrue(hasattr(__UpperCAmelCase	,  "embed_dim"		)		)
         self.parent.assertTrue(hasattr(__UpperCAmelCase	,  "num_heads"		)		)





class 					A							:


   def __init__(self					:						Union[str, Any]	,  __UpperCAmelCase					:						str	,  __UpperCAmelCase					:						Tuple=1_3	,  __UpperCAmelCase					:						Union[str, Any]=6_4	,  __UpperCAmelCase					:						str=3	,  __UpperCAmelCase					:						str=[1_6, 4_8, 9_6]	,  __UpperCAmelCase					:						List[str]=[1, 3, 6]	,  __UpperCAmelCase					:						str=[1, 2, 1_0]	,  __UpperCAmelCase					:						Optional[Any]=[7, 3, 3]	,  __UpperCAmelCase					:						List[str]=[4, 2, 2]	,  __UpperCAmelCase					:						Tuple=[2, 1, 1]	,  __UpperCAmelCase					:						Tuple=[2, 2, 2]	,  __UpperCAmelCase					:						int=[False, False, True]	,  __UpperCAmelCase					:						List[str]=[0.0, 0.0, 0.0]	,  __UpperCAmelCase					:						Tuple=0.02	,  __UpperCAmelCase					:						str=1E-12	,  __UpperCAmelCase					:						Union[str, Any]=True	,  __UpperCAmelCase					:						Optional[int]=True	,  __UpperCAmelCase					:						List[Any]=2	,  )						->    int:
         """simple docstring"""



         UpperCAmelCase__								=  parent
         UpperCAmelCase__								=  batch_size
         UpperCAmelCase__								=  image_size
         UpperCAmelCase__								=  patch_sizes
         UpperCAmelCase__								=  patch_stride
         UpperCAmelCase__								=  patch_padding
         UpperCAmelCase__								=  is_training
         UpperCAmelCase__								=  use_labels
         UpperCAmelCase__								=  num_labels
         UpperCAmelCase__								=  num_channels
         UpperCAmelCase__								=  embed_dim
         UpperCAmelCase__								=  num_heads
         UpperCAmelCase__								=  stride_kv
         UpperCAmelCase__								=  depth
         UpperCAmelCase__								=  cls_token
         UpperCAmelCase__								=  attention_drop_rate
         UpperCAmelCase__								=  initializer_range
         UpperCAmelCase__								=  layer_norm_eps


   def       lowercase_				(self					:						Optional[int]		)						->    Any:
         """simple docstring"""



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

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

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


   def       lowercase_				(self					:						Optional[int]		)						->    Any:
         """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       lowercase_				(self					:						List[Any]	,  __UpperCAmelCase					:						Any	,  __UpperCAmelCase					:						List[str]	,  __UpperCAmelCase					:						Tuple		)						->    int:
         """simple docstring"""



         UpperCAmelCase__								=  CvtModel(config=__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         UpperCAmelCase__								=  (self.image_size, self.image_size)
         UpperCAmelCase__		,				UpperCAmelCase__								=  image_size[0], image_size[1]
         for i in range(len(self.depth		)		):
               UpperCAmelCase__								=  floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
               UpperCAmelCase__								=  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       lowercase_				(self					:						Dict	,  __UpperCAmelCase					:						str	,  __UpperCAmelCase					:						Optional[Any]	,  __UpperCAmelCase					:						Tuple		)						->    List[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  self.num_labels
         UpperCAmelCase__								=  CvtForImageClassification(__UpperCAmelCase		)
         model.to(__UpperCAmelCase		)
         model.eval()
         UpperCAmelCase__								=  model(__UpperCAmelCase	,  labels=__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.num_labels)		)





   def       lowercase_				(self					:						Optional[int]		)						->    Dict:
         """simple docstring"""



         UpperCAmelCase__								=  self.prepare_config_and_inputs()
         UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__								=  config_and_inputs
         UpperCAmelCase__								=  {"pixel_values": pixel_values}
         return config, inputs_dict





@require_torch
class 					A							(    UpperCAmelCase_		,    UpperCAmelCase_		,    unittest.TestCase					):
   __UpperCAmelCase			:			List[str]              =      (CvtModel, CvtForImageClassification) if is_torch_available() else ()
   __UpperCAmelCase			:			Optional[Any]              =      (
       {'feature-extraction': CvtModel, 'image-classification': CvtForImageClassification}
       if is_torch_available()
       else {}
   )

   __UpperCAmelCase			:			Optional[int]              =      False
   __UpperCAmelCase			:			str              =      False
   __UpperCAmelCase			:			str              =      False
   __UpperCAmelCase			:			Tuple              =      False
   __UpperCAmelCase			:			Dict              =      False


   def       lowercase_				(self					:						Tuple		)						->    List[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  CvtModelTester(self		)
         UpperCAmelCase__								=  ConfigTester(self	,  config_class=__UpperCAmelCase	,  has_text_modality=__UpperCAmelCase	,  hidden_size=3_7		)


   def       lowercase_				(self					:						Union[str, Any]		)						->    Optional[Any]:
         """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       lowercase_				(self					:						Tuple		)						->    Optional[int]:
         """simple docstring"""



         return


   @unittest.skip(reason="Cvt does not output attentions"		)
   def       lowercase_				(self					:						List[str]		)						->    Tuple:
         """simple docstring"""



         pass


   @unittest.skip(reason="Cvt does not use inputs_embeds"		)
   def       lowercase_				(self					:						Optional[int]		)						->    Tuple:
         """simple docstring"""



         pass


   @unittest.skip(reason="Cvt does not support input and output embeddings"		)
   def       lowercase_				(self					:						int		)						->    Dict:
         """simple docstring"""



         pass


   def       lowercase_				(self					:						Optional[Any]		)						->    Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__		,				UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

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

               UpperCAmelCase__								=  ["pixel_values"]
               self.assertListEqual(arg_names[:1]	,  __UpperCAmelCase		)


   def       lowercase_				(self					:						Any		)						->    int:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCAmelCase		)


   def       lowercase_				(self					:						Optional[int]		)						->    Optional[Any]:
         """simple docstring"""



         def check_hidden_states_output(__UpperCAmelCase					:						List[Any]	,  __UpperCAmelCase					:						Optional[int]	,  __UpperCAmelCase					:						List[Any]		):
               UpperCAmelCase__								=  model_class(__UpperCAmelCase		)
               model.to(__UpperCAmelCase		)
               model.eval()

               with torch.no_grad():
                     UpperCAmelCase__								=  model(**self._prepare_for_class(__UpperCAmelCase	,  __UpperCAmelCase		)		)

               UpperCAmelCase__								=  outputs.hidden_states

               UpperCAmelCase__								=  len(self.model_tester.depth		)
               self.assertEqual(len(__UpperCAmelCase		)	,  __UpperCAmelCase		)

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

         UpperCAmelCase__		,				UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
               UpperCAmelCase__								=  True
               check_hidden_states_output(__UpperCAmelCase	,  __UpperCAmelCase	,  __UpperCAmelCase		)

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

               check_hidden_states_output(__UpperCAmelCase	,  __UpperCAmelCase	,  __UpperCAmelCase		)


   def       lowercase_				(self					:						int		)						->    Any:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase		)


   @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."		)
   def       lowercase_				(self					:						Any		)						->    Dict:
         """simple docstring"""



         pass





   @slow
   def       lowercase_				(self					:						List[Any]		)						->    Optional[int]:
         """simple docstring"""



         for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase__								=  CvtModel.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)




def        lowerCAmelCase_			(					)					->    str:







      '''simple docstring'''


      UpperCAmelCase__								=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
      return image







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


   @cached_property
   def       lowercase_				(self					:						List[Any]		)						->    Dict:
         """simple docstring"""



         return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)





   @slow
   def       lowercase_				(self					:						int		)						->    List[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(__UpperCAmelCase		)

         UpperCAmelCase__								=  self.default_image_processor
         UpperCAmelCase__								=  prepare_img()
         UpperCAmelCase__								=  image_processor(images=__UpperCAmelCase	,  return_tensors="pt"		).to(__UpperCAmelCase		)

         # forward pass
         with torch.no_grad():
               UpperCAmelCase__								=  model(**__UpperCAmelCase		)

         # verify the logits
         UpperCAmelCase__								=  torch.Size((1, 1_0_0_0)		)
         self.assertEqual(outputs.logits.shape	,  __UpperCAmelCase		)

         UpperCAmelCase__								=  torch.tensor([0.9285, 0.9015, -0.3150]		).to(__UpperCAmelCase		)

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




import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bart import BartTokenizer


UpperCamelCase__    							= logging.get_logger(__name__)


UpperCamelCase__    							= {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'}

# See all BART models at https://huggingface.co/models?filter=bart
UpperCamelCase__    							= {
    'vocab_file': {
        'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/vocab.json',
        'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/vocab.json',
        'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json',
        'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json',
        'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json',
        'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json',
    },
    'merges_file': {
        'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/merges.txt',
        'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/merges.txt',
        'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt',
        'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt',
        'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt',
        'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt',
    },
    'tokenizer_file': {
        'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/tokenizer.json',
        'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/tokenizer.json',
        'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/tokenizer.json',
        'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/tokenizer.json',
        'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/tokenizer.json',
        'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/tokenizer.json',
    },
}

UpperCamelCase__    							= {
    'facebook/bart-base': 1_0_2_4,
    'facebook/bart-large': 1_0_2_4,
    'facebook/bart-large-mnli': 1_0_2_4,
    'facebook/bart-large-cnn': 1_0_2_4,
    'facebook/bart-large-xsum': 1_0_2_4,
    'yjernite/bart_eli5': 1_0_2_4,
}





class 					A							(    UpperCAmelCase_					):
   __UpperCAmelCase			:			List[str]              =      VOCAB_FILES_NAMES
   __UpperCAmelCase			:			int              =      PRETRAINED_VOCAB_FILES_MAP
   __UpperCAmelCase			:			int              =      PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   __UpperCAmelCase			:			str              =      ['input_ids', 'attention_mask']
   __UpperCAmelCase			:			str              =      BartTokenizer


   def __init__(self					:						Tuple	,  __UpperCAmelCase					:						int=None	,  __UpperCAmelCase					:						Optional[int]=None	,  __UpperCAmelCase					:						int=None	,  __UpperCAmelCase					:						str="replace"	,  __UpperCAmelCase					:						List[str]="<s>"	,  __UpperCAmelCase					:						Tuple="</s>"	,  __UpperCAmelCase					:						List[Any]="</s>"	,  __UpperCAmelCase					:						Tuple="<s>"	,  __UpperCAmelCase					:						List[str]="<unk>"	,  __UpperCAmelCase					:						Optional[int]="<pad>"	,  __UpperCAmelCase					:						List[Any]="<mask>"	,  __UpperCAmelCase					:						Any=False	,  __UpperCAmelCase					:						str=True	,  **__UpperCAmelCase					:						Tuple	,  )						->    Any:
         """simple docstring"""



         super().__init__(
             __UpperCAmelCase	,  __UpperCAmelCase	,  tokenizer_file=__UpperCAmelCase	,  errors=__UpperCAmelCase	,  bos_token=__UpperCAmelCase	,  eos_token=__UpperCAmelCase	,  sep_token=__UpperCAmelCase	,  cls_token=__UpperCAmelCase	,  unk_token=__UpperCAmelCase	,  pad_token=__UpperCAmelCase	,  mask_token=__UpperCAmelCase	,  add_prefix_space=__UpperCAmelCase	,  trim_offsets=__UpperCAmelCase	,  **__UpperCAmelCase	,  )

         UpperCAmelCase__								=  json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()		)
         if pre_tok_state.get("add_prefix_space"	,  __UpperCAmelCase		) != add_prefix_space:
               UpperCAmelCase__								=  getattr(__UpperCAmelCase	,  pre_tok_state.pop("type"		)		)
               UpperCAmelCase__								=  add_prefix_space
               UpperCAmelCase__								=  pre_tok_class(**__UpperCAmelCase		)

         UpperCAmelCase__								=  add_prefix_space

         # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
         UpperCAmelCase__								=  "post_processor"
         UpperCAmelCase__								=  getattr(self.backend_tokenizer	,  __UpperCAmelCase	,  __UpperCAmelCase		)
         if tokenizer_component_instance:
               UpperCAmelCase__								=  json.loads(tokenizer_component_instance.__getstate__()		)

               # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
               if "sep" in state:
                     UpperCAmelCase__								=  tuple(state["sep"]		)
               if "cls" in state:
                     UpperCAmelCase__								=  tuple(state["cls"]		)

               UpperCAmelCase__								=  False

               if state.get("add_prefix_space"	,  __UpperCAmelCase		) != add_prefix_space:
                     UpperCAmelCase__								=  add_prefix_space
                     UpperCAmelCase__								=  True

               if state.get("trim_offsets"	,  __UpperCAmelCase		) != trim_offsets:
                     UpperCAmelCase__								=  trim_offsets
                     UpperCAmelCase__								=  True

               if changes_to_apply:
                     UpperCAmelCase__								=  getattr(__UpperCAmelCase	,  state.pop("type"		)		)
                     UpperCAmelCase__								=  component_class(**__UpperCAmelCase		)
                     setattr(self.backend_tokenizer	,  __UpperCAmelCase	,  __UpperCAmelCase		)


   @property
   def       lowercase_				(self					:						Tuple		)						->    str:
         """simple docstring"""



         if self._mask_token is None:
               if self.verbose:
                     logger.error("Using mask_token, but it is not set yet."		)
               return None
         return str(self._mask_token		)


   @mask_token.setter
   def       lowercase_				(self					:						Optional[int]	,  __UpperCAmelCase					:						Union[str, Any]		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  AddedToken(__UpperCAmelCase	,  lstrip=__UpperCAmelCase	,  rstrip=__UpperCAmelCase		) if isinstance(__UpperCAmelCase	,  __UpperCAmelCase		) else value
         UpperCAmelCase__								=  value


   def       lowercase_				(self					:						List[str]	,  *__UpperCAmelCase					:						str	,  **__UpperCAmelCase					:						List[str]		)						->    BatchEncoding:
         """simple docstring"""



         UpperCAmelCase__								=  kwargs.get("is_split_into_words"	,  __UpperCAmelCase		)

         if is_split_into_words and not self.add_prefix_space:
               raise ValueError(
                   f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """
                   "to use it with pretokenized inputs."		)

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


   def       lowercase_				(self					:						List[Any]	,  *__UpperCAmelCase					:						Optional[Any]	,  **__UpperCAmelCase					:						Optional[Any]		)						->    BatchEncoding:
         """simple docstring"""



         UpperCAmelCase__								=  kwargs.get("is_split_into_words"	,  __UpperCAmelCase		)

         if is_split_into_words and not self.add_prefix_space:
               raise ValueError(
                   f"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """
                   "to use it with pretokenized inputs."		)

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


   def       lowercase_				(self					:						Any	,  __UpperCAmelCase					:						str	,  __UpperCAmelCase					:						Optional[str] = None		)						->    Tuple[str]:
         """simple docstring"""



         UpperCAmelCase__								=  self._tokenizer.model.save(__UpperCAmelCase	,  name=__UpperCAmelCase		)
         return tuple(__UpperCAmelCase		)


   def       lowercase_				(self					:						Optional[Any]	,  __UpperCAmelCase					:						int	,  __UpperCAmelCase					:						Dict=None		)						->    Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  [self.bos_token_id] + token_ids_a + [self.eos_token_id]
         if token_ids_a is None:
               return output

         return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]





   def       lowercase_				(self					:						Union[str, Any]	,  __UpperCAmelCase					:						List[int]	,  __UpperCAmelCase					:						Optional[List[int]] = None		)						->    List[int]:
         """simple docstring"""



         UpperCAmelCase__								=  [self.sep_token_id]
         UpperCAmelCase__								=  [self.cls_token_id]

         if token_ids_a is None:
               return len(cls + token_ids_a + sep		) * [0]
         return len(cls + token_ids_a + sep + sep + token_ids_a + sep		) * [0]



