




'''simple docstring'''


import inspect
import unittest
import warnings
from typing import List

from transformers import EfficientFormerConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
					    MODEL_MAPPING,
					    EfficientFormerForImageClassification,
					    EfficientFormerForImageClassificationWithTeacher,
					    EfficientFormerModel,
					)
					from transformers.models.efficientformer.modeling_efficientformer import (
					    EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
					)


if is_vision_available():
					from PIL import Image

					from transformers import EfficientFormerImageProcessor






class 	lowerCAmelCase_  :







		'''simple docstring'''

		def __init__(   self							:       int				,     SCREAMING_SNAKE_CASE_							:       str				,     SCREAMING_SNAKE_CASE_							:       int = 13				,     SCREAMING_SNAKE_CASE_							:       int = 64				,     SCREAMING_SNAKE_CASE_							:       int = 2				,     SCREAMING_SNAKE_CASE_							:       int = 3				,     SCREAMING_SNAKE_CASE_							:       int = 3				,     SCREAMING_SNAKE_CASE_							:       bool = True				,     SCREAMING_SNAKE_CASE_							:       bool = True				,     SCREAMING_SNAKE_CASE_							:       int = 1_28				,     SCREAMING_SNAKE_CASE_							:       str=[16, 32, 64, 1_28]				,     SCREAMING_SNAKE_CASE_							:       int = 7				,     SCREAMING_SNAKE_CASE_							:       int = 4				,     SCREAMING_SNAKE_CASE_							:       int = 37				,     SCREAMING_SNAKE_CASE_							:       str = "gelu"				,     SCREAMING_SNAKE_CASE_							:       float = 0.1				,     SCREAMING_SNAKE_CASE_							:       float = 0.1				,     SCREAMING_SNAKE_CASE_							:       int = 10				,     SCREAMING_SNAKE_CASE_							:       float = 0.02				,     SCREAMING_SNAKE_CASE_							:       int = 2				,     SCREAMING_SNAKE_CASE_							:       int = 1				,     SCREAMING_SNAKE_CASE_							:       int = 1_28				,     SCREAMING_SNAKE_CASE_							:       List[int] = [2, 2, 2, 2]				,     SCREAMING_SNAKE_CASE_							:       int = 2				,     SCREAMING_SNAKE_CASE_							:       int = 2				,     ) ->		Union[str, Any]:







							'''simple docstring'''



							A:		Any        =		parent
							A:		Dict        =		batch_size
							A:		str        =		image_size
							A:		Optional[Any]        =		patch_size
							A:		List[str]        =		num_channels
							A:		str        =		is_training
							A:		List[str]        =		use_labels
							A:		Tuple        =		hidden_size
							A:		Optional[Any]        =		num_hidden_layers
							A:		Optional[int]        =		num_attention_heads
							A:		List[str]        =		intermediate_size
							A:		Union[str, Any]        =		hidden_act
							A:		Optional[int]        =		hidden_dropout_prob
							A:		Optional[int]        =		attention_probs_dropout_prob
							A:		Optional[int]        =		type_sequence_label_size
							A:		Union[str, Any]        =		initializer_range
							A:		Optional[int]        =		encoder_stride
							A:		List[Any]        =		num_attention_outputs
							A:		Dict        =		embed_dim
							A:		int        =		embed_dim + 1
							A:		Any        =		resolution
							A:		int        =		depths
							A:		str        =		hidden_sizes
							A:		Tuple        =		dim
							A:		Optional[int]        =		mlp_expansion_ratio



		def 				_snake_case    (   self							:       Dict  ) ->		Union[str, Any]:







							'''simple docstring'''



							A:		List[Any]        =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

							A:		Any        =		None
							if self.use_labels:
												A:		Dict        =		ids_tensor([self.batch_size]				,     self.type_sequence_label_size  )

							A:		int        =		self.get_config()
							return config, pixel_values, labels



		def 				_snake_case    (   self							:       List[Any]  ) ->		int:







							'''simple docstring'''



							return EfficientFormerConfig(
							    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=SCREAMING_SNAKE_CASE_				,     initializer_range=self.initializer_range				,     encoder_stride=self.encoder_stride				,     resolution=self.resolution				,     depths=self.depths				,     hidden_sizes=self.hidden_sizes				,     dim=self.dim				,     mlp_expansion_ratio=self.mlp_expansion_ratio				,     )



		def 				_snake_case    (   self							:       Optional[int]				,     SCREAMING_SNAKE_CASE_							:       Optional[int]				,     SCREAMING_SNAKE_CASE_							:       str				,     SCREAMING_SNAKE_CASE_							:       Any  ) ->		Any:







							'''simple docstring'''



							A:		List[str]        =		EfficientFormerModel(config=SCREAMING_SNAKE_CASE_  )
							model.to(SCREAMING_SNAKE_CASE_  )
							model.eval()
							A:		Any        =		model(SCREAMING_SNAKE_CASE_  )
							self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)  )



		def 				_snake_case    (   self							:       Union[str, Any]				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       List[str]				,     SCREAMING_SNAKE_CASE_							:       Optional[int]  ) ->		Optional[Any]:







							'''simple docstring'''



							A:		int        =		self.type_sequence_label_size
							A:		Optional[Any]        =		EfficientFormerForImageClassification(SCREAMING_SNAKE_CASE_  )
							model.to(SCREAMING_SNAKE_CASE_  )
							model.eval()
							A:		Dict        =		model(SCREAMING_SNAKE_CASE_				,     labels=SCREAMING_SNAKE_CASE_  )
							self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.type_sequence_label_size)  )

							# test greyscale images
							A:		str        =		1
							A:		Optional[Any]        =		EfficientFormerForImageClassification(SCREAMING_SNAKE_CASE_  )
							model.to(SCREAMING_SNAKE_CASE_  )
							model.eval()

							A:		Tuple        =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
							A:		Tuple        =		model(SCREAMING_SNAKE_CASE_  )
							self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.type_sequence_label_size)  )




		def 				_snake_case    (   self							:       List[Any]  ) ->		List[str]:







							'''simple docstring'''



							A:		Any        =		self.prepare_config_and_inputs()
							(
							    A  
							):		Union[str, Any]        =		config_and_inputs
							A:		Tuple        =		{"pixel_values": pixel_values}
							return config, inputs_dict






