





"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers.models.openai.modeling_tf_openai import (
         TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
         TFOpenAIGPTDoubleHeadsModel,
         TFOpenAIGPTForSequenceClassification,
         TFOpenAIGPTLMHeadModel,
         TFOpenAIGPTModel,
     )




class 			_lowerCamelCase	:



      """simple docstring"""






      def __init__(	self			,  UpperCAmelCase			,  )    ->				Dict:






        '''simple docstring'''






        __snake_case				:			Tuple            =    parent
        __snake_case				:			Optional[Any]            =    13
        __snake_case				:			str            =    7
        __snake_case				:			Optional[Any]            =    True
        __snake_case				:			Tuple            =    True
        __snake_case				:			str            =    True
        __snake_case				:			Dict            =    True
        __snake_case				:			Union[str, Any]            =    True
        __snake_case				:			Optional[Any]            =    99
        __snake_case				:			Union[str, Any]            =    32
        __snake_case				:			Optional[Any]            =    2
        __snake_case				:			Optional[Any]            =    4
        __snake_case				:			Union[str, Any]            =    37
        __snake_case				:			Dict            =    "gelu"
        __snake_case				:			int            =    0.1
        __snake_case				:			Optional[Any]            =    0.1
        __snake_case				:			Tuple            =    512
        __snake_case				:			str            =    16
        __snake_case				:			Tuple            =    2
        __snake_case				:			str            =    0.02
        __snake_case				:			List[str]            =    3
        __snake_case				:			List[Any]            =    4
        __snake_case				:			Union[str, Any]            =    None
        __snake_case				:			List[str]            =    self.vocab_size - 1







      def   UpperCAmelCase						(	self				)    ->				Any:






        '''simple docstring'''






        __snake_case				:			str            =    ids_tensor([self.batch_size, self.seq_length]			,  self.vocab_size				)

        __snake_case				:			Dict            =    None
        if self.use_input_mask:
          __snake_case				:			List[str]            =    random_attention_mask([self.batch_size, self.seq_length]				)

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

        __snake_case				:			Union[str, Any]            =    None
        if self.use_mc_token_ids:
          __snake_case				:			Union[str, Any]            =    ids_tensor([self.batch_size, self.num_choices]			,  self.seq_length				)

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

        __snake_case				:			Optional[int]            =    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				:			Any            =    ids_tensor([self.num_hidden_layers, self.num_attention_heads]			,  2				)

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







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  *UpperCAmelCase				)    ->				List[str]:






        '''simple docstring'''






        __snake_case				:			List[Any]            =    TFOpenAIGPTModel(config=lowerCAmelCase__				)
        __snake_case				:			int            =    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        __snake_case				:			int            =    model(lowerCAmelCase__				)

        __snake_case				:			List[Any]            =    [input_ids, input_mask]
        __snake_case				:			List[str]            =    model(lowerCAmelCase__				)

        __snake_case				:			int            =    model(lowerCAmelCase__				)

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







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  *UpperCAmelCase				)    ->				Union[str, Any]:






        '''simple docstring'''






        __snake_case				:			Dict            =    TFOpenAIGPTLMHeadModel(config=lowerCAmelCase__				)
        __snake_case				:			Union[str, Any]            =    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
        __snake_case				:			Tuple            =    model(lowerCAmelCase__				)
        self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.vocab_size)				)







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  *UpperCAmelCase				)    ->				str:






        '''simple docstring'''






        __snake_case				:			Optional[Any]            =    TFOpenAIGPTDoubleHeadsModel(config=lowerCAmelCase__				)

        __snake_case				:			int            =    tf.tile(tf.expand_dims(lowerCAmelCase__			,  1				)			,  (1, self.num_choices, 1)				)
        __snake_case				:			int            =    tf.tile(tf.expand_dims(lowerCAmelCase__			,  1				)			,  (1, self.num_choices, 1)				)
        __snake_case				:			Tuple            =    tf.tile(tf.expand_dims(lowerCAmelCase__			,  1				)			,  (1, self.num_choices, 1)				)

        __snake_case				:			Optional[Any]            =    {
            "input_ids": multiple_choice_inputs_ids,
            "mc_token_ids": mc_token_ids,
            "attention_mask": multiple_choice_input_mask,
            "token_type_ids": multiple_choice_token_type_ids,
        }
        __snake_case				:			Optional[Any]            =    model(lowerCAmelCase__				)
        self.parent.assertEqual(
            result.logits.shape			,  (self.batch_size, self.num_choices, self.seq_length, self.vocab_size)				)
        self.parent.assertEqual(result.mc_logits.shape			,  (self.batch_size, self.num_choices)				)







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  *UpperCAmelCase				)    ->				Union[str, Any]:






        '''simple docstring'''






        __snake_case				:			Optional[Any]            =    self.num_labels
        __snake_case				:			Optional[int]            =    ids_tensor([self.batch_size]			,  self.type_sequence_label_size				)
        __snake_case				:			int            =    {
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
            "labels": sequence_labels,
        }
        __snake_case				:			List[Any]            =    TFOpenAIGPTForSequenceClassification(lowerCAmelCase__				)
        __snake_case				:			Any            =    model(lowerCAmelCase__				)
        self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_labels)				)







      def   UpperCAmelCase						(	self				)    ->				Dict:






        '''simple docstring'''






        __snake_case				:			str            =    self.prepare_config_and_inputs()

        (
            (
            __snake_case 
        )      ,				(
            __snake_case 
        )      ,				(
            __snake_case 
        )      ,				(
            __snake_case 
        )      ,				(
            __snake_case 
        )      ,				(
            __snake_case 
        )      ,				(
            __snake_case 
        )      ,				(
            __snake_case 
        )      ,				(
            __snake_case 
        )      ,				 
        )				:			Optional[Any]            =    config_and_inputs

        __snake_case				:			Dict            =    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
        return config, inputs_dict




