





'''simple docstring'''





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



						"""simple docstring"""

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





										'''simple docstring'''







										_snake_case :			str  						=   parent
										_snake_case :			List[Any]  						=   batch_size
										_snake_case :			Dict  						=   image_size
										_snake_case :			Any  						=   patch_size
										_snake_case :			Any  						=   num_channels
										_snake_case :			str  						=   is_training
										_snake_case :			Any  						=   use_labels
										_snake_case :			Union[str, Any]  						=   hidden_size
										_snake_case :			Optional[int]  						=   num_hidden_layers
										_snake_case :			Any  						=   num_attention_heads
										_snake_case :			Any  						=   intermediate_size
										_snake_case :			str  						=   hidden_act
										_snake_case :			Tuple  						=   hidden_dropout_prob
										_snake_case :			List[str]  						=   attention_probs_dropout_prob
										_snake_case :			Union[str, Any]  						=   type_sequence_label_size
										_snake_case :			List[Any]  						=   initializer_range
										_snake_case :			Optional[int]  						=   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 :			Tuple  						=   (image_size // patch_size) ** 2
										_snake_case :			Optional[int]  						=   num_patches + 2







						def 		__UpperCAmelCase       (    self			:		Dict					):





										'''simple docstring'''







										_snake_case :			Union[str, Any]  						=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

										_snake_case :			Any  						=   None
										if self.use_labels:
														_snake_case :			List[str]  						=   ids_tensor([self.batch_size] ,				self.type_sequence_label_size					)

										_snake_case :			List[str]  						=   self.get_config()

										return config, pixel_values, labels







						def 		__UpperCAmelCase       (    self			:		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=lowerCamelCase_ ,				initializer_range=self.initializer_range ,				encoder_stride=self.encoder_stride ,				)







						def 		__UpperCAmelCase       (    self			:		Any ,				lowerCamelCase_			:		Union[str, Any] ,				lowerCamelCase_			:		int ,				lowerCamelCase_			:		List[str]					):





										'''simple docstring'''







										_snake_case :			Dict  						=   TFDeiTModel(config=lowerCamelCase_					)
										_snake_case :			Tuple  						=   model(lowerCamelCase_					)
										self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size)					)







						def 		__UpperCAmelCase       (    self			:		List[str] ,				lowerCamelCase_			:		List[str] ,				lowerCamelCase_			:		Optional[Any] ,				lowerCamelCase_			:		Any					):





										'''simple docstring'''







										_snake_case :			Tuple  						=   TFDeiTForMaskedImageModeling(config=lowerCamelCase_					)
										_snake_case :			Dict  						=   model(lowerCamelCase_					)
										self.parent.assertEqual(
										    result.reconstruction.shape ,				(self.batch_size, self.num_channels, self.image_size, self.image_size)					)

										# test greyscale images
										_snake_case :			Dict  						=   1
										_snake_case :			List[Any]  						=   TFDeiTForMaskedImageModeling(lowerCamelCase_					)

										_snake_case :			str  						=   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
										_snake_case :			Optional[int]  						=   model(lowerCamelCase_					)
										self.parent.assertEqual(result.reconstruction.shape ,				(self.batch_size, 1, self.image_size, self.image_size)					)







						def 		__UpperCAmelCase       (    self			:		str ,				lowerCamelCase_			:		str ,				lowerCamelCase_			:		Union[str, Any] ,				lowerCamelCase_			:		int					):





										'''simple docstring'''







										_snake_case :			List[Any]  						=   self.type_sequence_label_size
										_snake_case :			Dict  						=   TFDeiTForImageClassification(lowerCamelCase_					)
										_snake_case :			Tuple  						=   model(lowerCamelCase_ ,				labels=lowerCamelCase_					)
										self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.type_sequence_label_size)					)

										# test greyscale images
										_snake_case :			Optional[int]  						=   1
										_snake_case :			List[Any]  						=   TFDeiTForImageClassification(lowerCamelCase_					)

										_snake_case :			Dict  						=   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
										_snake_case :			List[str]  						=   model(lowerCamelCase_ ,				labels=lowerCamelCase_					)
										self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.type_sequence_label_size)					)







						def 		__UpperCAmelCase       (    self			:		List[Any]					):





										'''simple docstring'''







										_snake_case :			str  						=   self.prepare_config_and_inputs()
										_snake_case :			Union[str, Any]  						=   config_and_inputs
										_snake_case :			Optional[Any]  						=   {'pixel_values': pixel_values}
										return config, inputs_dict



