


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 					lowerCAmelCase_ (       _a):




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






									"""simple docstring"""






									a__ :Optional[Any]						  =    parent
									a__ :List[str]						  =    batch_size
									a__ :Any						  =    seq_length
									a__ :Any						  =    is_training
									a__ :List[str]						  =    use_input_mask
									a__ :Any						  =    use_token_type_ids
									a__ :List[Any]						  =    use_labels
									a__ :List[str]						  =    vocab_size
									a__ :Optional[int]						  =    hidden_size
									a__ :Tuple						  =    num_hidden_layers
									a__ :List[str]						  =    num_attention_heads
									a__ :List[str]						  =    intermediate_size
									a__ :Union[str, Any]						  =    hidden_act
									a__ :Tuple						  =    hidden_dropout_prob
									a__ :Optional[int]						  =    attention_probs_dropout_prob
									a__ :Dict						  =    max_position_embeddings
									a__ :List[str]						  =    type_vocab_size
									a__ :str						  =    type_sequence_label_size
									a__ :int						  =    initializer_range
									a__ :Union[str, Any]						  =    num_labels
									a__ :List[Any]						  =    num_choices
									a__ :str						  =    relative_attention
									a__ :Optional[int]						  =    position_biased_input
									a__ :str						  =    pos_att_type
									a__ :Optional[int]						  =    scope




						def  _snake_case					(	self						:   Tuple						)      ->int:






									"""simple docstring"""






									a__ :int						  =    ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size						)

									a__ :Union[str, Any]						  =    None
									if self.use_input_mask:
												a__ :List[Any]						  =    ids_tensor([self.batch_size, self.seq_length]   ,     vocab_size=2						)

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

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

									a__ :Any						  =    self.get_config()

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




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






									"""simple docstring"""






									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						:   List[str]						)      ->Any:






									"""simple docstring"""






									a__ :Union[str, Any]						  =    self.get_config()
									a__ :Tuple						  =    300
									return config




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






									"""simple docstring"""






									self.parent.assertListEqual(list(result.loss.size()						)   ,     []						)




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






									"""simple docstring"""






									a__ :Optional[int]						  =    DebertaModel(config=__A						)
									model.to(__A						)
									model.eval()
									a__ :Optional[int]						  =    model(__A   ,     attention_mask=__A   ,     token_type_ids=__A						)[0]
									a__ :List[Any]						  =    model(__A   ,     token_type_ids=__A						)[0]
									a__ :str						  =    model(__A						)[0]

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




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






									"""simple docstring"""






									a__ :Any						  =    DebertaForMaskedLM(config=__A						)
									model.to(__A						)
									model.eval()
									a__ :Optional[int]						  =    model(__A   ,     attention_mask=__A   ,     token_type_ids=__A   ,     labels=__A						)

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




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






									"""simple docstring"""






									a__ :int						  =    self.num_labels
									a__ :Union[str, Any]						  =    DebertaForSequenceClassification(__A						)
									model.to(__A						)
									model.eval()
									a__ :Any						  =    model(__A   ,     attention_mask=__A   ,     token_type_ids=__A   ,     labels=__A						)
									self.parent.assertListEqual(list(result.logits.size()						)   ,     [self.batch_size, self.num_labels]						)
									self.check_loss_output(__A						)




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






									"""simple docstring"""






									a__ :str						  =    self.num_labels
									a__ :Optional[int]						  =    DebertaForTokenClassification(config=__A						)
									model.to(__A						)
									model.eval()
									a__ :Any						  =    model(__A   ,     attention_mask=__A   ,     token_type_ids=__A   ,     labels=__A						)
									self.parent.assertEqual(result.logits.shape   ,     (self.batch_size, self.seq_length, self.num_labels)						)




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






									"""simple docstring"""






									a__ :Tuple						  =    DebertaForQuestionAnswering(config=__A						)
									model.to(__A						)
									model.eval()
									a__ :List[Any]						  =    model(
									    __A   ,     attention_mask=__A   ,     token_type_ids=__A   ,     start_positions=__A   ,     end_positions=__A   ,     )
									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						:   Tuple						)      ->int:






									"""simple docstring"""






									a__ :Optional[int]						  =    self.prepare_config_and_inputs()
									(
									    a__  
									) :List[Any]						  =    config_and_inputs
									a__ :Union[str, Any]						  =    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
									return config, inputs_dict






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

						lowerCamelCase_         =     True
						lowerCamelCase_         =     False
						lowerCamelCase_         =     False
						lowerCamelCase_         =     False
						lowerCamelCase_         =     False




						def  _snake_case					(	self						:   str						)      ->str:






									"""simple docstring"""






									a__ :int						  =    DebertaModelTester(self						)
									a__ :List[Any]						  =    ConfigTester(self   ,     config_class=__A   ,     hidden_size=37						)




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






									"""simple docstring"""






									self.config_tester.run_common_tests()




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






									"""simple docstring"""






									a__ :List[str]						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_model(*__A						)




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






									"""simple docstring"""






									a__ :Any						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_sequence_classification(*__A						)




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






									"""simple docstring"""






									a__ :List[Any]						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_masked_lm(*__A						)




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






									"""simple docstring"""






									a__ :int						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_question_answering(*__A						)




						def  _snake_case					(	self						:   Tuple						)      ->Any:






									"""simple docstring"""






									a__ :Any						  =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_deberta_for_token_classification(*__A						)






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






									"""simple docstring"""






									for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a__ :int						  =    DebertaModel.from_pretrained(__A						)
												self.assertIsNotNone(__A						)






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




						@unittest.skip(reason="Model not available yet"						)
						def  _snake_case					(	self						:   List[str]						)      ->Dict:






									"""simple docstring"""






									pass






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






									"""simple docstring"""






									a__ :Tuple						  =    DebertaModel.from_pretrained("microsoft/deberta-base"						)

									a__ :List[Any]						  =    torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]						)
									a__ :Optional[int]						  =    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
									with torch.no_grad():
												a__ :Tuple						  =    model(__A   ,     attention_mask=__A						)[0]
									# compare the actual values for a slice.
									a__ :Optional[int]						  =    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]   ,     __A   ,     atol=1E-4						)   ,     F'''{output[:, 1:4, 1:4]}'''						)