@require_torch
class 	lowerCAmelCase_  (					A__					,				A__					,				unittest.TestCase       ):







		'''simple docstring'''

		UpperCamelCase_ :			Optional[int]        =							(
		    (
		        EfficientFormerModel,
		        EfficientFormerForImageClassificationWithTeacher,
		        EfficientFormerForImageClassification,
		    )
		    if is_torch_available()
		    else ()
		)
		UpperCamelCase_ :			Optional[int]        =							(
		    {
		        '''feature-extraction''': EfficientFormerModel,
		        '''image-classification''': (
		            EfficientFormerForImageClassification,
		            EfficientFormerForImageClassificationWithTeacher,
		        ),
		    }
		    if is_torch_available()
		    else {}
		)
		UpperCamelCase_ :			int        =							False

		UpperCamelCase_ :			Union[str, Any]        =							False
		UpperCamelCase_ :			List[str]        =							False
		UpperCamelCase_ :			Optional[Any]        =							False



		def 				_snake_case    (   self							:       Any  ) ->		Optional[Any]:







							'''simple docstring'''



							A:		Any        =		EfficientFormerModelTester(self  )
							A:		int        =		ConfigTester(
							    self				,     config_class=SCREAMING_SNAKE_CASE_				,     has_text_modality=SCREAMING_SNAKE_CASE_				,     hidden_size=37  )



		def 				_snake_case    (   self							:       Dict  ) ->		Tuple:







							'''simple docstring'''



							self.config_tester.run_common_tests()



		@unittest.skip(reason='''EfficientFormer does not use inputs_embeds'''  )
		def 				_snake_case    (   self							:       Optional[Any]  ) ->		Tuple:







							'''simple docstring'''



							pass



		@unittest.skip(reason='''EfficientFormer does not support input and output embeddings'''  )
		def 				_snake_case    (   self							:       List[str]  ) ->		Tuple:







							'''simple docstring'''



							pass



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







							'''simple docstring'''



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

							for model_class in self.all_model_classes:
												A:		Optional[Any]        =		model_class(SCREAMING_SNAKE_CASE_  )
												A:		Union[str, Any]        =		inspect.signature(model.forward  )
												# signature.parameters is an OrderedDict => so arg_names order is deterministic
												A:		Optional[Any]        =		[*signature.parameters.keys()]

												A:		List[str]        =		["pixel_values"]
												self.assertListEqual(arg_names[:1]				,     SCREAMING_SNAKE_CASE_  )



		def 				_snake_case    (   self							:       int  ) ->		int:







							'''simple docstring'''



							def check_hidden_states_output(SCREAMING_SNAKE_CASE_							:       List[str]				,     SCREAMING_SNAKE_CASE_							:       int				,     SCREAMING_SNAKE_CASE_							:       Optional[int]  ):
												A:		List[Any]        =		model_class(SCREAMING_SNAKE_CASE_  )
												model.to(SCREAMING_SNAKE_CASE_  )
												model.eval()

												with torch.no_grad():
																	A:		Any        =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_  )  )

												A:		Tuple        =		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

												A:		List[str]        =		getattr(
												    self.model_tester				,     '''expected_num_hidden_layers'''				,     self.model_tester.num_hidden_layers + 1  )
												self.assertEqual(len(SCREAMING_SNAKE_CASE_  )				,     SCREAMING_SNAKE_CASE_  )

												if hasattr(self.model_tester				,     '''encoder_seq_length'''  ):
																	A:		Dict        =		self.model_tester.encoder_seq_length
																	if hasattr(self.model_tester				,     '''chunk_length'''  ) and self.model_tester.chunk_length > 1:
																						A:		Dict        =		seq_length * self.model_tester.chunk_length
												else:
																	A:		Dict        =		self.model_tester.seq_length

												self.assertListEqual(
												    list(hidden_states[-1].shape[-2:]  )				,     [seq_length, self.model_tester.hidden_size]				,     )

												if config.is_encoder_decoder:
																	A:		Dict        =		outputs.decoder_hidden_states

																	self.assertIsInstance(SCREAMING_SNAKE_CASE_				,     (list, tuple)  )
																	self.assertEqual(len(SCREAMING_SNAKE_CASE_  )				,     SCREAMING_SNAKE_CASE_  )
																	A:		Tuple        =		getattr(self.model_tester				,     '''seq_length'''				,     SCREAMING_SNAKE_CASE_  )
																	A:		Optional[int]        =		getattr(self.model_tester				,     '''decoder_seq_length'''				,     SCREAMING_SNAKE_CASE_  )

																	self.assertListEqual(
																	    list(hidden_states[-1].shape[-2:]  )				,     [decoder_seq_length, self.model_tester.hidden_size]				,     )

							A:		Union[str, Any]        =		self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
												A:		int        =		True
												check_hidden_states_output(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_  )

												# check that output_hidden_states also work using config
												del inputs_dict["output_hidden_states"]
												A:		Dict        =		True

												check_hidden_states_output(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_  )



		def 				_snake_case    (   self							:       Optional[int]				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       Union[str, Any]=False  ) ->		Union[str, Any]:







							'''simple docstring'''



							A:		str        =		super()._prepare_for_class(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_				,     return_labels=SCREAMING_SNAKE_CASE_  )

							if return_labels:
												if model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher":
																	del inputs_dict["labels"]

							return inputs_dict



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







							'''simple docstring'''



							A:		Dict        =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_  )



		@unittest.skip(reason='''EfficientFormer does not implement masked image modeling yet'''  )
		def 				_snake_case    (   self							:       List[str]  ) ->		Optional[int]:







							'''simple docstring'''



							A:		str        =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE_  )



		def 				_snake_case    (   self							:       Any  ) ->		Optional[int]:







							'''simple docstring'''



							A:		List[Any]        =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_  )



		def 				_snake_case    (   self							:       List[Any]  ) ->		List[str]:







							'''simple docstring'''



							if not self.model_tester.is_training:
												return

							A:		str        =		self.model_tester.prepare_config_and_inputs_for_common()
							A:		Dict        =		True

							for model_class in self.all_model_classes:
												# EfficientFormerForImageClassificationWithTeacher supports inference-only
												if (
												    model_class in get_values(SCREAMING_SNAKE_CASE_  )
												    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
												):
																	continue
												A:		int        =		model_class(SCREAMING_SNAKE_CASE_  )
												model.to(SCREAMING_SNAKE_CASE_  )
												model.train()
												A:		int        =		self._prepare_for_class(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_				,     return_labels=SCREAMING_SNAKE_CASE_  )
												A:		Tuple        =		model(**SCREAMING_SNAKE_CASE_  ).loss
												loss.backward()



		def 				_snake_case    (   self							:       Dict  ) ->		List[Any]:







							'''simple docstring'''



							A:		List[str]        =		self.model_tester.prepare_config_and_inputs_for_common()

							A:		int        =		[
							    {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float},
							    {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long},
							    {"title": "regression", "num_labels": 1, "dtype": torch.float},
							]

							for model_class in self.all_model_classes:
												if (
												    model_class
												    not in [
												        *get_values(SCREAMING_SNAKE_CASE_  ),
												    ]
												    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
												):
																	continue

												for problem_type in problem_types:
																	with self.subTest(msg=f"""Testing {model_class} with {problem_type['title']}"""  ):
																						A:		Any        =		problem_type["title"]
																						A:		List[str]        =		problem_type["num_labels"]

																						A:		Union[str, Any]        =		model_class(SCREAMING_SNAKE_CASE_  )
																						model.to(SCREAMING_SNAKE_CASE_  )
																						model.train()

																						A:		Any        =		self._prepare_for_class(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_				,     return_labels=SCREAMING_SNAKE_CASE_  )

																						if problem_type["num_labels"] > 1:
																											A:		Any        =		inputs["labels"].unsqueeze(1  ).repeat(1				,     problem_type['''num_labels''']  )

																						A:		Optional[Any]        =		inputs["labels"].to(problem_type['''dtype''']  )

																						# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																						# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																						# they have the same size." which is a symptom something in wrong for the regression problem.
																						# See https://github.com/huggingface/transformers/issues/11780
																						with warnings.catch_warnings(record=SCREAMING_SNAKE_CASE_  ) as warning_list:
																											A:		Dict        =		model(**SCREAMING_SNAKE_CASE_  ).loss
																						for w in warning_list:
																											if "Using a target size that is different to the input size" in str(w.message  ):
																																raise ValueError(
																																    f"""Something is going wrong in the regression problem: intercepted {w.message}"""  )

																						loss.backward()



		@slow
		def 				_snake_case    (   self							:       str  ) ->		Union[str, Any]:







							'''simple docstring'''



							for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												A:		List[Any]        =		EfficientFormerModel.from_pretrained(SCREAMING_SNAKE_CASE_  )
												self.assertIsNotNone(SCREAMING_SNAKE_CASE_  )




		def 				_snake_case    (   self							:       Union[str, Any]  ) ->		Dict:







							'''simple docstring'''



							A:		Optional[Any]        =		self.model_tester.prepare_config_and_inputs_for_common()
							A:		Dict        =		True

							A:		Tuple        =		getattr(self.model_tester				,     '''seq_length'''				,     SCREAMING_SNAKE_CASE_  )
							A:		Tuple        =		getattr(self.model_tester				,     '''encoder_seq_length'''				,     SCREAMING_SNAKE_CASE_  )
							A:		Optional[int]        =		getattr(self.model_tester				,     '''key_length'''				,     SCREAMING_SNAKE_CASE_  )
							A:		Union[str, Any]        =		getattr(self.model_tester				,     '''chunk_length'''				,     SCREAMING_SNAKE_CASE_  )

							if chunk_length is not None and hasattr(self.model_tester				,     '''num_hashes'''  ):
												A:		List[Any]        =		encoder_seq_length * self.model_tester.num_hashes

							for model_class in self.all_model_classes:
												A:		List[Any]        =		True
												A:		List[Any]        =		False
												A:		int        =		True
												A:		List[str]        =		model_class(SCREAMING_SNAKE_CASE_  )
												model.to(SCREAMING_SNAKE_CASE_  )
												model.eval()
												with torch.no_grad():
																	A:		Any        =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_  )  )
												A:		Optional[Any]        =		outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
												self.assertEqual(len(SCREAMING_SNAKE_CASE_  )				,     self.model_tester.num_attention_outputs  )

												# check that output_attentions also work using config
												del inputs_dict["output_attentions"]
												A:		List[str]        =		True
												A:		str        =		model_class(SCREAMING_SNAKE_CASE_  )
												model.to(SCREAMING_SNAKE_CASE_  )
												model.eval()
												with torch.no_grad():
																	A:		Any        =		model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_  )  )
												A:		Optional[int]        =		outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
												self.assertEqual(len(SCREAMING_SNAKE_CASE_  )				,     self.model_tester.num_attention_outputs  )

												if chunk_length is not None:
																	self.assertListEqual(
																	    list(attentions[0].shape[-4:]  )				,     [self.model_tester.num_attention_heads, encoder_seq_length, chunk_length, encoder_key_length]				,     )
												else:
																	self.assertListEqual(
																	    list(attentions[0].shape[-3:]  )				,     [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]				,     )






