


"""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 						snake_case_(				__snake_case	):






     def    lowerCamelCase__   (		self     :    Tuple ):
           lowerCAmelCase    :		Union[str, Any]				=      self.config_class(**self.inputs_dict )
           self.parent.assertTrue(hasattr(UpperCamelCase__    , '''embed_dim''' ) )
           self.parent.assertTrue(hasattr(UpperCamelCase__    , '''num_heads''' ) )



class 						snake_case_:






     def __init__(		self     :    List[str]    , UpperCamelCase_     :    int    , UpperCamelCase_     :    Optional[int]=1_3    , UpperCamelCase_     :    Dict=6_4    , UpperCamelCase_     :    Optional[int]=3    , UpperCamelCase_     :    List[str]=[1_6, 4_8, 9_6]    , UpperCamelCase_     :    List[Any]=[1, 3, 6]    , UpperCamelCase_     :    int=[1, 2, 1_0]    , UpperCamelCase_     :    List[Any]=[7, 3, 3]    , UpperCamelCase_     :    Dict=[4, 2, 2]    , UpperCamelCase_     :    Dict=[2, 1, 1]    , UpperCamelCase_     :    Tuple=[2, 2, 2]    , UpperCamelCase_     :    str=[False, False, True]    , UpperCamelCase_     :    Dict=[0.0, 0.0, 0.0]    , UpperCamelCase_     :    Any=0.02    , UpperCamelCase_     :    Optional[int]=1E-12    , UpperCamelCase_     :    Any=True    , UpperCamelCase_     :    str=True    , UpperCamelCase_     :    List[Any]=2    , ):
           lowerCAmelCase    :		List[Any]				=      parent
           lowerCAmelCase    :		Optional[Any]				=      batch_size
           lowerCAmelCase    :		List[Any]				=      image_size
           lowerCAmelCase    :		int				=      patch_sizes
           lowerCAmelCase    :		Tuple				=      patch_stride
           lowerCAmelCase    :		Union[str, Any]				=      patch_padding
           lowerCAmelCase    :		List[str]				=      is_training
           lowerCAmelCase    :		Union[str, Any]				=      use_labels
           lowerCAmelCase    :		Any				=      num_labels
           lowerCAmelCase    :		Tuple				=      num_channels
           lowerCAmelCase    :		Dict				=      embed_dim
           lowerCAmelCase    :		Any				=      num_heads
           lowerCAmelCase    :		List[Any]				=      stride_kv
           lowerCAmelCase    :		Tuple				=      depth
           lowerCAmelCase    :		List[Any]				=      cls_token
           lowerCAmelCase    :		Union[str, Any]				=      attention_drop_rate
           lowerCAmelCase    :		int				=      initializer_range
           lowerCAmelCase    :		Optional[Any]				=      layer_norm_eps






     def    lowerCamelCase__   (		self     :    List[str] ):
           lowerCAmelCase    :		Union[str, Any]				=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

           lowerCAmelCase    :		int				=      self.get_config()
           return config, pixel_values, labels






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






     def    lowerCamelCase__   (		self     :    str ):
           lowerCAmelCase    :		Tuple				=      self.prepare_config_and_inputs()
           lowerCAmelCase    :		Optional[Any]				=      config_and_inputs
           lowerCAmelCase    :		List[Any]				=      {"pixel_values": pixel_values}
           return config, inputs_dict



@require_tf
class 						snake_case_(				__snake_case							,    __snake_case							,    unittest.TestCase	):
     __UpperCamelCase     =    (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
     __UpperCamelCase     =    (
         {"""feature-extraction""": TFCvtModel, """image-classification""": TFCvtForImageClassification}
         if is_tf_available()
         else {}
     )
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False






     def    lowerCamelCase__   (		self     :    Optional[Any] ):
           lowerCAmelCase    :		Optional[Any]				=      TFCvtModelTester(self )
           lowerCAmelCase    :		Tuple				=      TFCvtConfigTester(self    , config_class=UpperCamelCase__    , has_text_modality=UpperCamelCase__    , hidden_size=3_7 )






     def    lowerCamelCase__   (		self     :    Any ):
           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    lowerCamelCase__   (		self     :    str ):
           pass






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






     @unittest.skip(reason='''Cvt does not support input and output embeddings''' )
     def    lowerCamelCase__   (		self     :    Any ):
           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    lowerCamelCase__   (		self     :    Tuple ):
           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    lowerCamelCase__   (		self     :    Tuple ):
           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    lowerCamelCase__   (		self     :    Optional[Any] ):
           lowerCAmelCase    :		str				=      tf.keras.mixed_precision.Policy('''mixed_float16''' )
           tf.keras.mixed_precision.set_global_policy(UpperCamelCase__ )
           super().test_keras_fit()
           tf.keras.mixed_precision.set_global_policy('''float32''' )






     def    lowerCamelCase__   (		self     :    Dict ):
           lowerCAmelCase    :		Any				=      self.model_tester.prepare_config_and_inputs_for_common()

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

                 lowerCAmelCase    :		Optional[Any]				=      ["pixel_values"]
                 self.assertListEqual(arg_names[:1]    , UpperCamelCase__ )






     def    lowerCamelCase__   (		self     :    List[Any] ):


           def check_hidden_states_output(UpperCamelCase_     :    Union[str, Any]    , UpperCamelCase_     :    int    , UpperCamelCase_     :    Dict ):
                 lowerCAmelCase    :		str				=      model_class(UpperCamelCase__ )

                 lowerCAmelCase    :		Optional[Any]				=      model(**self._prepare_for_class(UpperCamelCase__    , UpperCamelCase__ ) )
                 lowerCAmelCase    :		List[Any]				=      outputs.hidden_states

                 lowerCAmelCase    :		Any				=      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,
                     ]    , )

           lowerCAmelCase    :		List[str]				=      self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 lowerCAmelCase    :		Optional[Any]				=      True
                 check_hidden_states_output(UpperCamelCase__    , UpperCamelCase__    , UpperCamelCase__ )

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 lowerCAmelCase    :		Dict				=      True

                 check_hidden_states_output(UpperCamelCase__    , UpperCamelCase__    , UpperCamelCase__ )






     def    lowerCamelCase__   (		self     :    Dict ):
           lowerCAmelCase    :		str				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase__ )






     def    lowerCamelCase__   (		self     :    str ):
           lowerCAmelCase    :		int				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__ )






     @slow
     def    lowerCamelCase__   (		self     :    Union[str, Any] ):


           for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCAmelCase    :		Tuple				=      TFCvtModel.from_pretrained(UpperCamelCase__ )
                 self.assertIsNotNone(UpperCamelCase__ )



def    _snake_case  (  ):
      lowerCAmelCase    :		List[str]				=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
      return image






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






     @cached_property
     def    lowerCamelCase__   (		self     :    int ):
           return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )






     @slow
     def    lowerCamelCase__   (		self     :    Tuple ):
           lowerCAmelCase    :		Any				=      TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

           lowerCAmelCase    :		int				=      self.default_image_processor
           lowerCAmelCase    :		Tuple				=      prepare_img()
           lowerCAmelCase    :		Dict				=      image_processor(images=UpperCamelCase__    , return_tensors='''tf''' )

           # forward pass
           lowerCAmelCase    :		List[str]				=      model(**UpperCamelCase__ )

           # verify the logits
           lowerCAmelCase    :		str				=      tf.TensorShape((1, 1_0_0_0) )
           self.assertEqual(outputs.logits.shape    , UpperCamelCase__ )

           lowerCAmelCase    :		int				=      tf.constant([0.9_285, 0.9_015, -0.3_150] )
           self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()    , UpperCamelCase__    , atol=1E-4 ) )





"""simple docstring"""


import unittest

from datasets import load_dataset

from transformers import BloomTokenizerFast
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin





@require_tokenizers
class     snake_case	(				__snake_case,							unittest.TestCase  ):
				SCREAMING_SNAKE_CASE_   :      List[str]         =  None
				SCREAMING_SNAKE_CASE_   :      Tuple         =  BloomTokenizerFast
				SCREAMING_SNAKE_CASE_   :      str         =  BloomTokenizerFast
				SCREAMING_SNAKE_CASE_   :      int         =  True
				SCREAMING_SNAKE_CASE_   :      Tuple         =  False
				SCREAMING_SNAKE_CASE_   :      int         =  """tokenizer_file"""
				SCREAMING_SNAKE_CASE_   :      List[str]         =  {"""bos_token""": """<s>""", """eos_token""": """</s>""", """unk_token""": """<unk>""", """pad_token""": """<pad>"""}

				def 							lowercase_     (	self							:				List[Any])->   Dict:



						'''simple docstring'''


						super().setUp()
						__lowerCAmelCase:       Optional[Any]          =			BloomTokenizerFast.from_pretrained("bigscience/tokenizer")
						tokenizer.save_pretrained(self.tmpdirname)

				def 							lowercase_     (	self							:				List[Any]     ,  **UpperCamelCase__							:				Union[str, Any])->   Optional[Any]:



						'''simple docstring'''


						kwargs.update(self.special_tokens_map)
						return BloomTokenizerFast.from_pretrained(self.tmpdirname     ,  **UpperCamelCase__)

				def 							lowercase_     (	self							:				Union[str, Any])->   Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:       str          =			self.get_rust_tokenizer()

						__lowerCAmelCase:       int          =			["The quick brown fox</s>", "jumps over the lazy dog</s>"]
						__lowerCAmelCase:       List[str]          =			[[2_1_7_5, 2_3_7_1_4, 7_3_1_7_3, 1_4_4_2_5_2, 2], [7_7, 1_3_2_6_1_9, 3_4_7_8, 3_6_8, 1_0_9_5_8_6, 3_5_4_3_3, 2]]

						__lowerCAmelCase:       List[str]          =			tokenizer.batch_encode_plus(UpperCamelCase__)["input_ids"]
						self.assertListEqual(UpperCamelCase__     ,  UpperCamelCase__)

						__lowerCAmelCase:       List[Any]          =			tokenizer.batch_decode(UpperCamelCase__)
						self.assertListEqual(UpperCamelCase__     ,  UpperCamelCase__)

				def 							lowercase_     (	self							:				Optional[Any]     ,  UpperCamelCase__							:				Tuple=6)->   Tuple:



						'''simple docstring'''


						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
								with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"):
										__lowerCAmelCase:       Optional[Any]          =			self.rust_tokenizer_class.from_pretrained(UpperCamelCase__     ,  **UpperCamelCase__)
										# tokenizer_r.pad_token = None # Hotfixing padding = None
										# Simple input
										__lowerCAmelCase:       Dict          =			"This is a simple input"
										__lowerCAmelCase:       str          =			["This is a simple input 1", "This is a simple input 2"]
										__lowerCAmelCase:       int          =			("This is a simple input", "This is a pair")
										__lowerCAmelCase:       Union[str, Any]          =			[
										    ("This is a simple input 1", "This is a simple input 2"),
										    ("This is a simple pair 1", "This is a simple pair 2"),
										]

										# Simple input tests
										try:
												tokenizer_r.encode(UpperCamelCase__     ,  max_length=UpperCamelCase__)
												tokenizer_r.encode_plus(UpperCamelCase__     ,  max_length=UpperCamelCase__)

												tokenizer_r.batch_encode_plus(UpperCamelCase__     ,  max_length=UpperCamelCase__)
												tokenizer_r.encode(UpperCamelCase__     ,  max_length=UpperCamelCase__)
												tokenizer_r.batch_encode_plus(UpperCamelCase__     ,  max_length=UpperCamelCase__)
										except ValueError:
												self.fail("Bloom Tokenizer should be able to deal with padding")

										__lowerCAmelCase:       Tuple          =			None  # Hotfixing padding = None
										self.assertRaises(UpperCamelCase__     ,  tokenizer_r.encode     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length")

										# Simple input
										self.assertRaises(UpperCamelCase__     ,  tokenizer_r.encode_plus     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length")

										# Simple input
										self.assertRaises(
										    UpperCamelCase__     ,  tokenizer_r.batch_encode_plus     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length"     ,  )

										# Pair input
										self.assertRaises(UpperCamelCase__     ,  tokenizer_r.encode     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length")

										# Pair input
										self.assertRaises(UpperCamelCase__     ,  tokenizer_r.encode_plus     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length")

										# Pair input
										self.assertRaises(
										    UpperCamelCase__     ,  tokenizer_r.batch_encode_plus     ,  UpperCamelCase__     ,  max_length=UpperCamelCase__     ,  padding="max_length"     ,  )

				def 							lowercase_     (	self							:				Optional[Any])->   List[str]:



						'''simple docstring'''


						__lowerCAmelCase:       Dict          =			self.get_rust_tokenizer()
						__lowerCAmelCase:       List[str]          =			load_dataset("xnli"     ,  "all_languages"     ,  split="test"     ,  streaming=UpperCamelCase__)

						__lowerCAmelCase:       Union[str, Any]          =			next(iter(UpperCamelCase__))["premise"]  # pick up one data
						__lowerCAmelCase:       Any          =			list(sample_data.values())

						__lowerCAmelCase:       int          =			list(map(tokenizer.encode     ,  UpperCamelCase__))
						__lowerCAmelCase:       str          =			[tokenizer.decode(UpperCamelCase__     ,  clean_up_tokenization_spaces=UpperCamelCase__) for x in output_tokens]
						self.assertListEqual(UpperCamelCase__     ,  UpperCamelCase__)




				def 							lowercase_     (	self							:				Optional[int])->   str:



						'''simple docstring'''


						self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map)     ,  1)
						self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values())[0])     ,  1)


