



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







							def __init__(   self,			__a,			__a=13,			__a=7,			__a=True,			__a=True,			__a=True,			__a=True,			__a=99,			__a=32,			__a=5,			__a=4,			__a=37,			__a="gelu",			__a=0.1,			__a=0.1,			__a=512,			__a=16,			__a=2,			__a=0.02,			__a=False,			__a=True,			__a="None",			__a=3,			__a=4,			__a=None,			):

														'''simple docstring'''

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







							def    snake_case__      (   self):

														'''simple docstring'''

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

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

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

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

														_lowerCAmelCase     :							Any    =    self.get_config()

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







							def    snake_case__      (   self):

														'''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):

														'''simple docstring'''

														_lowerCAmelCase     :							str    =    self.get_config()
														_lowerCAmelCase     :							Optional[int]    =    300
														return config







							def    snake_case__      (   self,			__a):

														'''simple docstring'''

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







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							str    =    DebertaModel(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase     :							Optional[Any]    =    model(__a,			attention_mask=__a,			token_type_ids=__a)[0]
														_lowerCAmelCase     :							Any    =    model(__a,			token_type_ids=__a)[0]
														_lowerCAmelCase     :							Optional[Any]    =    model(__a)[0]

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







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							Union[str, Any]    =    DebertaForMaskedLM(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase     :							Union[str, 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.vocab_size))







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    self.num_labels
														_lowerCAmelCase     :							str    =    DebertaForSequenceClassification(__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase     :							List[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,			__a,			__a,			__a,			__a,			__a,			__a,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							str    =    self.num_labels
														_lowerCAmelCase     :							List[str]    =    DebertaForTokenClassification(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase     :							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.num_labels))







							def    snake_case__      (   self,			__a,			__a,			__a,			__a,			__a,			__a,			__a):

														'''simple docstring'''

														_lowerCAmelCase     :							List[Any]    =    DebertaForQuestionAnswering(config=__a)
														model.to(__a)
														model.eval()
														_lowerCAmelCase     :							int    =    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):

														'''simple docstring'''

														_lowerCAmelCase     :							Union[str, Any]    =    self.prepare_config_and_inputs()
														(
														    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    (
														    _lowerCAmelCase
														)   ,    
														)     :							Dict    =    config_and_inputs
														_lowerCAmelCase     :							Union[str, Any]    =    {"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):
							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):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    DebertaModelTester(self)
														_lowerCAmelCase     :							str    =    ConfigTester(self,			config_class=__a,			hidden_size=37)







							def    snake_case__      (   self):

														'''simple docstring'''

														self.config_tester.run_common_tests()







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_deberta_model(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Union[str, Any]    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_deberta_for_sequence_classification(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Union[str, Any]    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_deberta_for_masked_lm(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Tuple    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_deberta_for_question_answering(*__a)







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							str    =    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_deberta_for_token_classification(*__a)







							@slow
							def    snake_case__      (   self):

														'''simple docstring'''

														for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					_lowerCAmelCase     :							List[str]    =    DebertaModel.from_pretrained(__a)
																					self.assertIsNotNone(__a)



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







							@unittest.skip(reason="Model not available yet")
							def    snake_case__      (   self):

														'''simple docstring'''

														pass







							@slow
							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[Any]    =    DebertaModel.from_pretrained("microsoft/deberta-base")

														_lowerCAmelCase     :							Any    =    torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]])
														_lowerCAmelCase     :							str    =    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
														with torch.no_grad():
																					_lowerCAmelCase     :							Dict    =    model(__a,			attention_mask=__a)[0]
														# compare the actual values for a slice.
														_lowerCAmelCase     :							Dict    =    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]}")





def 				A				(					_lowerCamelCase  ):





							'''simple docstring'''




							if bit_count < 0:
														raise ValueError("The given input must be positive"  )

							# get the generated string sequence
							_lowerCAmelCase     :							List[str]    =    gray_code_sequence_string(_lowerCamelCase  )
							#
							# convert them to integers
							for i in range(len(_lowerCamelCase  )  ):
														_lowerCAmelCase     :							List[str]    =    int(sequence[i]				, 2  )

							return sequence






def 				A				(					_lowerCamelCase  ):





							'''simple docstring'''




							if bit_count == 0:
														return ["0"]

							if bit_count == 1:
														return ["0", "1"]

							_lowerCAmelCase     :							List[Any]    =    1 << bit_count  # defines the length of the sequence
							# 1<< n is equivalent to 2^n

							# recursive answer will generate answer for n-1 bits
							_lowerCAmelCase     :							Optional[int]    =    gray_code_sequence_string(bit_count - 1  )

							_lowerCAmelCase     :							str    =    []

							# append 0 to first half of the smaller sequence generated
							for i in range(seq_len // 2  ):
														_lowerCAmelCase     :							Dict    =    "0" + smaller_sequence[i]
														sequence.append(_lowerCamelCase  )

							# append 1 to second half ... start from the end of the list
							for i in reversed(range(seq_len // 2  )  ):
														_lowerCAmelCase     :							Optional[Any]    =    "1" + smaller_sequence[i]
														sequence.append(_lowerCamelCase  )

							return sequence


if __name__ == "__main__":
						import doctest

						doctest.testmod()