def 				SCREAMING_SNAKE_CASE(      ) ->   List[Any]:
					A:		str        =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
					return image






@require_torch
@require_vision
class 	lowerCAmelCase_  (					unittest.TestCase       ):







		'''simple docstring'''

		@cached_property
		def 				_snake_case    (   self							:       Any  ) ->		Optional[int]:







							'''simple docstring'''



							return (
							    EfficientFormerImageProcessor.from_pretrained('''snap-research/efficientformer-l1-300'''  )
							    if is_vision_available()
							    else None
							)



		@slow
		def 				_snake_case    (   self							:       Any  ) ->		Optional[int]:







							'''simple docstring'''



							A:		Tuple        =		EfficientFormerForImageClassification.from_pretrained('''snap-research/efficientformer-l1-300'''  ).to(
							    SCREAMING_SNAKE_CASE_  )

							A:		Any        =		self.default_image_processor
							A:		Optional[int]        =		prepare_img()
							A:		Any        =		image_processor(images=SCREAMING_SNAKE_CASE_				,     return_tensors='''pt'''  ).to(SCREAMING_SNAKE_CASE_  )

							# forward pass
							with torch.no_grad():
												A:		Any        =		model(**SCREAMING_SNAKE_CASE_  )

							# verify the logits
							A:		List[str]        =		(1, 10_00)
							self.assertEqual(outputs.logits.shape				,     SCREAMING_SNAKE_CASE_  )

							A:		List[str]        =		torch.tensor([-0.0555, 0.4825, -0.0852]  ).to(SCREAMING_SNAKE_CASE_  )
							self.assertTrue(torch.allclose(outputs.logits[0][:3]				,     SCREAMING_SNAKE_CASE_				,     atol=1E-4  )  )




		@slow
		def 				_snake_case    (   self							:       List[Any]  ) ->		Dict:







							'''simple docstring'''



							A:		Any        =		EfficientFormerForImageClassificationWithTeacher.from_pretrained(
							    '''snap-research/efficientformer-l1-300'''  ).to(SCREAMING_SNAKE_CASE_  )

							A:		str        =		self.default_image_processor
							A:		List[Any]        =		prepare_img()
							A:		Union[str, Any]        =		image_processor(images=SCREAMING_SNAKE_CASE_				,     return_tensors='''pt'''  ).to(SCREAMING_SNAKE_CASE_  )

							# forward pass
							with torch.no_grad():
												A:		int        =		model(**SCREAMING_SNAKE_CASE_  )

							# verify the logits
							A:		Dict        =		(1, 10_00)
							self.assertEqual(outputs.logits.shape				,     SCREAMING_SNAKE_CASE_  )

							A:		Any        =		torch.tensor([-0.1312, 0.4353, -1.0499]  ).to(SCREAMING_SNAKE_CASE_  )
							self.assertTrue(torch.allclose(outputs.logits[0][:3]				,     SCREAMING_SNAKE_CASE_				,     atol=1E-4  )  )












