



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__    (A__		):







						"""simple docstring"""




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




									a__       :						Optional[int]							=  self.config_class(**self.inputs_dict  )
									self.parent.assertTrue(hasattr(__lowercase ,       """embed_dim"""  )  )
									self.parent.assertTrue(hasattr(__lowercase ,       """num_heads"""  )  )




class        snake_case__    :







						"""simple docstring"""




						def __init__(       self ,       __lowercase ,       __lowercase=1_3 ,       __lowercase=6_4 ,       __lowercase=3 ,       __lowercase=[1_6, 4_8, 9_6] ,       __lowercase=[1, 3, 6] ,       __lowercase=[1, 2, 1_0] ,       __lowercase=[7, 3, 3] ,       __lowercase=[4, 2, 2] ,       __lowercase=[2, 1, 1] ,       __lowercase=[2, 2, 2] ,       __lowercase=[False, False, True] ,       __lowercase=[0.0, 0.0, 0.0] ,       __lowercase=0.0_2 ,       __lowercase=1E-12 ,       __lowercase=True ,       __lowercase=True ,       __lowercase=2 ,       ) ->				Tuple:
									"""simple docstring"""




									a__       :						List[Any]							=  parent
									a__       :						Optional[Any]							=  batch_size
									a__       :						Union[str, Any]							=  image_size
									a__       :						str							=  patch_sizes
									a__       :						Any							=  patch_stride
									a__       :						Optional[Any]							=  patch_padding
									a__       :						List[Any]							=  is_training
									a__       :						Dict							=  use_labels
									a__       :						Optional[int]							=  num_labels
									a__       :						Union[str, Any]							=  num_channels
									a__       :						List[str]							=  embed_dim
									a__       :						Any							=  num_heads
									a__       :						str							=  stride_kv
									a__       :						List[Any]							=  depth
									a__       :						List[str]							=  cls_token
									a__       :						Optional[int]							=  attention_drop_rate
									a__       :						Optional[Any]							=  initializer_range
									a__       :						Any							=  layer_norm_eps

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[str]:
									"""simple docstring"""




									a__       :						str							=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

									a__       :						Union[str, Any]							=  self.get_config()
									return config, pixel_values, labels

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Dict:
									"""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 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase ,       __lowercase ,       __lowercase  ) ->				Tuple:
									"""simple docstring"""




									a__       :						Dict							=  TFCvtModel(config=__lowercase  )
									a__       :						Dict							=  model(__lowercase ,       training=__lowercase  )
									a__       :						int							=  (self.image_size, self.image_size)
									a__	,						a__       :						Optional[int]							=  image_size[0], image_size[1]
									for i in range(len(self.depth  )  ):
												a__       :						str							=  floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1  )
												a__       :						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 							SCREAMING_SNAKE_CASE__(       self ,       __lowercase ,       __lowercase ,       __lowercase  ) ->				Optional[Any]:
									"""simple docstring"""




									a__       :						Union[str, Any]							=  self.num_labels
									a__       :						Union[str, Any]							=  TFCvtForImageClassification(__lowercase  )
									a__       :						str							=  model(__lowercase ,       labels=__lowercase ,       training=__lowercase  )
									self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels)  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Dict:
									"""simple docstring"""




									a__       :						List[Any]							=  self.prepare_config_and_inputs()
									a__	,						a__	,						a__       :						Union[str, Any]							=  config_and_inputs
									a__       :						Any							=  {"""pixel_values""": pixel_values}
									return config, inputs_dict




@require_tf
class        snake_case__    (A__ ,     A__ ,     unittest.TestCase		):







						"""simple docstring"""




						__lowerCAmelCase					:Optional[int]      =					(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
						__lowerCAmelCase					:str      =					(
						    {"feature-extraction": TFCvtModel, "image-classification": TFCvtForImageClassification}
						    if is_tf_available()
						    else {}
						)
						__lowerCAmelCase					:Any      =					False
						__lowerCAmelCase					:str      =					False
						__lowerCAmelCase					:int      =					False
						__lowerCAmelCase					:Optional[Any]      =					False
						__lowerCAmelCase					:Any      =					False

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Any:
									"""simple docstring"""




									a__       :						List[Any]							=  TFCvtModelTester(self  )
									a__       :						str							=  TFCvtConfigTester(self ,       config_class=__lowercase ,       has_text_modality=__lowercase ,       hidden_size=3_7  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[str]:
									"""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 							SCREAMING_SNAKE_CASE__(       self  ) ->				Optional[int]:
									"""simple docstring"""




									pass

						@unittest.skip(reason="""Cvt does not use inputs_embeds"""  )
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Union[str, Any]:
									"""simple docstring"""




									pass

						@unittest.skip(reason="""Cvt does not support input and output embeddings"""  )
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Dict:
									"""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 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[str]:
									"""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 							SCREAMING_SNAKE_CASE__(       self  ) ->				Union[str, 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 							SCREAMING_SNAKE_CASE__(       self  ) ->				str:
									"""simple docstring"""




									a__       :						Optional[int]							=  tf.keras.mixed_precision.Policy("""mixed_float16"""  )
									tf.keras.mixed_precision.set_global_policy(__lowercase  )
									super().test_keras_fit()
									tf.keras.mixed_precision.set_global_policy("""float32"""  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Union[str, Any]:
									"""simple docstring"""




									a__	,						a__       :						Tuple							=  self.model_tester.prepare_config_and_inputs_for_common()

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

												a__       :						List[str]							=  ["""pixel_values"""]
												self.assertListEqual(arg_names[:1] ,       __lowercase  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				str:
									"""simple docstring"""




									def check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase  ):
												a__       :						Union[str, Any]							=  model_class(__lowercase  )

												a__       :						Dict							=  model(**self._prepare_for_class(__lowercase ,       __lowercase  )  )
												a__       :						List[str]							=  outputs.hidden_states

												a__       :						Optional[int]							=  len(self.model_tester.depth  )
												self.assertEqual(len(__lowercase  ) ,       __lowercase  )

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

									a__	,						a__       :						int							=  self.model_tester.prepare_config_and_inputs_for_common()

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

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

												check_hidden_states_output(__lowercase ,       __lowercase ,       __lowercase  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				List[str]:
									"""simple docstring"""




									a__       :						int							=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__lowercase  )

						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Any:
									"""simple docstring"""




									a__       :						List[str]							=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__lowercase  )

						@slow
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Union[str, Any]:
									"""simple docstring"""




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




def      lowerCAmelCase_					(  )							->	Any:







			"""simple docstring"""
			a__       :						Optional[int]							=  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
			return image





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







						"""simple docstring"""




						@cached_property
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Tuple:
									"""simple docstring"""




									return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )

						@slow
						def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Tuple:
									"""simple docstring"""




									a__       :						Optional[Any]							=  TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )

									a__       :						Optional[int]							=  self.default_image_processor
									a__       :						Optional[int]							=  prepare_img()
									a__       :						int							=  image_processor(images=__lowercase ,       return_tensors="""tf"""  )

									# forward pass
									a__       :						Optional[Any]							=  model(**__lowercase  )

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

									a__       :						Dict							=  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() ,       __lowercase ,       atol=1E-4  )  )









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


_lowercase		:				List[Any]      		={}

try:
		if not is_sentencepiece_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase		:				int      		=["BartphoTokenizer"]

if TYPE_CHECKING:
		try:
				if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_bartpho import BartphoTokenizer

else:
		import sys

		_lowercase		:				Tuple      		=_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




