



from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
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 (
							    TFDeiTForImageClassification,
							    TFDeiTForImageClassificationWithTeacher,
							    TFDeiTForMaskedImageModeling,
							    TFDeiTModel,
							)
							from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import DeiTImageProcessor
class 				_snake_case			:




				def __init__(						self					:						Optional[Any],					__lowercase					:						str,					__lowercase					:						int=13,					__lowercase					:						Optional[Any]=30,					__lowercase					:						Optional[Any]=2,					__lowercase					:						int=3,					__lowercase					:						int=True,					__lowercase					:						int=True,					__lowercase					:						Tuple=32,					__lowercase					:						List[Any]=2,					__lowercase					:						List[Any]=4,					__lowercase					:						int=37,					__lowercase					:						Dict="gelu",					__lowercase					:						Union[str, Any]=0.1,					__lowercase					:						Optional[Any]=0.1,					__lowercase					:						Optional[int]=10,					__lowercase					:						int=0.02,					__lowercase					:						Tuple=3,					__lowercase					:						int=None,					__lowercase					:						Optional[int]=2,					):
							lowercase__					  =				parent
							lowercase__					  =				batch_size
							lowercase__					  =				image_size
							lowercase__					  =				patch_size
							lowercase__					  =				num_channels
							lowercase__					  =				is_training
							lowercase__					  =				use_labels
							lowercase__					  =				hidden_size
							lowercase__					  =				num_hidden_layers
							lowercase__					  =				num_attention_heads
							lowercase__					  =				intermediate_size
							lowercase__					  =				hidden_act
							lowercase__					  =				hidden_dropout_prob
							lowercase__					  =				attention_probs_dropout_prob
							lowercase__					  =				type_sequence_label_size
							lowercase__					  =				initializer_range
							lowercase__					  =				scope
							lowercase__					  =				encoder_stride

							# in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
							lowercase__					  =				(image_size // patch_size) ** 2
							lowercase__					  =				num_patches + 2




				def 							A__      (						self					:						int						):
							lowercase__					  =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

							lowercase__					  =				self.get_config()

							return config, pixel_values, labels




				def 							A__      (						self					:						Dict						):
							return DeiTConfig(
							    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=__lowercase,					initializer_range=self.initializer_range,					encoder_stride=self.encoder_stride,					)




				def 							A__      (						self					:						Optional[int],					__lowercase					:						Dict,					__lowercase					:						Optional[Any],					__lowercase					:						Union[str, Any]						):
							lowercase__					  =				TFDeiTModel(config=__lowercase						)
							lowercase__					  =				model(__lowercase						)
							self.parent.assertEqual(result.last_hidden_state.shape,					(self.batch_size, self.seq_length, self.hidden_size)						)




				def 							A__      (						self					:						List[str],					__lowercase					:						Dict,					__lowercase					:						int,					__lowercase					:						Dict						):
							lowercase__					  =				TFDeiTForMaskedImageModeling(config=__lowercase						)
							lowercase__					  =				model(__lowercase						)
							self.parent.assertEqual(
							    result.reconstruction.shape,					(self.batch_size, self.num_channels, self.image_size, self.image_size)						)

							# test greyscale images
							lowercase__					  =				1
							lowercase__					  =				TFDeiTForMaskedImageModeling(__lowercase						)

							lowercase__					  =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
							lowercase__					  =				model(__lowercase						)
							self.parent.assertEqual(result.reconstruction.shape,					(self.batch_size, 1, self.image_size, self.image_size)						)




				def 							A__      (						self					:						Tuple,					__lowercase					:						Dict,					__lowercase					:						Union[str, Any],					__lowercase					:						Tuple						):
							lowercase__					  =				self.type_sequence_label_size
							lowercase__					  =				TFDeiTForImageClassification(__lowercase						)
							lowercase__					  =				model(__lowercase,					labels=__lowercase						)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.type_sequence_label_size)						)

							# test greyscale images
							lowercase__					  =				1
							lowercase__					  =				TFDeiTForImageClassification(__lowercase						)

							lowercase__					  =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
							lowercase__					  =				model(__lowercase,					labels=__lowercase						)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.type_sequence_label_size)						)




				def 							A__      (						self					:						Union[str, Any]						):
							lowercase__					  =				self.prepare_config_and_inputs()
							lowercase__  ,       lowercase__  ,       lowercase__					  =				config_and_inputs
							lowercase__					  =				{"pixel_values": pixel_values}
							return config, inputs_dict
