
'''simple docstring'''


from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
      import tensorflow as tf

      from transformers import TFCvtForImageClassification, TFCvtModel
      from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor


class 						lowercase__	(  lowercase_   ):






   '''simple docstring'''







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.config_class(**self.inputs_dict   )
      self.parent.assertTrue(hasattr(lowerCamelCase__ ,   '''embed_dim'''   )   )
      self.parent.assertTrue(hasattr(lowerCamelCase__ ,   '''num_heads'''   )   )




class 						lowercase__	:






   '''simple docstring'''







   def __init__(			self ,   lowerCamelCase__ ,   lowerCamelCase__=1_3 ,   lowerCamelCase__=6_4 ,   lowerCamelCase__=3 ,   lowerCamelCase__=[1_6, 4_8, 9_6] ,   lowerCamelCase__=[1, 3, 6] ,   lowerCamelCase__=[1, 2, 1_0] ,   lowerCamelCase__=[7, 3, 3] ,   lowerCamelCase__=[4, 2, 2] ,   lowerCamelCase__=[2, 1, 1] ,   lowerCamelCase__=[2, 2, 2] ,   lowerCamelCase__=[False, False, True] ,   lowerCamelCase__=[0.0, 0.0, 0.0] ,   lowerCamelCase__=0.02 ,   lowerCamelCase__=1e-1_2 ,   lowerCamelCase__=True ,   lowerCamelCase__=True ,   lowerCamelCase__=2 ,   ):

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

      '''simple docstring'''







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

      UpperCamelCase       =  None
      if self.use_labels:
         # create a random int32 tensor of given shape
         UpperCamelCase       =  ids_tensor([self.batch_size] ,   self.num_labels   )

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







   def       UpperCAmelCase				(			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       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  TFCvtModel(config=lowerCamelCase__   )
      UpperCamelCase       =  model(lowerCamelCase__ ,   training=lowerCamelCase__   )
      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       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  self.num_labels
      UpperCamelCase       =  TFCvtForImageClassification(lowerCamelCase__   )
      UpperCamelCase       =  model(lowerCamelCase__ ,   labels=lowerCamelCase__ ,   training=lowerCamelCase__   )
      self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.num_labels)   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







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




@require_tf
class 						lowercase__	(  lowercase_,					lowercase_,					unittest.TestCase   ):






   '''simple docstring'''







   _snake_case		   =							(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
   _snake_case		   =							(
       {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification}
       if is_tf_available()
       else {}
   )
   _snake_case		   =							False
   _snake_case		   =							False
   _snake_case		   =							False
   _snake_case		   =							False
   _snake_case		   =							False







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  TFCvtModelTester(self   )
      UpperCamelCase       =  TFCvtConfigTester(self ,   config_class=lowerCamelCase__ ,   has_text_modality=lowerCamelCase__ ,   hidden_size=3_7   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      self.config_tester.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()







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

      '''simple docstring'''







      pass







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

      '''simple docstring'''







      pass







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

      '''simple docstring'''







      pass







   @unittest.skipIf(
       not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''   )   ) == 0 ,   reason='''TF does not support backprop for grouped convolutions on CPU.''' ,   )
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      super().test_dataset_conversion()







   @unittest.skipIf(
       not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''   )   ) == 0 ,   reason='''TF does not support backprop for grouped convolutions on CPU.''' ,   )
   @slow
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      super().test_keras_fit()







   @unittest.skip(reason='''Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8'''   )
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  tf.keras.mixed_precision.Policy('''mixed_float16'''   )
      tf.keras.mixed_precision.set_global_policy(lowerCamelCase__   )
      super().test_keras_fit()
      tf.keras.mixed_precision.set_global_policy('''float32'''   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase     , UpperCamelCase       =  self.model_tester.prepare_config_and_inputs_for_common()

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

         UpperCamelCase       =  ['''pixel_values''']
         self.assertListEqual(arg_names[:1] ,   lowerCamelCase__   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      def check_hidden_states_output(lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):
         UpperCamelCase       =  model_class(lowerCamelCase__   )

         UpperCamelCase       =  model(**self._prepare_for_class(lowerCamelCase__ ,   lowerCamelCase__   )   )
         UpperCamelCase       =  outputs.hidden_states

         UpperCamelCase       =  len(self.model_tester.depth   )
         self.assertEqual(len(lowerCamelCase__   ) ,   lowerCamelCase__   )

         # 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(lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   )

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

         check_hidden_states_output(lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCamelCase__   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__   )







   @slow
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         UpperCamelCase       =  TFCvtModel.from_pretrained(lowerCamelCase__   )
         self.assertIsNotNone(lowerCamelCase__   )




def  __snake_case     (    ):
   UpperCamelCase       =  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
   return image


@require_tf
@require_vision
class 						lowercase__	(  unittest.TestCase   ):






   '''simple docstring'''







   @cached_property
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )







   @slow
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )

      UpperCamelCase       =  self.default_image_processor
      UpperCamelCase       =  prepare_img()
      UpperCamelCase       =  image_processor(images=lowerCamelCase__ ,   return_tensors='''tf'''   )

      # forward pass
      UpperCamelCase       =  model(**lowerCamelCase__   )

      # verify the logits
      UpperCamelCase       =  tf.TensorShape((1, 1_0_0_0)   )
      self.assertEqual(outputs.logits.shape ,   lowerCamelCase__   )

      UpperCamelCase       =  tf.constant([0.9285, 0.9015, -0.3150]   )
      self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() ,   lowerCamelCase__ ,   atol=1e-4   )   )







