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 		_lowerCamelCase		( UpperCamelCase_     ):
    def     UpperCamelCase_					(	self )							->       str:
     SCREAMING_SNAKE_CASE__: Tuple=   self.config_class(**self.inputs_dict )
     self.parent.assertTrue(hasattr(lowerCAmelCase			,			'''embed_dim''' ) )
     self.parent.assertTrue(hasattr(lowerCAmelCase			,			'''num_heads''' ) )






class 		_lowerCamelCase		:
    def __init__(	self			,			lowerCAmelCase			,			lowerCAmelCase=13			,			lowerCAmelCase=64			,			lowerCAmelCase=3			,			lowerCAmelCase=[16, 48, 96]			,			lowerCAmelCase=[1, 3, 6]			,			lowerCAmelCase=[1, 2, 10]			,			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-12			,			lowerCAmelCase=True			,			lowerCAmelCase=True			,			lowerCAmelCase=2			,			)							->       str:
     SCREAMING_SNAKE_CASE__: Union[str, Any]=   parent
     SCREAMING_SNAKE_CASE__: int=   batch_size
     SCREAMING_SNAKE_CASE__: Optional[Any]=   image_size
     SCREAMING_SNAKE_CASE__: List[Any]=   patch_sizes
     SCREAMING_SNAKE_CASE__: Union[str, Any]=   patch_stride
     SCREAMING_SNAKE_CASE__: List[str]=   patch_padding
     SCREAMING_SNAKE_CASE__: str=   is_training
     SCREAMING_SNAKE_CASE__: int=   use_labels
     SCREAMING_SNAKE_CASE__: Optional[Any]=   num_labels
     SCREAMING_SNAKE_CASE__: int=   num_channels
     SCREAMING_SNAKE_CASE__: str=   embed_dim
     SCREAMING_SNAKE_CASE__: Optional[int]=   num_heads
     SCREAMING_SNAKE_CASE__: int=   stride_kv
     SCREAMING_SNAKE_CASE__: Dict=   depth
     SCREAMING_SNAKE_CASE__: Optional[Any]=   cls_token
     SCREAMING_SNAKE_CASE__: int=   attention_drop_rate
     SCREAMING_SNAKE_CASE__: Any=   initializer_range
     SCREAMING_SNAKE_CASE__: int=   layer_norm_eps
    def     UpperCamelCase_					(	self )							->       Union[str, Any]:
     SCREAMING_SNAKE_CASE__: Optional[int]=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

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



    def     UpperCamelCase_					(	self )							->       Tuple:
     SCREAMING_SNAKE_CASE__: Tuple=   self.prepare_config_and_inputs()
     SCREAMING_SNAKE_CASE__,						SCREAMING_SNAKE_CASE__,						SCREAMING_SNAKE_CASE__: int=   config_and_inputs
     SCREAMING_SNAKE_CASE__: Union[str, Any]=   {'''pixel_values''': pixel_values}
     return config, inputs_dict






