

'''simple docstring'''
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 						__magic_name__   (							__SCREAMING_SNAKE_CASE ):







 def __init__(       self  , snake_case_  , snake_case_=13  , snake_case_=7  , snake_case_=True  , snake_case_=True  , snake_case_=True  , snake_case_=True  , snake_case_=99  , snake_case_=32  , snake_case_=5  , snake_case_=4  , snake_case_=37  , snake_case_="gelu"  , snake_case_=0.1  , snake_case_=0.1  , snake_case_=5_12  , snake_case_=16  , snake_case_=2  , snake_case_=0.02  , snake_case_=False  , snake_case_=True  , snake_case_="None"  , snake_case_=3  , snake_case_=4  , snake_case_=None  , ):
   lowercase									=parent
   lowercase									=batch_size
   lowercase									=seq_length
   lowercase									=is_training
   lowercase									=use_input_mask
   lowercase									=use_token_type_ids
   lowercase									=use_labels
   lowercase									=vocab_size
   lowercase									=hidden_size
   lowercase									=num_hidden_layers
   lowercase									=num_attention_heads
   lowercase									=intermediate_size
   lowercase									=hidden_act
   lowercase									=hidden_dropout_prob
   lowercase									=attention_probs_dropout_prob
   lowercase									=max_position_embeddings
   lowercase									=type_vocab_size
   lowercase									=type_sequence_label_size
   lowercase									=initializer_range
   lowercase									=num_labels
   lowercase									=num_choices
   lowercase									=relative_attention
   lowercase									=position_biased_input
   lowercase									=pos_att_type
   lowercase									=scope







 def 						_A(       self   ):
   lowercase									=ids_tensor([self.batch_size, self.seq_length]  , self.vocab_size   )

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

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

   lowercase									=None
   lowercase									=None
   lowercase									=None
   if self.use_labels:
     lowercase									=ids_tensor([self.batch_size]  , self.type_sequence_label_size   )
     lowercase									=ids_tensor([self.batch_size, self.seq_length]  , self.num_labels   )
     lowercase									=ids_tensor([self.batch_size]  , self.num_choices   )

   lowercase									=self.get_config()

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







 def 						_A(       self   ):
   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 						_A(       self   ):
   lowercase									=self.get_config()
   lowercase									=3_00
   return config







 def 						_A(       self  , snake_case_   ):
   self.parent.assertListEqual(list(result.loss.size()   )  , []   )







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_   ):
   lowercase									=DebertaModel(config=snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(snake_case_  , attention_mask=snake_case_  , token_type_ids=snake_case_   )[0]
   lowercase									=model(snake_case_  , token_type_ids=snake_case_   )[0]
   lowercase									=model(snake_case_   )[0]

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







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_   ):
   lowercase									=DebertaForMaskedLM(config=snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(snake_case_  , attention_mask=snake_case_  , token_type_ids=snake_case_  , labels=snake_case_   )

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







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_   ):
   lowercase									=self.num_labels
   lowercase									=DebertaForSequenceClassification(snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(snake_case_  , attention_mask=snake_case_  , token_type_ids=snake_case_  , labels=snake_case_   )
   self.parent.assertListEqual(list(result.logits.size()   )  , [self.batch_size, self.num_labels]   )
   self.check_loss_output(snake_case_   )







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_   ):
   lowercase									=self.num_labels
   lowercase									=DebertaForTokenClassification(config=snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(snake_case_  , attention_mask=snake_case_  , token_type_ids=snake_case_  , labels=snake_case_   )
   self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.seq_length, self.num_labels)   )







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_   ):
   lowercase									=DebertaForQuestionAnswering(config=snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(
       snake_case_  , attention_mask=snake_case_  , token_type_ids=snake_case_  , start_positions=snake_case_  , end_positions=snake_case_  , )
   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   ):
   lowercase									=self.prepare_config_and_inputs()
   (
       (
       lowercase
   )							, (
       lowercase
   )							, (
       lowercase
   )							, (
       lowercase
   )							, (
       lowercase
   )							, (
       lowercase
   )							, (
       lowercase
   )							, 
   )									=config_and_inputs
   lowercase									={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
   return config, inputs_dict

@require_torch
class 						__magic_name__   (							__SCREAMING_SNAKE_CASE ,				__SCREAMING_SNAKE_CASE ,				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 						_A(       self   ):
   lowercase									=DebertaModelTester(self   )
   lowercase									=ConfigTester(self  , config_class=snake_case_  , hidden_size=37   )







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







 def 						_A(       self   ):
   lowercase									=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_model(*snake_case_   )







 def 						_A(       self   ):
   lowercase									=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case_   )







 def 						_A(       self   ):
   lowercase									=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case_   )







 def 						_A(       self   ):
   lowercase									=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_question_answering(*snake_case_   )







 def 						_A(       self   ):
   lowercase									=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_token_classification(*snake_case_   )







 @slow
 def 						_A(       self   ):
   for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowercase									=DebertaModel.from_pretrained(snake_case_   )
     self.assertIsNotNone(snake_case_   )

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







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







 @slow
 def 						_A(       self   ):
   lowercase									=DebertaModel.from_pretrained('''microsoft/deberta-base'''   )

   lowercase									=torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]   )
   lowercase									=torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
   with torch.no_grad():
     lowercase									=model(snake_case_  , attention_mask=snake_case_   )[0]
   # compare the actual values for a slice.
   lowercase									=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]  , snake_case_  , atol=1E-4   )  , f'{output[:, 1:4, 1:4]}'   )







