






"""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        __lowercase:






      def __init__(					self    ,						_SCREAMING_SNAKE_CASE    ,						)			->  Union[str, Any]:




           """simple docstring"""





           lowerCamelCase__				=parent
           lowerCamelCase__				=13
           lowerCamelCase__				=7
           lowerCamelCase__				=True
           lowerCamelCase__				=True
           lowerCamelCase__				=True
           lowerCamelCase__				=True
           lowerCamelCase__				=True
           lowerCamelCase__				=99
           lowerCamelCase__				=32
           lowerCamelCase__				=2
           lowerCamelCase__				=4
           lowerCamelCase__				=37
           lowerCamelCase__				='gelu'
           lowerCamelCase__				=0.1
           lowerCamelCase__				=0.1
           lowerCamelCase__				=5_12
           lowerCamelCase__				=16
           lowerCamelCase__				=2
           lowerCamelCase__				=0.02
           lowerCamelCase__				=3
           lowerCamelCase__				=4
           lowerCamelCase__				=None
           lowerCamelCase__				=self.vocab_size - 1






      def 		lowerCAmelCase   (					self)			->  Tuple:




           """simple docstring"""





           lowerCamelCase__				=ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size)

           lowerCamelCase__				=None
           if self.use_input_mask:
                lowerCamelCase__				=random_attention_mask([self.batch_size, self.seq_length])

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

           lowerCamelCase__				=None
           if self.use_mc_token_ids:
                lowerCamelCase__				=ids_tensor([self.batch_size, self.num_choices]    ,						self.seq_length)

           lowerCamelCase__				=None
           lowerCamelCase__				=None
           lowerCamelCase__				=None
           if self.use_labels:
                lowerCamelCase__				=ids_tensor([self.batch_size]    ,						self.type_sequence_label_size)
                lowerCamelCase__				=ids_tensor([self.batch_size, self.seq_length]    ,						self.num_labels)
                lowerCamelCase__				=ids_tensor([self.batch_size]    ,						self.num_choices)

           lowerCamelCase__				=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    ,						)

           lowerCamelCase__				=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    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						*_SCREAMING_SNAKE_CASE)			->  str:




           """simple docstring"""





           lowerCamelCase__				=TFOpenAIGPTModel(config=__a)
           lowerCamelCase__				={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           lowerCamelCase__				=model(__a)

           lowerCamelCase__				=[input_ids, input_mask]
           lowerCamelCase__				=model(__a)

           lowerCamelCase__				=model(__a)

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






      def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						*_SCREAMING_SNAKE_CASE)			->  int:




           """simple docstring"""





           lowerCamelCase__				=TFOpenAIGPTLMHeadModel(config=__a)
           lowerCamelCase__				={'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           lowerCamelCase__				=model(__a)
           self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size))






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




           """simple docstring"""





           lowerCamelCase__				=TFOpenAIGPTDoubleHeadsModel(config=__a)

           lowerCamelCase__				=tf.tile(tf.expand_dims(__a    ,						1)    ,						(1, self.num_choices, 1))
           lowerCamelCase__				=tf.tile(tf.expand_dims(__a    ,						1)    ,						(1, self.num_choices, 1))
           lowerCamelCase__				=tf.tile(tf.expand_dims(__a    ,						1)    ,						(1, self.num_choices, 1))

           lowerCamelCase__				={
               '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,
           }
           lowerCamelCase__				=model(__a)
           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    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						*_SCREAMING_SNAKE_CASE)			->  List[str]:




           """simple docstring"""





           lowerCamelCase__				=self.num_labels
           lowerCamelCase__				=ids_tensor([self.batch_size]    ,						self.type_sequence_label_size)
           lowerCamelCase__				={
               'input_ids': input_ids,
               'attention_mask': input_mask,
               'token_type_ids': token_type_ids,
               'labels': sequence_labels,
           }
           lowerCamelCase__				=TFOpenAIGPTForSequenceClassification(__a)
           lowerCamelCase__				=model(__a)
           self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels))






      def 		lowerCAmelCase   (					self)			->  str:




           """simple docstring"""





           lowerCamelCase__				=self.prepare_config_and_inputs()

           (
               lowerCamelCase__       
           )				=config_and_inputs

           lowerCamelCase__				={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
           return config, inputs_dict




@require_tf
class        __lowercase(       lowerCAmelCase_						,							lowerCAmelCase_						,							unittest.TestCase  ):
      lowerCAmelCase_		:							Any=       (
          (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
          if is_tf_available()
          else ()
      )
      lowerCAmelCase_		:							Tuple=       (
          (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      lowerCAmelCase_		:							Optional[int]=       (
          {
              """feature-extraction""": TFOpenAIGPTModel,
              """text-classification""": TFOpenAIGPTForSequenceClassification,
              """text-generation""": TFOpenAIGPTLMHeadModel,
              """zero-shot""": TFOpenAIGPTForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      lowerCAmelCase_		:							str=       False
      lowerCAmelCase_		:							str=       False






      def 		lowerCAmelCase   (					self    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE    ,						_SCREAMING_SNAKE_CASE)			->  Union[str, 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 		lowerCAmelCase   (					self)			->  Tuple:




           """simple docstring"""





           lowerCamelCase__				=TFOpenAIGPTModelTester(self)
           lowerCamelCase__				=ConfigTester(self    ,						config_class=__a    ,						n_embd=37)






      def 		lowerCAmelCase   (					self)			->  List[Any]:




           """simple docstring"""





           self.config_tester.run_common_tests()






      def 		lowerCAmelCase   (					self)			->  List[str]:




           """simple docstring"""





           lowerCamelCase__				=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*__a)






      def 		lowerCAmelCase   (					self)			->  Tuple:




           """simple docstring"""





           lowerCamelCase__				=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_lm_head(*__a)






      def 		lowerCAmelCase   (					self)			->  List[str]:




           """simple docstring"""





           lowerCamelCase__				=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_double_head(*__a)






      def 		lowerCAmelCase   (					self)			->  List[Any]:




           """simple docstring"""





           lowerCamelCase__				=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__a)






      @slow
      def 		lowerCAmelCase   (					self)			->  str:




           """simple docstring"""





           for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                lowerCamelCase__				=TFOpenAIGPTModel.from_pretrained(__a)
                self.assertIsNotNone(__a)




@require_tf
class        __lowercase(       unittest.TestCase  ):






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




           """simple docstring"""





           lowerCamelCase__				=TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt')
           lowerCamelCase__				=tf.convert_to_tensor([[4_81, 47_35, 5_44]]    ,						dtype=tf.intaa)  # the president is
           lowerCamelCase__				=[
               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

           lowerCamelCase__				=model.generate(__a    ,						do_sample=__a)
           self.assertListEqual(output_ids[0].numpy().tolist()    ,						__a)







"""simple docstring"""



from doctest import testmod
from math import sqrt




def 						lowerCamelCase_				(  __A  :		int			):
					lowerCamelCase__				=[]
					if num < 1:
										return facs
					facs.append(1			)
					if num == 1:
										return facs
					facs.append(__A			)
					for i in range(2			,	int(sqrt(__A			)			) + 1			):
										if num % i == 0:  # If i is a factor of num
															facs.append(__A			)
															lowerCamelCase__				=num // i  # num//i is the other factor of num
															if d != i:  # If d and i are distinct
																				facs.append(__A			)  # we have found another factor
					facs.sort()
					return facs


if __name__ == "__main__":
						testmod(name="factors_of_a_number", verbose=True)