@require_tf
class 		_lowerCamelCase		( UpperCamelCase_     ,  UpperCamelCase_     ,  unittest.TestCase     ):
    __a						=   (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
    __a						=   (
        {"feature-extraction": TFCvtModel, "image-classification": TFCvtForImageClassification}
        if is_tf_available()
        else {}
    )
    __a						=   False
    __a						=   False
    __a						=   False
    __a						=   False
    __a						=   False
    def     UpperCamelCase_					(	self )							->       Union[str, Any]:
     SCREAMING_SNAKE_CASE__: Union[str, Any]=   TFCvtModelTester(self )
     SCREAMING_SNAKE_CASE__: List[Any]=   TFCvtConfigTester(self			,			config_class=lowerCAmelCase			,			has_text_modality=lowerCAmelCase			,			hidden_size=37 )
    def     UpperCamelCase_					(	self )							->       List[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     UpperCamelCase_					(	self )							->       str:
     pass
    @unittest.skip(reason='''Cvt does not use inputs_embeds''' )
    def     UpperCamelCase_					(	self )							->       List[Any]:
     pass
    @unittest.skip(reason='''Cvt does not support input and output embeddings''' )
    def     UpperCamelCase_					(	self )							->       int:
     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 )							->       str:
     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 )							->       List[str]:
     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 )							->       Any:
     SCREAMING_SNAKE_CASE__: Optional[int]=   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 )							->       int:
     SCREAMING_SNAKE_CASE__,						SCREAMING_SNAKE_CASE__: Union[str, Any]=   self.model_tester.prepare_config_and_inputs_for_common()

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

      SCREAMING_SNAKE_CASE__: List[str]=   ['''pixel_values''']
      self.assertListEqual(arg_names[:1]			,			lowerCAmelCase )
    def     UpperCamelCase_					(	self )							->       List[str]:
     def check_hidden_states_output(lowerCAmelCase			,			lowerCAmelCase			,			lowerCAmelCase ):
      SCREAMING_SNAKE_CASE__: Optional[Any]=   model_class(lowerCAmelCase )

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

      SCREAMING_SNAKE_CASE__: str=   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,
          ]			,			)

     SCREAMING_SNAKE_CASE__,						SCREAMING_SNAKE_CASE__: Optional[Any]=   self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
      SCREAMING_SNAKE_CASE__: Union[str, Any]=   True
      check_hidden_states_output(lowerCAmelCase			,			lowerCAmelCase			,			lowerCAmelCase )

      # check that output_hidden_states also work using config
      del inputs_dict["output_hidden_states"]
      SCREAMING_SNAKE_CASE__: Optional[int]=   True

      check_hidden_states_output(lowerCAmelCase			,			lowerCAmelCase			,			lowerCAmelCase )
    def     UpperCamelCase_					(	self )							->       Optional[Any]:
     SCREAMING_SNAKE_CASE__: int=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCAmelCase )
    def     UpperCamelCase_					(	self )							->       Tuple:
     SCREAMING_SNAKE_CASE__: List[Any]=   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase )



    @slow
    def     UpperCamelCase_					(	self )							->       int:
     for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      SCREAMING_SNAKE_CASE__: Any=   TFCvtModel.from_pretrained(lowerCAmelCase )
      self.assertIsNotNone(lowerCAmelCase )







def       A__   (						):
 SCREAMING_SNAKE_CASE__: Optional[int]=   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
 return image






@require_tf
@require_vision
class 		_lowerCamelCase		( unittest.TestCase     ):
    @cached_property
    def     UpperCamelCase_					(	self )							->       List[Any]:
     return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )



    @slow
    def     UpperCamelCase_					(	self )							->       Optional[Any]:
     SCREAMING_SNAKE_CASE__: Dict=   TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

     SCREAMING_SNAKE_CASE__: Tuple=   self.default_image_processor
     SCREAMING_SNAKE_CASE__: Union[str, Any]=   prepare_img()
     SCREAMING_SNAKE_CASE__: Optional[int]=   image_processor(images=lowerCAmelCase			,			return_tensors='''tf''' )

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

     # verify the logits
     SCREAMING_SNAKE_CASE__: Optional[Any]=   tf.TensorShape((1, 1000) )
     self.assertEqual(outputs.logits.shape			,			lowerCAmelCase )

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






import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
	from PIL import Image

	from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor


