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_					(       UpperCamelCase_			):






    '''simple docstring'''






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







    def       UpperCamelCase__       (     self)	->     Any:
        __UpperCamelCase					:Tuple        =		ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size)

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

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

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

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

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







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







    def       UpperCamelCase__       (     self			,			__lowercase)	->     List[str]:
        self.parent.assertListEqual(list(result.loss.size())			,			[])







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase)	->     Dict:
        __UpperCamelCase					:Dict        =		DebertaModel(config=_a)
        model.to(_a)
        model.eval()
        __UpperCamelCase					:Any        =		model(_a			,			attention_mask=_a			,			token_type_ids=_a)[0]
        __UpperCamelCase					:Union[str, Any]        =		model(_a			,			token_type_ids=_a)[0]
        __UpperCamelCase					:Union[str, Any]        =		model(_a)[0]

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







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase)	->     Tuple:
        __UpperCamelCase					:Optional[Any]        =		DebertaForMaskedLM(config=_a)
        model.to(_a)
        model.eval()
        __UpperCamelCase					: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       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase)	->     Dict:
        __UpperCamelCase					:List[Any]        =		self.num_labels
        __UpperCamelCase					:Tuple        =		DebertaForSequenceClassification(_a)
        model.to(_a)
        model.eval()
        __UpperCamelCase					: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			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase)	->     List[Any]:
        __UpperCamelCase					:List[Any]        =		self.num_labels
        __UpperCamelCase					:Optional[Any]        =		DebertaForTokenClassification(config=_a)
        model.to(_a)
        model.eval()
        __UpperCamelCase					: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       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase)	->     Union[str, Any]:
        __UpperCamelCase					:Optional[Any]        =		DebertaForQuestionAnswering(config=_a)
        model.to(_a)
        model.eval()
        __UpperCamelCase					:List[str]        =		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)	->     Dict:
        __UpperCamelCase					:Optional[int]        =		self.prepare_config_and_inputs()
        (
            __UpperCamelCase						
        )					:Optional[int]        =		config_and_inputs
        __UpperCamelCase					:List[Any]        =		{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
        return config, inputs_dict


@require_torch
class      lowerCamelCase_					(       UpperCamelCase_						,     UpperCamelCase_						,     unittest.TestCase			):






    '''simple docstring'''






    a__     :						List[str]      =				(
        (
            DebertaModel,
            DebertaForMaskedLM,
            DebertaForSequenceClassification,
            DebertaForTokenClassification,
            DebertaForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    a__     :						str      =				(
        {
            """feature-extraction""": DebertaModel,
            """fill-mask""": DebertaForMaskedLM,
            """question-answering""": DebertaForQuestionAnswering,
            """text-classification""": DebertaForSequenceClassification,
            """token-classification""": DebertaForTokenClassification,
            """zero-shot""": DebertaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )

    a__     :						Union[str, Any]      =				True
    a__     :						str      =				False
    a__     :						Dict      =				False
    a__     :						Dict      =				False
    a__     :						Union[str, Any]      =				False







    def       UpperCamelCase__       (     self)	->     Optional[Any]:
        __UpperCamelCase					:List[Any]        =		DebertaModelTester(self)
        __UpperCamelCase					:str        =		ConfigTester(self			,			config_class=_a			,			hidden_size=37)







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







    def       UpperCamelCase__       (     self)	->     int:
        __UpperCamelCase					:List[str]        =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_model(*_a)







    def       UpperCamelCase__       (     self)	->     Optional[Any]:
        __UpperCamelCase					:Union[str, Any]        =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_sequence_classification(*_a)







    def       UpperCamelCase__       (     self)	->     Tuple:
        __UpperCamelCase					:Any        =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_masked_lm(*_a)







    def       UpperCamelCase__       (     self)	->     Dict:
        __UpperCamelCase					:str        =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_question_answering(*_a)







    def       UpperCamelCase__       (     self)	->     Union[str, Any]:
        __UpperCamelCase					:int        =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_token_classification(*_a)







    @slow
    def       UpperCamelCase__       (     self)	->     Optional[int]:


        for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __UpperCamelCase					:Dict        =		DebertaModel.from_pretrained(_a)
            self.assertIsNotNone(_a)


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






    '''simple docstring'''






    @unittest.skip(reason='''Model not available yet''')
    def       UpperCamelCase__       (     self)	->     Any:
        pass







    @slow
    def       UpperCamelCase__       (     self)	->     int:
        __UpperCamelCase					:List[str]        =		DebertaModel.from_pretrained('''microsoft/deberta-base''')

        __UpperCamelCase					:Tuple        =		torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]])
        __UpperCamelCase					:Union[str, Any]        =		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
        with torch.no_grad():
            __UpperCamelCase					:Optional[int]        =		model(_a			,			attention_mask=_a)[0]
        # compare the actual values for a slice.
        __UpperCamelCase					:Union[str, Any]        =		torch.tensor(
            [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]])
        self.assertTrue(torch.allclose(output[:, 1:4, 1:4]			,			_a			,			atol=1E-4)			,			f"""{output[:, 1:4, 1:4]}""")






import importlib
import sys
from argparse import REMAINDER, ArgumentParser
from pathlib import Path

import torch_xla.distributed.xla_multiprocessing as xmp



def lowerCamelCase  (       ):
    '''simple docstring'''







    __UpperCamelCase					:int        =		ArgumentParser(
        description=(
            '''PyTorch TPU distributed training launch '''
            '''helper utility that will spawn up '''
            '''multiple distributed processes'''
        )     )

    # Optional arguments for the launch helper
    parser.add_argument('''--num_cores'''  ,      type=SCREAMING_SNAKE_CASE  ,      default=1  ,      help='''Number of TPU cores to use (1 or 8).'''     )

    # positional
    parser.add_argument(
        '''training_script'''  ,      type=SCREAMING_SNAKE_CASE  ,      help=(
            '''The full path to the single TPU training '''
            '''program/script to be launched in parallel, '''
            '''followed by all the arguments for the '''
            '''training script'''
        )  ,      )

    # rest from the training program
    parser.add_argument('''training_script_args'''  ,      nargs=SCREAMING_SNAKE_CASE     )

    return parser.parse_args()



def lowerCamelCase  (       ):
    '''simple docstring'''







    __UpperCamelCase					:Tuple        =		parse_args()

    # Import training_script as a module.
    __UpperCamelCase					:Dict        =		Path(args.training_script     )
    sys.path.append(str(script_fpath.parent.resolve()     )     )
    __UpperCamelCase					:Any        =		script_fpath.stem
    __UpperCamelCase					:Union[str, Any]        =		importlib.import_module(SCREAMING_SNAKE_CASE     )

    # Patch sys.argv
    __UpperCamelCase					:Dict        =		[args.training_script] + args.training_script_args + ['''--tpu_num_cores''', str(args.num_cores     )]
    xmp.spawn(mod._mp_fn  ,      args=()  ,      nprocs=args.num_cores     )


if __name__ == "__main__":
    main()