@require_tf
class 			_lowerCamelCase	(      lowercase__	,				lowercase__	,				unittest.TestCase      ):



      """simple docstring"""






      UpperCAmelCase_  : Optional[int]				      =(
          (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
          if is_tf_available()
          else ()
      )
      UpperCAmelCase_  : Dict				      =(
          (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      UpperCAmelCase_  : Union[str, Any]				      =(
          {
              '''feature-extraction''': TFOpenAIGPTModel,
              '''text-classification''': TFOpenAIGPTForSequenceClassification,
              '''text-generation''': TFOpenAIGPTLMHeadModel,
              '''zero-shot''': TFOpenAIGPTForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      UpperCAmelCase_  : int				      =False
      UpperCAmelCase_  : Optional[Any]				      =False







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)    ->				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   UpperCAmelCase						(	self				)    ->				Union[str, Any]:






        '''simple docstring'''






        __snake_case				:			Any            =    TFOpenAIGPTModelTester(self				)
        __snake_case				:			int            =    ConfigTester(self			,  config_class=lowerCAmelCase__			,  n_embd=37				)







      def   UpperCAmelCase						(	self				)    ->				Dict:






        '''simple docstring'''






        self.config_tester.run_common_tests()







      def   UpperCAmelCase						(	self				)    ->				Tuple:






        '''simple docstring'''






        __snake_case				:			List[Any]            =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__				)







      def   UpperCAmelCase						(	self				)    ->				Tuple:






        '''simple docstring'''






        __snake_case				:			Union[str, Any]            =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_lm_head(*lowerCAmelCase__				)







      def   UpperCAmelCase						(	self				)    ->				int:






        '''simple docstring'''






        __snake_case				:			Optional[Any]            =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_double_head(*lowerCAmelCase__				)







      def   UpperCAmelCase						(	self				)    ->				Dict:






        '''simple docstring'''






        __snake_case				:			Dict            =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__				)







      @slow
      def   UpperCAmelCase						(	self				)    ->				Union[str, Any]:






        '''simple docstring'''






        for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          __snake_case				:			Tuple            =    TFOpenAIGPTModel.from_pretrained(lowerCAmelCase__				)
          self.assertIsNotNone(lowerCAmelCase__				)




@require_tf
class 			_lowerCamelCase	(      unittest.TestCase      ):



      """simple docstring"""






      @slow
      def   UpperCAmelCase						(	self				)    ->				List[str]:






        '''simple docstring'''






        __snake_case				:			int            =    TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"				)
        __snake_case				:			str            =    tf.convert_to_tensor([[481, 4735, 544]]			,  dtype=tf.intaa				)  # the president is
        __snake_case				:			Tuple            =    [
            481,
            4735,
            544,
            246,
            963,
            870,
            762,
            239,
            244,
            40477,
            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

        __snake_case				:			Union[str, Any]            =    model.generate(lowerCAmelCase__			,  do_sample=lowerCAmelCase__				)
        self.assertListEqual(output_ids[0].numpy().tolist()			,  lowerCAmelCase__				)








def  lowerCAmelCase__(		lowercase    :      int = 10**6			)	->      int:

  if n <= 0:
    raise ValueError("Please enter an integer greater than 0"			)

  __snake_case				:			List[Any]            =    list(range(n + 1			)			)
  for number in range(2				,       n + 1			):
    if phi[number] == number:
      phi[number] -= 1
      for multiple in range(number * 2				,       n + 1				,       lowercase			):
        __snake_case				:			Optional[int]            =    (phi[multiple] // number) * (number - 1)

  __snake_case				:			List[Any]            =    1
  for number in range(1				,       n + 1			):
    if (answer / phi[answer]) < (number / phi[number]):
      __snake_case				:			List[str]            =    number

  return answer


if __name__ == "__main__":
     print(solution())