@require_tokenizers
@require_vision
class 	__snake_case   ( unittest.TestCase ):
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  tempfile.mkdtemp()

						# fmt: off
						lowerCAmelCase__		  =  ['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest']
						# fmt: on
						lowerCAmelCase__		  =  os.path.join(self.tmpdirname    ,VOCAB_FILES_NAMES['vocab_file']						)
						with open(self.vocab_file    ,'w'    ,encoding='utf-8'						) as vocab_writer:
										vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]						)						)

						lowerCAmelCase__		  =  {
						    'do_resize': True,
						    'size': {'height': 18, 'width': 18},
						    'do_normalize': True,
						    'image_mean': [0.5, 0.5, 0.5],
						    'image_std': [0.5, 0.5, 0.5],
						}
						lowerCAmelCase__		  =  os.path.join(self.tmpdirname    ,a_						)
						with open(self.image_processor_file    ,'w'    ,encoding='utf-8'						) as fp:
										json.dump(a_    ,a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,**a_						):







						"""simple docstring"""
						return BertTokenizer.from_pretrained(self.tmpdirname    ,**a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,**a_						):







						"""simple docstring"""
						return ViTImageProcessor.from_pretrained(self.tmpdirname    ,**a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						shutil.rmtree(self.tmpdirname						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  [np.random.randint(255    ,size=(3, 30, 400)    ,dtype=np.uinta						)]

						lowerCAmelCase__		  =  [Image.fromarray(np.moveaxis(a_    ,0    ,-1						)						) for x in image_inputs]

						return image_inputs
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_tokenizer()
						lowerCAmelCase__		  =  self.get_image_processor()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						processor.save_pretrained(self.tmpdirname						)
						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname						)

						self.assertEqual(processor.tokenizer.get_vocab()    ,tokenizer.get_vocab()						)
						self.assertIsInstance(processor.tokenizer    ,(BertTokenizer, BertTokenizerFast)						)

						self.assertEqual(processor.image_processor.to_json_string()    ,image_processor.to_json_string()						)
						self.assertIsInstance(processor.image_processor    ,a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(
						    tokenizer=self.get_tokenizer()    ,image_processor=self.get_image_processor()						)
						processor.save_pretrained(self.tmpdirname						)

						lowerCAmelCase__		  =  self.get_tokenizer(bos_token='(BOS)'    ,eos_token='(EOS)'						)
						lowerCAmelCase__		  =  self.get_image_processor(do_normalize=a_    ,padding_value=1.0						)

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor.from_pretrained(
						    self.tmpdirname    ,bos_token='(BOS)'    ,eos_token='(EOS)'    ,do_normalize=a_    ,padding_value=1.0						)

						self.assertEqual(processor.tokenizer.get_vocab()    ,tokenizer_add_kwargs.get_vocab()						)
						self.assertIsInstance(processor.tokenizer    ,(BertTokenizer, BertTokenizerFast)						)

						self.assertEqual(processor.image_processor.to_json_string()    ,image_processor_add_kwargs.to_json_string()						)
						self.assertIsInstance(processor.image_processor    ,a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  self.prepare_image_inputs()

						lowerCAmelCase__		  =  image_processor(a_    ,return_tensors='np'						)
						lowerCAmelCase__		  =  processor(images=a_    ,return_tensors='np'						)

						for key in input_feat_extract.keys():
										self.assertAlmostEqual(input_feat_extract[key].sum()    ,input_processor[key].sum()    ,delta=1e-2						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  'lower newer'

						lowerCAmelCase__		  =  processor(text=a_						)

						lowerCAmelCase__		  =  tokenizer(a_						)

						for key in encoded_tok.keys():
										self.assertListEqual(encoded_tok[key]    ,encoded_processor[key]						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  'lower newer'
						lowerCAmelCase__		  =  self.prepare_image_inputs()

						lowerCAmelCase__		  =  processor(text=a_    ,images=a_						)

						self.assertListEqual(list(inputs.keys()						)    ,['input_ids', 'token_type_ids', 'attention_mask', 'pixel_values']						)

						# test if it raises when no input is passed
						with self.assertRaises(a_						):
										processor()
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

						lowerCAmelCase__		  =  processor.batch_decode(a_						)
						lowerCAmelCase__		  =  tokenizer.batch_decode(a_						)

						self.assertListEqual(a_    ,a_						)



		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.get_image_processor()
						lowerCAmelCase__		  =  self.get_tokenizer()

						lowerCAmelCase__		  =  VisionTextDualEncoderProcessor(tokenizer=a_    ,image_processor=a_						)

						lowerCAmelCase__		  =  'lower newer'
						lowerCAmelCase__		  =  self.prepare_image_inputs()

						lowerCAmelCase__		  =  processor(text=a_    ,images=a_						)

						self.assertListEqual(list(inputs.keys()						)    ,processor.model_input_names						)
