



'''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					,    _lowerCamelCase					,    )    ->    int:
    A_  :		List[str]						=					parent
    A_  :		List[Any]						=					13
    A_  :		Union[str, Any]						=					7
    A_  :		Tuple						=					True
    A_  :		Union[str, Any]						=					True
    A_  :		Union[str, Any]						=					True
    A_  :		Optional[int]						=					True
    A_  :		Any						=					True
    A_  :		List[str]						=					99
    A_  :		Any						=					32
    A_  :		Optional[int]						=					2
    A_  :		Dict						=					4
    A_  :		Union[str, Any]						=					37
    A_  :		int						=					"""gelu"""
    A_  :		int						=					0.1
    A_  :		List[str]						=					0.1
    A_  :		List[Any]						=					512
    A_  :		int						=					16
    A_  :		Tuple						=					2
    A_  :		Any						=					0.02
    A_  :		Tuple						=					3
    A_  :		int						=					4
    A_  :		Optional[Any]						=					None
    A_  :		Optional[Any]						=					self.vocab_size - 1







 def       UpperCAmelCase_       (      self  )    ->    Optional[Any]:
    A_  :		Dict						=					ids_tensor([self.batch_size, self.seq_length]					,    self.vocab_size  )

    A_  :		Union[str, Any]						=					None
    if self.use_input_mask:
       A_  :		Optional[Any]						=					random_attention_mask([self.batch_size, self.seq_length]  )

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

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

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

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

    A_  :		Optional[int]						=					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					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    *_lowerCamelCase  )    ->    Tuple:
    A_  :		Optional[Any]						=					TFOpenAIGPTModel(config=_lowerCamelCase  )
    A_  :		Optional[int]						=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
    A_  :		List[str]						=					model(_lowerCamelCase  )

    A_  :		Optional[int]						=					[input_ids, input_mask]
    A_  :		Union[str, Any]						=					model(_lowerCamelCase  )

    A_  :		int						=					model(_lowerCamelCase  )

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







 def       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    *_lowerCamelCase  )    ->    Optional[Any]:
    A_  :		Any						=					TFOpenAIGPTLMHeadModel(config=_lowerCamelCase  )
    A_  :		str						=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
    A_  :		str						=					model(_lowerCamelCase  )
    self.parent.assertEqual(result.logits.shape					,    (self.batch_size, self.seq_length, self.vocab_size)  )







 def       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    *_lowerCamelCase  )    ->    Dict:
    A_  :		Tuple						=					TFOpenAIGPTDoubleHeadsModel(config=_lowerCamelCase  )

    A_  :		Any						=					tf.tile(tf.expand_dims(_lowerCamelCase					,    1  )					,    (1, self.num_choices, 1)  )
    A_  :		str						=					tf.tile(tf.expand_dims(_lowerCamelCase					,    1  )					,    (1, self.num_choices, 1)  )
    A_  :		List[str]						=					tf.tile(tf.expand_dims(_lowerCamelCase					,    1  )					,    (1, self.num_choices, 1)  )

    A_  :		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,
    }
    A_  :		int						=					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					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    *_lowerCamelCase  )    ->    Union[str, Any]:
    A_  :		List[Any]						=					self.num_labels
    A_  :		Optional[int]						=					ids_tensor([self.batch_size]					,    self.type_sequence_label_size  )
    A_  :		str						=					{
        """input_ids""": input_ids,
        """attention_mask""": input_mask,
        """token_type_ids""": token_type_ids,
        """labels""": sequence_labels,
    }
    A_  :		List[str]						=					TFOpenAIGPTForSequenceClassification(_lowerCamelCase  )
    A_  :		str						=					model(_lowerCamelCase  )
    self.parent.assertEqual(result.logits.shape					,    (self.batch_size, self.num_labels)  )







 def       UpperCAmelCase_       (      self  )    ->    Tuple:
    A_  :		Any						=					self.prepare_config_and_inputs()

    (
        (
        A_
    )						,  (
        A_
    )						,  (
        A_
    )						,  (
        A_
    )						,  (
        A_
    )						,  (
        A_
    )						,  (
        A_
    )						,  (
        A_
    )						,  (
        A_
    )						,  
    )  :		Any						=					config_and_inputs

    A_  :		List[Any]						=					{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
    return config, inputs_dict


@require_tf
class 							_lowerCAmelCase	(  __A,							__A,							unittest.TestCase							):


 """simple docstring"""

 lowerCamelCase									=     (
     (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
     if is_tf_available()
     else ()
 )
 lowerCamelCase									=     (
     (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 lowerCamelCase									=     (
     {
         '''feature-extraction''': TFOpenAIGPTModel,
         '''text-classification''': TFOpenAIGPTForSequenceClassification,
         '''text-generation''': TFOpenAIGPTLMHeadModel,
         '''zero-shot''': TFOpenAIGPTForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 lowerCamelCase									=     False
 lowerCamelCase									=     False







 def       UpperCAmelCase_       (      self					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase					,    _lowerCamelCase  )    ->    Union[str, 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       UpperCAmelCase_       (      self  )    ->    List[str]:
    A_  :		Dict						=					TFOpenAIGPTModelTester(self  )
    A_  :		List[Any]						=					ConfigTester(self					,    config_class=_lowerCamelCase					,    n_embd=37  )







 def       UpperCAmelCase_       (      self  )    ->    Dict:
    self.config_tester.run_common_tests()







 def       UpperCAmelCase_       (      self  )    ->    List[Any]:
    A_  :		Optional[int]						=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_model(*_lowerCamelCase  )







 def       UpperCAmelCase_       (      self  )    ->    Tuple:
    A_  :		Optional[Any]						=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_lm_head(*_lowerCamelCase  )







 def       UpperCAmelCase_       (      self  )    ->    str:
    A_  :		int						=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_double_head(*_lowerCamelCase  )







 def       UpperCAmelCase_       (      self  )    ->    List[Any]:
    A_  :		str						=					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCamelCase  )







 @slow
 def       UpperCAmelCase_       (      self  )    ->    str:
    for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       A_  :		int						=					TFOpenAIGPTModel.from_pretrained(_lowerCamelCase  )
       self.assertIsNotNone(_lowerCamelCase  )


@require_tf
class 							_lowerCAmelCase	(  unittest.TestCase							):


 """simple docstring"""

 @slow
 def       UpperCAmelCase_       (      self  )    ->    Union[str, Any]:
    A_  :		Optional[Any]						=					TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""  )
    A_  :		Tuple						=					tf.convert_to_tensor([[481, 4735, 544]]					,    dtype=tf.intaa  )  # the president is
    A_  :		Optional[Any]						=					[
        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

    A_  :		Dict						=					model.generate(_lowerCamelCase					,    do_sample=_lowerCamelCase  )
    self.assertListEqual(output_ids[0].numpy().tolist()					,    _lowerCamelCase  )





'''simple docstring'''





def     UpperCAmelCase     (    a_ = 9_9_8_0_0_1		)   ->       int:



   """simple docstring"""






   for number in range(n - 1      ,		9_9_9_9      ,		-1		):
      A_  :		Optional[Any]						=					str(a_		)

      # checks whether 'str_number' is a palindrome.
      if str_number == str_number[::-1]:
         A_  :		int						=					9_9_9

         # if 'number' is a product of two 3-digit numbers
         # then number is the answer otherwise fetch next number.
         while divisor != 9_9:
            if (number % divisor == 0) and (len(str(number // divisor		)		) == 3.0):
               return number
            divisor -= 1
   raise ValueError("""That number is larger than our acceptable range."""		)


if __name__ == "__main__":
  print(f'{solution() = }')
