

"""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   __lowercase      (     __lowerCamelCase       ):

  def __init__(	self		:      List[Any]       ,A		:      Optional[Any]       ,A		:      int=13       ,A		:      Union[str, Any]=7       ,A		:      Union[str, Any]=True       ,A		:      Dict=True       ,A		:      int=True       ,A		:      Optional[Any]=True       ,A		:      str=99       ,A		:      Union[str, Any]=32       ,A		:      Optional[int]=5       ,A		:      Optional[int]=4       ,A		:      str=37       ,A		:      str="gelu"       ,A		:      Optional[int]=0.1       ,A		:      Optional[Any]=0.1       ,A		:      Optional[int]=512       ,A		:      Tuple=16       ,A		:      Dict=2       ,A		:      Union[str, Any]=0.0_2       ,A		:      int=False       ,A		:      Tuple=True       ,A		:      List[str]="None"       ,A		:      Optional[int]=3       ,A		:      Union[str, Any]=4       ,A		:      Dict=None       ,):






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

  def  __lowercase							(	self		:      Any     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Union[str, Any]        =       ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size     )

   UpperCAmelCase__       :	Optional[int]        =       None
   if self.use_input_mask:
    UpperCAmelCase__       :	Dict        =       ids_tensor([self.batch_size, self.seq_length]       ,vocab_size=2     )

   UpperCAmelCase__       :	List[Any]        =       None
   if self.use_token_type_ids:
    UpperCAmelCase__       :	Optional[int]        =       ids_tensor([self.batch_size, self.seq_length]       ,self.type_vocab_size     )

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

   UpperCAmelCase__       :	Any        =       self.get_config()

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

  def  __lowercase							(	self		:      int     ):






   '''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  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       self.get_config()
   UpperCAmelCase__       :	Any        =       300
   return config

  def  __lowercase							(	self		:      List[Any]       ,A		:      str     ):






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

  def  __lowercase							(	self		:      List[Any]       ,A		:      int       ,A		:      str       ,A		:      List[str]       ,A		:      Tuple       ,A		:      Union[str, Any]       ,A		:      Union[str, Any]       ,A		:      Optional[int]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	int        =       DebertaModel(config=__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	Any        =       model(__a       ,attention_mask=__a       ,token_type_ids=__a     )[0]
   UpperCAmelCase__       :	Optional[int]        =       model(__a       ,token_type_ids=__a     )[0]
   UpperCAmelCase__       :	Optional[int]        =       model(__a     )[0]

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

  def  __lowercase							(	self		:      Union[str, Any]       ,A		:      Optional[Any]       ,A		:      Tuple       ,A		:      Dict       ,A		:      Dict       ,A		:      int       ,A		:      int       ,A		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	int        =       DebertaForMaskedLM(config=__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	str        =       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  __lowercase							(	self		:      Optional[int]       ,A		:      Optional[int]       ,A		:      Union[str, Any]       ,A		:      Optional[int]       ,A		:      Tuple       ,A		:      List[Any]       ,A		:      Tuple       ,A		:      List[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	int        =       self.num_labels
   UpperCAmelCase__       :	Tuple        =       DebertaForSequenceClassification(__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	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  __lowercase							(	self		:      Optional[int]       ,A		:      Any       ,A		:      Dict       ,A		:      int       ,A		:      Dict       ,A		:      Any       ,A		:      Union[str, Any]       ,A		:      Any     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Dict        =       self.num_labels
   UpperCAmelCase__       :	List[Any]        =       DebertaForTokenClassification(config=__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	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  __lowercase							(	self		:      int       ,A		:      Any       ,A		:      str       ,A		:      str       ,A		:      Optional[Any]       ,A		:      List[Any]       ,A		:      List[Any]       ,A		:      List[str]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       DebertaForQuestionAnswering(config=__a     )
   model.to(__a     )
   model.eval()
   UpperCAmelCase__       :	Any        =       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  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Union[str, Any]        =       self.prepare_config_and_inputs()
   (
       (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,   (
       UpperCAmelCase__      
   ) ,         
   )       :	Tuple        =       config_and_inputs
   UpperCAmelCase__       :	Union[str, Any]        =       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
   return config, inputs_dict







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

  snake_case_												=							True
  snake_case_												=							False
  snake_case_												=							False
  snake_case_												=							False
  snake_case_												=							False

  def  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       DebertaModelTester(self     )
   UpperCAmelCase__       :	Dict        =       ConfigTester(self       ,config_class=__a       ,hidden_size=37     )

  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   self.config_tester.run_common_tests()

  def  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Union[str, Any]        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_model(*__a     )

  def  __lowercase							(	self		:      List[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_sequence_classification(*__a     )

  def  __lowercase							(	self		:      Any     ):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_masked_lm(*__a     )

  def  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_question_answering(*__a     )

  def  __lowercase							(	self		:      Any     ):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_deberta_for_token_classification(*__a     )




  @slow
  def  __lowercase							(	self		:      List[str]     ):






   '''simple docstring'''
   for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    UpperCAmelCase__       :	List[str]        =       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  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   pass




  @slow
  def  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[int]        =       DebertaModel.from_pretrained("""microsoft/deberta-base"""     )

   UpperCAmelCase__       :	Optional[int]        =       torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]     )
   UpperCAmelCase__       :	Optional[int]        =       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
   with torch.no_grad():
    UpperCAmelCase__       :	str        =       model(__a       ,attention_mask=__a     )[0]
   # compare the actual values for a slice.
   UpperCAmelCase__       :	List[str]        =       torch.tensor(
       [[[-0.5_9_8_6, -0.8_0_5_5, -0.8_4_6_2], [1.4_4_8_4, -0.9_3_4_8, -0.8_0_5_9], [0.3_1_2_3, 0.0_0_3_2, -1.4_1_3_1]]]     )
   self.assertTrue(torch.allclose(output[:, 1:4, 1:4]       ,__a       ,atol=1e-4     )       ,f"{output[:, 1:4, 1:4]}"     )






'''simple docstring'''


import math
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils import SchedulerMixin, SchedulerOutput






class     A ( UpperCAmelCase							,						UpperCAmelCase ):
							a_										=   1





							@register_to_config
							def __init__(   self					:				Tuple  ,		__a					:				int = 1_0_0_0  ,		__a					:				Optional[Union[np.ndarray, List[float]]] = None						)				->						int:
								# set `betas`, `alphas`, `timesteps`
								self.set_timesteps(__a						)

								# standard deviation of the initial noise distribution
								__UpperCAmelCase      					=      1.0

								# For now we only support F-PNDM, i.e. the runge-kutta method
								# For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf
								# mainly at formula (9), (12), (13) and the Algorithm 2.
								__UpperCAmelCase      					=      4

								# running values
								__UpperCAmelCase      					=      []





							def    snake_case__    (   self					:				str  ,		__a					:				int  ,		__a					:				Union[str, torch.device] = None						)				->						str:
								__UpperCAmelCase      					=      num_inference_steps
								__UpperCAmelCase      					=      torch.linspace(1  ,		0  ,		num_inference_steps + 1						)[:-1]
								__UpperCAmelCase      					=      torch.cat([steps, torch.tensor([0.0]						)]						)

								if self.config.trained_betas is not None:
									__UpperCAmelCase      					=      torch.tensor(self.config.trained_betas  ,		dtype=torch.floataa						)
								else:
									__UpperCAmelCase      					=      torch.sin(steps * math.pi / 2						) ** 2

								__UpperCAmelCase      					=      (1.0 - self.betas**2) ** 0.5

								__UpperCAmelCase      					=      (torch.atana(self.betas  ,		self.alphas						) / math.pi * 2)[:-1]
								__UpperCAmelCase      					=      timesteps.to(__a						)

								__UpperCAmelCase      					=      []





							def    snake_case__    (   self					:				List[Any]  ,		__a					:				torch.FloatTensor  ,		__a					:				int  ,		__a					:				torch.FloatTensor  ,		__a					:				bool = True  ,		)				->						Union[SchedulerOutput, Tuple]:
								if self.num_inference_steps is None:
									raise ValueError(
									    '''Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler'''						)

								__UpperCAmelCase      					=      (self.timesteps == timestep).nonzero().item()
								__UpperCAmelCase      					=      timestep_index + 1

								__UpperCAmelCase      					=      sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index]
								self.ets.append(__a						)

								if len(self.ets						) == 1:
									__UpperCAmelCase      					=      self.ets[-1]
								elif len(self.ets						) == 2:
									__UpperCAmelCase      					=      (3 * self.ets[-1] - self.ets[-2]) / 2
								elif len(self.ets						) == 3:
									__UpperCAmelCase      					=      (2_3 * self.ets[-1] - 1_6 * self.ets[-2] + 5 * self.ets[-3]) / 1_2
								else:
									__UpperCAmelCase      					=      (1 / 2_4) * (5_5 * self.ets[-1] - 5_9 * self.ets[-2] + 3_7 * self.ets[-3] - 9 * self.ets[-4])

								__UpperCAmelCase      					=      self._get_prev_sample(__a  ,		__a  ,		__a  ,		__a						)

								if not return_dict:
									return (prev_sample,)

								return SchedulerOutput(prev_sample=__a						)





							def    snake_case__    (   self					:				List[Any]  ,		__a					:				torch.FloatTensor  ,		*__a					:				Any  ,		**__a					:				Optional[int]						)				->						torch.FloatTensor:
								return sample





							def    snake_case__    (   self					:				int  ,		__a					:				List[Any]  ,		__a					:				Any  ,		__a					:				int  ,		__a					:				Union[str, Any]						)				->						Optional[Any]:
								__UpperCAmelCase      					=      self.alphas[timestep_index]
								__UpperCAmelCase      					=      self.betas[timestep_index]

								__UpperCAmelCase      					=      self.alphas[prev_timestep_index]
								__UpperCAmelCase      					=      self.betas[prev_timestep_index]

								__UpperCAmelCase      					=      (sample - sigma * ets) / max(__a  ,		1e-8						)
								__UpperCAmelCase      					=      next_alpha * pred + ets * next_sigma

								return prev_sample






							def __len__(   self					:				Dict						)				->						str:
								return self.config.num_train_timesteps
