






'''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        __SCREAMING_SNAKE_CASE :




 def __init__( self		:							Tuple   , __lowercase		:							Optional[int]   , __lowercase		:							List[Any]=13   , __lowercase		:							Union[str, Any]=7   , __lowercase		:							int=True   , __lowercase		:							int=True   , __lowercase		:							Any=True   , __lowercase		:							Tuple=99   , __lowercase		:							Optional[int]=32   , __lowercase		:							Dict=5   , __lowercase		:							List[Any]=4   , __lowercase		:							Tuple=37   , __lowercase		:							str="gelu"   , __lowercase		:							List[Any]=0.1   , __lowercase		:							Optional[int]=0.1   , __lowercase		:							Dict=5_12   , __lowercase		:							Any=16   , __lowercase		:							Optional[int]=2   , __lowercase		:							Any=0.02   , __lowercase		:							Tuple=3   , __lowercase		:							Optional[Any]=4   , __lowercase		:							List[Any]=None   , )		->  Optional[int]:
       SCREAMING_SNAKE_CASE__       :			List[str]													=parent
       SCREAMING_SNAKE_CASE__       :			int													=batch_size
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=seq_length
       SCREAMING_SNAKE_CASE__       :			Dict													=is_training
       SCREAMING_SNAKE_CASE__       :			Any													=use_token_type_ids
       SCREAMING_SNAKE_CASE__       :			Tuple													=use_labels
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=vocab_size
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=hidden_size
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=num_hidden_layers
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=num_attention_heads
       SCREAMING_SNAKE_CASE__       :			List[str]													=intermediate_size
       SCREAMING_SNAKE_CASE__       :			Any													=hidden_act
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=hidden_dropout_prob
       SCREAMING_SNAKE_CASE__       :			Any													=attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE__       :			str													=max_position_embeddings
       SCREAMING_SNAKE_CASE__       :			Dict													=type_vocab_size
       SCREAMING_SNAKE_CASE__       :			Tuple													=type_sequence_label_size
       SCREAMING_SNAKE_CASE__       :			List[Any]													=initializer_range
       SCREAMING_SNAKE_CASE__       :			List[str]													=num_labels
       SCREAMING_SNAKE_CASE__       :			str													=num_choices
       SCREAMING_SNAKE_CASE__       :			List[Any]													=scope
       SCREAMING_SNAKE_CASE__       :			Any													=self.vocab_size - 1




 def        __magic_name__		( self		:							Optional[Any] )		->  List[str]:
       SCREAMING_SNAKE_CASE__       :			str													=ids_tensor([self.batch_size, self.seq_length]   , self.vocab_size )

       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=None
       if self.use_token_type_ids:
             SCREAMING_SNAKE_CASE__       :			Dict													=ids_tensor([self.batch_size, self.seq_length]   , self.type_vocab_size )

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

       SCREAMING_SNAKE_CASE__       :			List[Any]													=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__       :			List[str]													=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        __magic_name__		( self		:							Optional[Any]   , __lowercase		:							List[Any]   , __lowercase		:							List[Any]   , __lowercase		:							Union[str, Any]   , __lowercase		:							List[str]   , *__lowercase		:							Any )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=OpenAIGPTModel(config=_lowerCamelCase )
       model.to(_lowerCamelCase )
       model.eval()

       SCREAMING_SNAKE_CASE__       :			Dict													=model(_lowerCamelCase   , token_type_ids=_lowerCamelCase   , head_mask=_lowerCamelCase )
       SCREAMING_SNAKE_CASE__       :			List[Any]													=model(_lowerCamelCase   , token_type_ids=_lowerCamelCase )
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=model(_lowerCamelCase )

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




 def        __magic_name__		( self		:							List[Any]   , __lowercase		:							str   , __lowercase		:							int   , __lowercase		:							Union[str, Any]   , __lowercase		:							Tuple   , *__lowercase		:							List[Any] )		->  Any:
       SCREAMING_SNAKE_CASE__       :			Any													=OpenAIGPTLMHeadModel(_lowerCamelCase )
       model.to(_lowerCamelCase )
       model.eval()

       SCREAMING_SNAKE_CASE__       :			Dict													=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        __magic_name__		( self		:							Union[str, Any]   , __lowercase		:							Optional[Any]   , __lowercase		:							Any   , __lowercase		:							List[str]   , __lowercase		:							Optional[int]   , *__lowercase		:							Tuple )		->  List[str]:
       SCREAMING_SNAKE_CASE__       :			Tuple													=OpenAIGPTDoubleHeadsModel(_lowerCamelCase )
       model.to(_lowerCamelCase )
       model.eval()

       SCREAMING_SNAKE_CASE__       :			Dict													=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        __magic_name__		( self		:							Optional[Any]   , __lowercase		:							Tuple   , __lowercase		:							Tuple   , __lowercase		:							Optional[int]   , __lowercase		:							str   , *__lowercase		:							str )		->  Any:
       SCREAMING_SNAKE_CASE__       :			Tuple													=self.num_labels
       SCREAMING_SNAKE_CASE__       :			List[str]													=OpenAIGPTForSequenceClassification(_lowerCamelCase )
       model.to(_lowerCamelCase )
       model.eval()

       SCREAMING_SNAKE_CASE__       :			Any													=ids_tensor([self.batch_size]   , self.type_sequence_label_size )
       SCREAMING_SNAKE_CASE__       :			Dict													=model(_lowerCamelCase   , token_type_ids=_lowerCamelCase   , labels=_lowerCamelCase )
       self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_labels) )




 def        __magic_name__		( self		:							Optional[Any] )		->  int:
       SCREAMING_SNAKE_CASE__       :			str													=self.prepare_config_and_inputs()
       (
           SCREAMING_SNAKE_CASE__    
       )       :			Optional[int]													=config_and_inputs
       SCREAMING_SNAKE_CASE__       :			List[str]													={
           '''input_ids''': input_ids,
           '''token_type_ids''': token_type_ids,
           '''head_mask''': head_mask,
       }

       return config, inputs_dict






@require_torch
class        __SCREAMING_SNAKE_CASE (     a__		,			a__		,			a__		,			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        __magic_name__		( self		:							str   , __lowercase		:							str   , __lowercase		:							int   , __lowercase		:							List[str]   , __lowercase		:							Union[str, Any]   , __lowercase		:							Optional[int] )		->  Optional[Any]:
       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        __magic_name__		( self		:							List[Any]   , __lowercase		:							Optional[Any]   , __lowercase		:							Tuple   , __lowercase		:							Optional[int]=False )		->  int:
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=super()._prepare_for_class(_lowerCamelCase   , _lowerCamelCase   , return_labels=_lowerCamelCase )

       if return_labels:
             if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                   SCREAMING_SNAKE_CASE__       :			int													=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__       :			Dict													=inputs_dict['''labels''']
                   SCREAMING_SNAKE_CASE__       :			Dict													=inputs_dict['''labels''']
                   SCREAMING_SNAKE_CASE__       :			List[Any]													=torch.zeros(
                       (self.model_tester.batch_size, self.model_tester.num_choices)   , dtype=torch.long   , device=_lowerCamelCase   , )
                   SCREAMING_SNAKE_CASE__       :			Optional[Any]													=torch.zeros(
                       self.model_tester.batch_size   , dtype=torch.long   , device=_lowerCamelCase )
       return inputs_dict




 def        __magic_name__		( self		:							Tuple )		->  str:
       SCREAMING_SNAKE_CASE__       :			Any													=OpenAIGPTModelTester(self )
       SCREAMING_SNAKE_CASE__       :			Any													=ConfigTester(self   , config_class=_lowerCamelCase   , n_embd=37 )




 def        __magic_name__		( self		:							Union[str, Any] )		->  Union[str, Any]:
       self.config_tester.run_common_tests()




 def        __magic_name__		( self		:							Union[str, Any] )		->  Any:
       SCREAMING_SNAKE_CASE__       :			Tuple													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_model(*_lowerCamelCase )




 def        __magic_name__		( self		:							List[Any] )		->  Optional[Any]:
       SCREAMING_SNAKE_CASE__       :			Dict													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_lm_head_model(*_lowerCamelCase )




 def        __magic_name__		( self		:							List[Any] )		->  Any:
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_double_lm_head_model(*_lowerCamelCase )




 def        __magic_name__		( self		:							Union[str, Any] )		->  Union[str, Any]:
       SCREAMING_SNAKE_CASE__       :			Tuple													=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCamelCase )




 @slow
 def        __magic_name__		( self		:							Tuple )		->  Optional[int]:
       for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE__       :			Optional[int]													=OpenAIGPTModel.from_pretrained(_lowerCamelCase )
             self.assertIsNotNone(_lowerCamelCase )






@require_torch
class        __SCREAMING_SNAKE_CASE (     unittest.TestCase			):




 @slow
 def        __magic_name__		( self		:							Union[str, Any] )		->  Optional[Any]:
       SCREAMING_SNAKE_CASE__       :			List[str]													=OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt''' )
       model.to(_lowerCamelCase )
       SCREAMING_SNAKE_CASE__       :			str													=torch.tensor([[4_81, 47_35, 5_44]]   , dtype=torch.long   , device=_lowerCamelCase )  # the president is
       SCREAMING_SNAKE_CASE__       :			int													=[
           4_81,
           47_35,
           5_44,
           2_46,
           9_63,
           8_70,
           7_62,
           2_39,
           2_44,
           4_04_77,
           2_44,
           2_49,
           7_19,
           8_81,
           4_87,
           5_44,
           2_40,
           2_44,
           6_03,
           4_81,
       ]  # the president is a very good man. " \n " i\'m sure he is, " said the

       SCREAMING_SNAKE_CASE__       :			Optional[int]													=model.generate(_lowerCamelCase   , do_sample=_lowerCamelCase )
       self.assertListEqual(output_ids[0].tolist()   , _lowerCamelCase )