@require_tf
class 			lowercase				(							a_		,			a_		,			unittest.TestCase     ):



						"""simple docstring"""

						_UpperCamelCase  :	Optional[Any]								=	(
						    (
						        TFDeiTModel,
						        TFDeiTForImageClassification,
						        TFDeiTForImageClassificationWithTeacher,
						        TFDeiTForMaskedImageModeling,
						    )
						    if is_tf_available()
						    else ()
						)
						_UpperCamelCase  :	int								=	(
						    {
						        "feature-extraction": TFDeiTModel,
						        "image-classification": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
						    }
						    if is_tf_available()
						    else {}
						)

						_UpperCamelCase  :	Optional[int]								=	False
						_UpperCamelCase  :	Optional[Any]								=	False
						_UpperCamelCase  :	Tuple								=	False
						_UpperCamelCase  :	Optional[int]								=	False







						def 		__UpperCAmelCase       (    self			:		Tuple					):





										'''simple docstring'''







										_snake_case :			Any  						=   TFDeiTModelTester(self					)
										_snake_case :			Optional[Any]  						=   ConfigTester(self ,				config_class=lowerCamelCase_ ,				has_text_modality=lowerCamelCase_ ,				hidden_size=37					)







						def 		__UpperCAmelCase       (    self			:		List[str]					):





										'''simple docstring'''







										self.config_tester.run_common_tests()







						@unittest.skip(reason='DeiT does not use inputs_embeds'					)
						def 		__UpperCAmelCase       (    self			:		Dict					):





										'''simple docstring'''







										pass







						def 		__UpperCAmelCase       (    self			:		str					):





										'''simple docstring'''







										_snake_case :			Optional[Any]  						=   self.model_tester.prepare_config_and_inputs_for_common()

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







						def 		__UpperCAmelCase       (    self			:		List[str]					):





										'''simple docstring'''







										_snake_case :			Optional[Any]  						=   self.model_tester.prepare_config_and_inputs_for_common()

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

														_snake_case :			Optional[int]  						=   ['pixel_values']
														self.assertListEqual(arg_names[:1] ,				lowerCamelCase_					)







						def 		__UpperCAmelCase       (    self			:		Optional[int]					):





										'''simple docstring'''







										_snake_case :			Dict  						=   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCamelCase_					)







						def 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







										_snake_case :			Optional[Any]  						=   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_					)







						def 		__UpperCAmelCase       (    self			:		Optional[Any]					):





										'''simple docstring'''







										_snake_case :			List[Any]  						=   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_					)







						def 		__UpperCAmelCase       (    self			:		Tuple ,				lowerCamelCase_			:		Tuple ,				lowerCamelCase_			:		int ,				lowerCamelCase_			:		List[str]=False					):





										'''simple docstring'''







										_snake_case :			Union[str, Any]  						=   super()._prepare_for_class(lowerCamelCase_ ,				lowerCamelCase_ ,				return_labels=lowerCamelCase_					)

										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			:		Tuple					):





										'''simple docstring'''







										for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_snake_case :			Union[str, Any]  						=   TFDeiTModel.from_pretrained(lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)



def       A__(		):
				_snake_case :			List[Any]  						=   Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
				return image


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



						"""simple docstring"""

						@cached_property
						def 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







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







						@slow
						def 		__UpperCAmelCase       (    self			:		Optional[Any]					):





										'''simple docstring'''







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

										_snake_case :			Any  						=   self.default_image_processor
										_snake_case :			List[Any]  						=   prepare_img()
										_snake_case :			Dict  						=   image_processor(images=lowerCamelCase_ ,				return_tensors='tf'					)

										# forward pass
										_snake_case :			Any  						=   model(**lowerCamelCase_					)

										# verify the logits
										_snake_case :			Tuple  						=   tf.TensorShape((1, 10_00)					)
										self.assertEqual(outputs.logits.shape ,				lowerCamelCase_					)

										_snake_case :			Optional[int]  						=   tf.constant([-1.0266, 0.1912, -1.2861]					)

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













from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


lowercase_		:				Optional[Any]				=						{
    '''configuration_roc_bert''': ['''ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RoCBertConfig'''],
    '''tokenization_roc_bert''': ['''RoCBertTokenizer'''],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		pass

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_		:				Tuple				=						[
		    '''ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''RoCBertForCausalLM''',
		    '''RoCBertForMaskedLM''',
		    '''RoCBertForMultipleChoice''',
		    '''RoCBertForPreTraining''',
		    '''RoCBertForQuestionAnswering''',
		    '''RoCBertForSequenceClassification''',
		    '''RoCBertForTokenClassification''',
		    '''RoCBertLayer''',
		    '''RoCBertModel''',
		    '''RoCBertPreTrainedModel''',
		    '''load_tf_weights_in_roc_bert''',
		]

if TYPE_CHECKING:
		from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig
		from .tokenization_roc_bert import RoCBertTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				raise OptionalDependencyNotAvailable()

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_roc_bert import (
				    ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    RoCBertForCausalLM,
				    RoCBertForMaskedLM,
				    RoCBertForMultipleChoice,
				    RoCBertForPreTraining,
				    RoCBertForQuestionAnswering,
				    RoCBertForSequenceClassification,
				    RoCBertForTokenClassification,
				    RoCBertLayer,
				    RoCBertModel,
				    RoCBertPreTrainedModel,
				    load_tf_weights_in_roc_bert,
				)


else:
		import sys

		lowercase_		:				Tuple				=						_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






