

'''simple docstring'''


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 					lowercase						(  a_				):


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


							def 						_snake_case							(			self)							->				Union[str, Any]:
								UpperCAmelCase_		:      List[str]       	=					ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size)

								UpperCAmelCase_		:      Dict       	=					None
								if self.use_input_mask:
									UpperCAmelCase_		:      Tuple       	=					ids_tensor([self.batch_size, self.seq_length]    ,			vocab_size=2)

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

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

								UpperCAmelCase_		:      Dict       	=					self.get_config()

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


							def 						_snake_case							(			self)							->				int:
								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 						_snake_case							(			self)							->				Optional[Any]:
								UpperCAmelCase_		:      Tuple       	=					self.get_config()
								UpperCAmelCase_		:      List[str]       	=					300
								return config


							def 						_snake_case							(			self    ,			_snake_case)							->				Optional[Any]:
								self.parent.assertListEqual(list(result.loss.size())    ,			[])


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Dict:
								UpperCAmelCase_		:      List[Any]       	=					DebertaModel(config=_snake_case)
								model.to(_snake_case)
								model.eval()
								UpperCAmelCase_		:      int       	=					model(_snake_case    ,			attention_mask=_snake_case    ,			token_type_ids=_snake_case)[0]
								UpperCAmelCase_		:      Optional[Any]       	=					model(_snake_case    ,			token_type_ids=_snake_case)[0]
								UpperCAmelCase_		:      Any       	=					model(_snake_case)[0]

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


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				List[Any]:
								UpperCAmelCase_		:      Tuple       	=					DebertaForMaskedLM(config=_snake_case)
								model.to(_snake_case)
								model.eval()
								UpperCAmelCase_		:      Union[str, 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 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Optional[int]:
								UpperCAmelCase_		:      str       	=					self.num_labels
								UpperCAmelCase_		:      str       	=					DebertaForSequenceClassification(_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.assertListEqual(list(result.logits.size())    ,			[self.batch_size, self.num_labels])
								self.check_loss_output(_snake_case)


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				int:
								UpperCAmelCase_		:      Optional[Any]       	=					self.num_labels
								UpperCAmelCase_		:      List[Any]       	=					DebertaForTokenClassification(config=_snake_case)
								model.to(_snake_case)
								model.eval()
								UpperCAmelCase_		:      int       	=					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 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Dict:
								UpperCAmelCase_		:      Union[str, Any]       	=					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 						_snake_case							(			self)							->				Union[str, Any]:
								UpperCAmelCase_		:      List[str]       	=					self.prepare_config_and_inputs()
								(
								    UpperCAmelCase_    
								)		:      Optional[Any]       	=					config_and_inputs
								UpperCAmelCase_		:      List[str]       	=					{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
								return config, inputs_dict



@require_torch
class 					lowercase						(  a_,					a_,					unittest.TestCase				):
							_lowerCamelCase		:      Optional[int]=   (
							    (
							        DebertaModel,
							        DebertaForMaskedLM,
							        DebertaForSequenceClassification,
							        DebertaForTokenClassification,
							        DebertaForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							_lowerCamelCase		:      int=   (
							    {
							        "feature-extraction": DebertaModel,
							        "fill-mask": DebertaForMaskedLM,
							        "question-answering": DebertaForQuestionAnswering,
							        "text-classification": DebertaForSequenceClassification,
							        "token-classification": DebertaForTokenClassification,
							        "zero-shot": DebertaForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)

							_lowerCamelCase		:      Optional[int]=   True
							_lowerCamelCase		:      List[str]=   False
							_lowerCamelCase		:      Union[str, Any]=   False
							_lowerCamelCase		:      Any=   False
							_lowerCamelCase		:      Optional[Any]=   False


							def 						_snake_case							(			self)							->				List[Any]:
								UpperCAmelCase_		:      List[str]       	=					DebertaModelTester(self)
								UpperCAmelCase_		:      Tuple       	=					ConfigTester(self    ,			config_class=_snake_case    ,			hidden_size=37)


							def 						_snake_case							(			self)							->				str:
								self.config_tester.run_common_tests()


							def 						_snake_case							(			self)							->				Any:
								UpperCAmelCase_		:      Tuple       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_model(*_snake_case)


							def 						_snake_case							(			self)							->				Optional[Any]:
								UpperCAmelCase_		:      Tuple       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_sequence_classification(*_snake_case)


							def 						_snake_case							(			self)							->				Any:
								UpperCAmelCase_		:      Dict       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_masked_lm(*_snake_case)


							def 						_snake_case							(			self)							->				Optional[Any]:
								UpperCAmelCase_		:      str       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_question_answering(*_snake_case)


							def 						_snake_case							(			self)							->				List[str]:
								UpperCAmelCase_		:      Tuple       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_token_classification(*_snake_case)





							@slow
							def 						_snake_case							(			self)							->				List[Any]:
								for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									UpperCAmelCase_		:      Tuple       	=					DebertaModel.from_pretrained(_snake_case)
									self.assertIsNotNone(_snake_case)



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


							@unittest.skip(reason='Model not available yet')
							def 						_snake_case							(			self)							->				Optional[int]:
								pass





							@slow
							def 						_snake_case							(			self)							->				Any:
								UpperCAmelCase_		:      Union[str, Any]       	=					DebertaModel.from_pretrained('microsoft/deberta-base')

								UpperCAmelCase_		:      Union[str, Any]       	=					torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]])
								UpperCAmelCase_		:      List[str]       	=					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
								with torch.no_grad():
									UpperCAmelCase_		:      Optional[Any]       	=					model(_snake_case    ,			attention_mask=_snake_case)[0]
								# compare the actual values for a slice.
								UpperCAmelCase_		:      Any       	=					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]    ,			_snake_case    ,			atol=1e-4)    ,			F"""{output[:, 1:4, 1:4]}""")