@require_tf
class 				_snake_case			(					lowercase__    ,	lowercase__    ,	unittest.TestCase):
				UpperCamelCase__				:					List[Any]     		=(
				    (
				        TFDeiTModel,
				        TFDeiTForImageClassification,
				        TFDeiTForImageClassificationWithTeacher,
				        TFDeiTForMaskedImageModeling,
				    )
				    if is_tf_available()
				    else ()
				)
				UpperCamelCase__				:					Optional[Any]     		=(
				    {
				        """feature-extraction""": TFDeiTModel,
				        """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
				    }
				    if is_tf_available()
				    else {}
				)

				UpperCamelCase__				:					List[Any]     		=False
				UpperCamelCase__				:					Optional[Any]     		=False
				UpperCamelCase__				:					Tuple     		=False
				UpperCamelCase__				:					int     		=False




				def 							A__      (						self					:						Dict						):
							lowercase__					  =				TFDeiTModelTester(self						)
							lowercase__					  =				ConfigTester(self,					config_class=__lowercase,					has_text_modality=__lowercase,					hidden_size=37						)




				def 							A__      (						self					:						Union[str, Any]						):
							self.config_tester.run_common_tests()




				@unittest.skip(reason="DeiT does not use inputs_embeds"						)
				def 							A__      (						self					:						List[str]						):
							pass




				def 							A__      (						self					:						int						):
							lowercase__  ,       lowercase__					  =				self.model_tester.prepare_config_and_inputs_for_common()

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




				def 							A__      (						self					:						List[str]						):
							lowercase__  ,       lowercase__					  =				self.model_tester.prepare_config_and_inputs_for_common()

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




				def 							A__      (						self					:						Union[str, Any]						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowercase						)




				def 							A__      (						self					:						List[str]						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*__lowercase						)




				def 							A__      (						self					:						Union[str, Any]						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__lowercase						)




				def 							A__      (						self					:						Optional[Any],					__lowercase					:						List[str],					__lowercase					:						str,					__lowercase					:						Optional[int]=False						):
							lowercase__					  =				super()._prepare_for_class(__lowercase,					__lowercase,					return_labels=__lowercase						)

							if return_labels:
										if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call						).parameters:
													del inputs_dict["labels"]

							return inputs_dict




				@slow
				def 							A__      (						self					:						Optional[int]						):
							for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowercase__					  =				TFDeiTModel.from_pretrained(__lowercase						)
										self.assertIsNotNone(__lowercase						)



def __lowerCAmelCase (     ):
			lowercase__					  =				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
			return image




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




				@cached_property
				def 							A__      (						self					:						List[str]						):
							return (
							    DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224"						)
							    if is_vision_available()
							    else None
							)




				@slow
				def 							A__      (						self					:						Dict						):
							lowercase__					  =				TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"						)

							lowercase__					  =				self.default_image_processor
							lowercase__					  =				prepare_img()
							lowercase__					  =				image_processor(images=__lowercase,					return_tensors="tf"						)

							# forward pass
							lowercase__					  =				model(**__lowercase						)

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

							lowercase__					  =				tf.constant([-1.0266, 0.1912, -1.2861]						)

							self.assertTrue(np.allclose(outputs.logits[0, :3],					__lowercase,					atol=1e-4						)						)









import os
import unittest

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

from ...test_tokenization_common import TokenizerTesterMixin
class 				_snake_case			(					lowercase__    ,	unittest.TestCase):
				UpperCamelCase__				:					Dict     		=TransfoXLTokenizer
				UpperCamelCase__				:					List[Any]     		=False
				UpperCamelCase__				:					List[Any]     		=False




				def 							A__      (						self					:						Union[str, Any]						):
							super().setUp()

							lowercase__					  =				[
							    "<unk>",
							    "[CLS]",
							    "[SEP]",
							    "want",
							    "unwanted",
							    "wa",
							    "un",
							    "running",
							    ",",
							    "low",
							    "l",
							]
							lowercase__					  =				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 							A__      (						self					:						Union[str, Any],					**__lowercase					:						Any						):
							lowercase__					  =				True
							return TransfoXLTokenizer.from_pretrained(self.tmpdirname,					**__lowercase						)




				def 							A__      (						self					:						Tuple,					__lowercase					:						Optional[int]						):
							lowercase__					  =				"<unk> UNwanted , running"
							lowercase__					  =				"<unk> unwanted, running"
							return input_text, output_text




				def 							A__      (						self					:						str						):
							lowercase__					  =				TransfoXLTokenizer(vocab_file=self.vocab_file,					lower_case=__lowercase						)

							lowercase__					  =				tokenizer.tokenize("<unk> UNwanted , running"						)
							self.assertListEqual(__lowercase,					["<unk>", "unwanted", ",", "running"]						)

							self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowercase						),					[0, 4, 8, 7]						)




				def 							A__      (						self					:						Tuple						):
							lowercase__					  =				TransfoXLTokenizer(lower_case=__lowercase						)

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




				def 							A__      (						self					:						Tuple						):
							lowercase__					  =				TransfoXLTokenizer(lower_case=__lowercase						)

							self.assertListEqual(
							    tokenizer.tokenize(" \tHeLLo ! how  \n Are yoU ?  "						),					["HeLLo", "!", "how", "Are", "yoU", "?"]						)




				def 							A__      (						self					:						str						):
							lowercase__					  =				TransfoXLTokenizer(lower_case=__lowercase						)
							lowercase__					  =				"Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"
							lowercase__					  =				[
							    "Hello",
							    "(",
							    "bracket",
							    ")",
							    "and",
							    "side",
							    "@-@",
							    "scrolled",
							    "[",
							    "and",
							    "]",
							    "Henry",
							    "'s",
							    "$",
							    "5",
							    "@,@",
							    "000",
							    "with",
							    "3",
							    "@.@",
							    "34",
							    "m",
							    ".",
							    "What",
							    "'s",
							    "up",
							    "!",
							    "?",
							]

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

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




				def 							A__      (						self					:						List[str]						):
							lowercase__					  =				self.get_tokenizer()
							lowercase__					  =				len(__lowercase						)

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

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




