





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 SCREAMING_SNAKE_CASE_  :






						def __init__(					self					:					Optional[Any]						,      _A					:					Dict						,      _A					:					Tuple=13						,      _A					:					Any=30						,      _A					:					Optional[Any]=2						,      _A					:					Optional[int]=3						,      _A					:					Optional[int]=True						,      _A					:					Union[str, Any]=True						,      _A					:					Optional[Any]=32						,      _A					:					List[Any]=2						,      _A					:					str=4						,      _A					:					Any=37						,      _A					:					Optional[Any]="gelu"						,      _A					:					List[str]=0.1						,      _A					:					Dict=0.1						,      _A					:					Optional[Any]=10						,      _A					:					str=0.0_2						,      _A					:					str=3						,      _A					:					Dict=None						,      _A					:					List[str]=2						,      )							->			Union[str, Any]:







							"""simple docstring"""


							snake_case_						:	int      =		parent
							snake_case_						:	Optional[int]      =		batch_size
							snake_case_						:	Any      =		image_size
							snake_case_						:	List[str]      =		patch_size
							snake_case_						:	Tuple      =		num_channels
							snake_case_						:	List[Any]      =		is_training
							snake_case_						:	List[str]      =		use_labels
							snake_case_						:	int      =		hidden_size
							snake_case_						:	Tuple      =		num_hidden_layers
							snake_case_						:	Tuple      =		num_attention_heads
							snake_case_						:	List[Any]      =		intermediate_size
							snake_case_						:	Any      =		hidden_act
							snake_case_						:	Dict      =		hidden_dropout_prob
							snake_case_						:	Tuple      =		attention_probs_dropout_prob
							snake_case_						:	Optional[Any]      =		type_sequence_label_size
							snake_case_						:	Union[str, Any]      =		initializer_range
							snake_case_						:	List[str]      =		scope
							snake_case_						:	List[Any]      =		encoder_stride

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






						def UpperCAmelCase_ (					self					:					Tuple		)							->			Optional[Any]:







							"""simple docstring"""


							snake_case_						:	Optional[Any]      =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

							snake_case_						:	Optional[int]      =		None
							if self.use_labels:
								snake_case_						:	Optional[int]      =		ids_tensor([self.batch_size]						,      self.type_sequence_label_size		)

							snake_case_						:	Tuple      =		self.get_config()

							return config, pixel_values, labels






						def UpperCAmelCase_ (					self					:					Union[str, Any]		)							->			Optional[int]:







							"""simple docstring"""


							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=UpperCamelCase__						,      initializer_range=self.initializer_range						,      encoder_stride=self.encoder_stride						,      )






						def UpperCAmelCase_ (					self					:					str						,      _A					:					List[Any]						,      _A					:					Dict						,      _A					:					List[Any]		)							->			List[str]:







							"""simple docstring"""


							snake_case_						:	List[str]      =		TFDeiTModel(config=UpperCamelCase__		)
							snake_case_						:	Union[str, Any]      =		model(UpperCamelCase__		)
							self.parent.assertEqual(result.last_hidden_state.shape						,      (self.batch_size, self.seq_length, self.hidden_size)		)






						def UpperCAmelCase_ (					self					:					Any						,      _A					:					Optional[int]						,      _A					:					Tuple						,      _A					:					str		)							->			Optional[int]:







							"""simple docstring"""


							snake_case_						:	int      =		TFDeiTForMaskedImageModeling(config=UpperCamelCase__		)
							snake_case_						:	List[str]      =		model(UpperCamelCase__		)
							self.parent.assertEqual(
							    result.reconstruction.shape						,      (self.batch_size, self.num_channels, self.image_size, self.image_size)		)

							# test greyscale images
							snake_case_						:	str      =		1
							snake_case_						:	Optional[int]      =		TFDeiTForMaskedImageModeling(UpperCamelCase__		)

							snake_case_						:	Optional[int]      =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
							snake_case_						:	Dict      =		model(UpperCamelCase__		)
							self.parent.assertEqual(result.reconstruction.shape						,      (self.batch_size, 1, self.image_size, self.image_size)		)






						def UpperCAmelCase_ (					self					:					Optional[int]						,      _A					:					Dict						,      _A					:					Tuple						,      _A					:					Tuple		)							->			int:







							"""simple docstring"""


							snake_case_						:	Optional[Any]      =		self.type_sequence_label_size
							snake_case_						:	Optional[int]      =		TFDeiTForImageClassification(UpperCamelCase__		)
							snake_case_						:	Tuple      =		model(UpperCamelCase__						,      labels=UpperCamelCase__		)
							self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.type_sequence_label_size)		)

							# test greyscale images
							snake_case_						:	List[str]      =		1
							snake_case_						:	str      =		TFDeiTForImageClassification(UpperCamelCase__		)

							snake_case_						:	Optional[int]      =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
							snake_case_						:	Optional[int]      =		model(UpperCamelCase__						,      labels=UpperCamelCase__		)
							self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.type_sequence_label_size)		)






						def UpperCAmelCase_ (					self					:					int		)							->			str:







							"""simple docstring"""


							snake_case_						:	int      =		self.prepare_config_and_inputs()
							snake_case_						:	Any      =		config_and_inputs
							snake_case_						:	Dict      =		{"pixel_values": pixel_values}
							return config, inputs_dict