'''simple docstring'''


class 					lowercase						:


							def __init__(			self)							->				Dict:
								UpperCAmelCase_		:      List[str]       	=					0
								UpperCAmelCase_		:      str       	=					0
								UpperCAmelCase_		:      Union[str, Any]       	=					{}


							def 						_snake_case							(			self    ,			_snake_case)							->				Tuple:
								if vertex not in self.adjacency:
									UpperCAmelCase_		:      Tuple       	=					{}
									self.num_vertices += 1


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Any:
								self.add_vertex(_snake_case)
								self.add_vertex(_snake_case)

								if head == tail:
									return

								UpperCAmelCase_		:      str       	=					weight
								UpperCAmelCase_		:      Optional[int]       	=					weight


							def 						_snake_case							(			self)							->				Union[str, Any]:
								UpperCAmelCase_		:      Dict       	=					self.get_edges()
								for edge in edges:
									UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      str       	=					edge
									edges.remove((tail, head, weight))
								for i in range(len(_snake_case)):
									UpperCAmelCase_		:      Union[str, Any]       	=					list(edges[i])

								edges.sort(key=lambda _snake_case: e[2])
								for i in range(len(_snake_case) - 1):
									if edges[i][2] >= edges[i + 1][2]:
										UpperCAmelCase_		:      Tuple       	=					edges[i][2] + 1
								for edge in edges:
									UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      List[Any]       	=					edge
									UpperCAmelCase_		:      Any       	=					weight
									UpperCAmelCase_		:      Union[str, Any]       	=					weight


							def __str__(			self)							->				str:
								UpperCAmelCase_		:      int       	=					''
								for tail in self.adjacency:
									for head in self.adjacency[tail]:
										UpperCAmelCase_		:      Dict       	=					self.adjacency[head][tail]
										string += F"""{head} -> {tail} == {weight}\n"""
								return string.rstrip('\n')


							def 						_snake_case							(			self)							->				str:
								UpperCAmelCase_		:      Optional[int]       	=					[]
								for tail in self.adjacency:
									for head in self.adjacency[tail]:
										output.append((tail, head, self.adjacency[head][tail]))
								return output


							def 						_snake_case							(			self)							->				Any:
								return self.adjacency.keys()


							@staticmethod
							def 						_snake_case							(			_snake_case=None    ,			_snake_case=None)							->				Dict:
								UpperCAmelCase_		:      List[str]       	=					Graph()
								if vertices is None:
									UpperCAmelCase_		:      Optional[Any]       	=					[]
								if edges is None:
									UpperCAmelCase_		:      int       	=					[]
								for vertex in vertices:
									g.add_vertex(_snake_case)
								for edge in edges:
									g.add_edge(*_snake_case)
								return g



							class 					lowercase						:


														def __init__(			self)							->				Dict:
															UpperCAmelCase_		:      Tuple       	=					{}
															UpperCAmelCase_		:      str       	=					{}


														def __len__(			self)							->				List[Any]:
															return len(self.parent)


														def 						_snake_case							(			self    ,			_snake_case)							->				Union[str, Any]:
															if item in self.parent:
																return self.find(_snake_case)

															UpperCAmelCase_		:      Any       	=					item
															UpperCAmelCase_		:      List[str]       	=					0
															return item


														def 						_snake_case							(			self    ,			_snake_case)							->				str:
															if item not in self.parent:
																return self.make_set(_snake_case)
															if item != self.parent[item]:
																UpperCAmelCase_		:      Dict       	=					self.find(self.parent[item])
															return self.parent[item]





														def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case)							->				int:
															UpperCAmelCase_		:      List[str]       	=					self.find(_snake_case)
															UpperCAmelCase_		:      List[str]       	=					self.find(_snake_case)

															if roota == roota:
																return roota

															if self.rank[roota] > self.rank[roota]:
																UpperCAmelCase_		:      int       	=					roota
																return roota

															if self.rank[roota] < self.rank[roota]:
																UpperCAmelCase_		:      Dict       	=					roota
																return roota

															if self.rank[roota] == self.rank[roota]:
																self.rank[roota] += 1
																UpperCAmelCase_		:      Any       	=					roota
																return roota
															return None


							@staticmethod
							def 						_snake_case							(			_snake_case)							->				int:
								UpperCAmelCase_		:      Optional[Any]       	=					graph.num_vertices

								UpperCAmelCase_		:      Optional[int]       	=					Graph.UnionFind()
								UpperCAmelCase_		:      int       	=					[]
								while num_components > 1:
									UpperCAmelCase_		:      Any       	=					{}
									for vertex in graph.get_vertices():
										UpperCAmelCase_		:      Optional[Any]       	=					-1

									UpperCAmelCase_		:      List[str]       	=					graph.get_edges()
									for edge in edges:
										UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      List[str]       	=					edge
										edges.remove((tail, head, weight))
									for edge in edges:
										UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      Union[str, Any]       	=					edge
										UpperCAmelCase_		:      Any       	=					union_find.find(_snake_case)
										UpperCAmelCase_		:      Dict       	=					union_find.find(_snake_case)
										if seta != seta:
											if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
												UpperCAmelCase_		:      List[str]       	=					[head, tail, weight]

											if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
												UpperCAmelCase_		:      str       	=					[head, tail, weight]
									for vertex in cheap_edge:
										if cheap_edge[vertex] != -1:
											UpperCAmelCase_	,    UpperCAmelCase_	,    UpperCAmelCase_		:      Any       	=					cheap_edge[vertex]
											if union_find.find(_snake_case) != union_find.find(_snake_case):
												union_find.union(_snake_case    ,			_snake_case)
												mst_edges.append(cheap_edge[vertex])
												UpperCAmelCase_		:      Tuple       	=					num_components - 1
								UpperCAmelCase_		:      int       	=					Graph.build(edges=_snake_case)
								return mst






