




'''simple docstring'''



import unittest

from transformers import 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
     OpenAIGPTConfig,
     OpenAIGPTDoubleHeadsModel,
     OpenAIGPTForSequenceClassification,
     OpenAIGPTLMHeadModel,
     OpenAIGPTModel,
 )







class 		lowercase_   :


   """simple docstring"""

   def __init__(   self :     Dict  ,				__lowerCamelCase :     Optional[int]  ,				__lowerCamelCase :     Any=1_3  ,				__lowerCamelCase :     Optional[Any]=7  ,				__lowerCamelCase :     List[str]=True  ,				__lowerCamelCase :     Union[str, Any]=True  ,				__lowerCamelCase :     Union[str, Any]=True  ,				__lowerCamelCase :     Union[str, Any]=9_9  ,				__lowerCamelCase :     Optional[Any]=3_2  ,				__lowerCamelCase :     Any=5  ,				__lowerCamelCase :     Union[str, Any]=4  ,				__lowerCamelCase :     Dict=3_7  ,				__lowerCamelCase :     Any="gelu"  ,				__lowerCamelCase :     Dict=0.1  ,				__lowerCamelCase :     List[str]=0.1  ,				__lowerCamelCase :     Any=5_1_2  ,				__lowerCamelCase :     List[Any]=1_6  ,				__lowerCamelCase :     int=2  ,				__lowerCamelCase :     List[Any]=0.0_2  ,				__lowerCamelCase :     Tuple=3  ,				__lowerCamelCase :     Any=4  ,				__lowerCamelCase :     str=None  ,				):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				parent
      _SCREAMING_SNAKE_CASE	=				batch_size
      _SCREAMING_SNAKE_CASE	=				seq_length
      _SCREAMING_SNAKE_CASE	=				is_training
      _SCREAMING_SNAKE_CASE	=				use_token_type_ids
      _SCREAMING_SNAKE_CASE	=				use_labels
      _SCREAMING_SNAKE_CASE	=				vocab_size
      _SCREAMING_SNAKE_CASE	=				hidden_size
      _SCREAMING_SNAKE_CASE	=				num_hidden_layers
      _SCREAMING_SNAKE_CASE	=				num_attention_heads
      _SCREAMING_SNAKE_CASE	=				intermediate_size
      _SCREAMING_SNAKE_CASE	=				hidden_act
      _SCREAMING_SNAKE_CASE	=				hidden_dropout_prob
      _SCREAMING_SNAKE_CASE	=				attention_probs_dropout_prob
      _SCREAMING_SNAKE_CASE	=				max_position_embeddings
      _SCREAMING_SNAKE_CASE	=				type_vocab_size
      _SCREAMING_SNAKE_CASE	=				type_sequence_label_size
      _SCREAMING_SNAKE_CASE	=				initializer_range
      _SCREAMING_SNAKE_CASE	=				num_labels
      _SCREAMING_SNAKE_CASE	=				num_choices
      _SCREAMING_SNAKE_CASE	=				scope
      _SCREAMING_SNAKE_CASE	=				self.vocab_size - 1



   def 							lowerCAmelCase_      (   self :     Optional[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				ids_tensor([self.batch_size, self.seq_length]  ,				self.vocab_size   )

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

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

      _SCREAMING_SNAKE_CASE	=				OpenAIGPTConfig(
          vocab_size=self.vocab_size  ,				n_embd=self.hidden_size  ,				n_layer=self.num_hidden_layers  ,				n_head=self.num_attention_heads  ,				n_positions=self.max_position_embeddings  ,				pad_token_id=self.pad_token_id  ,				)

      _SCREAMING_SNAKE_CASE	=				ids_tensor([self.num_hidden_layers, self.num_attention_heads]  ,				2   )

      return (
          config,
          input_ids,
          head_mask,
          token_type_ids,
          sequence_labels,
          token_labels,
          choice_labels,
      )



   def 							lowerCAmelCase_      (   self :     List[str]  ,				__lowerCamelCase :     Dict  ,				__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     Optional[Any]  ,				__lowerCamelCase :     str  ,				*__lowerCamelCase :     Dict   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				OpenAIGPTModel(config=__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()

      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase  ,				token_type_ids=__lowerCamelCase  ,				head_mask=__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase  ,				token_type_ids=__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase   )

      self.parent.assertEqual(result.last_hidden_state.shape  ,				(self.batch_size, self.seq_length, self.hidden_size)   )



   def 							lowerCAmelCase_      (   self :     Optional[int]  ,				__lowerCamelCase :     List[Any]  ,				__lowerCamelCase :     str  ,				__lowerCamelCase :     List[Any]  ,				__lowerCamelCase :     Dict  ,				*__lowerCamelCase :     Optional[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				OpenAIGPTLMHeadModel(__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()

      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase  ,				token_type_ids=__lowerCamelCase  ,				labels=__lowerCamelCase   )
      self.parent.assertEqual(result.loss.shape  ,				()   )
      self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.seq_length, self.vocab_size)   )



   def 							lowerCAmelCase_      (   self :     str  ,				__lowerCamelCase :     Dict  ,				__lowerCamelCase :     Union[str, Any]  ,				__lowerCamelCase :     List[str]  ,				__lowerCamelCase :     int  ,				*__lowerCamelCase :     List[str]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				OpenAIGPTDoubleHeadsModel(__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()

      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase  ,				token_type_ids=__lowerCamelCase  ,				labels=__lowerCamelCase   )
      self.parent.assertEqual(result.loss.shape  ,				()   )
      self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.seq_length, self.vocab_size)   )



   def 							lowerCAmelCase_      (   self :     str  ,				__lowerCamelCase :     List[Any]  ,				__lowerCamelCase :     str  ,				__lowerCamelCase :     Union[str, Any]  ,				__lowerCamelCase :     Tuple  ,				*__lowerCamelCase :     Union[str, Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.num_labels
      _SCREAMING_SNAKE_CASE	=				OpenAIGPTForSequenceClassification(__lowerCamelCase   )
      model.to(__lowerCamelCase   )
      model.eval()

      _SCREAMING_SNAKE_CASE	=				ids_tensor([self.batch_size]  ,				self.type_sequence_label_size   )
      _SCREAMING_SNAKE_CASE	=				model(__lowerCamelCase  ,				token_type_ids=__lowerCamelCase  ,				labels=__lowerCamelCase   )
      self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.num_labels)   )



   def 							lowerCAmelCase_      (   self :     Union[str, Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.prepare_config_and_inputs()
      (
          (
          _SCREAMING_SNAKE_CASE
      ),	(
          _SCREAMING_SNAKE_CASE
      ),	(
          _SCREAMING_SNAKE_CASE
      ),	(
          _SCREAMING_SNAKE_CASE
      ),	(
          _SCREAMING_SNAKE_CASE
      ),	(
          _SCREAMING_SNAKE_CASE
      ),	(
          _SCREAMING_SNAKE_CASE
      ),	
      )	=				config_and_inputs
      _SCREAMING_SNAKE_CASE	=				{
          "input_ids": input_ids,
          "token_type_ids": token_type_ids,
          "head_mask": head_mask,
      }

      return config, inputs_dict







@require_torch
class 		lowercase_   (			A					,  A					,  A					,  unittest.TestCase       ):


   """simple docstring"""

   lowerCamelCase_      =						(
       (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
       if is_torch_available()
       else ()
   )
   lowerCamelCase_      =						(
       (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
   )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
   lowerCamelCase_      =						(
       {
           '''feature-extraction''': OpenAIGPTModel,
           '''text-classification''': OpenAIGPTForSequenceClassification,
           '''text-generation''': OpenAIGPTLMHeadModel,
           '''zero-shot''': OpenAIGPTForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )



   def 							lowerCAmelCase_      (   self :     Dict  ,				__lowerCamelCase :     str  ,				__lowerCamelCase :     str  ,				__lowerCamelCase :     Dict  ,				__lowerCamelCase :     Optional[Any]  ,				__lowerCamelCase :     Tuple   ):

      """simple docstring"""
      if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
         # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
         # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
         # tiny config could not be created.
         return True

      return False



   def 							lowerCAmelCase_      (   self :     Tuple  ,				__lowerCamelCase :     Any  ,				__lowerCamelCase :     List[Any]  ,				__lowerCamelCase :     int=False   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				super()._prepare_for_class(__lowerCamelCase  ,				__lowerCamelCase  ,				return_labels=__lowerCamelCase   )

      if return_labels:
         if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
            _SCREAMING_SNAKE_CASE	=				torch.zeros(
                (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)  ,				dtype=torch.long  ,				device=__lowerCamelCase  ,				)
            _SCREAMING_SNAKE_CASE	=				inputs_dict["labels"]
            _SCREAMING_SNAKE_CASE	=				inputs_dict["labels"]
            _SCREAMING_SNAKE_CASE	=				torch.zeros(
                (self.model_tester.batch_size, self.model_tester.num_choices)  ,				dtype=torch.long  ,				device=__lowerCamelCase  ,				)
            _SCREAMING_SNAKE_CASE	=				torch.zeros(
                self.model_tester.batch_size  ,				dtype=torch.long  ,				device=__lowerCamelCase   )
      return inputs_dict



   def 							lowerCAmelCase_      (   self :     Dict   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				OpenAIGPTModelTester(self   )
      _SCREAMING_SNAKE_CASE	=				ConfigTester(self  ,				config_class=__lowerCamelCase  ,				n_embd=3_7   )



   def 							lowerCAmelCase_      (   self :     Union[str, Any]   ):

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



   def 							lowerCAmelCase_      (   self :     str   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     Tuple   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(*__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_double_lm_head_model(*__lowerCamelCase   )



   def 							lowerCAmelCase_      (   self :     Optional[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__lowerCamelCase   )



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

      """simple docstring"""
      for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _SCREAMING_SNAKE_CASE	=				OpenAIGPTModel.from_pretrained(__lowerCamelCase   )
         self.assertIsNotNone(__lowerCamelCase   )







@require_torch
class 		lowercase_   (			unittest.TestCase       ):


   """simple docstring"""

   @slow
   def 							lowerCAmelCase_      (   self :     Dict   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"   )
      model.to(__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]  ,				dtype=torch.long  ,				device=__lowerCamelCase   )  # the president is
      _SCREAMING_SNAKE_CASE	=				[
          4_8_1,
          4_7_3_5,
          5_4_4,
          2_4_6,
          9_6_3,
          8_7_0,
          7_6_2,
          2_3_9,
          2_4_4,
          4_0_4_7_7,
          2_4_4,
          2_4_9,
          7_1_9,
          8_8_1,
          4_8_7,
          5_4_4,
          2_4_0,
          2_4_4,
          6_0_3,
          4_8_1,
      ]  # the president is a very good man. " \n " i\'m sure he is, " said the

      _SCREAMING_SNAKE_CASE	=				model.generate(__lowerCamelCase  ,				do_sample=__lowerCamelCase   )
      self.assertListEqual(output_ids[0].tolist()  ,				__lowerCamelCase   )







'''simple docstring'''



from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


if TYPE_CHECKING:
 from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType


lowerCamelCase_			=						logging.get_logger(__name__)

lowerCamelCase_			=						{
    'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json',
    'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json',
    'microsoft/deberta-v2-xlarge-mnli': (
        'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json'
    ),
    'microsoft/deberta-v2-xxlarge-mnli': (
        'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json'
    ),
}







class 		lowercase_   (			A       ):


   """simple docstring"""

   lowerCamelCase_      =						'''deberta-v2'''



   def __init__(   self :     str  ,				__lowerCamelCase :     Union[str, Any]=1_2_8_1_0_0  ,				__lowerCamelCase :     Optional[int]=1_5_3_6  ,				__lowerCamelCase :     Optional[int]=2_4  ,				__lowerCamelCase :     Optional[int]=2_4  ,				__lowerCamelCase :     Tuple=6_1_4_4  ,				__lowerCamelCase :     List[str]="gelu"  ,				__lowerCamelCase :     int=0.1  ,				__lowerCamelCase :     Optional[Any]=0.1  ,				__lowerCamelCase :     Union[str, Any]=5_1_2  ,				__lowerCamelCase :     Optional[Any]=0  ,				__lowerCamelCase :     str=0.0_2  ,				__lowerCamelCase :     int=1e-7  ,				__lowerCamelCase :     Any=False  ,				__lowerCamelCase :     Any=-1  ,				__lowerCamelCase :     Tuple=0  ,				__lowerCamelCase :     str=True  ,				__lowerCamelCase :     List[Any]=None  ,				__lowerCamelCase :     Optional[int]=0  ,				__lowerCamelCase :     Any="gelu"  ,				**__lowerCamelCase :     Union[str, Any]  ,				):

      """simple docstring"""
      super().__init__(**__lowerCamelCase   )

      _SCREAMING_SNAKE_CASE	=				hidden_size
      _SCREAMING_SNAKE_CASE	=				num_hidden_layers
      _SCREAMING_SNAKE_CASE	=				num_attention_heads
      _SCREAMING_SNAKE_CASE	=				intermediate_size
      _SCREAMING_SNAKE_CASE	=				hidden_act
      _SCREAMING_SNAKE_CASE	=				hidden_dropout_prob
      _SCREAMING_SNAKE_CASE	=				attention_probs_dropout_prob
      _SCREAMING_SNAKE_CASE	=				max_position_embeddings
      _SCREAMING_SNAKE_CASE	=				type_vocab_size
      _SCREAMING_SNAKE_CASE	=				initializer_range
      _SCREAMING_SNAKE_CASE	=				relative_attention
      _SCREAMING_SNAKE_CASE	=				max_relative_positions
      _SCREAMING_SNAKE_CASE	=				pad_token_id
      _SCREAMING_SNAKE_CASE	=				position_biased_input

      # Backwards compatibility
      if type(__lowerCamelCase   ) == str:
         _SCREAMING_SNAKE_CASE	=				[x.strip() for x in pos_att_type.lower().split("|"   )]

      _SCREAMING_SNAKE_CASE	=				pos_att_type
      _SCREAMING_SNAKE_CASE	=				vocab_size
      _SCREAMING_SNAKE_CASE	=				layer_norm_eps

      _SCREAMING_SNAKE_CASE	=				kwargs.get("pooler_hidden_size"  ,				__lowerCamelCase   )
      _SCREAMING_SNAKE_CASE	=				pooler_dropout
      _SCREAMING_SNAKE_CASE	=				pooler_hidden_act







class 		lowercase_   (			A       ):


   """simple docstring"""

   @property
   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""
      if self.task == "multiple-choice":
         _SCREAMING_SNAKE_CASE	=				{0: "batch", 1: "choice", 2: "sequence"}
      else:
         _SCREAMING_SNAKE_CASE	=				{0: "batch", 1: "sequence"}
      if self._config.type_vocab_size > 0:
         return OrderedDict(
             [("input_ids", dynamic_axis), ("attention_mask", dynamic_axis), ("token_type_ids", dynamic_axis)]   )
      else:
         return OrderedDict([("input_ids", dynamic_axis), ("attention_mask", dynamic_axis)]   )



   @property
   def 							lowerCAmelCase_      (   self :     List[str]   ):

      """simple docstring"""
      return 1_2



   def 							lowerCAmelCase_      (   self :     List[str]  ,				__lowerCamelCase :     Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]  ,				__lowerCamelCase :     int = -1  ,				__lowerCamelCase :     int = -1  ,				__lowerCamelCase :     int = -1  ,				__lowerCamelCase :     bool = False  ,				__lowerCamelCase :     Optional["TensorType"] = None  ,				__lowerCamelCase :     int = 3  ,				__lowerCamelCase :     int = 4_0  ,				__lowerCamelCase :     int = 4_0  ,				__lowerCamelCase :     "PreTrainedTokenizerBase" = None  ,				):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				super().generate_dummy_inputs(preprocessor=__lowerCamelCase  ,				framework=__lowerCamelCase   )
      if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs:
         del dummy_inputs["token_type_ids"]
      return dummy_inputs