import numpy as np


def 				lowerCamelCase__	(      a	:   np.ndarray	,  a	:   np.ndarray	,  a	:   float = 1e-12	,  a	:   int = 100	,  )    ->	tuple[float, np.ndarray]:





			"""simple docstring"""
			assert np.shape(a				)[0] == np.shape(a				)[1]
			# Ensure proper dimensionality.
			assert np.shape(a				)[0] == np.shape(a				)[0]
			# Ensure inputs are either both complex or both real
			assert np.iscomplexobj(a				) == np.iscomplexobj(a				)
			a__ :List[str]						  =    np.iscomplexobj(a				)
			if is_complex:
						# Ensure complex input_matrix is Hermitian
						assert np.array_equal(a	,  input_matrix.conj().T				)

			# Set convergence to False. Will define convergence when we exceed max_iterations
			# or when we have small changes from one iteration to next.

			a__ :List[str]						  =    False
			a__ :str						  =    0
			a__ :Dict						  =    0
			a__ :Tuple						  =    1e12

			while not convergence:
						# Multiple matrix by the vector.
						a__ :Tuple						  =    np.dot(a	,  a				)
						# Normalize the resulting output vector.
						a__ :List[Any]						  =    w / np.linalg.norm(a				)
						# Find rayleigh quotient
						# (faster than usual b/c we know vector is normalized already)
						a__ :List[str]						  =    vector.conj().T if is_complex else vector.T
						a__ :Any						  =    np.dot(a	,  np.dot(a	,  a				)				)

						# Check convergence.
						a__ :Optional[Any]						  =    np.abs(lambda_ - lambda_previous				) / lambda_
						iterations += 1

						if error <= error_tol or iterations >= max_iterations:
									a__ :List[str]						  =    True

						a__ :Optional[int]						  =    lambda_

			if is_complex:
						a__ :Union[str, Any]						  =    np.real(lambda_				)

			return lambda_, vector


def 				lowerCamelCase__	(      )    ->	None:





			"""simple docstring"""
			a__ :List[str]						  =    np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]				)
			a__ :Dict						  =    np.array([41, 4, 20]				)
			a__ :Union[str, Any]						  =    real_input_matrix.astype(np.complexaaa				)
			a__ :Optional[Any]						  =    np.triu(1j * complex_input_matrix	,  1				)
			complex_input_matrix += imag_matrix
			complex_input_matrix += -1 * imag_matrix.T
			a__ :Union[str, Any]						  =    np.array([41, 4, 20]				).astype(np.complexaaa				)

			for problem_type in ["real", "complex"]:
						if problem_type == "real":
									a__ :Union[str, Any]						  =    real_input_matrix
									a__ :int						  =    real_vector
						elif problem_type == "complex":
									a__ :Optional[Any]						  =    complex_input_matrix
									a__ :List[Any]						  =    complex_vector

						# Our implementation.
						a__  ,  a__ :Any						  =    power_iteration(a	,  a				)

						# Numpy implementation.

						# Get eigenvalues and eigenvectors using built-in numpy
						# eigh (eigh used for symmetric or hermetian matrices).
						a__  ,  a__ :int						  =    np.linalg.eigh(a				)
						# Last eigenvalue is the maximum one.
						a__ :int						  =    eigen_values[-1]
						# Last column in this matrix is eigenvector corresponding to largest eigenvalue.
						a__ :Optional[Any]						  =    eigen_vectors[:, -1]

						# Check our implementation and numpy gives close answers.
						assert np.abs(eigen_value - eigen_value_max				) <= 1e-6
						# Take absolute values element wise of each eigenvector.
						# as they are only unique to a minus sign.
						assert np.linalg.norm(np.abs(a				) - np.abs(a				)				) <= 1e-6


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		test_power_iteration()


