





import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin

class 	A_       (a_     ,							unittest.TestCase			):
	UpperCAmelCase__					=			TransfoXLTokenizer
	UpperCAmelCase__					=			False
	UpperCAmelCase__					=			False
	def      _lowercase				(    self		):






				'''simple docstring'''







				super().setUp()

				UpperCAmelCase   		=				[
				    '''<unk>''',
				    '''[CLS]''',
				    '''[SEP]''',
				    '''want''',
				    '''unwanted''',
				    '''wa''',
				    '''un''',
				    '''running''',
				    ''',''',
				    '''low''',
				    '''l''',
				]
				UpperCAmelCase   		=				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]		)		)
	def      _lowercase				(    self							,					**_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				True
				return TransfoXLTokenizer.from_pretrained(self.tmpdirname							,					**_A		)
	def      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''<unk> UNwanted , running'''
				UpperCAmelCase   		=				'''<unk> unwanted, running'''
				return input_text, output_text
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				TransfoXLTokenizer(vocab_file=self.vocab_file							,					lower_case=_A		)

				UpperCAmelCase   		=				tokenizer.tokenize('''<unk> UNwanted , running'''		)
				self.assertListEqual(_A							,					['''<unk>''', '''unwanted''', ''',''', '''running''']		)

				self.assertListEqual(tokenizer.convert_tokens_to_ids(_A		)							,					[0, 4, 8, 7]		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				TransfoXLTokenizer(lower_case=_A		)

				self.assertListEqual(
				    tokenizer.tokenize(''' \tHeLLo ! how  \n Are yoU ?  '''		)							,					['''hello''', '''!''', '''how''', '''are''', '''you''', '''?''']		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				TransfoXLTokenizer(lower_case=_A		)

				self.assertListEqual(
				    tokenizer.tokenize(''' \tHeLLo ! how  \n Are yoU ?  '''		)							,					['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				TransfoXLTokenizer(lower_case=_A		)
				UpperCAmelCase   		=				'''Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?'''
				UpperCAmelCase   		=				[
				    '''Hello''',
				    '''(''',
				    '''bracket''',
				    ''')''',
				    '''and''',
				    '''side''',
				    '''@-@''',
				    '''scrolled''',
				    '''[''',
				    '''and''',
				    ''']''',
				    '''Henry''',
				    '''\'s''',
				    '''$''',
				    '''5''',
				    '''@,@''',
				    '''000''',
				    '''with''',
				    '''3''',
				    '''@.@''',
				    '''34''',
				    '''m''',
				    '''.''',
				    '''What''',
				    '''\'s''',
				    '''up''',
				    '''!''',
				    '''?''',
				]

				self.assertListEqual(tokenizer.tokenize(_A		)							,					_A		)

				self.assertEqual(tokenizer.convert_tokens_to_string(_A		)							,					_A		)







	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.get_tokenizer()
				UpperCAmelCase   		=				len(_A		)

				tokenizer.add_tokens(['''new1''', '''new2''']		)
				tokenizer.move_added_token('''new1'''							,					1		)

				# Check that moved token is not copied (duplicate)
				self.assertEqual(len(_A		)							,					original_len + 2		)
				# Check that token is moved to specified id
				self.assertEqual(tokenizer.encode('''new1'''		)							,					[1]		)
				self.assertEqual(tokenizer.decode([1]		)							,					'''new1'''		)










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 	A_       :
	def __init__(    self							,					_A							,					_A=1_3							,					_A=3_0							,					_A=2							,					_A=3							,					_A=True							,					_A=True							,					_A=3_2							,					_A=2							,					_A=4							,					_A=3_7							,					_A="gelu"							,					_A=0.1							,					_A=0.1							,					_A=1_0							,					_A=0.02							,					_A=3							,					_A=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      _lowercase				(    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      _lowercase				(    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      _lowercase				(    self							,					_A							,					_A							,					_A		):






				'''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      _lowercase				(    self							,					_A							,					_A							,					_A		):






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






				'''simple docstring'''







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

@require_tf
class 	A_       (a_     ,							a_     ,							unittest.TestCase			):
	UpperCAmelCase__					=			(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
	UpperCAmelCase__					=			(
	    {'''feature-extraction''': TFViTModel, '''image-classification''': TFViTForImageClassification}
	    if is_tf_available()
	    else {}
	)

	UpperCAmelCase__					=			False
	UpperCAmelCase__					=			False
	UpperCAmelCase__					=			False
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				TFViTModelTester(self		)
				UpperCAmelCase   		=				ConfigTester(self							,					config_class=_A							,					has_text_modality=_A							,					hidden_size=3_7		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				self.config_tester.run_common_tests()
	@unittest.skip(reason='''ViT does not use inputs_embeds'''		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				pass
	@unittest.skip(reason='''ViT does not use inputs_embeds'''		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				pass
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase		,			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      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase		,			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      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_A		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_A		)







	@slow
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				TFViTModel.from_pretrained('''google/vit-base-patch16-224'''		)
				self.assertIsNotNone(_A		)





def 						__SCREAMING_SNAKE_CASE  (				)  ->				Optional[Any]:







			'''simple docstring'''

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






@require_tf
@require_vision
class 	A_       (unittest.TestCase			):
	@cached_property
	def      _lowercase				(    self		):






				'''simple docstring'''







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







	@slow
	def      _lowercase				(    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, 1_0_0_0)		)
				self.assertEqual(outputs.logits.shape							,					_A		)

				UpperCAmelCase   		=				tf.constant([-0.27_44, 0.82_15, -0.08_36]		)

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



