




'''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       :  Optional[Any]							,			lowerCAmelCase_       :  Any							,			)			->							Any:


														"""simple docstring"""


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







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


														"""simple docstring"""


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

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

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

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

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

														_a	     =				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	     =				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       :  Tuple							,			lowerCAmelCase_       :  List[str]							,			lowerCAmelCase_       :  List[str]							,			lowerCAmelCase_       :  Any							,			lowerCAmelCase_       :  Optional[Any]							,			lowerCAmelCase_       :  Union[str, Any]							,			*lowerCAmelCase_       :  Dict					)			->							List[Any]:


														"""simple docstring"""


														_a	     =				TFOpenAIGPTModel(config=lowerCAmelCase_					)
														_a	     =				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														_a	     =				model(lowerCAmelCase_					)

														_a	     =				[input_ids, input_mask]
														_a	     =				model(lowerCAmelCase_					)

														_a	     =				model(lowerCAmelCase_					)

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







							def    __lowerCAmelCase			(  self       :  str							,			lowerCAmelCase_       :  List[Any]							,			lowerCAmelCase_       :  List[Any]							,			lowerCAmelCase_       :  int							,			lowerCAmelCase_       :  Optional[Any]							,			lowerCAmelCase_       :  Union[str, Any]							,			*lowerCAmelCase_       :  Any					)			->							Dict:


														"""simple docstring"""


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







							def    __lowerCAmelCase			(  self       :  Optional[int]							,			lowerCAmelCase_       :  Optional[Any]							,			lowerCAmelCase_       :  Tuple							,			lowerCAmelCase_       :  List[Any]							,			lowerCAmelCase_       :  Dict							,			lowerCAmelCase_       :  List[str]							,			lowerCAmelCase_       :  Dict							,			*lowerCAmelCase_       :  Optional[int]					)			->							Optional[int]:


														"""simple docstring"""


														_a	     =				TFOpenAIGPTDoubleHeadsModel(config=lowerCAmelCase_					)

														_a	     =				tf.tile(tf.expand_dims(lowerCAmelCase_							,			1					)							,			(1, self.num_choices, 1)					)
														_a	     =				tf.tile(tf.expand_dims(lowerCAmelCase_							,			1					)							,			(1, self.num_choices, 1)					)
														_a	     =				tf.tile(tf.expand_dims(lowerCAmelCase_							,			1					)							,			(1, self.num_choices, 1)					)

														_a	     =				{
														    '''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	     =				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    __lowerCAmelCase			(  self       :  str							,			lowerCAmelCase_       :  Optional[Any]							,			lowerCAmelCase_       :  Optional[Any]							,			lowerCAmelCase_       :  Dict							,			lowerCAmelCase_       :  Tuple							,			lowerCAmelCase_       :  List[Any]							,			*lowerCAmelCase_       :  int					)			->							List[str]:


														"""simple docstring"""


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







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


														"""simple docstring"""


														_a	     =				self.prepare_config_and_inputs()

														(
														    (
														    _a
														)     ,							(
														    _a
														)     ,							(
														    _a
														)     ,							(
														    _a
														)     ,							(
														    _a
														)     ,							(
														    _a
														)     ,							(
														    _a
														)     ,							(
														    _a
														)     ,							(
														    _a
														)     ,							
														)	     =				config_and_inputs

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



@require_tf
class 			A    (		_a     ,_a     ,unittest.TestCase    ):
							lowercase_							=				(
							    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
							    if is_tf_available()
							    else ()
							)
							lowercase_							=				(
							    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
							)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
							lowercase_							=				(
							    {
							        'feature-extraction': TFOpenAIGPTModel,
							        'text-classification': TFOpenAIGPTForSequenceClassification,
							        'text-generation': TFOpenAIGPTLMHeadModel,
							        'zero-shot': TFOpenAIGPTForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							lowercase_							=				False
							lowercase_							=				False







							def    __lowerCAmelCase			(  self       :  Tuple							,			lowerCAmelCase_       :  Tuple							,			lowerCAmelCase_       :  List[Any]							,			lowerCAmelCase_       :  int							,			lowerCAmelCase_       :  Optional[Any]							,			lowerCAmelCase_       :  Union[str, Any]					)			->							Tuple:


														"""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       :  List[Any]					)			->							Any:


														"""simple docstring"""


														_a	     =				TFOpenAIGPTModelTester(self					)
														_a	     =				ConfigTester(self							,			config_class=lowerCAmelCase_							,			n_embd=37					)







							def    __lowerCAmelCase			(  self       :  Optional[Any]					)			->							List[str]:


														"""simple docstring"""


														self.config_tester.run_common_tests()







							def    __lowerCAmelCase			(  self       :  str					)			->							Optional[Any]:


														"""simple docstring"""


														_a	     =				self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase_					)







							def    __lowerCAmelCase			(  self       :  int					)			->							Dict:


														"""simple docstring"""


														_a	     =				self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_openai_gpt_lm_head(*lowerCAmelCase_					)







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


														"""simple docstring"""


														_a	     =				self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_openai_gpt_double_head(*lowerCAmelCase_					)







							def    __lowerCAmelCase			(  self       :  List[str]					)			->							Tuple:


														"""simple docstring"""


														_a	     =				self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase_					)







							@slow
							def    __lowerCAmelCase			(  self       :  Any					)			->							int:


														"""simple docstring"""


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



@require_tf
class 			A    (		unittest.TestCase    ):







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


														"""simple docstring"""


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

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







'''simple docstring'''


from dataclasses import dataclass
from typing import Callable, Optional

import datasets

@dataclass
class 			A    (		datasets.BuilderConfig    ):
							lowercase_							=				None
							lowercase_							=				None
							lowercase_							=				None







							def    __lowerCAmelCase			(  self       :  Dict					)			->							List[str]:


														"""simple docstring"""


														assert self.generator is not None, "generator must be specified"

														if self.gen_kwargs is None:
																					_a	     =				{}



class 			A    (		datasets.GeneratorBasedBuilder    ):
							lowercase_							=				GeneratorConfig







							def    __lowerCAmelCase			(  self       :  Any					)			->							List[str]:


														"""simple docstring"""


														return datasets.DatasetInfo(features=self.config.features					)







							def    __lowerCAmelCase			(  self       :  Dict							,			lowerCAmelCase_       :  Optional[int]					)			->							Optional[Any]:


														"""simple docstring"""


														return [datasets.SplitGenerator(name=datasets.Split.TRAIN							,			gen_kwargs=self.config.gen_kwargs					)]







							def    __lowerCAmelCase			(  self       :  str							,			**lowerCAmelCase_       :  int					)			->							List[str]:


														"""simple docstring"""


														for idx, ex in enumerate(self.config.generator(**lowerCAmelCase_					)					):
																					yield idx, ex