@require_tf
class SCREAMING_SNAKE_CASE_  (    lowerCAmelCase__				, lowerCAmelCase__				, unittest.TestCase    ):
						__magic_name__:	List[Any]       					=				(
						    (
						        TFDeiTModel,
						        TFDeiTForImageClassification,
						        TFDeiTForImageClassificationWithTeacher,
						        TFDeiTForMaskedImageModeling,
						    )
						    if is_tf_available()
						    else ()
						)
						__magic_name__:	Optional[Any]       					=				(
						    {
						        """feature-extraction""": TFDeiTModel,
						        """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
						    }
						    if is_tf_available()
						    else {}
						)

						__magic_name__:	Dict       					=				False
						__magic_name__:	Tuple       					=				False
						__magic_name__:	str       					=				False
						__magic_name__:	List[Any]       					=				False






						def UpperCAmelCase_ (					self					:					List[Any]		)							->			Union[str, Any]:







							"""simple docstring"""


							snake_case_						:	Optional[int]      =		TFDeiTModelTester(self		)
							snake_case_						:	Any      =		ConfigTester(self						,      config_class=UpperCamelCase__						,      has_text_modality=UpperCamelCase__						,      hidden_size=37		)






						def UpperCAmelCase_ (					self					:					str		)							->			Optional[int]:







							"""simple docstring"""


							self.config_tester.run_common_tests()






						@unittest.skip(reason='DeiT does not use inputs_embeds'		)
						def UpperCAmelCase_ (					self					:					Optional[int]		)							->			int:







							"""simple docstring"""


							pass






						def UpperCAmelCase_ (					self					:					Union[str, Any]		)							->			List[Any]:







							"""simple docstring"""


							snake_case_						:	List[Any]      =		self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_						:	Optional[Any]      =		model_class(UpperCamelCase__		)
								self.assertIsInstance(model.get_input_embeddings()						,      (tf.keras.layers.Layer)		)
								snake_case_						:	Optional[int]      =		model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(UpperCamelCase__						,      tf.keras.layers.Dense		)		)






						def UpperCAmelCase_ (					self					:					Dict		)							->			Tuple:







							"""simple docstring"""


							snake_case_						:	str      =		self.model_tester.prepare_config_and_inputs_for_common()

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

								snake_case_						:	str      =		["pixel_values"]
								self.assertListEqual(arg_names[:1]						,      UpperCamelCase__		)






						def UpperCAmelCase_ (					self					:					Optional[Any]		)							->			str:







							"""simple docstring"""


							snake_case_						:	int      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCamelCase__		)






						def UpperCAmelCase_ (					self					:					List[str]		)							->			Tuple:







							"""simple docstring"""


							snake_case_						:	Optional[int]      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase__		)






						def UpperCAmelCase_ (					self					:					Tuple		)							->			Optional[int]:







							"""simple docstring"""


							snake_case_						:	Union[str, Any]      =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__		)






						def UpperCAmelCase_ (					self					:					int						,      _A					:					Union[str, Any]						,      _A					:					List[str]						,      _A					:					List[Any]=False		)							->			List[str]:







							"""simple docstring"""


							snake_case_						:	Optional[Any]      =		super()._prepare_for_class(UpperCamelCase__						,      UpperCamelCase__						,      return_labels=UpperCamelCase__		)

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







							"""simple docstring"""


							for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								snake_case_						:	Any      =		TFDeiTModel.from_pretrained(UpperCamelCase__		)
								self.assertIsNotNone(UpperCamelCase__		)


def 							SCREAMING_SNAKE_CASE__  (			):
	snake_case_						:	str      =		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
	return image


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






						@cached_property
						def UpperCAmelCase_ (					self					:					str		)							->			List[str]:







							"""simple docstring"""


							return (
							    DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224'		)
							    if is_vision_available()
							    else None
							)






						@slow
						def UpperCAmelCase_ (					self					:					str		)							->			str:







							"""simple docstring"""


							snake_case_						:	int      =		TFDeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'		)

							snake_case_						:	str      =		self.default_image_processor
							snake_case_						:	Optional[Any]      =		prepare_img()
							snake_case_						:	Tuple      =		image_processor(images=UpperCamelCase__						,      return_tensors='tf'		)

							# forward pass
							snake_case_						:	Optional[int]      =		model(**UpperCamelCase__		)

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

							snake_case_						:	Optional[Any]      =		tf.constant([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1]		)

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







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


SCREAMING_SNAKE_CASE__       :      List[Any]  =   {'processing_layoutxlm': ['LayoutXLMProcessor']}

try:
       if not is_sentencepiece_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__       :      Optional[Any]  =   ['LayoutXLMTokenizer']

try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__       :      Dict  =   ['LayoutXLMTokenizerFast']

if TYPE_CHECKING:
       from .processing_layoutxlm import LayoutXLMProcessor

       try:
              if not is_sentencepiece_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_layoutxlm import LayoutXLMTokenizer

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_layoutxlm_fast import LayoutXLMTokenizerFast

else:
       import sys

       SCREAMING_SNAKE_CASE__       :      str  =   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






