

import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     FalconForCausalLM,
     FalconForQuestionAnswering,
     FalconForSequenceClassification,
     FalconForTokenClassification,
     FalconModel,
 )







class   __lowercase      :
 def __init__(							self      ,							A_      ,							A_=3      ,							A_=7      ,							A_=True      ,							A_=True      ,							A_=False      ,							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_=3      ,							A_=4      ,							A_=None      ,							)					->List[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					parent
       __lowerCAmelCase					:						Optional[Any]					=					batch_size
       __lowerCAmelCase					:						Any					=					seq_length
       __lowerCAmelCase					:						Dict					=					is_training
       __lowerCAmelCase					:						Optional[int]					=					use_input_mask
       __lowerCAmelCase					:						Optional[Any]					=					use_token_type_ids
       __lowerCAmelCase					:						Optional[Any]					=					use_labels
       __lowerCAmelCase					:						List[str]					=					vocab_size
       __lowerCAmelCase					:						List[Any]					=					hidden_size
       __lowerCAmelCase					:						List[Any]					=					num_hidden_layers
       __lowerCAmelCase					:						List[str]					=					num_attention_heads
       __lowerCAmelCase					:						Dict					=					intermediate_size
       __lowerCAmelCase					:						Optional[Any]					=					hidden_act
       __lowerCAmelCase					:						Union[str, Any]					=					hidden_dropout_prob
       __lowerCAmelCase					:						int					=					attention_probs_dropout_prob
       __lowerCAmelCase					:						Tuple					=					max_position_embeddings
       __lowerCAmelCase					:						int					=					type_vocab_size
       __lowerCAmelCase					:						Union[str, Any]					=					type_sequence_label_size
       __lowerCAmelCase					:						Any					=					initializer_range
       __lowerCAmelCase					:						Any					=					num_labels
       __lowerCAmelCase					:						Union[str, Any]					=					num_choices
       __lowerCAmelCase					:						Any					=					scope
 def 	UpperCamelCase__  (							self					)					->Union[str, Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size					)

       __lowerCAmelCase					:						Tuple					=					None
       if self.use_input_mask:
             __lowerCAmelCase					:						List[Any]					=					random_attention_mask([self.batch_size, self.seq_length]					)

       __lowerCAmelCase					:						str					=					None

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

       __lowerCAmelCase					:						Union[str, Any]					=					self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 	UpperCamelCase__  (							self					)					->Dict:



       '''simple docstring'''





       return FalconConfig(
           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      ,							is_decoder=A_      ,							initializer_range=self.initializer_range      ,							pad_token_id=1      ,							new_decoder_architecture=A_      ,							)
 def 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					FalconModel(config=A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Optional[int]					=					model(A_      ,							attention_mask=A_					)
       __lowerCAmelCase					:						List[str]					=					model(A_					)
       self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.hidden_size)					)
 def 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							)					->Dict:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					True
       __lowerCAmelCase					:						Dict					=					FalconModel(A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						str					=					model(
           A_      ,							attention_mask=A_      ,							encoder_hidden_states=A_      ,							encoder_attention_mask=A_      ,							)
       __lowerCAmelCase					:						Tuple					=					model(
           A_      ,							attention_mask=A_      ,							encoder_hidden_states=A_      ,							)
       __lowerCAmelCase					:						int					=					model(A_      ,							attention_mask=A_					)
       self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.hidden_size)					)
 def 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					FalconForCausalLM(config=A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Any					=					model(A_      ,							attention_mask=A_      ,							labels=A_					)
       self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.vocab_size)					)
 def 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					True
       __lowerCAmelCase					:						Any					=					True
       __lowerCAmelCase					:						Any					=					FalconForCausalLM(config=A_					)
       model.to(A_					)
       model.eval()

       # first forward pass
       __lowerCAmelCase					:						List[Any]					=					model(
           A_      ,							attention_mask=A_      ,							encoder_hidden_states=A_      ,							encoder_attention_mask=A_      ,							use_cache=A_      ,							)
       __lowerCAmelCase					:						int					=					outputs.past_key_values

       # create hypothetical multiple next token and extent to next_input_ids
       __lowerCAmelCase					:						List[str]					=					ids_tensor((self.batch_size, 3)      ,							config.vocab_size					)
       __lowerCAmelCase					:						List[str]					=					ids_tensor((self.batch_size, 3)      ,							vocab_size=2					)

       # append to next input_ids and
       __lowerCAmelCase					:						str					=					torch.cat([input_ids, next_tokens]      ,							dim=-1					)
       __lowerCAmelCase					:						Any					=					torch.cat([input_mask, next_mask]      ,							dim=-1					)

       __lowerCAmelCase					:						Optional[int]					=					model(
           A_      ,							attention_mask=A_      ,							encoder_hidden_states=A_      ,							encoder_attention_mask=A_      ,							output_hidden_states=A_      ,							)['''hidden_states'''][0]
       __lowerCAmelCase					:						List[Any]					=					model(
           A_      ,							attention_mask=A_      ,							encoder_hidden_states=A_      ,							encoder_attention_mask=A_      ,							past_key_values=A_      ,							output_hidden_states=A_      ,							)['''hidden_states'''][0]

       # select random slice
       __lowerCAmelCase					:						List[Any]					=					ids_tensor((1,)      ,							output_from_past.shape[-1]					).item()
       __lowerCAmelCase					:						Any					=					output_from_no_past[:, -3:, random_slice_idx].detach()
       __lowerCAmelCase					:						Tuple					=					output_from_past[:, :, random_slice_idx].detach()

       self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]					)

       # test that outputs are equal for slice
       self.parent.assertTrue(torch.allclose(A_      ,							A_      ,							atol=1e-3					)					)





 def 	UpperCamelCase__  (							self					)					->int:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					self.prepare_config_and_inputs()
       (
           (
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							
       )					:						str					=					config_and_inputs
       __lowerCAmelCase					:						List[str]					=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
       return config, inputs_dict







@require_torch
class   __lowercase      (_UpperCAmelCase		,   _UpperCAmelCase		,   _UpperCAmelCase		,   unittest.TestCase ):
 _UpperCamelCase			  =  (
     (
         FalconModel,
         FalconForCausalLM,
         FalconForSequenceClassification,
         FalconForTokenClassification,
         FalconForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 _UpperCamelCase			  =  (FalconForCausalLM,) if is_torch_available() else ()
 _UpperCamelCase			  =  (
     {
         """feature-extraction""": FalconModel,
         """text-classification""": FalconForSequenceClassification,
         """text-generation""": FalconForCausalLM,
         """question-answering""": FalconForQuestionAnswering,
         """token-classification""": FalconForTokenClassification,
         """zero-shot""": FalconForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 _UpperCamelCase			  =  False
 _UpperCamelCase			  =  False
 def 	UpperCamelCase__  (							self					)					->List[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						Optional[Any]					=					FalconModelTester(self					)
       __lowerCAmelCase					:						Union[str, Any]					=					ConfigTester(self      ,							config_class=A_      ,							hidden_size=37					)
 def 	UpperCamelCase__  (							self					)					->List[str]:



       '''simple docstring'''





       self.config_tester.run_common_tests()
 def 	UpperCamelCase__  (							self					)					->Optional[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_					)
 def 	UpperCamelCase__  (							self					)					->Dict:



       '''simple docstring'''





       __lowerCAmelCase,							*__lowerCAmelCase					:						str					=					self.model_tester.prepare_config_and_inputs()
       for alibi in [True, False]:
             __lowerCAmelCase					:						int					=					alibi
             self.model_tester.create_and_check_model(A_      ,							*A_					)
 def 	UpperCamelCase__  (							self					)					->int:



       '''simple docstring'''





       __lowerCAmelCase,							__lowerCAmelCase					:						Union[str, Any]					=					self.model_tester.prepare_config_and_inputs_for_common()
       __lowerCAmelCase					:						Dict					=					3
       __lowerCAmelCase					:						Union[str, Any]					=					input_dict['''input_ids''']
       __lowerCAmelCase					:						str					=					input_ids.ne(1					).to(A_					)
       __lowerCAmelCase					:						Any					=					ids_tensor([self.model_tester.batch_size]      ,							self.model_tester.type_sequence_label_size					)
       __lowerCAmelCase					:						Tuple					=					FalconForSequenceClassification(A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Optional[int]					=					model(A_      ,							attention_mask=A_      ,							labels=A_					)
       self.assertEqual(result.logits.shape      ,							(self.model_tester.batch_size, self.model_tester.num_labels)					)
 def 	UpperCamelCase__  (							self					)					->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase,							__lowerCAmelCase					:						Any					=					self.model_tester.prepare_config_and_inputs_for_common()
       __lowerCAmelCase					:						Union[str, Any]					=					3
       __lowerCAmelCase					:						Optional[Any]					=					'''single_label_classification'''
       __lowerCAmelCase					:						Optional[int]					=					input_dict['''input_ids''']
       __lowerCAmelCase					:						Optional[Any]					=					input_ids.ne(1					).to(A_					)
       __lowerCAmelCase					:						List[Any]					=					ids_tensor([self.model_tester.batch_size]      ,							self.model_tester.type_sequence_label_size					)
       __lowerCAmelCase					:						int					=					FalconForSequenceClassification(A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Union[str, Any]					=					model(A_      ,							attention_mask=A_      ,							labels=A_					)
       self.assertEqual(result.logits.shape      ,							(self.model_tester.batch_size, self.model_tester.num_labels)					)
 def 	UpperCamelCase__  (							self					)					->List[str]:



       '''simple docstring'''





       __lowerCAmelCase,							__lowerCAmelCase					:						str					=					self.model_tester.prepare_config_and_inputs_for_common()
       __lowerCAmelCase					:						List[Any]					=					input_dict['''input_ids''']
       __lowerCAmelCase					:						List[str]					=					FalconForCausalLM(A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Tuple					=					model(A_      ,							use_cache=A_					)
       __lowerCAmelCase					:						Any					=					input_ids.shape[0]
       __lowerCAmelCase					:						Optional[int]					=					model._convert_to_rw_cache(result.past_key_values					)
       __lowerCAmelCase					:						Optional[Any]					=					model._convert_cache_to_standard_format(A_      ,							A_					)
       for layer in range(len(A_					)					):
             for tensor_idx in range(2					):
                   self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3					)
                   self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4					)
                   self.assertTrue(
                       torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]					)					)
 def 	UpperCamelCase__  (							self					)					->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase,							__lowerCAmelCase					:						int					=					self.model_tester.prepare_config_and_inputs_for_common()
       __lowerCAmelCase					:						str					=					3
       __lowerCAmelCase					:						Union[str, Any]					=					'''multi_label_classification'''
       __lowerCAmelCase					:						List[Any]					=					input_dict['''input_ids''']
       __lowerCAmelCase					:						Tuple					=					input_ids.ne(1					).to(A_					)
       __lowerCAmelCase					:						Optional[int]					=					ids_tensor(
           [self.model_tester.batch_size, config.num_labels]      ,							self.model_tester.type_sequence_label_size					).to(torch.float					)
       __lowerCAmelCase					:						Optional[int]					=					FalconForSequenceClassification(A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Dict					=					model(A_      ,							attention_mask=A_      ,							labels=A_					)
       self.assertEqual(result.logits.shape      ,							(self.model_tester.batch_size, self.model_tester.num_labels)					)





 def 	UpperCamelCase__  (							self					)					->Tuple:



       '''simple docstring'''





       for model_class in self.all_generative_model_classes:
             __lowerCAmelCase,							__lowerCAmelCase					:						str					=					self.model_tester.prepare_config_and_inputs_for_common()

             # If it doesn't support cache, pass the test
             if not hasattr(A_      ,							'''use_cache'''					):
                   return

             __lowerCAmelCase					:						Optional[int]					=					model_class(A_					).to(A_					)
             if "use_cache" not in inputs:
                   __lowerCAmelCase					:						Union[str, Any]					=					True
             __lowerCAmelCase					:						Tuple					=					model(**A_					)

             # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
             if "past_key_values" not in outputs:
                   return

             __lowerCAmelCase					:						Union[str, Any]					=					(
                 getattr(A_      ,							'''decoder_layers'''      ,							A_					)
                 or getattr(A_      ,							'''num_decoder_layers'''      ,							A_					)
                 or config.num_hidden_layers
             )
             __lowerCAmelCase					:						List[str]					=					getattr(A_      ,							'''num_kv_heads'''      ,							config.num_attention_heads					)
             __lowerCAmelCase					:						Optional[int]					=					getattr(A_      ,							'''d_model'''      ,							config.hidden_size					)
             __lowerCAmelCase					:						List[str]					=					embed_dim // num_attention_heads

             __lowerCAmelCase					:						List[Any]					=					outputs['''past_key_values''']
             self.assertEqual(len(A_					)      ,							A_					)

             __lowerCAmelCase,							__lowerCAmelCase					:						List[str]					=					inputs['''input_ids'''].shape
             for i in range(A_					):
                   if config.new_decoder_architecture:
                         __lowerCAmelCase					:						List[Any]					=					config.num_attention_heads
                   elif config.multi_query:
                         __lowerCAmelCase					:						int					=					1
                   self.assertEqual(len(past_kv[0]					)      ,							2					)  # K V for the decoder = 2
                   self.assertEqual(
                       past_kv[i][0].shape      ,							(batch_size, num_attention_heads, seq_length, per_head_embed_dim)					)
                   self.assertEqual(
                       past_kv[i][1].shape      ,							(batch_size, num_attention_heads, seq_length, per_head_embed_dim)					)







@require_torch
class   __lowercase      (unittest.TestCase ):
 @slow
 def 	UpperCamelCase__  (							self					)					->Union[str, Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b'''					)
       __lowerCAmelCase					:						List[Any]					=					FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b'''					)
       model.eval()
       model.to(A_					)
       __lowerCAmelCase					:						Tuple					=					tokenizer('''My favorite food is'''      ,							return_tensors='''pt'''					).to(A_					)

       __lowerCAmelCase					:						Union[str, Any]					=					(
           '''My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'''
       )

       __lowerCAmelCase					:						Tuple					=					model.generate(**A_      ,							do_sample=A_      ,							max_new_tokens=19					)
       __lowerCAmelCase					:						Optional[int]					=					tokenizer.batch_decode(A_					)[0]

       self.assertEqual(A_      ,							A_					)
 @slow
 def 	UpperCamelCase__  (							self					)					->List[str]:



       '''simple docstring'''





       for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
             __lowerCAmelCase					:						Union[str, Any]					=					AutoTokenizer.from_pretrained(A_					)
             __lowerCAmelCase					:						Optional[Any]					=					FalconForCausalLM.from_pretrained(A_					)
             model.eval()
             model.to(A_					)
             __lowerCAmelCase					:						Any					=					tokenizer('''My favorite food is'''      ,							return_tensors='''pt'''					).to(A_					)

             # We just test that these run without errors - the models are randomly initialized
             # and so the actual text outputs will be garbage
             model.generate(**A_      ,							do_sample=A_      ,							max_new_tokens=4					)
             model.generate(**A_      ,							do_sample=A_      ,							max_new_tokens=4					)
             model.generate(**A_      ,							num_beams=2      ,							max_new_tokens=4					)





 @slow
 def 	UpperCamelCase__  (							self					)					->Any:



       '''simple docstring'''





       with torch.no_grad():
             for repo in [
                 "Rocketknight1/falcon-rw-1b",
                 "Rocketknight1/tiny-random-falcon-7b",
                 "Rocketknight1/tiny-random-falcon-40b",
             ]:
                   __lowerCAmelCase					:						Any					=					AutoTokenizer.from_pretrained(A_					)
                   __lowerCAmelCase					:						Tuple					=					FalconForCausalLM.from_pretrained(A_					)
                   model.eval()
                   model.to(device=A_					)
                   __lowerCAmelCase					:						Dict					=					tokenizer('''My favorite food is'''      ,							return_tensors='''pt'''					).to(A_					)

                   # Test results are the same with and without cache
                   __lowerCAmelCase					:						Dict					=					model.generate(**A_      ,							do_sample=A_      ,							max_new_tokens=20      ,							use_cache=A_					)
                   __lowerCAmelCase					:						List[str]					=					model.generate(**A_      ,							do_sample=A_      ,							max_new_tokens=20      ,							use_cache=A_					)
                   self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0					)





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      (_UpperCAmelCase ):
 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      ,							)					->List[Any]:



       '''simple docstring'''





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



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size					)

       __lowerCAmelCase					:						int					=					None
       if self.use_input_mask:
             __lowerCAmelCase					:						List[Any]					=					ids_tensor([self.batch_size, self.seq_length]      ,							vocab_size=2					)

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

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

       __lowerCAmelCase					:						Tuple					=					self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 	UpperCamelCase__  (							self					)					->List[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 	UpperCamelCase__  (							self					)					->str:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					self.get_config()
       __lowerCAmelCase					:						Dict					=					300
       return config
 def 	UpperCamelCase__  (							self      ,							A_					)					->Union[str, Any]:



       '''simple docstring'''





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



       '''simple docstring'''





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

       self.parent.assertListEqual(list(sequence_output.size()					)      ,							[self.batch_size, self.seq_length, self.hidden_size]					)
 def 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->int:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					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 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.num_labels
       __lowerCAmelCase					:						Tuple					=					DebertaForSequenceClassification(A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Union[str, 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 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->Dict:



       '''simple docstring'''





       __lowerCAmelCase					:						List[Any]					=					self.num_labels
       __lowerCAmelCase					:						Optional[int]					=					DebertaForTokenClassification(config=A_					)
       model.to(A_					)
       model.eval()
       __lowerCAmelCase					:						Tuple					=					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 	UpperCamelCase__  (							self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_					)					->str:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					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 	UpperCamelCase__  (							self					)					->List[str]:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.prepare_config_and_inputs()
       (
           (
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							(
           __lowerCAmelCase
       ),							
       )					:						Tuple					=					config_and_inputs
       __lowerCAmelCase					:						Tuple					=					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict







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

 _UpperCamelCase			  =  True
 _UpperCamelCase			  =  False
 _UpperCamelCase			  =  False
 _UpperCamelCase			  =  False
 _UpperCamelCase			  =  False
 def 	UpperCamelCase__  (							self					)					->int:



       '''simple docstring'''





       __lowerCAmelCase					:						int					=					DebertaModelTester(self					)
       __lowerCAmelCase					:						List[Any]					=					ConfigTester(self      ,							config_class=A_      ,							hidden_size=37					)
 def 	UpperCamelCase__  (							self					)					->Optional[Any]:



       '''simple docstring'''





       self.config_tester.run_common_tests()
 def 	UpperCamelCase__  (							self					)					->List[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_model(*A_					)
 def 	UpperCamelCase__  (							self					)					->int:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_sequence_classification(*A_					)
 def 	UpperCamelCase__  (							self					)					->int:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_masked_lm(*A_					)
 def 	UpperCamelCase__  (							self					)					->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase					:						List[Any]					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_question_answering(*A_					)
 def 	UpperCamelCase__  (							self					)					->List[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_token_classification(*A_					)





 @slow
 def 	UpperCamelCase__  (							self					)					->Tuple:



       '''simple docstring'''





       for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __lowerCAmelCase					:						Optional[int]					=					DebertaModel.from_pretrained(A_					)
             self.assertIsNotNone(A_					)







@require_torch
@require_sentencepiece
@require_tokenizers
class   __lowercase      (unittest.TestCase ):
 @unittest.skip(reason='''Model not available yet'''					)
 def 	UpperCamelCase__  (							self					)					->Dict:



       '''simple docstring'''





       pass





 @slow
 def 	UpperCamelCase__  (							self					)					->Tuple:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					DebertaModel.from_pretrained('''microsoft/deberta-base'''					)

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


