


from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
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 TFViTForImageClassification, TFViTModel


if is_vision_available():
						from PIL import Image

						from transformers import ViTImageProcessor







class        lowerCamelCase__	:






			"""simple docstring"""






			def __init__(							self      ,						snake_case      ,						snake_case=13      ,						snake_case=30      ,						snake_case=2      ,						snake_case=3      ,						snake_case=True      ,						snake_case=True      ,						snake_case=32      ,						snake_case=2      ,						snake_case=4      ,						snake_case=37      ,						snake_case="gelu"      ,						snake_case=0.1      ,						snake_case=0.1      ,						snake_case=10      ,						snake_case=0.02      ,						snake_case=3      ,						snake_case=None      ,						):

						'''simple docstring'''


						UpperCamelCase__					=       parent
						UpperCamelCase__					=       batch_size
						UpperCamelCase__					=       image_size
						UpperCamelCase__					=       patch_size
						UpperCamelCase__					=       num_channels
						UpperCamelCase__					=       is_training
						UpperCamelCase__					=       use_labels
						UpperCamelCase__					=       hidden_size
						UpperCamelCase__					=       num_hidden_layers
						UpperCamelCase__					=       num_attention_heads
						UpperCamelCase__					=       intermediate_size
						UpperCamelCase__					=       hidden_act
						UpperCamelCase__					=       hidden_dropout_prob
						UpperCamelCase__					=       attention_probs_dropout_prob
						UpperCamelCase__					=       type_sequence_label_size
						UpperCamelCase__					=       initializer_range
						UpperCamelCase__					=       scope

						# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
						UpperCamelCase__					=       (image_size // patch_size) ** 2
						UpperCamelCase__					=       num_patches + 1




			def 				snake_case__				(							self     ):

						'''simple docstring'''


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

						UpperCamelCase__					=       None
						if self.use_labels:
									UpperCamelCase__					=       ids_tensor([self.batch_size]      ,						self.type_sequence_label_size     )

						UpperCamelCase__					=       self.get_config()

						return config, pixel_values, labels




			def 				snake_case__				(							self     ):

						'''simple docstring'''


						return ViTConfig(
						    image_size=self.image_size      ,						patch_size=self.patch_size      ,						num_channels=self.num_channels      ,						hidden_size=self.hidden_size      ,						num_hidden_layers=self.num_hidden_layers      ,						num_attention_heads=self.num_attention_heads      ,						intermediate_size=self.intermediate_size      ,						hidden_act=self.hidden_act      ,						hidden_dropout_prob=self.hidden_dropout_prob      ,						attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,						is_decoder=__A      ,						initializer_range=self.initializer_range      ,						)




			def 				snake_case__				(							self      ,						snake_case      ,						snake_case      ,						snake_case     ):

						'''simple docstring'''


						UpperCamelCase__					=       TFViTModel(config=__A     )
						UpperCamelCase__					=       model(__A      ,						training=__A     )
						self.parent.assertEqual(result.last_hidden_state.shape      ,						(self.batch_size, self.seq_length, self.hidden_size)     )

						# Test with an image with different size than the one specified in config.
						UpperCamelCase__					=       self.image_size // 2
						UpperCamelCase__					=       pixel_values[:, :, :image_size, :image_size]
						UpperCamelCase__					=       model(__A      ,						interpolate_pos_encoding=__A      ,						training=__A     )
						UpperCamelCase__					=       (image_size // self.patch_size) ** 2 + 1
						self.parent.assertEqual(result.last_hidden_state.shape      ,						(self.batch_size, seq_length, self.hidden_size)     )




			def 				snake_case__				(							self      ,						snake_case      ,						snake_case      ,						snake_case     ):

						'''simple docstring'''


						UpperCamelCase__					=       self.type_sequence_label_size
						UpperCamelCase__					=       TFViTForImageClassification(__A     )
						UpperCamelCase__					=       model(__A      ,						labels=__A      ,						training=__A     )
						self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.type_sequence_label_size)     )

						# Test with an image with different size than the one specified in config.
						UpperCamelCase__					=       self.image_size // 2
						UpperCamelCase__					=       pixel_values[:, :, :image_size, :image_size]
						UpperCamelCase__					=       model(__A      ,						interpolate_pos_encoding=__A      ,						training=__A     )
						self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.type_sequence_label_size)     )

						# test greyscale images
						UpperCamelCase__					=       1
						UpperCamelCase__					=       TFViTForImageClassification(__A     )
						UpperCamelCase__					=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
						UpperCamelCase__					=       model(__A     )
						self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.type_sequence_label_size)     )







			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       self.prepare_config_and_inputs()
						UpperCamelCase__					=       config_and_inputs
						UpperCamelCase__					=       {"pixel_values": pixel_values}
						return config, inputs_dict







