


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       :						Optional[int]	,      _lowerCAmelCase       :						Union[str, Any]	,      )							->					str:



											'''simple docstring'''





											__magic_name__					=      parent
											__magic_name__					=      1_3
											__magic_name__					=      7
											__magic_name__					=      True
											__magic_name__					=      True
											__magic_name__					=      True
											__magic_name__					=      True
											__magic_name__					=      True
											__magic_name__					=      9_9
											__magic_name__					=      3_2
											__magic_name__					=      2
											__magic_name__					=      4
											__magic_name__					=      3_7
											__magic_name__					=      'gelu'
											__magic_name__					=      0.1
											__magic_name__					=      0.1
											__magic_name__					=      5_1_2
											__magic_name__					=      1_6
											__magic_name__					=      2
											__magic_name__					=      0.02
											__magic_name__					=      3
											__magic_name__					=      4
											__magic_name__					=      None
											__magic_name__					=      self.vocab_size - 1
					def 	a			(						self       :						Optional[int]  )							->					Union[str, Any]:



											'''simple docstring'''





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

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

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

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

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

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

											__magic_name__					=      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       :						Tuple	,      _lowerCAmelCase       :						Any	,      _lowerCAmelCase       :						Tuple	,      _lowerCAmelCase       :						List[Any]	,      _lowerCAmelCase       :						Tuple	,      _lowerCAmelCase       :						Any	,      *_lowerCAmelCase       :						Union[str, Any]  )							->					Union[str, Any]:



											'''simple docstring'''





											__magic_name__					=      TFOpenAIGPTModel(config=_lowerCAmelCase  )
											__magic_name__					=      {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											__magic_name__					=      model(_lowerCAmelCase  )

											__magic_name__					=      [input_ids, input_mask]
											__magic_name__					=      model(_lowerCAmelCase  )

											__magic_name__					=      model(_lowerCAmelCase  )

											self.parent.assertEqual(result.last_hidden_state.shape	,      (self.batch_size, self.seq_length, self.hidden_size)  )
					def 	a			(						self       :						Any	,      _lowerCAmelCase       :						Union[str, Any]	,      _lowerCAmelCase       :						int	,      _lowerCAmelCase       :						List[Any]	,      _lowerCAmelCase       :						Tuple	,      _lowerCAmelCase       :						Union[str, Any]	,      *_lowerCAmelCase       :						str  )							->					List[Any]:



											'''simple docstring'''





											__magic_name__					=      TFOpenAIGPTLMHeadModel(config=_lowerCAmelCase  )
											__magic_name__					=      {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
											__magic_name__					=      model(_lowerCAmelCase  )
											self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.seq_length, self.vocab_size)  )
					def 	a			(						self       :						List[Any]	,      _lowerCAmelCase       :						Any	,      _lowerCAmelCase       :						Optional[int]	,      _lowerCAmelCase       :						Optional[int]	,      _lowerCAmelCase       :						Optional[int]	,      _lowerCAmelCase       :						Any	,      _lowerCAmelCase       :						Any	,      *_lowerCAmelCase       :						Dict  )							->					Optional[int]:



											'''simple docstring'''





											__magic_name__					=      TFOpenAIGPTDoubleHeadsModel(config=_lowerCAmelCase  )

											__magic_name__					=      tf.tile(tf.expand_dims(_lowerCAmelCase	,      1  )	,      (1, self.num_choices, 1)  )
											__magic_name__					=      tf.tile(tf.expand_dims(_lowerCAmelCase	,      1  )	,      (1, self.num_choices, 1)  )
											__magic_name__					=      tf.tile(tf.expand_dims(_lowerCAmelCase	,      1  )	,      (1, self.num_choices, 1)  )

											__magic_name__					=      {
											    '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,
											}
											__magic_name__					=      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       :						Union[str, Any]	,      _lowerCAmelCase       :						List[str]	,      _lowerCAmelCase       :						List[Any]	,      _lowerCAmelCase       :						Optional[Any]	,      _lowerCAmelCase       :						Dict	,      _lowerCAmelCase       :						str	,      *_lowerCAmelCase       :						int  )							->					str:



											'''simple docstring'''





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







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



											'''simple docstring'''





											__magic_name__					=      self.prepare_config_and_inputs()

											(
											    (
											    __magic_name__
											)		,      (
											    __magic_name__
											)		,      (
											    __magic_name__
											)		,      (
											    __magic_name__
											)		,      (
											    __magic_name__
											)		,      (
											    __magic_name__
											)		,      (
											    __magic_name__
											)		,      (
											    __magic_name__
											)		,      (
											    __magic_name__
											)		,      
											)					=      config_and_inputs

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







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



											'''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       :						str  )							->					Union[str, Any]:



											'''simple docstring'''





											__magic_name__					=      TFOpenAIGPTModelTester(self  )
											__magic_name__					=      ConfigTester(self	,      config_class=_lowerCAmelCase	,      n_embd=3_7  )
					def 	a			(						self       :						Optional[Any]  )							->					Union[str, Any]:



											'''simple docstring'''





											self.config_tester.run_common_tests()
					def 	a			(						self       :						int  )							->					Optional[int]:



											'''simple docstring'''





											__magic_name__					=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_model(*_lowerCAmelCase  )
					def 	a			(						self       :						Any  )							->					List[Any]:



											'''simple docstring'''





											__magic_name__					=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_lm_head(*_lowerCAmelCase  )
					def 	a			(						self       :						Optional[Any]  )							->					Tuple:



											'''simple docstring'''





											__magic_name__					=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_double_head(*_lowerCAmelCase  )
					def 	a			(						self       :						Optional[Any]  )							->					List[str]:



											'''simple docstring'''





											__magic_name__					=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCAmelCase  )







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



											'''simple docstring'''





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







@require_tf
class      __snake_case				(   unittest.TestCase):
					@slow
					def 	a			(						self       :						Union[str, Any]  )							->					Optional[int]:



											'''simple docstring'''





											__magic_name__					=      TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'  )
											__magic_name__					=      tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]	,      dtype=tf.intaa  )  # the president is
											__magic_name__					=      [
											    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

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





from collections import OrderedDict
from dataclasses import fields
from typing import Any, Tuple

import numpy as np

from .import_utils import is_torch_available
def 				UpperCamelCase_	(					__snake_case      :    Dict  )->   str:




						'''simple docstring'''
						if is_torch_available():
												import torch

												if isinstance(__snake_case						,      torch.Tensor  ):
																		return True

						return isinstance(__snake_case						,      np.ndarray  )




class      __snake_case				(   _UpperCAmelCase):
					def 	a			(						self       :						Optional[Any]  )							->					Optional[Any]:



											'''simple docstring'''





											__magic_name__					=      fields(self  )

											# Safety and consistency checks
											if not len(_lowerCAmelCase  ):
																	raise ValueError(F'''{self.__class__.__name__} has no fields.'''  )

											__magic_name__					=      getattr(self	,      class_fields[0].name  )
											__magic_name__					=      all(getattr(self	,      field.name  ) is None for field in class_fields[1:]  )

											if other_fields_are_none and isinstance(_lowerCAmelCase	,      _lowerCAmelCase  ):
																	for key, value in first_field.items():
																							__magic_name__					=      value
											else:
																	for field in class_fields:
																							__magic_name__					=      getattr(self	,      field.name  )
																							if v is not None:
																													__magic_name__					=      v
					def __delitem__(						self       :						Dict	,      *_lowerCAmelCase       :						List[str]	,      **_lowerCAmelCase       :						List[str]  )							->					List[str]:



											'''simple docstring'''





											raise Exception(F'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.'''  )
					def 	a			(						self       :						Optional[int]	,      *_lowerCAmelCase       :						Optional[Any]	,      **_lowerCAmelCase       :						Optional[Any]  )							->					str:



											'''simple docstring'''





											raise Exception(F'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.'''  )
					def 	a			(						self       :						Union[str, Any]	,      *_lowerCAmelCase       :						List[str]	,      **_lowerCAmelCase       :						Any  )							->					str:



											'''simple docstring'''





											raise Exception(F'''You cannot use ``pop`` on a {self.__class__.__name__} instance.'''  )
					def 	a			(						self       :						Any	,      *_lowerCAmelCase       :						str	,      **_lowerCAmelCase       :						Optional[int]  )							->					List[Any]:



											'''simple docstring'''





											raise Exception(F'''You cannot use ``update`` on a {self.__class__.__name__} instance.'''  )
					def __getitem__(						self       :						Optional[Any]	,      _lowerCAmelCase       :						Any  )							->					Tuple:



											'''simple docstring'''





											if isinstance(_lowerCAmelCase	,      _lowerCAmelCase  ):
																	__magic_name__					=      dict(self.items()  )
																	return inner_dict[k]
											else:
																	return self.to_tuple()[k]
					def __setattr__(						self       :						str	,      _lowerCAmelCase       :						Optional[Any]	,      _lowerCAmelCase       :						Optional[int]  )							->					int:



											'''simple docstring'''





											if name in self.keys() and value is not None:
																	# Don't call self.__setitem__ to avoid recursion errors
																	super().__setitem__(_lowerCAmelCase	,      _lowerCAmelCase  )
											super().__setattr__(_lowerCAmelCase	,      _lowerCAmelCase  )
					def __setitem__(						self       :						Optional[Any]	,      _lowerCAmelCase       :						Optional[Any]	,      _lowerCAmelCase       :						List[str]  )							->					Optional[int]:



											'''simple docstring'''





											super().__setitem__(_lowerCAmelCase	,      _lowerCAmelCase  )
											# Don't call self.__setattr__ to avoid recursion errors
											super().__setattr__(_lowerCAmelCase	,      _lowerCAmelCase  )







					def 	a			(						self       :						str  )							->					Tuple[Any]:



											'''simple docstring'''





											return tuple(self[k] for k in self.keys()  )

