


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_							(					lowerCAmelCase__					):



					'''simple docstring'''







					def A   (					self    ) ->      str:







								'''simple docstring'''

								__lowercase			       = self.config_class(**self.inputs_dict    )
								self.parent.assertTrue(hasattr(snake_case_			,							'''embed_dim'''    )    )
								self.parent.assertTrue(hasattr(snake_case_			,							'''num_heads'''    )    )




class 						lowerCamelCase_							:



					'''simple docstring'''







					def __init__(					self			,							snake_case_			,							snake_case_=1_3			,							snake_case_=6_4			,							snake_case_=3			,							snake_case_=[1_6, 4_8, 9_6]			,							snake_case_=[1, 3, 6]			,							snake_case_=[1, 2, 1_0]			,							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.0_2			,							snake_case_=1e-1_2			,							snake_case_=True			,							snake_case_=True			,							snake_case_=2			,							) ->      Dict:







								'''simple docstring'''

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






					def A   (					self    ) ->      Tuple:







								'''simple docstring'''

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

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

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






					def A   (					self    ) ->      Optional[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 A   (					self			,							snake_case_			,							snake_case_			,							snake_case_    ) ->      List[str]:







								'''simple docstring'''

								__lowercase			       = TFCvtModel(config=snake_case_    )
								__lowercase			       = model(snake_case_			,							training=snake_case_    )
								__lowercase			       = (self.image_size, self.image_size)
								__lowercase       ,			__lowercase			       = image_size[0], image_size[1]
								for i in range(len(self.depth    )    ):
											__lowercase			       = floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1    )
											__lowercase			       = 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_    ) ->      str:







								'''simple docstring'''

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






					def A   (					self    ) ->      Optional[Any]:







								'''simple docstring'''

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




@require_tf
class 						lowerCamelCase_							(					lowerCAmelCase__  ,  lowerCAmelCase__  ,  unittest.TestCase					):



					'''simple docstring'''







					__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 A   (					self    ) ->      List[str]:







								'''simple docstring'''

								__lowercase			       = TFCvtModelTester(self    )
								__lowercase			       = TFCvtConfigTester(self			,							config_class=snake_case_			,							has_text_modality=snake_case_			,							hidden_size=3_7    )






					def A   (					self    ) ->      Dict:







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







								'''simple docstring'''

								pass






					@unittest.skip(reason='''Cvt does not use inputs_embeds'''    )
					def A   (					self    ) ->      str:







								'''simple docstring'''

								pass






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







								'''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 A   (					self    ) ->      List[Any]:







								'''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 A   (					self    ) ->      Optional[Any]:







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







								'''simple docstring'''

								__lowercase			       = tf.keras.mixed_precision.Policy('''mixed_float16'''    )
								tf.keras.mixed_precision.set_global_policy(snake_case_    )
								super().test_keras_fit()
								tf.keras.mixed_precision.set_global_policy('''float32'''    )






					def A   (					self    ) ->      List[str]:







								'''simple docstring'''

								__lowercase       ,			__lowercase			       = self.model_tester.prepare_config_and_inputs_for_common()

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

											__lowercase			       = ['''pixel_values''']
											self.assertListEqual(arg_names[:1]			,							snake_case_    )






					def A   (					self    ) ->      Any:







								'''simple docstring'''

								def check_hidden_states_output(snake_case_			,							snake_case_			,							snake_case_    ):
											__lowercase			       = model_class(snake_case_    )

											__lowercase			       = model(**self._prepare_for_class(snake_case_			,							snake_case_    )    )
											__lowercase			       = outputs.hidden_states

											__lowercase			       = 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,
											    ]			,							)

								__lowercase       ,			__lowercase			       = self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
											__lowercase			       = 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"]
											__lowercase			       = True

											check_hidden_states_output(snake_case_			,							snake_case_			,							snake_case_    )






					def A   (					self    ) ->      Optional[int]:







								'''simple docstring'''

								__lowercase			       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*snake_case_    )






					def A   (					self    ) ->      Optional[int]:







								'''simple docstring'''

								__lowercase			       = self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*snake_case_    )






					@slow
					def A   (					self    ) ->      str:







								'''simple docstring'''

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