@require_tf
class        lowerCamelCase__	(							UpperCamelCase_					,	UpperCamelCase_					,	unittest.TestCase      ):






			"""simple docstring"""






			_UpperCamelCase		:					Dict    =		(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
			_UpperCamelCase		:					Any    =		(
			    {'feature-extraction': TFViTModel, 'image-classification': TFViTForImageClassification}
			    if is_tf_available()
			    else {}
			)

			_UpperCamelCase		:					Tuple    =		False
			_UpperCamelCase		:					Union[str, Any]    =		False
			_UpperCamelCase		:					Optional[Any]    =		False




			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       TFViTModelTester(self     )
						UpperCamelCase__					=       ConfigTester(self      ,						config_class=__A      ,						has_text_modality=__A      ,						hidden_size=37     )




			def 				snake_case__				(							self     ):

						'''simple docstring'''


						self.config_tester.run_common_tests()




			@unittest.skip(reason="ViT does not use inputs_embeds"     )
			def 				snake_case__				(							self     ):

						'''simple docstring'''


						pass




			@unittest.skip(reason="ViT does not use inputs_embeds"     )
			def 				snake_case__				(							self     ):

						'''simple docstring'''


						pass




			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									UpperCamelCase__					=       model_class(__A     )
									self.assertIsInstance(model.get_input_embeddings()      ,						(tf.keras.layers.Layer)     )
									UpperCamelCase__					=       model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(__A      ,						tf.keras.layers.Layer     )     )




			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									UpperCamelCase__					=       model_class(__A     )
									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]      ,						__A     )




			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__A     )




			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__A     )







			@slow
			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       TFViTModel.from_pretrained("google/vit-base-patch16-224"     )
						self.assertIsNotNone(__A     )



def        UpperCamelCase_(				)->		Optional[int]:
			UpperCamelCase__					=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
			return image







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






			"""simple docstring"""






			@cached_property
			def 				snake_case__				(							self     ):

						'''simple docstring'''


						return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"     ) if is_vision_available() else None







			@slow
			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224"     )

						UpperCamelCase__					=       self.default_image_processor
						UpperCamelCase__					=       prepare_img()
						UpperCamelCase__					=       image_processor(images=__A      ,						return_tensors="tf"     )

						# forward pass
						UpperCamelCase__					=       model(**__A     )

						# verify the logits
						UpperCamelCase__					=       tf.TensorShape((1, 1000)     )
						self.assertEqual(outputs.logits.shape      ,						__A     )

						UpperCamelCase__					=       tf.constant([-0.2744, 0.8215, -0.0836]     )

						tf.debugging.assert_near(outputs.logits[0, :3]      ,						__A      ,						atol=1E-4     )






import os
import tempfile
import unittest

from transformers.models.marian.convert_marian_tatoeba_to_pytorch import DEFAULT_REPO, TatoebaConverter
from transformers.testing_utils import slow
from transformers.utils import cached_property







@unittest.skipUnless(os.path.exists(UpperCAmelCase      )					,	'Tatoeba directory does not exist.'      )
class        lowerCamelCase__	(							unittest.TestCase      ):






			"""simple docstring"""






			@cached_property
			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       tempfile.mkdtemp()
						return TatoebaConverter(save_dir=snake_case     )




			@slow
			def 				snake_case__				(							self     ):

						'''simple docstring'''


						self.resolver.convert_models(["heb-eng"]     )







			@slow
			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__,      UpperCamelCase__					=       self.resolver.write_model_card("opus-mt-he-en"      ,						dry_run=snake_case     )
						assert mmeta["long_pair"] == "heb-eng"


