"""simple docstring"""




import unittest

from transformers import DebertaVaConfig, 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 (
         DebertaVaForMaskedLM,
         DebertaVaForMultipleChoice,
         DebertaVaForQuestionAnswering,
         DebertaVaForSequenceClassification,
         DebertaVaForTokenClassification,
         DebertaVaModel,
     )
     from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST
class 						__A							( SCREAMING_SNAKE_CASE_		):






     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.0_2				,     a__=False				,     a__=True				,     a__="None"				,     a__=3				,     a__=4				,     a__=None				,     ):
       _lowerCAmelCase						:       Dict  			=     parent
       _lowerCAmelCase						:       str  			=     batch_size
       _lowerCAmelCase						:       List[Any]  			=     seq_length
       _lowerCAmelCase						:       Dict  			=     is_training
       _lowerCAmelCase						:       Dict  			=     use_input_mask
       _lowerCAmelCase						:       int  			=     use_token_type_ids
       _lowerCAmelCase						:       int  			=     use_labels
       _lowerCAmelCase						:       Optional[int]  			=     vocab_size
       _lowerCAmelCase						:       Tuple  			=     hidden_size
       _lowerCAmelCase						:       Tuple  			=     num_hidden_layers
       _lowerCAmelCase						:       Dict  			=     num_attention_heads
       _lowerCAmelCase						:       Union[str, Any]  			=     intermediate_size
       _lowerCAmelCase						:       str  			=     hidden_act
       _lowerCAmelCase						:       Any  			=     hidden_dropout_prob
       _lowerCAmelCase						:       List[str]  			=     attention_probs_dropout_prob
       _lowerCAmelCase						:       List[str]  			=     max_position_embeddings
       _lowerCAmelCase						:       List[str]  			=     type_vocab_size
       _lowerCAmelCase						:       Tuple  			=     type_sequence_label_size
       _lowerCAmelCase						:       List[Any]  			=     initializer_range
       _lowerCAmelCase						:       Union[str, Any]  			=     num_labels
       _lowerCAmelCase						:       Optional[Any]  			=     num_choices
       _lowerCAmelCase						:       Tuple  			=     relative_attention
       _lowerCAmelCase						:       Tuple  			=     position_biased_input
       _lowerCAmelCase						:       Dict  			=     pos_att_type
       _lowerCAmelCase						:       Any  			=     scope






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

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

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

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

       _lowerCAmelCase						:       List[str]  			=     self.get_config()

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






     def     __A  (		self  ):
       return DebertaVaConfig(
           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     __A  (		self				,     a__  ):
       self.parent.assertListEqual(list(result.loss.size()  )				,     []  )






     def     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__  ):
       _lowerCAmelCase						:       Union[str, Any]  			=     DebertaVaModel(config=a__  )
       model.to(a__  )
       model.eval()
       _lowerCAmelCase						:       List[Any]  			=     model(a__				,     attention_mask=a__				,     token_type_ids=a__  )[0]
       _lowerCAmelCase						:       List[Any]  			=     model(a__				,     token_type_ids=a__  )[0]
       _lowerCAmelCase						:       Any  			=     model(a__  )[0]

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






     def     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__  ):
       _lowerCAmelCase						:       List[str]  			=     DebertaVaForMaskedLM(config=a__  )
       model.to(a__  )
       model.eval()
       _lowerCAmelCase						:       Optional[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     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__  ):
       _lowerCAmelCase						:       int  			=     self.num_labels
       _lowerCAmelCase						:       int  			=     DebertaVaForSequenceClassification(a__  )
       model.to(a__  )
       model.eval()
       _lowerCAmelCase						:       Optional[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     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__  ):
       _lowerCAmelCase						:       List[Any]  			=     self.num_labels
       _lowerCAmelCase						:       str  			=     DebertaVaForTokenClassification(config=a__  )
       model.to(a__  )
       model.eval()
       _lowerCAmelCase						:       Optional[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     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__  ):
       _lowerCAmelCase						:       Any  			=     DebertaVaForQuestionAnswering(config=a__  )
       model.to(a__  )
       model.eval()
       _lowerCAmelCase						:       Dict  			=     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     __A  (		self				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__				,     a__  ):
       _lowerCAmelCase						:       Union[str, Any]  			=     DebertaVaForMultipleChoice(config=a__  )
       model.to(a__  )
       model.eval()
       _lowerCAmelCase						:       Optional[int]  			=     input_ids.unsqueeze(1  ).expand(-1				,     self.num_choices				,     -1  ).contiguous()
       _lowerCAmelCase						:       Optional[Any]  			=     token_type_ids.unsqueeze(1  ).expand(-1				,     self.num_choices				,     -1  ).contiguous()
       _lowerCAmelCase						:       str  			=     input_mask.unsqueeze(1  ).expand(-1				,     self.num_choices				,     -1  ).contiguous()
       _lowerCAmelCase						:       List[str]  			=     model(
           a__				,     attention_mask=a__				,     token_type_ids=a__				,     labels=a__				,     )
       self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices)  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       Tuple  			=     self.prepare_config_and_inputs()
       (
           (
           _lowerCAmelCase
       )			,	(
           _lowerCAmelCase
       )			,	(
           _lowerCAmelCase
       )			,	(
           _lowerCAmelCase
       )			,	(
           _lowerCAmelCase
       )			,	(
           _lowerCAmelCase
       )			,	(
           _lowerCAmelCase
       )			,	
       )						:       Union[str, Any]  			=     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 						__A							( SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_    ,	unittest.TestCase		):
     _UpperCamelCase							:			Union[str, Any]							=		(
         (
             DebertaVaModel,
             DebertaVaForMaskedLM,
             DebertaVaForSequenceClassification,
             DebertaVaForTokenClassification,
             DebertaVaForQuestionAnswering,
             DebertaVaForMultipleChoice,
         )
         if is_torch_available()
         else ()
     )
     _UpperCamelCase							:			str							=		(
         {
             "feature-extraction": DebertaVaModel,
             "fill-mask": DebertaVaForMaskedLM,
             "question-answering": DebertaVaForQuestionAnswering,
             "text-classification": DebertaVaForSequenceClassification,
             "token-classification": DebertaVaForTokenClassification,
             "zero-shot": DebertaVaForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )

     _UpperCamelCase							:			Optional[Any]							=		True
     _UpperCamelCase							:			List[Any]							=		False
     _UpperCamelCase							:			List[Any]							=		False
     _UpperCamelCase							:			Dict							=		False
     _UpperCamelCase							:			Tuple							=		False






     def     __A  (		self  ):
       _lowerCAmelCase						:       Optional[Any]  			=     DebertaVaModelTester(self  )
       _lowerCAmelCase						:       Any  			=     ConfigTester(self				,     config_class=a__				,     hidden_size=37  )






     def     __A  (		self  ):
       self.config_tester.run_common_tests()






     def     __A  (		self  ):
       _lowerCAmelCase						:       Optional[int]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_model(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[str]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_sequence_classification(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       Any  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_masked_lm(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[Any]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_question_answering(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[Any]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_token_classification(*a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       Union[str, Any]  			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_multiple_choice(*a__  )






     @slow
     def     __A  (		self  ):
       for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowerCAmelCase						:       Tuple  			=     DebertaVaModel.from_pretrained(a__  )
         self.assertIsNotNone(a__  )







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






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






     @slow
     def     __A  (		self  ):
       _lowerCAmelCase						:       Union[str, Any]  			=     DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge"""  )

       _lowerCAmelCase						:       Dict  			=     torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]  )
       _lowerCAmelCase						:       Any  			=     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
       with torch.no_grad():
         _lowerCAmelCase						:       Optional[Any]  			=     model(a__				,     attention_mask=a__  )[0]
       # compare the actual values for a slice.
       _lowerCAmelCase						:       str  			=     torch.tensor(
           [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]  )
       self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,     a__				,     atol=1e-4  )				,     F"{output[:, 1:4, 1:4]}"  )





"""simple docstring"""




import pickle
import shutil
import tempfile
import unittest

from transformers import SPIECE_UNDERLINE, XGLMTokenizer, XGLMTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


_a      :		Optional[int]           =   get_tests_dir('fixtures/test_sentencepiece.model')
@require_sentencepiece
@require_tokenizers
class 						__A							( SCREAMING_SNAKE_CASE_    ,	unittest.TestCase		):
     _UpperCamelCase							:			List[Any]							=		XGLMTokenizer
     _UpperCamelCase							:			List[Any]							=		XGLMTokenizerFast
     _UpperCamelCase							:			Dict							=		True
     _UpperCamelCase							:			Tuple							=		True






     def     __A  (		self  ):
       super().setUp()

       # We have a SentencePiece fixture for testing
       _lowerCAmelCase						:       List[Any]  			=     XGLMTokenizer(a__				,     keep_accents=a__  )
       tokenizer.save_pretrained(self.tmpdirname  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[str]  			=     """<pad>"""
       _lowerCAmelCase						:       List[Any]  			=     1

       self.assertEqual(self.get_tokenizer()._convert_token_to_id(a__  )				,     a__  )
       self.assertEqual(self.get_tokenizer()._convert_id_to_token(a__  )				,     a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       Optional[int]  			=     list(self.get_tokenizer().get_vocab().keys()  )

       self.assertEqual(vocab_keys[0]				,     """<s>"""  )
       self.assertEqual(vocab_keys[1]				,     """<pad>"""  )
       self.assertEqual(len(a__  )				,     1008  )






     def     __A  (		self  ):
       self.assertEqual(self.get_tokenizer().vocab_size				,     1008  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       List[Any]  			=     XGLMTokenizer(a__				,     keep_accents=a__  )

       _lowerCAmelCase						:       Dict  			=     tokenizer.tokenize("""This is a test"""  )
       self.assertListEqual(a__				,     ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""]  )

       self.assertListEqual(
           tokenizer.convert_tokens_to_ids(a__  )				,     [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]				,     )

       _lowerCAmelCase						:       Any  			=     tokenizer.tokenize("""I was born in 92000, and this is falsé."""  )
       self.assertListEqual(
           a__				,     [
               SPIECE_UNDERLINE + """I""",
               SPIECE_UNDERLINE + """was""",
               SPIECE_UNDERLINE + """b""",
               """or""",
               """n""",
               SPIECE_UNDERLINE + """in""",
               SPIECE_UNDERLINE + """""",
               """9""",
               """2""",
               """0""",
               """0""",
               """0""",
               """,""",
               SPIECE_UNDERLINE + """and""",
               SPIECE_UNDERLINE + """this""",
               SPIECE_UNDERLINE + """is""",
               SPIECE_UNDERLINE + """f""",
               """al""",
               """s""",
               """é""",
               """.""",
           ]				,     )
       _lowerCAmelCase						:       List[str]  			=     tokenizer.convert_tokens_to_ids(a__  )
       self.assertListEqual(
           a__				,     [
               value + tokenizer.fairseq_offset
               for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
           ]				,     )

       _lowerCAmelCase						:       Optional[int]  			=     tokenizer.convert_ids_to_tokens(a__  )
       self.assertListEqual(
           a__				,     [
               SPIECE_UNDERLINE + """I""",
               SPIECE_UNDERLINE + """was""",
               SPIECE_UNDERLINE + """b""",
               """or""",
               """n""",
               SPIECE_UNDERLINE + """in""",
               SPIECE_UNDERLINE + """""",
               """<unk>""",
               """2""",
               """0""",
               """0""",
               """0""",
               """,""",
               SPIECE_UNDERLINE + """and""",
               SPIECE_UNDERLINE + """this""",
               SPIECE_UNDERLINE + """is""",
               SPIECE_UNDERLINE + """f""",
               """al""",
               """s""",
               """<unk>""",
               """.""",
           ]				,     )






     @cached_property
     def     __A  (		self  ):
       return XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""  )






     def     __A  (		self  ):
       with tempfile.NamedTemporaryFile() as f:
         shutil.copyfile(a__				,     f.name  )
         _lowerCAmelCase						:       Union[str, Any]  			=     XGLMTokenizer(f.name				,     keep_accents=a__  )
         _lowerCAmelCase						:       List[str]  			=     pickle.dumps(a__  )
       pickle.loads(a__  )






     def     __A  (		self  ):
       if not self.test_rust_tokenizer:
         return

       _lowerCAmelCase						:       List[str]  			=     self.get_tokenizer()
       _lowerCAmelCase						:       Optional[Any]  			=     self.get_rust_tokenizer()

       _lowerCAmelCase						:       Tuple  			=     """I was born in 92000, and this is falsé."""

       _lowerCAmelCase						:       List[Any]  			=     tokenizer.tokenize(a__  )
       _lowerCAmelCase						:       Tuple  			=     rust_tokenizer.tokenize(a__  )
       self.assertListEqual(a__				,     a__  )

       _lowerCAmelCase						:       Union[str, Any]  			=     tokenizer.encode(a__				,     add_special_tokens=a__  )
       _lowerCAmelCase						:       str  			=     rust_tokenizer.encode(a__				,     add_special_tokens=a__  )
       self.assertListEqual(a__				,     a__  )

       _lowerCAmelCase						:       int  			=     self.get_rust_tokenizer()
       _lowerCAmelCase						:       Dict  			=     tokenizer.encode(a__  )
       _lowerCAmelCase						:       List[Any]  			=     rust_tokenizer.encode(a__  )
       self.assertListEqual(a__				,     a__  )






     @slow
     def     __A  (		self  ):
       _lowerCAmelCase						:       int  			=     """Hello World!"""
       _lowerCAmelCase						:       Optional[int]  			=     [2, 31227, 4447, 35]

       self.assertListEqual(a__				,     self.big_tokenizer.encode(a__  )  )






     @slow
     def     __A  (		self  ):
       _lowerCAmelCase						:       Any  			=     (
           """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will"""
           """ add words that should not exsist and be tokenized to unk, such as saoneuhaoesuth"""
       )
       # fmt: off
       _lowerCAmelCase						:       List[str]  			=     [2, 1018, 67, 11, 1988, 2617, 5631, 278, 11, 3407, 48, 71630, 28085, 4, 3234, 157, 13, 6, 5, 6, 4, 3526, 768, 15, 659, 57, 298, 3983, 864, 129, 21, 6, 5, 13675, 377, 652, 7580, 10341, 155, 2817, 422, 1666, 7, 1674, 53, 113, 202277, 17892, 33, 60, 87, 4, 3234, 157, 61, 2667, 52376, 19, 88, 23, 735]
       # fmt: on

       self.assertListEqual(a__				,     self.big_tokenizer.encode(a__  )  )






     @slow
     def     __A  (		self  ):
       # fmt: off
       _lowerCAmelCase						:       List[str]  			=     {
           """input_ids""": [[2, 108825, 1163, 15, 88010, 473, 15898, 157, 13672, 1857, 312, 8, 238021, 1163, 53, 13672, 1857, 312, 8, 53283, 182396, 8, 18566, 16, 36733, 4101, 8, 230, 244017, 122553, 7, 15, 132597, 4, 293, 12511, 7610, 4, 3414, 132597, 9, 4, 32361, 362, 4, 734, 28512, 32569, 18, 4, 32361, 26096, 14982, 73, 18715, 21433, 235261, 15, 492, 12427, 16, 53, 18715, 21433, 65454, 15, 23659, 563, 16, 278, 597, 2843, 595, 7931, 182396, 64186, 22, 886, 595, 132981, 53, 25540, 3449, 43982, 39901, 5951, 878, 330, 4, 27694, 80269, 312, 53, 6517, 11780, 611, 20408, 5], [2, 6, 132597, 67, 42897, 33, 592, 8, 163729, 25540, 361, 136997, 109514, 173230, 7, 501, 60, 102913, 196, 5631, 235, 63243, 473, 6, 231757, 74, 5277, 7905, 53, 3095, 37317, 22, 454, 183874, 5], [2, 268, 31298, 46530, 6, 132935, 43831, 7, 597, 32, 24, 3688, 9865, 5]],
           """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
       }  # noqa: E501
       # fmt: on

       self.tokenizer_integration_test_util(
           expected_encoding=a__				,     model_name="""facebook/xglm-564M"""				,     padding=a__				,     )




