


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 			__lowerCAmelCase   :






  """simple docstring"""





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




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



  def     lowerCAmelCase__     (   self				:				Optional[Any]     )      ->       List[Any]:




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

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

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

     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			, )

     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				:				Tuple			, _lowerCAmelCase				:				List[Any]			, _lowerCAmelCase				:				List[str]			, _lowerCAmelCase				:				Dict			, _lowerCAmelCase				:				Any			, *_lowerCAmelCase				:				Union[str, Any]     )      ->       Any:




     """simple docstring"""
     snake_case_    			=    OpenAIGPTModel(config=lowerCAmelCase__     )
     model.to(lowerCAmelCase__     )
     model.eval()

     snake_case_    			=    model(lowerCAmelCase__			, token_type_ids=lowerCAmelCase__			, head_mask=lowerCAmelCase__     )
     snake_case_    			=    model(lowerCAmelCase__			, token_type_ids=lowerCAmelCase__     )
     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[Any]			, _lowerCAmelCase				:				Union[str, Any]			, _lowerCAmelCase				:				Union[str, Any]			, _lowerCAmelCase				:				Any			, _lowerCAmelCase				:				Dict			, *_lowerCAmelCase				:				List[Any]     )      ->       Dict:




     """simple docstring"""
     snake_case_    			=    OpenAIGPTLMHeadModel(lowerCAmelCase__     )
     model.to(lowerCAmelCase__     )
     model.eval()

     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				:				Tuple			, _lowerCAmelCase				:				Dict			, _lowerCAmelCase				:				str			, _lowerCAmelCase				:				Optional[Any]			, _lowerCAmelCase				:				List[str]			, *_lowerCAmelCase				:				Optional[Any]     )      ->       Any:




     """simple docstring"""
     snake_case_    			=    OpenAIGPTDoubleHeadsModel(lowerCAmelCase__     )
     model.to(lowerCAmelCase__     )
     model.eval()

     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				:				Dict			, _lowerCAmelCase				:				List[Any]			, _lowerCAmelCase				:				Union[str, Any]			, _lowerCAmelCase				:				List[Any]			, _lowerCAmelCase				:				str			, *_lowerCAmelCase				:				Optional[int]     )      ->       Dict:




     """simple docstring"""
     snake_case_    			=    self.num_labels
     snake_case_    			=    OpenAIGPTForSequenceClassification(lowerCAmelCase__     )
     model.to(lowerCAmelCase__     )
     model.eval()

     snake_case_    			=    ids_tensor([self.batch_size]			, self.type_sequence_label_size     )
     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				:				Optional[Any]     )      ->       str:




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

     return config, inputs_dict






@require_torch
class 			__lowerCAmelCase   (       A__       ,     A__       ,     A__       ,     unittest.TestCase    ):






  """simple docstring"""





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



  def     lowerCAmelCase__     (   self				:				Optional[int]			, _lowerCAmelCase				:				str			, _lowerCAmelCase				:				List[Any]			, _lowerCAmelCase				:				List[Any]			, _lowerCAmelCase				:				List[Any]			, _lowerCAmelCase				:				Union[str, Any]     )      ->       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				:				List[str]			, _lowerCAmelCase				:				Dict			, _lowerCAmelCase				:				int			, _lowerCAmelCase				:				int=False     )      ->       Dict:




     """simple docstring"""
     snake_case_    			=    super()._prepare_for_class(lowerCAmelCase__			, lowerCAmelCase__			, return_labels=lowerCAmelCase__     )

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



  def     lowerCAmelCase__     (   self				:				Optional[int]     )      ->       Union[str, Any]:




     """simple docstring"""
     snake_case_    			=    OpenAIGPTModelTester(self     )
     snake_case_    			=    ConfigTester(self			, config_class=lowerCAmelCase__			, n_embd=3_7     )



  def     lowerCAmelCase__     (   self				:				Any     )      ->       Optional[Any]:




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



  def     lowerCAmelCase__     (   self				:				List[str]     )      ->       Union[str, Any]:




     """simple docstring"""
     snake_case_    			=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__     )



  def     lowerCAmelCase__     (   self				:				int     )      ->       int:




     """simple docstring"""
     snake_case_    			=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__     )



  def     lowerCAmelCase__     (   self				:				List[str]     )      ->       Dict:




     """simple docstring"""
     snake_case_    			=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__     )



  def     lowerCAmelCase__     (   self				:				List[str]     )      ->       str:




     """simple docstring"""
     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				:				Any     )      ->       List[Any]:




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






@require_torch
class 			__lowerCAmelCase   (       unittest.TestCase    ):






  """simple docstring"""





  @slow
  def     lowerCAmelCase__     (   self				:				Union[str, Any]     )      ->       Optional[int]:




     """simple docstring"""
     snake_case_    			=    OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"     )
     model.to(lowerCAmelCase__     )
     snake_case_    			=    torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]			, dtype=torch.long			, device=lowerCAmelCase__     )  # the president is
     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

     snake_case_    			=    model.generate(lowerCAmelCase__			, do_sample=lowerCAmelCase__     )
     self.assertListEqual(output_ids[0].tolist()			, lowerCAmelCase__     )








def        _UpperCamelCase							(      lowercase__    ,       lowercase__    ,       lowercase__=False      ):

						if isinstance(lowercase__    ,       lowercase__      ) and isinstance(lowercase__    ,       lowercase__      ):
												__SCREAMING_SNAKE_CASE						:     List[str]					=		len(set_a.intersection(lowercase__      )      )

												if alternative_union:
																		__SCREAMING_SNAKE_CASE						:     int					=		len(lowercase__      ) + len(lowercase__      )
												else:
																		__SCREAMING_SNAKE_CASE						:     int					=		len(set_a.union(lowercase__      )      )

												return intersection / union

						if isinstance(lowercase__    ,       (list, tuple)      ) and isinstance(lowercase__    ,       (list, tuple)      ):
												__SCREAMING_SNAKE_CASE						:     Dict					=		[element for element in set_a if element in set_b]

												if alternative_union:
																		__SCREAMING_SNAKE_CASE						:     Optional[int]					=		len(lowercase__      ) + len(lowercase__      )
																		return len(lowercase__      ) / union
												else:
																		__SCREAMING_SNAKE_CASE						:     Tuple					=		set_a + [element for element in set_b if element not in set_a]
																		return len(lowercase__      ) / len(lowercase__      )

												return len(lowercase__      ) / len(lowercase__      )
						return None


if __name__ == "__main__":
						__lowerCAmelCase	: List[Any]  				={'a', 'b', 'c', 'd', 'e'}
						__lowerCAmelCase	: Optional[Any]  				={'c', 'd', 'e', 'f', 'h', 'i'}
						print(jaccard_similarity(set_a, set_b))