'''simple docstring'''


import os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
   from .tokenization_pegasus import PegasusTokenizer
else:
   snake_case_ :  Dict     =					None


snake_case_ :  int     =					logging.get_logger(__name__)


snake_case_ :  int     =					'▁'

snake_case_ :  Optional[int]     =					{'vocab_file': 'spiece.model', 'tokenizer_file': 'tokenizer.json'}

snake_case_ :  List[Any]     =					{
    'vocab_file': {'google/pegasus-xsum': 'https://huggingface.co/google/pegasus-xsum/resolve/main/spiece.model'},
    'tokenizer_file': {
        'google/pegasus-xsum': 'https://huggingface.co/google/pegasus-xsum/resolve/main/tokenizer.json'
    },
}

snake_case_ :  Tuple     =					{
    'google/pegasus-xsum': 512,
}


class 						lowercase__	(  snake_case_   ):






   '''simple docstring'''







   _snake_case		   =							VOCAB_FILES_NAMES
   _snake_case		   =							PRETRAINED_VOCAB_FILES_MAP
   _snake_case		   =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   _snake_case		   =							PegasusTokenizer
   _snake_case		   =							['''input_ids''', '''attention_mask''']







   def __init__(			self ,   lowerCamelCase__=None ,   lowerCamelCase__=None ,   lowerCamelCase__="<pad>" ,   lowerCamelCase__="</s>" ,   lowerCamelCase__="<unk>" ,   lowerCamelCase__="<mask_2>" ,   lowerCamelCase__="<mask_1>" ,   lowerCamelCase__=None ,   lowerCamelCase__=1_0_3 ,   **lowerCamelCase__ ,   ):

      '''simple docstring'''







      UpperCamelCase       =  offset

      if additional_special_tokens is not None:
         if not isinstance(lowerCamelCase__ ,   lowerCamelCase__   ):
            raise TypeError(
                f'additional_special_tokens should be of type {type(lowerCamelCase__   )}, but is'
                f' {type(lowerCamelCase__   )}'   )

         UpperCamelCase       =  (
             ([mask_token_sent] + additional_special_tokens)
             if mask_token_sent not in additional_special_tokens and mask_token_sent is not None
             else additional_special_tokens
         )
         # fill additional tokens with ..., <unk_token_102> in case not all additional tokens are already taken
         additional_special_tokens_extended += [
             f'<unk_{i}>' for i in range(len(lowerCamelCase__   ) ,   self.offset - 1   )
         ]

         if len(set(lowerCamelCase__   )   ) != len(lowerCamelCase__   ):
            raise ValueError(
                '''Please make sure that the provided additional_special_tokens do not contain an incorrectly'''
                f' shifted list of <unk_x> tokens. Found {additional_special_tokens_extended}.'   )
         UpperCamelCase       =  additional_special_tokens_extended
      else:
         UpperCamelCase       =  [mask_token_sent] if mask_token_sent is not None else []
         additional_special_tokens += [f'<unk_{i}>' for i in range(2 ,   self.offset   )]

      super().__init__(
          lowerCamelCase__ ,   tokenizer_file=lowerCamelCase__ ,   pad_token=lowerCamelCase__ ,   eos_token=lowerCamelCase__ ,   unk_token=lowerCamelCase__ ,   mask_token=lowerCamelCase__ ,   mask_token_sent=lowerCamelCase__ ,   offset=lowerCamelCase__ ,   additional_special_tokens=lowerCamelCase__ ,   **lowerCamelCase__ ,   )
      UpperCamelCase       =  vocab_file
      UpperCamelCase       =  False if not self.vocab_file else True







   def       UpperCAmelCase				(			self ,   lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  set(self.all_special_ids   )  # call it once instead of inside list comp
      all_special_ids.remove(self.unk_token_id   )  # <unk> is only sometimes special

      if all_special_ids != set(range(len(self.additional_special_tokens   ) + 3   )   ):
         raise ValueError(
             '''There should be 3 special tokens: mask_token, pad_token, and eos_token +'''
             f' {len(self.additional_special_tokens   )} additional_special_tokens, but got {all_special_ids}'   )

      return [1 if x in all_special_ids else 0 for x in seq]







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ = None ,   lowerCamelCase__ = False   ):

      '''simple docstring'''







      if already_has_special_tokens:
         return self._special_token_mask(lowerCamelCase__   )
      elif token_ids_a is None:
         return self._special_token_mask(lowerCamelCase__   ) + [1]
      else:
         return self._special_token_mask(token_ids_a + token_ids_a   ) + [1]







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__=None   ):

      '''simple docstring'''







      if token_ids_a is None:
         return token_ids_a + [self.eos_token_id]
      # We don't expect to process pairs, but leave the pair logic for API consistency
      return token_ids_a + token_ids_a + [self.eos_token_id]







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ = None   ):

      '''simple docstring'''







      if not self.can_save_slow_tokenizer:
         raise ValueError(
             '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '''
             '''tokenizer.'''   )

      if not os.path.isdir(lowerCamelCase__   ):
         logger.error(f'Vocabulary path ({save_directory}) should be a directory'   )
         return
      UpperCamelCase       =  os.path.join(
          lowerCamelCase__ ,   (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']   )

      if os.path.abspath(self.vocab_file   ) != os.path.abspath(lowerCamelCase__   ):
         copyfile(self.vocab_file ,   lowerCamelCase__   )

      return (out_vocab_file,)