'''simple docstring'''


from __future__ import annotations

import unittest

from transformers import DebertaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TFDebertaForMaskedLM,
         TFDebertaForQuestionAnswering,
         TFDebertaForSequenceClassification,
         TFDebertaForTokenClassification,
         TFDebertaModel,
     )






class 	lowerCAmelCase_  :







  '''simple docstring'''

  def __init__(   self							:       Optional[int]				,     SCREAMING_SNAKE_CASE_							:       Any				,     SCREAMING_SNAKE_CASE_							:       Optional[int]=13				,     SCREAMING_SNAKE_CASE_							:       List[Any]=7				,     SCREAMING_SNAKE_CASE_							:       List[str]=True				,     SCREAMING_SNAKE_CASE_							:       Dict=True				,     SCREAMING_SNAKE_CASE_							:       Any=True				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]=True				,     SCREAMING_SNAKE_CASE_							:       str=99				,     SCREAMING_SNAKE_CASE_							:       Union[str, Any]=32				,     SCREAMING_SNAKE_CASE_							:       List[Any]=2				,     SCREAMING_SNAKE_CASE_							:       List[Any]=4				,     SCREAMING_SNAKE_CASE_							:       Tuple=37				,     SCREAMING_SNAKE_CASE_							:       Tuple="gelu"				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]=0.1				,     SCREAMING_SNAKE_CASE_							:       Optional[int]=0.1				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]=5_12				,     SCREAMING_SNAKE_CASE_							:       str=16				,     SCREAMING_SNAKE_CASE_							:       str=2				,     SCREAMING_SNAKE_CASE_							:       List[Any]=0.02				,     SCREAMING_SNAKE_CASE_							:       List[str]=3				,     SCREAMING_SNAKE_CASE_							:       List[Any]=4				,     SCREAMING_SNAKE_CASE_							:       Optional[int]=None				,     ) ->		List[str]:







       '''simple docstring'''



       A:		List[Any]        =		parent
       A:		int        =		13
       A:		Union[str, Any]        =		7
       A:		int        =		True
       A:		str        =		True
       A:		Any        =		True
       A:		Optional[Any]        =		True
       A:		Optional[Any]        =		99
       A:		List[Any]        =		32
       A:		Optional[Any]        =		2
       A:		int        =		4
       A:		Union[str, Any]        =		37
       A:		Union[str, Any]        =		'''gelu'''
       A:		Optional[int]        =		0.1
       A:		str        =		0.1
       A:		Union[str, Any]        =		5_12
       A:		int        =		16
       A:		Optional[int]        =		False
       A:		int        =		-1
       A:		List[Any]        =		True
       A:		str        =		2
       A:		Any        =		0.02
       A:		Dict        =		3
       A:		List[Any]        =		4
       A:		Union[str, Any]        =		None



  def 				_snake_case    (   self							:       Dict  ) ->		List[Any]:







       '''simple docstring'''



       A:		str        =		ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size  )

       A:		Any        =		None
       if self.use_input_mask:
            A:		List[Any]        =		random_attention_mask([self.batch_size, self.seq_length]  )

       A:		List[Any]        =		None
       if self.use_token_type_ids:
            A:		Optional[int]        =		ids_tensor([self.batch_size, self.seq_length]				,     self.type_vocab_size  )

       A:		Dict        =		None
       A:		Any        =		None
       A:		Tuple        =		None
       if self.use_labels:
            A:		Optional[Any]        =		ids_tensor([self.batch_size]				,     self.type_sequence_label_size  )
            A:		Any        =		ids_tensor([self.batch_size, self.seq_length]				,     self.num_labels  )

       A:		Tuple        =		DebertaConfig(
           vocab_size=self.vocab_size				,     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				,     max_position_embeddings=self.max_position_embeddings				,     type_vocab_size=self.type_vocab_size				,     relative_attention=self.relative_attention				,     max_relative_positions=self.max_relative_positions				,     position_biased_input=self.position_biased_input				,     initializer_range=self.initializer_range				,     return_dict=SCREAMING_SNAKE_CASE_				,     )

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



  def 				_snake_case    (   self							:       Dict				,     SCREAMING_SNAKE_CASE_							:       str				,     SCREAMING_SNAKE_CASE_							:       Dict				,     SCREAMING_SNAKE_CASE_							:       List[str]				,     SCREAMING_SNAKE_CASE_							:       Tuple				,     SCREAMING_SNAKE_CASE_							:       List[Any]				,     SCREAMING_SNAKE_CASE_							:       Union[str, Any]				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]  ) ->		str:







       '''simple docstring'''



       A:		Optional[Any]        =		TFDebertaModel(config=SCREAMING_SNAKE_CASE_  )
       A:		List[Any]        =		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

       A:		Union[str, Any]        =		[input_ids, input_mask]
       A:		List[Any]        =		model(SCREAMING_SNAKE_CASE_  )

       A:		List[str]        =		model(SCREAMING_SNAKE_CASE_  )

       self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)  )



  def 				_snake_case    (   self							:       Dict				,     SCREAMING_SNAKE_CASE_							:       str				,     SCREAMING_SNAKE_CASE_							:       List[Any]				,     SCREAMING_SNAKE_CASE_							:       Tuple				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       Union[str, Any]				,     SCREAMING_SNAKE_CASE_							:       str				,     SCREAMING_SNAKE_CASE_							:       Union[str, Any]  ) ->		Dict:







       '''simple docstring'''



       A:		List[Any]        =		TFDebertaForMaskedLM(config=SCREAMING_SNAKE_CASE_  )
       A:		Optional[Any]        =		{
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }
       A:		Union[str, Any]        =		model(SCREAMING_SNAKE_CASE_  )
       self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.vocab_size)  )



  def 				_snake_case    (   self							:       List[str]				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       str				,     SCREAMING_SNAKE_CASE_							:       int				,     SCREAMING_SNAKE_CASE_							:       Any				,     SCREAMING_SNAKE_CASE_							:       str				,     SCREAMING_SNAKE_CASE_							:       List[Any]				,     SCREAMING_SNAKE_CASE_							:       Union[str, Any]  ) ->		Optional[int]:







       '''simple docstring'''



       A:		Union[str, Any]        =		self.num_labels
       A:		Optional[Any]        =		TFDebertaForSequenceClassification(config=SCREAMING_SNAKE_CASE_  )
       A:		str        =		{
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }

       A:		Union[str, Any]        =		model(SCREAMING_SNAKE_CASE_  )
       self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels)  )



  def 				_snake_case    (   self							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       int				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       Dict				,     SCREAMING_SNAKE_CASE_							:       Tuple				,     SCREAMING_SNAKE_CASE_							:       str				,     SCREAMING_SNAKE_CASE_							:       Union[str, Any]  ) ->		Union[str, Any]:







       '''simple docstring'''



       A:		Tuple        =		self.num_labels
       A:		Union[str, Any]        =		TFDebertaForTokenClassification(config=SCREAMING_SNAKE_CASE_  )
       A:		List[Any]        =		{
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }
       A:		List[str]        =		model(SCREAMING_SNAKE_CASE_  )
       self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels)  )



  def 				_snake_case    (   self							:       Dict				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       int				,     SCREAMING_SNAKE_CASE_							:       Union[str, Any]				,     SCREAMING_SNAKE_CASE_							:       Optional[Any]				,     SCREAMING_SNAKE_CASE_							:       Tuple				,     SCREAMING_SNAKE_CASE_							:       Dict  ) ->		int:







       '''simple docstring'''



       A:		Any        =		TFDebertaForQuestionAnswering(config=SCREAMING_SNAKE_CASE_  )
       A:		Tuple        =		{
           '''input_ids''': input_ids,
           '''attention_mask''': input_mask,
           '''token_type_ids''': token_type_ids,
       }

       A:		Dict        =		model(SCREAMING_SNAKE_CASE_  )
       self.parent.assertEqual(result.start_logits.shape				,     (self.batch_size, self.seq_length)  )
       self.parent.assertEqual(result.end_logits.shape				,     (self.batch_size, self.seq_length)  )




  def 				_snake_case    (   self							:       Dict  ) ->		Dict:







       '''simple docstring'''



       A:		List[Any]        =		self.prepare_config_and_inputs()
       (
           (
           A
       )    ,						(
           A
       )    ,						(
           A
       )    ,						(
           A
       )    ,						(
           A
       )    ,						(
           A
       )    ,						(
           A
       )    ,						
       ):		List[str]        =		config_and_inputs
       A:		Dict        =		{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict






@require_tf
class 	lowerCAmelCase_  (					UpperCAmelCase_					,				UpperCAmelCase_					,				unittest.TestCase       ):







  '''simple docstring'''

  UpperCamelCase_ :			List[Any]        =							(
      (
          TFDebertaModel,
          TFDebertaForMaskedLM,
          TFDebertaForQuestionAnswering,
          TFDebertaForSequenceClassification,
          TFDebertaForTokenClassification,
      )
      if is_tf_available()
      else ()
  )
  UpperCamelCase_ :			Dict        =							(
      {
          """feature-extraction""": TFDebertaModel,
          """fill-mask""": TFDebertaForMaskedLM,
          """question-answering""": TFDebertaForQuestionAnswering,
          """text-classification""": TFDebertaForSequenceClassification,
          """token-classification""": TFDebertaForTokenClassification,
          """zero-shot""": TFDebertaForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )

  UpperCamelCase_ :			List[Any]        =							False
  UpperCamelCase_ :			Optional[Any]        =							False



  def 				_snake_case    (   self							:       Optional[int]  ) ->		int:







       '''simple docstring'''



       A:		str        =		TFDebertaModelTester(self  )
       A:		int        =		ConfigTester(self				,     config_class=SCREAMING_SNAKE_CASE_				,     hidden_size=37  )



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







       '''simple docstring'''



       self.config_tester.run_common_tests()



  def 				_snake_case    (   self							:       int  ) ->		Tuple:







       '''simple docstring'''



       A:		Optional[Any]        =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_  )



  def 				_snake_case    (   self							:       Any  ) ->		Optional[Any]:







       '''simple docstring'''



       A:		Union[str, Any]        =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_  )



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







       '''simple docstring'''



       A:		Dict        =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE_  )



  def 				_snake_case    (   self							:       Any  ) ->		Optional[Any]:







       '''simple docstring'''



       A:		Any        =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE_  )



  def 				_snake_case    (   self							:       List[str]  ) ->		List[str]:







       '''simple docstring'''



       A:		Tuple        =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE_  )




  @slow
  def 				_snake_case    (   self							:       Tuple  ) ->		Optional[int]:







       '''simple docstring'''



       A:		Optional[int]        =		TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''  )
       self.assertIsNotNone(SCREAMING_SNAKE_CASE_  )






@require_tf
class 	lowerCAmelCase_  (					unittest.TestCase       ):







  '''simple docstring'''

  @unittest.skip(reason='''Model not available yet'''  )
  def 				_snake_case    (   self							:       List[Any]  ) ->		Union[str, Any]:







       '''simple docstring'''



       pass




  @slow
  def 				_snake_case    (   self							:       Any  ) ->		Any:







       '''simple docstring'''



       A:		Any        =		TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''  )
       A:		Dict        =		tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]  )
       A:		List[Any]        =		tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
       A:		Optional[int]        =		model(SCREAMING_SNAKE_CASE_				,     attention_mask=SCREAMING_SNAKE_CASE_  )[0]

       A:		List[str]        =		tf.constant(
           [
               [
                   [-0.5985_5896, -0.8055_2566, -0.846_2135],
                   [1.448_4025, -0.9348_3794, -0.8059_3085],
                   [0.312_2741, 0.0031_6059, -1.413_1377],
               ]
           ]  )
       tf.debugging.assert_near(output[:, 1:4, 1:4]				,     SCREAMING_SNAKE_CASE_				,     atol=1E-4  )