'''simple docstring'''

import os
import tempfile
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
  import torch
  from torch import nn

  from transformers import (
      Adafactor,
      AdamW,
      get_constant_schedule,
      get_constant_schedule_with_warmup,
      get_cosine_schedule_with_warmup,
      get_cosine_with_hard_restarts_schedule_with_warmup,
      get_inverse_sqrt_schedule,
      get_linear_schedule_with_warmup,
      get_polynomial_decay_schedule_with_warmup,
  )





def 		UpperCamelCase							(				_lowerCamelCase   :   Any		,			_lowerCamelCase   :   Optional[Any]=10			):
    A__							=     []
    for _ in range(_lowerCamelCase			):
        lrs.append(scheduler.get_lr()[0]			)
        scheduler.step()
    return lrs





def 		UpperCamelCase							(				_lowerCamelCase   :   Tuple		,			_lowerCamelCase   :   List[str]=10			):
    A__							=     []
    for step in range(_lowerCamelCase			):
        lrs.append(scheduler.get_lr()[0]			)
        scheduler.step()
        if step == num_steps // 2:
            with tempfile.TemporaryDirectory() as tmpdirname:
                A__							=     os.path.join(_lowerCamelCase		,			"schedule.bin"			)
                torch.save(scheduler.state_dict()		,			_lowerCamelCase			)

                A__							=     torch.load(_lowerCamelCase			)
                scheduler.load_state_dict(_lowerCamelCase			)
    return lrs






@require_torch
class   UpperCAmelCase      (			unittest.TestCase							):
 def 	UpperCAmelCase_				(							self :int		,			lowercase_ :Any		,			lowercase_ :List[Any]		,			lowercase_ :Optional[Any]				)->				int:
     self.assertEqual(len(lowercase_				)		,			len(lowercase_				)				)
     for a, b in zip(lowercase_		,			lowercase_				):
         self.assertAlmostEqual(lowercase_		,			lowercase_		,			delta=lowercase_				)
 def 	UpperCAmelCase_				(							self :Union[str, Any]				)->				Dict:
     A__							=     torch.tensor([0.1, -0.2, -0.1]		,			requires_grad=lowercase_				)
     A__							=     torch.tensor([0.4, 0.2, -0.5]				)
     A__							=     nn.MSELoss()
     # No warmup, constant schedule, no gradient clipping
     A__							=     AdamW(params=[w]		,			lr=2E-1		,			weight_decay=0.0				)
     for _ in range(1_00				):
         A__							=     criterion(lowercase_		,			lowercase_				)
         loss.backward()
         optimizer.step()
         w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
         w.grad.zero_()
     self.assertListAlmostEqual(w.tolist()		,			[0.4, 0.2, -0.5]		,			tol=1E-2				)



 def 	UpperCAmelCase_				(							self :Tuple				)->				List[str]:
     A__							=     torch.tensor([0.1, -0.2, -0.1]		,			requires_grad=lowercase_				)
     A__							=     torch.tensor([0.4, 0.2, -0.5]				)
     A__							=     nn.MSELoss()
     # No warmup, constant schedule, no gradient clipping
     A__							=     Adafactor(
         params=[w]		,			lr=1E-2		,			eps=(1E-30, 1E-3)		,			clip_threshold=1.0		,			decay_rate=-0.8		,			betaa=lowercase_		,			weight_decay=0.0		,			relative_step=lowercase_		,			scale_parameter=lowercase_		,			warmup_init=lowercase_		,			)
     for _ in range(10_00				):
         A__							=     criterion(lowercase_		,			lowercase_				)
         loss.backward()
         optimizer.step()
         w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
         w.grad.zero_()
     self.assertListAlmostEqual(w.tolist()		,			[0.4, 0.2, -0.5]		,			tol=1E-2				)






