
"""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    :      Any    ,			snake_case__    :      str    ,			):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       parent
												__a:    List[Any]		     =       1_3
												__a:    Any		     =       7
												__a:    str		     =       True
												__a:    Optional[Any]		     =       True
												__a:    Union[str, Any]		     =       True
												__a:    Any		     =       True
												__a:    Optional[Any]		     =       True
												__a:    int		     =       9_9
												__a:    int		     =       3_2
												__a:    Tuple		     =       2
												__a:    Any		     =       4
												__a:    List[str]		     =       3_7
												__a:    int		     =       """gelu"""
												__a:    int		     =       0.1
												__a:    str		     =       0.1
												__a:    Any		     =       5_1_2
												__a:    int		     =       1_6
												__a:    int		     =       2
												__a:    str		     =       0.02
												__a:    int		     =       3
												__a:    Dict		     =       4
												__a:    Optional[int]		     =       None
												__a:    str		     =       self.vocab_size - 1






							def 				__lowercase	(       self    :      str     ):






												"""simple docstring"""



												__a:    Optional[int]		     =       ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size     )

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

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

												__a:    List[str]		     =       None
												if self.use_mc_token_ids:
																	__a:    Optional[Any]		     =       ids_tensor([self.batch_size, self.num_choices]    ,			self.seq_length     )

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

												__a:    int		     =       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:    Optional[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 				__lowercase	(       self    :      List[Any]    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      Optional[int]    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      int    ,			snake_case__    :      Tuple    ,			*snake_case__    :      Dict     ):






												"""simple docstring"""



												__a:    Optional[int]		     =       TFOpenAIGPTModel(config=snake_case__     )
												__a:    str		     =       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
												__a:    List[Any]		     =       model(snake_case__     )

												__a:    List[Any]		     =       [input_ids, input_mask]
												__a:    Optional[Any]		     =       model(snake_case__     )

												__a:    Dict		     =       model(snake_case__     )

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






							def 				__lowercase	(       self    :      List[str]    ,			snake_case__    :      List[str]    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      List[str]    ,			snake_case__    :      List[str]    ,			snake_case__    :      Any    ,			*snake_case__    :      Optional[Any]     ):






												"""simple docstring"""



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






							def 				__lowercase	(       self    :      Optional[int]    ,			snake_case__    :      Optional[int]    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      List[Any]    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      List[str]    ,			*snake_case__    :      Union[str, Any]     ):






												"""simple docstring"""



												__a:    List[str]		     =       TFOpenAIGPTDoubleHeadsModel(config=snake_case__     )

												__a:    str		     =       tf.tile(tf.expand_dims(snake_case__    ,			1     )    ,			(1, self.num_choices, 1)     )
												__a:    str		     =       tf.tile(tf.expand_dims(snake_case__    ,			1     )    ,			(1, self.num_choices, 1)     )
												__a:    List[str]		     =       tf.tile(tf.expand_dims(snake_case__    ,			1     )    ,			(1, self.num_choices, 1)     )

												__a:    int		     =       {
												    """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:    List[str]		     =       model(snake_case__     )
												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 				__lowercase	(       self    :      Optional[Any]    ,			snake_case__    :      List[Any]    ,			snake_case__    :      List[Any]    ,			snake_case__    :      str    ,			snake_case__    :      Optional[int]    ,			snake_case__    :      List[str]    ,			*snake_case__    :      Optional[Any]     ):






												"""simple docstring"""



												__a:    Optional[Any]		     =       self.num_labels
												__a:    Optional[Any]		     =       ids_tensor([self.batch_size]    ,			self.type_sequence_label_size     )
												__a:    Dict		     =       {
												    """input_ids""": input_ids,
												    """attention_mask""": input_mask,
												    """token_type_ids""": token_type_ids,
												    """labels""": sequence_labels,
												}
												__a:    List[str]		     =       TFOpenAIGPTForSequenceClassification(snake_case__     )
												__a:    int		     =       model(snake_case__     )
												self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels)     )






							def 				__lowercase	(       self    :      Tuple     ):






												"""simple docstring"""



												__a:    List[Any]		     =       self.prepare_config_and_inputs()

												(
												    (
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					(
												    __a
												)     ,					
												):    Optional[int]		     =       config_and_inputs

												__a:    Optional[Any]		     =       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
												return config, inputs_dict






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






							def 				__lowercase	(       self    :      Dict    ,			snake_case__    :      Union[str, Any]    ,			snake_case__    :      Optional[int]    ,			snake_case__    :      Tuple    ,			snake_case__    :      List[Any]    ,			snake_case__    :      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 				__lowercase	(       self    :      List[Any]     ):






												"""simple docstring"""



												__a:    str		     =       TFOpenAIGPTModelTester(self     )
												__a:    Optional[int]		     =       ConfigTester(self    ,			config_class=snake_case__    ,			n_embd=3_7     )






							def 				__lowercase	(       self    :      str     ):






												"""simple docstring"""



												self.config_tester.run_common_tests()






							def 				__lowercase	(       self    :      Union[str, Any]     ):






												"""simple docstring"""



												__a:    Optional[Any]		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_model(*snake_case__     )






							def 				__lowercase	(       self    :      List[Any]     ):






												"""simple docstring"""



												__a:    Dict		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_lm_head(*snake_case__     )






							def 				__lowercase	(       self    :      str     ):






												"""simple docstring"""



												__a:    str		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_double_head(*snake_case__     )






							def 				__lowercase	(       self    :      Tuple     ):






												"""simple docstring"""



												__a:    List[Any]		     =       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case__     )






							@slow
							def 				__lowercase	(       self    :      Optional[Any]     ):






												"""simple docstring"""



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






@require_tf
class  _snake_case	(    unittest.TestCase							):






							@slow
							def 				__lowercase	(       self    :      Union[str, Any]     ):






												"""simple docstring"""



												__a:    int		     =       TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""     )
												__a:    Optional[int]		     =       tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]    ,			dtype=tf.intaa     )  # the president is
												__a:    Dict		     =       [
												    4_8_1,
												    4_7_3_5,
												    5_4_4,
												    2_4_6,
												    9_6_3,
												    8_7_0,
												    7_6_2,
												    2_3_9,
												    2_4_4,
												    4_0_4_7_7,
												    2_4_4,
												    2_4_9,
												    7_1_9,
												    8_8_1,
												    4_8_7,
												    5_4_4,
												    2_4_0,
												    2_4_4,
												    6_0_3,
												    4_8_1,
												]  # the president is a very good man. " \n " i\'m sure he is, " said the

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


