
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			(   _A					):






			'''simple docstring'''

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

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_:							Any		=  ids_tensor([self.batch_size, self.seq_length]  ,		self.vocab_size )

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

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

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

							UpperCamelCase_:							Tuple		=  self.get_config()

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

			def  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							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  lowerCAmelCase__		(    self	:    List[Any] ):
							UpperCamelCase_:							List[str]		=  self.get_config()
							UpperCamelCase_:							Union[str, Any]		=  300
							return config

			def  lowerCAmelCase__		(    self	:    List[Any]  ,		snake_case_	:    str ):
							self.parent.assertListEqual(list(result.loss.size() )  ,		[] )

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

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

			def  lowerCAmelCase__		(    self	:    int  ,		snake_case_	:    str  ,		snake_case_	:    List[Any]  ,		snake_case_	:    Any  ,		snake_case_	:    Any  ,		snake_case_	:    List[Any]  ,		snake_case_	:    Any  ,		snake_case_	:    str ):
							UpperCamelCase_:							List[Any]		=  DebertaForMaskedLM(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							Any		=  model(snake_case_  ,		attention_mask=snake_case_  ,		token_type_ids=snake_case_  ,		labels=snake_case_ )

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

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

			def  lowerCAmelCase__		(    self	:    Tuple  ,		snake_case_	:    List[str]  ,		snake_case_	:    int  ,		snake_case_	:    Tuple  ,		snake_case_	:    List[str]  ,		snake_case_	:    Union[str, Any]  ,		snake_case_	:    Tuple  ,		snake_case_	:    List[str] ):
							UpperCamelCase_:							int		=  self.num_labels
							UpperCamelCase_:							List[str]		=  DebertaForTokenClassification(config=snake_case_ )
							model.to(snake_case_ )
							model.eval()
							UpperCamelCase_:							List[str]		=  model(snake_case_  ,		attention_mask=snake_case_  ,		token_type_ids=snake_case_  ,		labels=snake_case_ )
							self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.seq_length, self.num_labels) )

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




@require_torch
class     _UpperCamelCase			(   _A       ,    _A       ,    unittest.TestCase					):






			'''simple docstring'''

			__UpperCamelCase :  Union[str, Any]    =	(
			    (
			        DebertaModel,
			        DebertaForMaskedLM,
			        DebertaForSequenceClassification,
			        DebertaForTokenClassification,
			        DebertaForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			__UpperCamelCase :  Optional[int]    =	(
			    {
			        """feature-extraction""": DebertaModel,
			        """fill-mask""": DebertaForMaskedLM,
			        """question-answering""": DebertaForQuestionAnswering,
			        """text-classification""": DebertaForSequenceClassification,
			        """token-classification""": DebertaForTokenClassification,
			        """zero-shot""": DebertaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)

			__UpperCamelCase :  List[Any]    =	True
			__UpperCamelCase :  Union[str, Any]    =	False
			__UpperCamelCase :  List[Any]    =	False
			__UpperCamelCase :  List[Any]    =	False
			__UpperCamelCase :  Dict    =	False

			def  lowerCAmelCase__		(    self	:    int ):
							UpperCamelCase_:							Union[str, Any]		=  DebertaModelTester(self )
							UpperCamelCase_:							Tuple		=  ConfigTester(self  ,		config_class=snake_case_  ,		hidden_size=37 )

			def  lowerCAmelCase__		(    self	:    Dict ):
							self.config_tester.run_common_tests()

			def  lowerCAmelCase__		(    self	:    List[str] ):
							UpperCamelCase_:							Tuple		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_model(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    Union[str, Any] ):
							UpperCamelCase_:							Any		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    Optional[int] ):
							UpperCamelCase_:							Dict		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    Optional[Any] ):
							UpperCamelCase_:							Any		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_question_answering(*snake_case_ )

			def  lowerCAmelCase__		(    self	:    Dict ):
							UpperCamelCase_:							Union[str, Any]		=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_token_classification(*snake_case_ )




			@slow
			def  lowerCAmelCase__		(    self	:    Any ):
							for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											UpperCamelCase_:							Optional[Any]		=  DebertaModel.from_pretrained(snake_case_ )
											self.assertIsNotNone(snake_case_ )




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






			'''simple docstring'''

			@unittest.skip(reason="""Model not available yet""" )
			def  lowerCAmelCase__		(    self	:    List[str] ):
							pass




			@slow
			def  lowerCAmelCase__		(    self	:    List[Any] ):
							UpperCamelCase_:							Tuple		=  DebertaModel.from_pretrained("""microsoft/deberta-base""" )

							UpperCamelCase_:							int		=  torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]] )
							UpperCamelCase_:							str		=  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
							with torch.no_grad():
											UpperCamelCase_:							str		=  model(snake_case_  ,		attention_mask=snake_case_ )[0]
							# compare the actual values for a slice.
							UpperCamelCase_:							List[str]		=  torch.tensor(
							    [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]] )
							self.assertTrue(torch.allclose(output[:, 1:4, 1:4]  ,		snake_case_  ,		atol=1e-4 )  ,		f'''{output[:, 1:4, 1:4]}''' )