@require_torch
class   UpperCAmelCase      (			unittest.TestCase							):
 __lowercase  						=							nn.Linear(50     ,			50							) if is_torch_available() else None
 __lowercase  						=							AdamW(m.parameters()     ,			lr=1_0.0							) if is_torch_available() else None
 __lowercase  						=							10
 def 	UpperCAmelCase_				(							self :Tuple		,			lowercase_ :Any		,			lowercase_ :List[Any]		,			lowercase_ :List[Any]		,			lowercase_ :str=None				)->				Optional[int]:
     self.assertEqual(len(lowercase_				)		,			len(lowercase_				)				)
     for a, b in zip(lowercase_		,			lowercase_				):
         self.assertAlmostEqual(lowercase_		,			lowercase_		,			delta=lowercase_		,			msg=lowercase_				)



 def 	UpperCAmelCase_				(							self :Optional[Any]				)->				Any:
     A__							=     {"num_warmup_steps": 2, "num_training_steps": 10}
     # schedulers doct format
     # function: (sched_args_dict, expected_learning_rates)
     A__							=     {
         get_constant_schedule: ({}, [1_0.0] * self.num_steps),
         get_constant_schedule_with_warmup: (
             {"num_warmup_steps": 4},
             [0.0, 2.5, 5.0, 7.5, 1_0.0, 1_0.0, 1_0.0, 1_0.0, 1_0.0, 1_0.0],
         ),
         get_linear_schedule_with_warmup: (
             {**common_kwargs},
             [0.0, 5.0, 1_0.0, 8.7_5, 7.5, 6.2_5, 5.0, 3.7_5, 2.5, 1.2_5],
         ),
         get_cosine_schedule_with_warmup: (
             {**common_kwargs},
             [0.0, 5.0, 1_0.0, 9.6_1, 8.5_3, 6.9_1, 5.0, 3.0_8, 1.4_6, 0.3_8],
         ),
         get_cosine_with_hard_restarts_schedule_with_warmup: (
             {**common_kwargs, "num_cycles": 2},
             [0.0, 5.0, 1_0.0, 8.5_3, 5.0, 1.4_6, 1_0.0, 8.5_3, 5.0, 1.4_6],
         ),
         get_polynomial_decay_schedule_with_warmup: (
             {**common_kwargs, "power": 2.0, "lr_end": 1E-7},
             [0.0, 5.0, 1_0.0, 7.6_5_6, 5.6_2_5, 3.9_0_6, 2.5, 1.4_0_6, 0.6_2_5, 0.1_5_6],
         ),
         get_inverse_sqrt_schedule: (
             {"num_warmup_steps": 2},
             [0.0, 5.0, 1_0.0, 8.1_6_5, 7.0_7_1, 6.3_2_5, 5.7_7_4, 5.3_4_5, 5.0, 4.7_1_4],
         ),
     }

     for scheduler_func, data in scheds.items():
         A__,      A__							=     data

         A__							=     scheduler_func(self.optimizer		,			**lowercase_				)
         self.assertEqual(len([scheduler.get_lr()[0]]				)		,			1				)
         A__							=     unwrap_schedule(lowercase_		,			self.num_steps				)
         self.assertListAlmostEqual(
             lowercase_		,			lowercase_		,			tol=1E-2		,			msg=F"failed for {scheduler_func} in normal scheduler"		,			)

         A__							=     scheduler_func(self.optimizer		,			**lowercase_				)
         if scheduler_func.__name__ != "get_constant_schedule":
             LambdaScheduleWrapper.wrap_scheduler(lowercase_				)  # wrap to test picklability of the schedule
         A__							=     unwrap_and_save_reload_schedule(lowercase_		,			self.num_steps				)
         self.assertListEqual(lowercase_		,			lowercase_		,			msg=F"failed for {scheduler_func} in save and reload"				)






class   UpperCAmelCase      :
 def __init__(							self :str		,			lowercase_ :List[str]				)->				Tuple:
     A__							=     fn
 def __call__(							self :List[Any]		,			*lowercase_ :Dict		,			**lowercase_ :Dict				)->				Tuple:
     return self.fn(*lowercase_		,			**lowercase_				)



 @classmethod
 def 	UpperCAmelCase_				(							self :Any		,			lowercase_ :Tuple				)->				List[Any]:
     A__							=     list(map(self		,			scheduler.lr_lambdas				)				)



