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







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





    """simple docstring"""


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







   def 						lowerCamelCase_	(     self						:						int				):





    """simple docstring"""


    UpperCamelCase			  =      ids_tensor([self.batch_size, self.seq_length]   ,   self.vocab_size				)

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

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

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

    UpperCamelCase			  =      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						:						Dict   ,   lowerCamelCase_						:						Tuple   ,   lowerCamelCase_						:						Dict   ,   lowerCamelCase_						:						List[str]   ,   lowerCamelCase_						:						str   ,   *lowerCamelCase_						:						int				):





    """simple docstring"""


    UpperCamelCase			  =      OpenAIGPTModel(config=lowerCamelCase_				)
    model.to(lowerCamelCase_				)
    model.eval()

    UpperCamelCase			  =      model(lowerCamelCase_   ,   token_type_ids=lowerCamelCase_   ,   head_mask=lowerCamelCase_				)
    UpperCamelCase			  =      model(lowerCamelCase_   ,   token_type_ids=lowerCamelCase_				)
    UpperCamelCase			  =      model(lowerCamelCase_				)

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







   def 						lowerCamelCase_	(     self						:						List[Any]   ,   lowerCamelCase_						:						Union[str, Any]   ,   lowerCamelCase_						:						Union[str, Any]   ,   lowerCamelCase_						:						Optional[Any]   ,   lowerCamelCase_						:						Union[str, Any]   ,   *lowerCamelCase_						:						Union[str, Any]				):





    """simple docstring"""


    UpperCamelCase			  =      OpenAIGPTLMHeadModel(lowerCamelCase_				)
    model.to(lowerCamelCase_				)
    model.eval()

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





    """simple docstring"""


    UpperCamelCase			  =      OpenAIGPTDoubleHeadsModel(lowerCamelCase_				)
    model.to(lowerCamelCase_				)
    model.eval()

    UpperCamelCase			  =      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						:						Any   ,   lowerCamelCase_						:						Optional[Any]   ,   lowerCamelCase_						:						int   ,   lowerCamelCase_						:						Optional[Any]   ,   lowerCamelCase_						:						Any   ,   *lowerCamelCase_						:						List[str]				):





    """simple docstring"""


    UpperCamelCase			  =      self.num_labels
    UpperCamelCase			  =      OpenAIGPTForSequenceClassification(lowerCamelCase_				)
    model.to(lowerCamelCase_				)
    model.eval()

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







   def 						lowerCamelCase_	(     self						:						Dict				):





    """simple docstring"""


    UpperCamelCase			  =      self.prepare_config_and_inputs()
    (
        UpperCamelCase    
    )			  =      config_and_inputs
    UpperCamelCase			  =      {
        'input_ids': input_ids,
        'token_type_ids': token_type_ids,
        'head_mask': head_mask,
    }

    return config, inputs_dict





@require_torch
class SCREAMING_SNAKE_CASE_		(      __lowerCAmelCase  ,     __lowerCAmelCase  ,     __lowerCAmelCase  ,     unittest.TestCase		):
   __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						:						List[Any]   ,   lowerCamelCase_						:						Optional[int]   ,   lowerCamelCase_						:						Dict   ,   lowerCamelCase_						:						int   ,   lowerCamelCase_						:						List[str]   ,   lowerCamelCase_						:						Optional[Any]				):





    """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						:						Any   ,   lowerCamelCase_						:						Dict   ,   lowerCamelCase_						:						Any   ,   lowerCamelCase_						:						List[Any]=False				):





    """simple docstring"""


    UpperCamelCase			  =      super()._prepare_for_class(lowerCamelCase_   ,   lowerCamelCase_   ,   return_labels=lowerCamelCase_				)

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







   def 						lowerCamelCase_	(     self						:						Dict				):





    """simple docstring"""


    UpperCamelCase			  =      OpenAIGPTModelTester(self				)
    UpperCamelCase			  =      ConfigTester(self   ,   config_class=lowerCamelCase_   ,   n_embd=37				)







   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    self.config_tester.run_common_tests()







   def 						lowerCamelCase_	(     self						:						Tuple				):





    """simple docstring"""


    UpperCamelCase			  =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_model(*lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    UpperCamelCase			  =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_lm_head_model(*lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						str				):





    """simple docstring"""


    UpperCamelCase			  =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_double_lm_head_model(*lowerCamelCase_				)







   def 						lowerCamelCase_	(     self						:						Any				):





    """simple docstring"""


    UpperCamelCase			  =      self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCamelCase_				)







   @slow
   def 						lowerCamelCase_	(     self						:						Tuple				):





    """simple docstring"""


    for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     UpperCamelCase			  =      OpenAIGPTModel.from_pretrained(lowerCamelCase_				)
     self.assertIsNotNone(lowerCamelCase_				)





@require_torch
class SCREAMING_SNAKE_CASE_		(      unittest.TestCase		):







   @slow
   def 						lowerCamelCase_	(     self						:						Tuple				):





    """simple docstring"""


    UpperCamelCase			  =      OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""				)
    model.to(lowerCamelCase_				)
    UpperCamelCase			  =      torch.tensor([[481, 4735, 544]]   ,   dtype=torch.long   ,   device=lowerCamelCase_				)  # the president is
    UpperCamelCase			  =      [
        481,
        4735,
        544,
        246,
        963,
        870,
        762,
        239,
        244,
        4_0477,
        244,
        249,
        719,
        881,
        487,
        544,
        240,
        244,
        603,
        481,
    ]  # the president is a very good man. " \n " i\'m sure he is, " said the

    UpperCamelCase			  =      model.generate(lowerCamelCase_   ,   do_sample=lowerCamelCase_				)
    self.assertListEqual(output_ids[0].tolist()   ,   lowerCamelCase_				)






'''simple docstring'''



from collections import defaultdict

from graphs.minimum_spanning_tree_prims import prisms_algorithm as mst




def   lowercase__(			):

      """simple docstring"""






      SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      str    					=						9, 14  # noqa: F841
      SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						[
          [0, 1, 4],
          [0, 7, 8],
          [1, 2, 8],
          [7, 8, 7],
          [7, 6, 1],
          [2, 8, 2],
          [8, 6, 6],
          [2, 3, 7],
          [2, 5, 4],
          [6, 5, 2],
          [3, 5, 14],
          [3, 4, 9],
          [5, 4, 10],
          [1, 7, 11],
      ]

      SCREAMING_SNAKE_CASE   :      Optional[int]    					=						defaultdict(__UpperCamelCase    )
      for nodea, nodea, cost in edges:
            adjancency[nodea].append([nodea, cost]    )
            adjancency[nodea].append([nodea, cost]    )

      SCREAMING_SNAKE_CASE   :      Dict    					=						mst(__UpperCamelCase    )

      SCREAMING_SNAKE_CASE   :      Optional[int]    					=						[
          [7, 6, 1],
          [2, 8, 2],
          [6, 5, 2],
          [0, 1, 4],
          [2, 5, 4],
          [2, 3, 7],
          [0, 7, 8],
          [3, 4, 9],
      ]

      for answer in expected:
            SCREAMING_SNAKE_CASE   :      Any    					=						tuple(answer[:2]    )
            SCREAMING_SNAKE_CASE   :      List[Any]    					=						tuple(edge[::-1]    )
            assert edge in result or reverse in result