import json
import os
import unittest

from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin

class     _UpperCamelCase			(   _A       ,    unittest.TestCase					):






			'''simple docstring'''

			__UpperCamelCase :  Tuple    =	CTRLTokenizer
			__UpperCamelCase :  int    =	False
			__UpperCamelCase :  List[str]    =	False

			def  lowerCAmelCase__		(    self	:    List[str] ):
							super().setUp()

							# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
							UpperCamelCase_:							int		=  ["""adapt""", """re@@""", """a@@""", """apt""", """c@@""", """t""", """<unk>"""]
							UpperCamelCase_:							int		=  dict(zip(snake_case_  ,		range(len(snake_case_ ) ) ) )
							UpperCamelCase_:							Union[str, Any]		=  ["""#version: 0.2""", """a p""", """ap t</w>""", """r e""", """a d""", """ad apt</w>""", """"""]
							UpperCamelCase_:							Tuple		=  {"""unk_token""": """<unk>"""}

							UpperCamelCase_:							Optional[Any]		=  os.path.join(self.tmpdirname  ,		VOCAB_FILES_NAMES["""vocab_file"""] )
							UpperCamelCase_:							Dict		=  os.path.join(self.tmpdirname  ,		VOCAB_FILES_NAMES["""merges_file"""] )
							with open(self.vocab_file  ,		"""w"""  ,		encoding="""utf-8""" ) as fp:
											fp.write(json.dumps(snake_case_ ) + """\n""" )
							with open(self.merges_file  ,		"""w"""  ,		encoding="""utf-8""" ) as fp:
											fp.write("""\n""".join(snake_case_ ) )

			def  lowerCAmelCase__		(    self	:    Optional[int]  ,		**snake_case_	:    int ):
							kwargs.update(self.special_tokens_map )
							return CTRLTokenizer.from_pretrained(self.tmpdirname  ,		**snake_case_ )

			def  lowerCAmelCase__		(    self	:    Optional[int]  ,		snake_case_	:    List[str] ):
							UpperCamelCase_:							Dict		=  """adapt react readapt apt"""
							UpperCamelCase_:							List[str]		=  """adapt react readapt apt"""
							return input_text, output_text




			def  lowerCAmelCase__		(    self	:    List[str] ):
							UpperCamelCase_:							str		=  CTRLTokenizer(self.vocab_file  ,		self.merges_file  ,		**self.special_tokens_map )
							UpperCamelCase_:							List[Any]		=  """adapt react readapt apt"""
							UpperCamelCase_:							Optional[int]		=  """adapt re@@ a@@ c@@ t re@@ adapt apt""".split()
							UpperCamelCase_:							int		=  tokenizer.tokenize(snake_case_ )
							self.assertListEqual(snake_case_  ,		snake_case_ )

							UpperCamelCase_:							List[Any]		=  tokens + [tokenizer.unk_token]

							UpperCamelCase_:							Union[str, Any]		=  [0, 1, 2, 4, 5, 1, 0, 3, 6]
							self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case_ )  ,		snake_case_ )






