

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			:

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






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

  def    __lowercase       (  self		:			List[str]    ):






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

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

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

   UpperCAmelCase__					:  List[str]					   =					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__					:  Union[str, Any]					   =					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    __lowercase       (  self		:			int					,A		:			Optional[Any]					,A		:			List[Any]					,A		:			List[Any]					,A		:			Dict					,*A		:			Optional[Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Union[str, Any]					   =					OpenAIGPTModel(config=A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  Optional[int]					   =					model(A					,token_type_ids=A					,head_mask=A    )
   UpperCAmelCase__					:  Union[str, Any]					   =					model(A					,token_type_ids=A    )
   UpperCAmelCase__					:  Any					   =					model(A    )

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

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






   '''simple docstring'''
   UpperCAmelCase__					:  Dict					   =					OpenAIGPTLMHeadModel(A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  List[Any]					   =					model(A					,token_type_ids=A					,labels=A    )
   self.parent.assertEqual(result.loss.shape					,()    )
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.vocab_size)    )

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






   '''simple docstring'''
   UpperCAmelCase__					:  str					   =					OpenAIGPTDoubleHeadsModel(A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  Dict					   =					model(A					,token_type_ids=A					,labels=A    )
   self.parent.assertEqual(result.loss.shape					,()    )
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.vocab_size)    )

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






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					self.num_labels
   UpperCAmelCase__					:  str					   =					OpenAIGPTForSequenceClassification(A    )
   model.to(A    )
   model.eval()

   UpperCAmelCase__					:  Optional[Any]					   =					ids_tensor([self.batch_size]					,self.type_sequence_label_size    )
   UpperCAmelCase__					:  Optional[int]					   =					model(A					,token_type_ids=A					,labels=A    )
   self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_labels)    )




  def    __lowercase       (  self		:			int    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[int]					   =					self.prepare_config_and_inputs()
   (
       UpperCAmelCase__      
   )					:  Tuple					   =					config_and_inputs
   UpperCAmelCase__					:  Tuple					   =					{
       """input_ids""": input_ids,
       """token_type_ids""": token_type_ids,
       """head_mask""": head_mask,
   }

   return config, inputs_dict







@require_torch
class      __lowercase			(    __lowerCamelCase	,			__lowerCamelCase	,			__lowerCamelCase	,			unittest.TestCase					):
  snake_case_				       =       (
      (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
      if is_torch_available()
      else ()
  )
  snake_case_				       =       (
      (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  snake_case_				       =       (
      {
          """feature-extraction""": OpenAIGPTModel,
          """text-classification""": OpenAIGPTForSequenceClassification,
          """text-generation""": OpenAIGPTLMHeadModel,
          """zero-shot""": OpenAIGPTForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )

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






   '''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    __lowercase       (  self		:			Any					,A		:			Optional[int]					,A		:			Dict					,A		:			List[str]=False    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					super()._prepare_for_class(A					,A					,return_labels=A    )

   if return_labels:
    if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
     UpperCAmelCase__					:  str					   =					torch.zeros(
         (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)					,dtype=torch.long					,device=A					,)
     UpperCAmelCase__					:  str					   =					inputs_dict["""labels"""]
     UpperCAmelCase__					:  Optional[int]					   =					inputs_dict["""labels"""]
     UpperCAmelCase__					:  Dict					   =					torch.zeros(
         (self.model_tester.batch_size, self.model_tester.num_choices)					,dtype=torch.long					,device=A					,)
     UpperCAmelCase__					:  Optional[int]					   =					torch.zeros(
         self.model_tester.batch_size					,dtype=torch.long					,device=A    )
   return inputs_dict

  def    __lowercase       (  self		:			List[str]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					OpenAIGPTModelTester(self    )
   UpperCAmelCase__					:  Any					   =					ConfigTester(self					,config_class=A					,n_embd=37    )

  def    __lowercase       (  self		:			Optional[int]    ):






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

  def    __lowercase       (  self		:			str    ):






   '''simple docstring'''
   UpperCAmelCase__					:  List[Any]					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_openai_gpt_model(*A    )

  def    __lowercase       (  self		:			List[str]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Dict					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_lm_head_model(*A    )

  def    __lowercase       (  self		:			Optional[int]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  int					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_double_lm_head_model(*A    )

  def    __lowercase       (  self		:			List[Any]    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Any					   =					self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*A    )




  @slow
  def    __lowercase       (  self		:			int    ):






   '''simple docstring'''
   for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    UpperCAmelCase__					:  List[str]					   =					OpenAIGPTModel.from_pretrained(A    )
    self.assertIsNotNone(A    )







@require_torch
class      __lowercase			(    unittest.TestCase					):

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






   '''simple docstring'''
   UpperCAmelCase__					:  Tuple					   =					OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""    )
   model.to(A    )
   UpperCAmelCase__					:  int					   =					torch.tensor([[481, 4_735, 544]]					,dtype=torch.long					,device=A    )  # the president is
   UpperCAmelCase__					:  List[Any]					   =					[
       481,
       4_735,
       544,
       246,
       963,
       870,
       762,
       239,
       244,
       40_477,
       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__					:  Optional[Any]					   =					model.generate(A					,do_sample=A    )
   self.assertListEqual(output_ids[0].tolist()					,A    )








"""simple docstring"""




import random
from typing import Any
def   lowerCAmelCase						(					__UpperCamelCase      ):
 '''simple docstring'''





 for _ in range(len(__UpperCamelCase      )      ):
  UpperCAmelCase__					:  Dict					   =					random.randint(0							,    len(__UpperCamelCase      ) - 1      )
  UpperCAmelCase__					:  Tuple					   =					random.randint(0							,    len(__UpperCamelCase      ) - 1      )
  UpperCAmelCase__   ,      UpperCAmelCase__					:  int					   =					data[b], data[a]
 return data


if __name__ == "__main__":
      __UpperCAmelCase =				[0, 1, 2, 3, 4, 5, 6, 7]
      __UpperCAmelCase =				['python', 'says', 'hello', '!']
      print('Fisher-Yates Shuffle:')
      print('List', integers, strings)
      print('FY Shuffle', fisher_yates_shuffle(integers), fisher_yates_shuffle(strings))





