





'''simple docstring'''


from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
    if not (is_transformers_available() and is_torch_available() and is_transformers_version('>=', '4.25.0')):
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
    from .pipeline_unclip import UnCLIPPipeline
    from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
    from .text_proj import UnCLIPTextProjModel









import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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


if is_torch_available():
		import torch

		from transformers import (
		    DebertaForMaskedLM,
		    DebertaForQuestionAnswering,
		    DebertaForSequenceClassification,
		    DebertaForTokenClassification,
		    DebertaModel,
		)
		from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST






class 						UpperCamelCase_  (  snake_case__							):



					def __init__(		self     :     str       ,      lowerCamelCase     :     Optional[int]       ,      lowerCamelCase     :     str=13       ,      lowerCamelCase     :     Optional[Any]=7       ,      lowerCamelCase     :     List[Any]=True       ,      lowerCamelCase     :     Dict=True       ,      lowerCamelCase     :     List[Any]=True       ,      lowerCamelCase     :     Any=True       ,      lowerCamelCase     :     Optional[Any]=99       ,      lowerCamelCase     :     Union[str, Any]=32       ,      lowerCamelCase     :     Any=5       ,      lowerCamelCase     :     Tuple=4       ,      lowerCamelCase     :     Optional[int]=37       ,      lowerCamelCase     :     Optional[Any]="gelu"       ,      lowerCamelCase     :     Optional[Any]=0.1       ,      lowerCamelCase     :     str=0.1       ,      lowerCamelCase     :     Tuple=5_12       ,      lowerCamelCase     :     Union[str, Any]=16       ,      lowerCamelCase     :     Union[str, Any]=2       ,      lowerCamelCase     :     Dict=0.02       ,      lowerCamelCase     :     Any=False       ,      lowerCamelCase     :     int=True       ,      lowerCamelCase     :     Optional[Any]="None"       ,      lowerCamelCase     :     Optional[int]=3       ,      lowerCamelCase     :     List[Any]=4       ,      lowerCamelCase     :     int=None       ,      ):
							lowerCamelCase_	:     int 						=      parent
							lowerCamelCase_	:     List[Any] 						=      batch_size
							lowerCamelCase_	:     Dict 						=      seq_length
							lowerCamelCase_	:     Union[str, Any] 						=      is_training
							lowerCamelCase_	:     str 						=      use_input_mask
							lowerCamelCase_	:     int 						=      use_token_type_ids
							lowerCamelCase_	:     Tuple 						=      use_labels
							lowerCamelCase_	:     str 						=      vocab_size
							lowerCamelCase_	:     int 						=      hidden_size
							lowerCamelCase_	:     Optional[int] 						=      num_hidden_layers
							lowerCamelCase_	:     Optional[Any] 						=      num_attention_heads
							lowerCamelCase_	:     Any 						=      intermediate_size
							lowerCamelCase_	:     List[Any] 						=      hidden_act
							lowerCamelCase_	:     List[Any] 						=      hidden_dropout_prob
							lowerCamelCase_	:     List[Any] 						=      attention_probs_dropout_prob
							lowerCamelCase_	:     List[str] 						=      max_position_embeddings
							lowerCamelCase_	:     Tuple 						=      type_vocab_size
							lowerCamelCase_	:     str 						=      type_sequence_label_size
							lowerCamelCase_	:     Optional[int] 						=      initializer_range
							lowerCamelCase_	:     List[Any] 						=      num_labels
							lowerCamelCase_	:     str 						=      num_choices
							lowerCamelCase_	:     Dict 						=      relative_attention
							lowerCamelCase_	:     Optional[int] 						=      position_biased_input
							lowerCamelCase_	:     List[Any] 						=      pos_att_type
							lowerCamelCase_	:     Optional[int] 						=      scope



					def   __a     (		self     :     List[Any]						):
							lowerCamelCase_	:     Tuple 						=      ids_tensor([self.batch_size, self.seq_length]       ,      self.vocab_size						)

							lowerCamelCase_	:     Dict 						=      None
							if self.use_input_mask:
									lowerCamelCase_	:     Optional[int] 						=      ids_tensor([self.batch_size, self.seq_length]       ,      vocab_size=2						)

							lowerCamelCase_	:     str 						=      None
							if self.use_token_type_ids:
									lowerCamelCase_	:     Dict 						=      ids_tensor([self.batch_size, self.seq_length]       ,      self.type_vocab_size						)

							lowerCamelCase_	:     Union[str, Any] 						=      None
							lowerCamelCase_	:     List[Any] 						=      None
							lowerCamelCase_	:     str 						=      None
							if self.use_labels:
									lowerCamelCase_	:     Any 						=      ids_tensor([self.batch_size]       ,      self.type_sequence_label_size						)
									lowerCamelCase_	:     Optional[Any] 						=      ids_tensor([self.batch_size, self.seq_length]       ,      self.num_labels						)
									lowerCamelCase_	:     Any 						=      ids_tensor([self.batch_size]       ,      self.num_choices						)

							lowerCamelCase_	:     Optional[Any] 						=      self.get_config()

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



					def   __a     (		self     :     Tuple						):
							return 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       ,      initializer_range=self.initializer_range       ,      relative_attention=self.relative_attention       ,      position_biased_input=self.position_biased_input       ,      pos_att_type=self.pos_att_type       ,      )



					def   __a     (		self     :     int						):
							lowerCamelCase_	:     int 						=      self.get_config()
							lowerCamelCase_	:     Any 						=      3_00
							return config



					def   __a     (		self     :     Optional[int]       ,      lowerCamelCase     :     Dict						):
							self.parent.assertListEqual(list(result.loss.size()						)       ,      []						)



					def   __a     (		self     :     Dict       ,      lowerCamelCase     :     Tuple       ,      lowerCamelCase     :     List[Any]       ,      lowerCamelCase     :     Tuple       ,      lowerCamelCase     :     Any       ,      lowerCamelCase     :     Tuple       ,      lowerCamelCase     :     str       ,      lowerCamelCase     :     List[str]						):
							lowerCamelCase_	:     str 						=      DebertaModel(config=lowerCamelCase						)
							model.to(lowerCamelCase						)
							model.eval()
							lowerCamelCase_	:     Optional[int] 						=      model(lowerCamelCase       ,      attention_mask=lowerCamelCase       ,      token_type_ids=lowerCamelCase						)[0]
							lowerCamelCase_	:     Union[str, Any] 						=      model(lowerCamelCase       ,      token_type_ids=lowerCamelCase						)[0]
							lowerCamelCase_	:     List[Any] 						=      model(lowerCamelCase						)[0]

							self.parent.assertListEqual(list(sequence_output.size()						)       ,      [self.batch_size, self.seq_length, self.hidden_size]						)



					def   __a     (		self     :     Optional[Any]       ,      lowerCamelCase     :     Tuple       ,      lowerCamelCase     :     str       ,      lowerCamelCase     :     str       ,      lowerCamelCase     :     int       ,      lowerCamelCase     :     int       ,      lowerCamelCase     :     str       ,      lowerCamelCase     :     Any						):
							lowerCamelCase_	:     Any 						=      DebertaForMaskedLM(config=lowerCamelCase						)
							model.to(lowerCamelCase						)
							model.eval()
							lowerCamelCase_	:     Any 						=      model(lowerCamelCase       ,      attention_mask=lowerCamelCase       ,      token_type_ids=lowerCamelCase       ,      labels=lowerCamelCase						)

							self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.seq_length, self.vocab_size)						)



					def   __a     (		self     :     Tuple       ,      lowerCamelCase     :     Any       ,      lowerCamelCase     :     Any       ,      lowerCamelCase     :     int       ,      lowerCamelCase     :     str       ,      lowerCamelCase     :     Optional[int]       ,      lowerCamelCase     :     Optional[int]       ,      lowerCamelCase     :     Tuple						):
							lowerCamelCase_	:     List[str] 						=      self.num_labels
							lowerCamelCase_	:     str 						=      DebertaForSequenceClassification(lowerCamelCase						)
							model.to(lowerCamelCase						)
							model.eval()
							lowerCamelCase_	:     List[str] 						=      model(lowerCamelCase       ,      attention_mask=lowerCamelCase       ,      token_type_ids=lowerCamelCase       ,      labels=lowerCamelCase						)
							self.parent.assertListEqual(list(result.logits.size()						)       ,      [self.batch_size, self.num_labels]						)
							self.check_loss_output(lowerCamelCase						)



					def   __a     (		self     :     List[str]       ,      lowerCamelCase     :     Dict       ,      lowerCamelCase     :     Tuple       ,      lowerCamelCase     :     str       ,      lowerCamelCase     :     Optional[int]       ,      lowerCamelCase     :     Tuple       ,      lowerCamelCase     :     List[str]       ,      lowerCamelCase     :     Optional[int]						):
							lowerCamelCase_	:     List[Any] 						=      self.num_labels
							lowerCamelCase_	:     str 						=      DebertaForTokenClassification(config=lowerCamelCase						)
							model.to(lowerCamelCase						)
							model.eval()
							lowerCamelCase_	:     List[str] 						=      model(lowerCamelCase       ,      attention_mask=lowerCamelCase       ,      token_type_ids=lowerCamelCase       ,      labels=lowerCamelCase						)
							self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.seq_length, self.num_labels)						)



					def   __a     (		self     :     List[str]       ,      lowerCamelCase     :     Union[str, Any]       ,      lowerCamelCase     :     List[Any]       ,      lowerCamelCase     :     int       ,      lowerCamelCase     :     Union[str, Any]       ,      lowerCamelCase     :     List[Any]       ,      lowerCamelCase     :     List[str]       ,      lowerCamelCase     :     str						):
							lowerCamelCase_	:     int 						=      DebertaForQuestionAnswering(config=lowerCamelCase						)
							model.to(lowerCamelCase						)
							model.eval()
							lowerCamelCase_	:     List[Any] 						=      model(
							    lowerCamelCase       ,      attention_mask=lowerCamelCase       ,      token_type_ids=lowerCamelCase       ,      start_positions=lowerCamelCase       ,      end_positions=lowerCamelCase       ,      )
							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   __a     (		self     :     int						):
							lowerCamelCase_	:     Any 						=      self.prepare_config_and_inputs()
							(
							    (
							    lowerCamelCase_
							)						,      (
							    lowerCamelCase_
							)						,      (
							    lowerCamelCase_
							)						,      (
							    lowerCamelCase_
							)						,      (
							    lowerCamelCase_
							)						,      (
							    lowerCamelCase_
							)						,      (
							    lowerCamelCase_
							)						,      
							)	:     Dict 						=      config_and_inputs
							lowerCamelCase_	:     str 						=      {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
							return config, inputs_dict






@require_torch
class 						UpperCamelCase_  (  snake_case__  ,     snake_case__  ,     unittest.TestCase							):
					_a      :						str						    =					(
					    (
					        DebertaModel,
					        DebertaForMaskedLM,
					        DebertaForSequenceClassification,
					        DebertaForTokenClassification,
					        DebertaForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					_a      :						Any						    =					(
					    {
					        'feature-extraction': DebertaModel,
					        'fill-mask': DebertaForMaskedLM,
					        'question-answering': DebertaForQuestionAnswering,
					        'text-classification': DebertaForSequenceClassification,
					        'token-classification': DebertaForTokenClassification,
					        'zero-shot': DebertaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					_a      :						Optional[int]						    =					True
					_a      :						List[str]						    =					False
					_a      :						List[str]						    =					False
					_a      :						List[str]						    =					False
					_a      :						List[Any]						    =					False



					def   __a     (		self     :     List[str]						):
							lowerCamelCase_	:     List[Any] 						=      DebertaModelTester(self						)
							lowerCamelCase_	:     Any 						=      ConfigTester(self       ,      config_class=lowerCamelCase       ,      hidden_size=37						)



					def   __a     (		self     :     Any						):
							self.config_tester.run_common_tests()



					def   __a     (		self     :     List[str]						):
							lowerCamelCase_	:     int 						=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_model(*lowerCamelCase						)



					def   __a     (		self     :     int						):
							lowerCamelCase_	:     str 						=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCamelCase						)



					def   __a     (		self     :     Optional[int]						):
							lowerCamelCase_	:     Optional[Any] 						=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCamelCase						)



					def   __a     (		self     :     str						):
							lowerCamelCase_	:     List[Any] 						=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_question_answering(*lowerCamelCase						)



					def   __a     (		self     :     Union[str, Any]						):
							lowerCamelCase_	:     List[Any] 						=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_token_classification(*lowerCamelCase						)





					@slow
					def   __a     (		self     :     Optional[int]						):
							for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase_	:     Optional[int] 						=      DebertaModel.from_pretrained(lowerCamelCase						)
									self.assertIsNotNone(lowerCamelCase						)






@require_torch
@require_sentencepiece
@require_tokenizers
class 						UpperCamelCase_  (  unittest.TestCase							):



					@unittest.skip(reason='Model not available yet'						)
					def   __a     (		self     :     str						):
							pass





					@slow
					def   __a     (		self     :     Dict						):
							lowerCamelCase_	:     List[str] 						=      DebertaModel.from_pretrained('microsoft/deberta-base'						)

							lowerCamelCase_	:     Optional[Any] 						=      torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]						)
							lowerCamelCase_	:     List[str] 						=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
							with torch.no_grad():
									lowerCamelCase_	:     Dict 						=      model(lowerCamelCase       ,      attention_mask=lowerCamelCase						)[0]
							# compare the actual values for a slice.
							lowerCamelCase_	:     str 						=      torch.tensor(
							    [[[-0.5_986, -0.8_055, -0.8_462], [1.4_484, -0.9_348, -0.8_059], [0.3_123, 0.0_032, -1.4_131]]]						)
							self.assertTrue(torch.allclose(output[:, 1:4, 1:4]       ,      lowerCamelCase       ,      atol=1E-4						)       ,      F"{output[:, 1:4, 1:4]}"						)