'''simple docstring'''
from __future__ import annotations





def 	_a(					UpperCamelCase__			:  List[str],		UpperCamelCase__			:  Union[str, Any],		UpperCamelCase__			:  int,		UpperCamelCase__			:  List[str]							):  # noqa: E741




      '''simple docstring'''






      while r - l > 1:
            SCREAMING_SNAKE_CASE__       :			List[str]													=(l + r) // 2
            if v[m] >= key:
                  SCREAMING_SNAKE_CASE__       :			Dict													=m
            else:
                  SCREAMING_SNAKE_CASE__       :			Any													=m  # noqa: E741
      return r





def 	_a(					UpperCamelCase__			:  list[int]							):




      '''simple docstring'''






      if len(UpperCamelCase__							) == 0:
            return 0

      SCREAMING_SNAKE_CASE__       :			List[Any]													=[0] * len(UpperCamelCase__							)
      SCREAMING_SNAKE_CASE__       :			Dict													=1

      SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=v[0]

      for i in range(1,		len(UpperCamelCase__							)							):
            if v[i] < tail[0]:
                  SCREAMING_SNAKE_CASE__       :			List[Any]													=v[i]
            elif v[i] > tail[length - 1]:
                  SCREAMING_SNAKE_CASE__       :			int													=v[i]
                  length += 1
            else:
                  SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=v[i]

      return length


if __name__ == "__main__":
  import doctest

  doctest.testmod()