


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




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

										__lowerCamelCase        =			[input_ids, input_mask]
										__lowerCamelCase        =			model(_lowerCamelCase      )

										__lowerCamelCase        =			model(_lowerCamelCase      )

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




							def      __a      (    self:						List[str]    ,       A_:						List[Any]    ,       A_:						Tuple    ,       A_:						Union[str, Any]    ,       A_:						Dict    ,       A_:						List[Any]    ,       *A_:						str      ):
										__lowerCamelCase        =			TFOpenAIGPTLMHeadModel(config=_lowerCamelCase      )
										__lowerCamelCase        =			{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										__lowerCamelCase        =			model(_lowerCamelCase      )
										self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.seq_length, self.vocab_size)      )




							def      __a      (    self:						Union[str, Any]    ,       A_:						Dict    ,       A_:						Tuple    ,       A_:						List[str]    ,       A_:						Optional[Any]    ,       A_:						Optional[int]    ,       A_:						Any    ,       *A_:						str      ):
										__lowerCamelCase        =			TFOpenAIGPTDoubleHeadsModel(config=_lowerCamelCase      )

										__lowerCamelCase        =			tf.tile(tf.expand_dims(_lowerCamelCase    ,       1      )    ,       (1, self.num_choices, 1)      )
										__lowerCamelCase        =			tf.tile(tf.expand_dims(_lowerCamelCase    ,       1      )    ,       (1, self.num_choices, 1)      )
										__lowerCamelCase        =			tf.tile(tf.expand_dims(_lowerCamelCase    ,       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(_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      __a      (    self:						str    ,       A_:						Any    ,       A_:						Tuple    ,       A_:						Dict    ,       A_:						List[str]    ,       A_:						List[Any]    ,       *A_:						List[Any]      ):
										__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(_lowerCamelCase      )
										__lowerCamelCase        =			model(_lowerCamelCase      )
										self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.num_labels)      )




							def      __a      (    self:						Any      ):
										__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  __snake_case      (lowerCAmelCase__ ,		lowerCAmelCase__ ,		unittest.TestCase     ):
							__a				   =					(
							    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
							    if is_tf_available()
							    else ()
							)
							__a				   =					(
							    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
							)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
							__a				   =					(
							    {
							        '''feature-extraction''': TFOpenAIGPTModel,
							        '''text-classification''': TFOpenAIGPTForSequenceClassification,
							        '''text-generation''': TFOpenAIGPTLMHeadModel,
							        '''zero-shot''': TFOpenAIGPTForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							__a				   =					False
							__a				   =					False




							def      __a      (    self:						int    ,       A_:						List[str]    ,       A_:						List[Any]    ,       A_:						Dict    ,       A_:						Tuple    ,       A_:						int      ):

										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      __a      (    self:						str      ):
										__lowerCamelCase        =			TFOpenAIGPTModelTester(self      )
										__lowerCamelCase        =			ConfigTester(self    ,       config_class=_lowerCamelCase    ,       n_embd=37      )




							def      __a      (    self:						Union[str, Any]      ):
										self.config_tester.run_common_tests()




							def      __a      (    self:						Optional[Any]      ):
										__lowerCamelCase        =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*_lowerCamelCase      )




							def      __a      (    self:						List[Any]      ):
										__lowerCamelCase        =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_lm_head(*_lowerCamelCase      )




							def      __a      (    self:						Tuple      ):
										__lowerCamelCase        =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_double_head(*_lowerCamelCase      )




							def      __a      (    self:						str      ):
										__lowerCamelCase        =			self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCamelCase      )




							@slow
							def      __a      (    self:						List[str]      ):

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




@require_tf
class  __snake_case      (unittest.TestCase     ):




							@slow
							def      __a      (    self:						List[Any]      ):
										__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(_lowerCamelCase    ,       do_sample=_lowerCamelCase      )
										self.assertListEqual(output_ids[0].numpy().tolist()    ,       _lowerCamelCase      )








"""simple docstring"""




from __future__ import annotations

import re





def 			a_   (  lowercase__   :list[str]    ):

			def alphanum_key(lowercase__   :int    ):
						return [int(lowercase__    ) if s.isdigit() else s.lower() for s in re.split("""([0-9]+)""", lowercase__    )]

			return sorted(lowercase__, key=lowercase__    )


if __name__ == "__main__":
					import doctest

					doctest.testmod()




