



'''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 _A  :





		def __init__(     self ,       __UpperCAmelCase ,       )       ->     Optional[Any]:

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





		def 							__A       (     self				)       ->     str:

					'''simple docstring'''
					__UpperCAmelCase   :      Dict										=     ids_tensor([self.batch_size, self.seq_length] ,       self.vocab_size				)

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

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

					__UpperCAmelCase   :      Tuple										=     None
					if self.use_mc_token_ids:
								__UpperCAmelCase   :      List[Any]										=     ids_tensor([self.batch_size, self.num_choices] ,       self.seq_length				)

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

					__UpperCAmelCase   :      Union[str, 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 ,       )

					__UpperCAmelCase   :      Union[str, 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 							__A       (     self ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       *__UpperCAmelCase				)       ->     Tuple:

					'''simple docstring'''
					__UpperCAmelCase   :      Any										=     TFOpenAIGPTModel(config=__UpperCAmelCase				)
					__UpperCAmelCase   :      str										=     {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
					__UpperCAmelCase   :      Dict										=     model(__UpperCAmelCase				)

					__UpperCAmelCase   :      Union[str, Any]										=     [input_ids, input_mask]
					__UpperCAmelCase   :      Tuple										=     model(__UpperCAmelCase				)

					__UpperCAmelCase   :      Dict										=     model(__UpperCAmelCase				)

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





		def 							__A       (     self ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       *__UpperCAmelCase				)       ->     Optional[int]:

					'''simple docstring'''
					__UpperCAmelCase   :      List[str]										=     TFOpenAIGPTLMHeadModel(config=__UpperCAmelCase				)
					__UpperCAmelCase   :      Tuple										=     {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
					__UpperCAmelCase   :      List[Any]										=     model(__UpperCAmelCase				)
					self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.seq_length, self.vocab_size)				)





		def 							__A       (     self ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       *__UpperCAmelCase				)       ->     int:

					'''simple docstring'''
					__UpperCAmelCase   :      str										=     TFOpenAIGPTDoubleHeadsModel(config=__UpperCAmelCase				)

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

					__UpperCAmelCase   :      str										=     {
					    """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,
					}
					__UpperCAmelCase   :      List[str]										=     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 							__A       (     self ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       *__UpperCAmelCase				)       ->     Tuple:

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





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

					'''simple docstring'''
					__UpperCAmelCase   :      Optional[int]										=     self.prepare_config_and_inputs()

					(
					    (
					    __UpperCAmelCase
					)				,  (
					    __UpperCAmelCase
					)				,  (
					    __UpperCAmelCase
					)				,  (
					    __UpperCAmelCase
					)				,  (
					    __UpperCAmelCase
					)				,  (
					    __UpperCAmelCase
					)				,  (
					    __UpperCAmelCase
					)				,  (
					    __UpperCAmelCase
					)				,  (
					    __UpperCAmelCase
					)				,  
					)   :      Dict										=     config_and_inputs

					__UpperCAmelCase   :      int										=     {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
					return config, inputs_dict



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





		def 							__A       (     self ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase ,       __UpperCAmelCase				)       ->     List[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 							__A       (     self				)       ->     Optional[int]:

					'''simple docstring'''
					__UpperCAmelCase   :      List[str]										=     TFOpenAIGPTModelTester(self				)
					__UpperCAmelCase   :      Optional[Any]										=     ConfigTester(self ,       config_class=__UpperCAmelCase ,       n_embd=37				)





		def 							__A       (     self				)       ->     Dict:

					'''simple docstring'''
					self.config_tester.run_common_tests()





		def 							__A       (     self				)       ->     Optional[Any]:

					'''simple docstring'''
					__UpperCAmelCase   :      List[str]										=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_model(*__UpperCAmelCase				)





		def 							__A       (     self				)       ->     Any:

					'''simple docstring'''
					__UpperCAmelCase   :      Dict										=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_lm_head(*__UpperCAmelCase				)





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

					'''simple docstring'''
					__UpperCAmelCase   :      Optional[int]										=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_double_head(*__UpperCAmelCase				)





		def 							__A       (     self				)       ->     Optional[Any]:

					'''simple docstring'''
					__UpperCAmelCase   :      Optional[int]										=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__UpperCAmelCase				)





		@slow
		def 							__A       (     self				)       ->     Optional[Any]:

					'''simple docstring'''
					for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__UpperCAmelCase   :      Optional[int]										=     TFOpenAIGPTModel.from_pretrained(__UpperCAmelCase				)
								self.assertIsNotNone(__UpperCAmelCase				)



@require_tf
class _A  (    unittest.TestCase    ):





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

					'''simple docstring'''
					__UpperCAmelCase   :      List[str]										=     TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""				)
					__UpperCAmelCase   :      List[str]										=     tf.convert_to_tensor([[481, 4_735, 544]] ,       dtype=tf.intaa				)  # the president is
					__UpperCAmelCase   :      Optional[int]										=     [
					    481,
					    4_735,
					    544,
					    246,
					    963,
					    870,
					    762,
					    239,
					    244,
					    40_477,
					    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

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









'''simple docstring'''
from __future__ import annotations

from typing import Any



class _A  :





		def __init__(     self ,       __UpperCAmelCase ,       __UpperCAmelCase				)       ->     None:  # noqa: A002

					'''simple docstring'''
					__UpperCAmelCase   :      Tuple										=     item
					__UpperCAmelCase   :      str										=     next



class _A  :





		def __init__(     self				)       ->     None:

					'''simple docstring'''
					__UpperCAmelCase   :      Node | None										=     None
					__UpperCAmelCase   :      Optional[int]										=     0





		def 							__A       (     self ,       __UpperCAmelCase				)       ->     None:

					'''simple docstring'''
					__UpperCAmelCase   :      Optional[Any]										=     Node(__UpperCAmelCase ,       self.head				)
					self.size += 1





		def 							__A       (     self				)       ->     Any:

					'''simple docstring'''
					# Switched 'self.is_empty()' to 'self.head is None'
					# because mypy was considering the possibility that 'self.head'
					# can be None in below else part and giving error
					if self.head is None:
								return None
					else:
								__UpperCAmelCase   :      str										=     self.head.item
								__UpperCAmelCase   :      Tuple										=     self.head.next
								self.size -= 1
								return item





		def 							__A       (     self				)       ->     bool:

					'''simple docstring'''
					return self.head is None





		def __str__(     self				)       ->     str:

					'''simple docstring'''
					if self.is_empty():
								return ""
					else:
								__UpperCAmelCase   :      Union[str, Any]										=     self.head
								__UpperCAmelCase   :      List[Any]										=     """"""
								__UpperCAmelCase   :      list[str]										=     []
								while iterate:
											item_list.append(str(iterate.item				)				)
											__UpperCAmelCase   :      Any										=     iterate.next

								__UpperCAmelCase   :      Dict										=     """ --> """.join(__UpperCAmelCase				)

								return item_str





		def __len__(     self				)       ->     int:

					'''simple docstring'''
					return self.size




