





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



      def __init__(							self    : Optional[Any]   ,UpperCamelCase    : Union[str, Any]   ,)		->  Dict:
           _lowercase							:      Dict  						=							parent
           _lowercase							:      Tuple  						=							13
           _lowercase							:      Any  						=							7
           _lowercase							:      List[str]  						=							True
           _lowercase							:      Dict  						=							True
           _lowercase							:      Union[str, Any]  						=							True
           _lowercase							:      int  						=							True
           _lowercase							:      str  						=							True
           _lowercase							:      Any  						=							99
           _lowercase							:      Optional[int]  						=							32
           _lowercase							:      List[Any]  						=							2
           _lowercase							:      int  						=							4
           _lowercase							:      Tuple  						=							37
           _lowercase							:      str  						=							'gelu'
           _lowercase							:      Union[str, Any]  						=							0.1
           _lowercase							:      Dict  						=							0.1
           _lowercase							:      int  						=							512
           _lowercase							:      str  						=							16
           _lowercase							:      Dict  						=							2
           _lowercase							:      List[Any]  						=							0.0_2
           _lowercase							:      Dict  						=							3
           _lowercase							:      List[str]  						=							4
           _lowercase							:      Optional[Any]  						=							None
           _lowercase							:      List[str]  						=							self.vocab_size - 1

      def   _lowerCamelCase		(							self    : Any  )		->  List[Any]:
           _lowercase							:      Optional[Any]  						=							ids_tensor([self.batch_size, self.seq_length]   ,self.vocab_size  )

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

           _lowercase							:      Tuple  						=							None
           if self.use_token_type_ids:
                _lowercase							:      List[Any]  						=							ids_tensor([self.batch_size, self.seq_length]   ,self.type_vocab_size  )

           _lowercase							:      str  						=							None
           if self.use_mc_token_ids:
                _lowercase							:      Optional[int]  						=							ids_tensor([self.batch_size, self.num_choices]   ,self.seq_length  )

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

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

           _lowercase							:      Dict  						=							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   _lowerCamelCase		(							self    : Union[str, Any]   ,UpperCamelCase    : Optional[Any]   ,UpperCamelCase    : Optional[Any]   ,UpperCamelCase    : int   ,UpperCamelCase    : List[str]   ,UpperCamelCase    : List[Any]   ,*UpperCamelCase    : Union[str, Any]  )		->  str:
           _lowercase							:      List[Any]  						=							TFOpenAIGPTModel(config=UpperCamelCase  )
           _lowercase							:      Dict  						=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           _lowercase							:      Union[str, Any]  						=							model(UpperCamelCase  )

           _lowercase							:      List[str]  						=							[input_ids, input_mask]
           _lowercase							:      Optional[Any]  						=							model(UpperCamelCase  )

           _lowercase							:      List[Any]  						=							model(UpperCamelCase  )

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

      def   _lowerCamelCase		(							self    : List[Any]   ,UpperCamelCase    : List[str]   ,UpperCamelCase    : Any   ,UpperCamelCase    : str   ,UpperCamelCase    : Dict   ,UpperCamelCase    : Union[str, Any]   ,*UpperCamelCase    : Union[str, Any]  )		->  int:
           _lowercase							:      Optional[Any]  						=							TFOpenAIGPTLMHeadModel(config=UpperCamelCase  )
           _lowercase							:      Tuple  						=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           _lowercase							:      int  						=							model(UpperCamelCase  )
           self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.seq_length, self.vocab_size)  )

      def   _lowerCamelCase		(							self    : List[str]   ,UpperCamelCase    : List[Any]   ,UpperCamelCase    : Tuple   ,UpperCamelCase    : List[Any]   ,UpperCamelCase    : str   ,UpperCamelCase    : int   ,UpperCamelCase    : Dict   ,*UpperCamelCase    : int  )		->  List[Any]:
           _lowercase							:      List[Any]  						=							TFOpenAIGPTDoubleHeadsModel(config=UpperCamelCase  )

           _lowercase							:      Tuple  						=							tf.tile(tf.expand_dims(UpperCamelCase   ,1  )   ,(1, self.num_choices, 1)  )
           _lowercase							:      str  						=							tf.tile(tf.expand_dims(UpperCamelCase   ,1  )   ,(1, self.num_choices, 1)  )
           _lowercase							:      Tuple  						=							tf.tile(tf.expand_dims(UpperCamelCase   ,1  )   ,(1, self.num_choices, 1)  )

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



      def   _lowerCamelCase		(							self    : List[str]  )		->  List[str]:
           _lowercase							:      Optional[Any]  						=							self.prepare_config_and_inputs()

           (
               _lowercase 
           )							:      str  						=							config_and_inputs

           _lowercase							:      List[str]  						=							{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
           return config, inputs_dict



@require_tf
class 							__SCREAMING_SNAKE_CASE			(       lowerCAmelCase__							,   lowerCAmelCase__							,   unittest.TestCase      ):
      '''simple docstring'''



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

      def   _lowerCamelCase		(							self    : int   ,UpperCamelCase    : str   ,UpperCamelCase    : Optional[Any]   ,UpperCamelCase    : Dict   ,UpperCamelCase    : Optional[Any]   ,UpperCamelCase    : Union[str, Any]  )		->  Tuple:
           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    : str  )		->  int:
           _lowercase							:      Any  						=							TFOpenAIGPTModelTester(self  )
           _lowercase							:      int  						=							ConfigTester(self   ,config_class=UpperCamelCase   ,n_embd=37  )

      def   _lowerCamelCase		(							self    : Any  )		->  str:
           self.config_tester.run_common_tests()

      def   _lowerCamelCase		(							self    : Dict  )		->  List[Any]:
           _lowercase							:      List[str]  						=							self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase  )

      def   _lowerCamelCase		(							self    : Dict  )		->  Optional[int]:
           _lowercase							:      int  						=							self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_lm_head(*UpperCamelCase  )

      def   _lowerCamelCase		(							self    : Dict  )		->  List[Any]:
           _lowercase							:      List[Any]  						=							self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_double_head(*UpperCamelCase  )

      def   _lowerCamelCase		(							self    : int  )		->  Optional[Any]:
           _lowercase							:      Any  						=							self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase  )



      @slow
      def   _lowerCamelCase		(							self    : Any  )		->  List[str]:
           for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _lowercase							:      Optional[Any]  						=							TFOpenAIGPTModel.from_pretrained(UpperCamelCase  )
                self.assertIsNotNone(UpperCamelCase  )



@require_tf
class 							__SCREAMING_SNAKE_CASE			(       unittest.TestCase      ):
      '''simple docstring'''



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

           _lowercase							:      Any  						=							model.generate(UpperCamelCase   ,do_sample=UpperCamelCase  )
           self.assertListEqual(output_ids[0].numpy().tolist()   ,UpperCamelCase  )






'''simple docstring'''







def 		SCREAMING_SNAKE_CASE      (					lowerCAmelCase__     :	str)			->						str:

     '''simple docstring'''




     return " ".join(sorted(set(sentence.split())))


if __name__ == "__main__":
    import doctest

    doctest.testmod()