"""simple docstring"""
import unittest
from dataclasses import dataclass
from typing import Optional

from transformers.utils import ModelOutput




@dataclass
class  _snake_case	(    _lowercase							):
							SCREAMING_SNAKE_CASE		  =    42
							SCREAMING_SNAKE_CASE		  =    None
							SCREAMING_SNAKE_CASE		  =    None






class  _snake_case	(    unittest.TestCase							):






							def 				__lowercase	(       self    :      Optional[Any]     ):






												"""simple docstring"""



												__a:    Dict		     =       ModelOutputTest(a=3_0     )
												self.assertEqual(x.a    ,			3_0     )
												self.assertIsNone(x.b     )
												self.assertIsNone(x.c     )
												with self.assertRaises(snake_case__     ):
																	__a:    int		     =       x.d






							def 				__lowercase	(       self    :      int     ):






												"""simple docstring"""



												__a:    Tuple		     =       ModelOutputTest(a=3_0    ,			b=1_0     )
												self.assertEqual(x[0]    ,			3_0     )
												self.assertEqual(x[1]    ,			1_0     )
												self.assertEqual(x[:2]    ,			(3_0, 1_0)     )
												self.assertEqual(x[:]    ,			(3_0, 1_0)     )

												__a:    Tuple		     =       ModelOutputTest(a=3_0    ,			c=1_0     )
												self.assertEqual(x[0]    ,			3_0     )
												self.assertEqual(x[1]    ,			1_0     )
												self.assertEqual(x[:2]    ,			(3_0, 1_0)     )
												self.assertEqual(x[:]    ,			(3_0, 1_0)     )






							def 				__lowercase	(       self    :      Tuple     ):






												"""simple docstring"""



												__a:    List[str]		     =       ModelOutputTest(a=3_0    ,			b=1_0     )
												self.assertEqual(x["""a"""]    ,			3_0     )
												self.assertEqual(x["""b"""]    ,			1_0     )
												with self.assertRaises(snake_case__     ):
																	__a:    Union[str, Any]		     =       x["""c"""]

												__a:    str		     =       ModelOutputTest(a=3_0    ,			c=1_0     )
												self.assertEqual(x["""a"""]    ,			3_0     )
												self.assertEqual(x["""c"""]    ,			1_0     )
												with self.assertRaises(snake_case__     ):
																	__a:    List[str]		     =       x["""b"""]






							def 				__lowercase	(       self    :      List[str]     ):






												"""simple docstring"""



												__a:    str		     =       ModelOutputTest(a=3_0     )
												self.assertEqual(list(x.keys()     )    ,			["""a"""]     )
												self.assertEqual(list(x.values()     )    ,			[3_0]     )
												self.assertEqual(list(x.items()     )    ,			[("""a""", 3_0)]     )
												self.assertEqual(list(snake_case__     )    ,			["""a"""]     )

												__a:    Optional[Any]		     =       ModelOutputTest(a=3_0    ,			b=1_0     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """b"""]     )
												self.assertEqual(list(x.values()     )    ,			[3_0, 1_0]     )
												self.assertEqual(list(x.items()     )    ,			[("""a""", 3_0), ("""b""", 1_0)]     )
												self.assertEqual(list(snake_case__     )    ,			["""a""", """b"""]     )

												__a:    Union[str, Any]		     =       ModelOutputTest(a=3_0    ,			c=1_0     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """c"""]     )
												self.assertEqual(list(x.values()     )    ,			[3_0, 1_0]     )
												self.assertEqual(list(x.items()     )    ,			[("""a""", 3_0), ("""c""", 1_0)]     )
												self.assertEqual(list(snake_case__     )    ,			["""a""", """c"""]     )

												with self.assertRaises(snake_case__     ):
																	__a:    List[Any]		     =       x.update({"""d""": 2_0}     )
												with self.assertRaises(snake_case__     ):
																	del x["a"]
												with self.assertRaises(snake_case__     ):
																	__a:    Union[str, Any]		     =       x.pop("""a"""     )
												with self.assertRaises(snake_case__     ):
																	__a:    List[Any]		     =       x.setdefault("""d"""    ,			3_2     )






							def 				__lowercase	(       self    :      Any     ):






												"""simple docstring"""



												__a:    List[str]		     =       ModelOutputTest(a=3_0     )
												__a:    str		     =       1_0
												self.assertEqual(x.a    ,			1_0     )
												self.assertEqual(x["""a"""]    ,			1_0     )






							def 				__lowercase	(       self    :      Dict     ):






												"""simple docstring"""



												__a:    List[str]		     =       ModelOutputTest(a=3_0     )
												__a:    Any		     =       1_0
												self.assertEqual(x.a    ,			1_0     )
												self.assertEqual(x["""a"""]    ,			1_0     )






							def 				__lowercase	(       self    :      int     ):






												"""simple docstring"""



												__a:    List[Any]		     =       ModelOutputTest({"""a""": 3_0, """b""": 1_0}     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """b"""]     )
												self.assertEqual(x.a    ,			3_0     )
												self.assertEqual(x.b    ,			1_0     )






							def 				__lowercase	(       self    :      List[str]     ):






												"""simple docstring"""



												__a:    Union[str, Any]		     =       ModelOutputTest([("""a""", 3_0), ("""b""", 1_0)]     )
												self.assertEqual(list(x.keys()     )    ,			["""a""", """b"""]     )
												self.assertEqual(x.a    ,			3_0     )
												self.assertEqual(x.b    ,			1_0     )

												with self.assertRaises(snake_case__     ):
																	__a:    str		     =       ModelOutputTest([("""a""", 3_0), (1_0, 1_0)]     )

												__a:    str		     =       ModelOutputTest(a=(3_0, 3_0)     )
												self.assertEqual(list(x.keys()     )    ,			["""a"""]     )
												self.assertEqual(x.a    ,			(3_0, 3_0)     )