def 		lowercase_	(  ):


			'''simple docstring'''







			__lowercase			       = Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
			return image





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



					'''simple docstring'''







					@cached_property
					def A   (					self    ) ->      Any:







								'''simple docstring'''

								return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )






					@slow
					def A   (					self    ) ->      Any:







								'''simple docstring'''

								__lowercase			       = TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )

								__lowercase			       = self.default_image_processor
								__lowercase			       = prepare_img()
								__lowercase			       = image_processor(images=snake_case_			,							return_tensors='''tf'''    )

								# forward pass
								__lowercase			       = model(**snake_case_    )

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

								__lowercase			       = tf.constant([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0]    )
								self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()			,							snake_case_			,							atol=1e-4    )    )






import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a			:			Optional[int]												=					logging.get_logger(__name__)

a			:			Dict												=					{
    '''google/pix2struct-textcaps-base''': (
        '''https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json'''
    ),
}
class 						lowerCamelCase_							(					lowerCAmelCase__					):



					'''simple docstring'''







					__UpperCAmelCase							  =					"pix2struct_text_model"
					__UpperCAmelCase							  =					["past_key_values"]
					__UpperCAmelCase							  =					{
					    "hidden_size": "hidden_size",
					    "num_attention_heads": "num_heads",
					    "num_hidden_layers": "num_layers",
					}






					def __init__(					self			,							snake_case_=5_0_2_4_4			,							snake_case_=7_6_8			,							snake_case_=6_4			,							snake_case_=2_0_4_8			,							snake_case_=1_2			,							snake_case_=1_2			,							snake_case_=3_2			,							snake_case_=1_2_8			,							snake_case_=0.1			,							snake_case_=1e-6			,							snake_case_=1.0			,							snake_case_="gelu_new"			,							snake_case_=0			,							snake_case_=False			,							snake_case_=0			,							snake_case_=1			,							snake_case_=False			,							snake_case_=True			,							**snake_case_			,							) ->      Optional[int]:







								'''simple docstring'''

								__lowercase			       = vocab_size
								__lowercase			       = hidden_size
								__lowercase			       = d_kv
								__lowercase			       = d_ff
								__lowercase			       = num_layers
								__lowercase			       = num_heads
								__lowercase			       = relative_attention_num_buckets
								__lowercase			       = relative_attention_max_distance
								__lowercase			       = dropout_rate
								__lowercase			       = layer_norm_epsilon
								__lowercase			       = initializer_factor
								__lowercase			       = use_cache

								__lowercase			       = eos_token_id
								__lowercase			       = decoder_start_token_id

								# for backwards compatibility
								__lowercase			       = dense_act_fn

								super().__init__(
								    pad_token_id=snake_case_			,							eos_token_id=snake_case_			,							decoder_start_token_id=snake_case_			,							tie_word_embeddings=snake_case_			,							is_decoder=snake_case_			,							**snake_case_			,							)






					@classmethod
					def A   (					cls			,							snake_case_			,							**snake_case_    ) ->      "PretrainedConfig":







								'''simple docstring'''

								cls._set_token_in_kwargs(snake_case_    )

								__lowercase       ,			__lowercase			       = cls.get_config_dict(snake_case_			,							**snake_case_    )

								# get the text config dict if we are loading from Pix2StructConfig
								if config_dict.get('''model_type'''    ) == "pix2struct":
											__lowercase			       = config_dict['''text_config''']

								if "model_type" in config_dict and hasattr(cls			,							'''model_type'''    ) and config_dict["model_type"] != cls.model_type:
											logger.warning(
											    F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
											    F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

								return cls.from_dict(snake_case_			,							**snake_case_    )




class 						lowerCamelCase_							(					lowerCAmelCase__					):



					'''simple docstring'''







					__UpperCAmelCase							  =					"pix2struct_vision_model"






					def __init__(					self			,							snake_case_=7_6_8			,							snake_case_=7_6_8			,							snake_case_=2_0_4_8			,							snake_case_=6_4			,							snake_case_=1_2			,							snake_case_=1_2			,							snake_case_="gelu_new"			,							snake_case_=1e-6			,							snake_case_=0.0			,							snake_case_=0.0			,							snake_case_=1e-1_0			,							snake_case_=1.0			,							snake_case_=4_0_9_6			,							snake_case_=3_2			,							snake_case_=1_2_8			,							**snake_case_			,							) ->      Union[str, Any]:







								'''simple docstring'''

								super().__init__(**snake_case_    )

								__lowercase			       = hidden_size
								__lowercase			       = patch_embed_hidden_size
								__lowercase			       = d_ff
								__lowercase			       = dropout_rate
								__lowercase			       = num_hidden_layers
								__lowercase			       = num_attention_heads
								__lowercase			       = initializer_range
								__lowercase			       = initializer_factor
								__lowercase			       = attention_dropout
								__lowercase			       = layer_norm_eps
								__lowercase			       = dense_act_fn
								__lowercase			       = seq_len
								__lowercase			       = relative_attention_num_buckets
								__lowercase			       = relative_attention_max_distance
								__lowercase			       = d_kv






					@classmethod
					def A   (					cls			,							snake_case_			,							**snake_case_    ) ->      "PretrainedConfig":







								'''simple docstring'''

								cls._set_token_in_kwargs(snake_case_    )

								__lowercase       ,			__lowercase			       = cls.get_config_dict(snake_case_			,							**snake_case_    )

								# get the vision config dict if we are loading from Pix2StructConfig
								if config_dict.get('''model_type'''    ) == "pix2struct":
											__lowercase			       = config_dict['''vision_config''']

								if "model_type" in config_dict and hasattr(cls			,							'''model_type'''    ) and config_dict["model_type"] != cls.model_type:
											logger.warning(
											    F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
											    F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

								return cls.from_dict(snake_case_			,							**snake_case_    )




class 						lowerCamelCase_							(					lowerCAmelCase__					):



					'''simple docstring'''







					__UpperCAmelCase							  =					"pix2struct"
					__UpperCAmelCase							  =					True






					def __init__(					self			,							snake_case_=None			,							snake_case_=None			,							snake_case_=1.0			,							snake_case_=0.0_2			,							snake_case_=False			,							snake_case_=False			,							snake_case_=True			,							**snake_case_			,							) ->      Optional[int]:







								'''simple docstring'''

								super().__init__(tie_word_embeddings=snake_case_			,							is_encoder_decoder=snake_case_			,							**snake_case_    )

								if text_config is None:
											__lowercase			       = {}
											logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''    )

								if vision_config is None:
											__lowercase			       = {}
											logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.'''    )

								__lowercase			       = PixaStructTextConfig(**snake_case_    )
								__lowercase			       = PixaStructVisionConfig(**snake_case_    )

								__lowercase			       = self.text_config.decoder_start_token_id
								__lowercase			       = self.text_config.pad_token_id
								__lowercase			       = self.text_config.eos_token_id

								__lowercase			       = initializer_factor
								__lowercase			       = initializer_range

								__lowercase			       = self.initializer_range
								__lowercase			       = self.initializer_range

								__lowercase			       = is_vqa






					@classmethod
					def A   (					cls			,							snake_case_			,							snake_case_			,							**snake_case_    ) ->      List[str]:







								'''simple docstring'''

								return cls(text_config=text_config.to_dict()			,							vision_config=vision_config.to_dict()			,							**snake_case_    )






					def A   (					self    ) ->      Tuple:







								'''simple docstring'''

								__lowercase			       = copy.deepcopy(self.__dict__    )
								__lowercase			       = self.text_config.to_dict()
								__lowercase			       = self.vision_config.to_dict()
								__lowercase			       = self.__class__.model_type
								return output


