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




					"""simple docstring"""

					def __init__(							self  :      Any       ,lowerCamelCase__  :      List[str]       ,):
									UpperCAmelCase__			 =				parent
									UpperCAmelCase__			 =				13
									UpperCAmelCase__			 =				7
									UpperCAmelCase__			 =				True
									UpperCAmelCase__			 =				True
									UpperCAmelCase__			 =				True
									UpperCAmelCase__			 =				True
									UpperCAmelCase__			 =				True
									UpperCAmelCase__			 =				99
									UpperCAmelCase__			 =				32
									UpperCAmelCase__			 =				2
									UpperCAmelCase__			 =				4
									UpperCAmelCase__			 =				37
									UpperCAmelCase__			 =				'gelu'
									UpperCAmelCase__			 =				0.1
									UpperCAmelCase__			 =				0.1
									UpperCAmelCase__			 =				512
									UpperCAmelCase__			 =				16
									UpperCAmelCase__			 =				2
									UpperCAmelCase__			 =				0.0_2
									UpperCAmelCase__			 =				3
									UpperCAmelCase__			 =				4
									UpperCAmelCase__			 =				None
									UpperCAmelCase__			 =				self.vocab_size - 1







					def 						__lowerCAmelCase		(							self  :      Dict     ):
									UpperCAmelCase__			 =				ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size     )

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

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

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

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

									UpperCAmelCase__			 =				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__			 =				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  :      Optional[Any]       ,lowerCamelCase__  :      List[Any]       ,lowerCamelCase__  :      List[Any]       ,lowerCamelCase__  :      List[str]       ,lowerCamelCase__  :      Any       ,lowerCamelCase__  :      int       ,*lowerCamelCase__  :      Tuple     ):
									UpperCAmelCase__			 =				TFOpenAIGPTModel(config=__A     )
									UpperCAmelCase__			 =				{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
									UpperCAmelCase__			 =				model(__A     )

									UpperCAmelCase__			 =				[input_ids, input_mask]
									UpperCAmelCase__			 =				model(__A     )

									UpperCAmelCase__			 =				model(__A     )

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







					def 						__lowerCAmelCase		(							self  :      Optional[int]       ,lowerCamelCase__  :      Tuple       ,lowerCamelCase__  :      Dict       ,lowerCamelCase__  :      Optional[int]       ,lowerCamelCase__  :      Union[str, Any]       ,lowerCamelCase__  :      int       ,*lowerCamelCase__  :      Dict     ):
									UpperCAmelCase__			 =				TFOpenAIGPTLMHeadModel(config=__A     )
									UpperCAmelCase__			 =				{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
									UpperCAmelCase__			 =				model(__A     )
									self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length, self.vocab_size)     )







					def 						__lowerCAmelCase		(							self  :      int       ,lowerCamelCase__  :      int       ,lowerCamelCase__  :      List[Any]       ,lowerCamelCase__  :      Optional[int]       ,lowerCamelCase__  :      List[Any]       ,lowerCamelCase__  :      List[str]       ,lowerCamelCase__  :      Optional[int]       ,*lowerCamelCase__  :      List[str]     ):
									UpperCAmelCase__			 =				TFOpenAIGPTDoubleHeadsModel(config=__A     )

									UpperCAmelCase__			 =				tf.tile(tf.expand_dims(__A       ,1     )       ,(1, self.num_choices, 1)     )
									UpperCAmelCase__			 =				tf.tile(tf.expand_dims(__A       ,1     )       ,(1, self.num_choices, 1)     )
									UpperCAmelCase__			 =				tf.tile(tf.expand_dims(__A       ,1     )       ,(1, self.num_choices, 1)     )

									UpperCAmelCase__			 =				{
									    '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__			 =				model(__A     )
									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  :      Optional[int]       ,lowerCamelCase__  :      List[str]       ,lowerCamelCase__  :      Dict       ,lowerCamelCase__  :      List[Any]       ,lowerCamelCase__  :      Union[str, Any]       ,lowerCamelCase__  :      Any       ,*lowerCamelCase__  :      str     ):
									UpperCAmelCase__			 =				self.num_labels
									UpperCAmelCase__			 =				ids_tensor([self.batch_size]       ,self.type_sequence_label_size     )
									UpperCAmelCase__			 =				{
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									    'token_type_ids': token_type_ids,
									    'labels': sequence_labels,
									}
									UpperCAmelCase__			 =				TFOpenAIGPTForSequenceClassification(__A     )
									UpperCAmelCase__			 =				model(__A     )
									self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_labels)     )







					def 						__lowerCAmelCase		(							self  :      Optional[Any]     ):
									UpperCAmelCase__			 =				self.prepare_config_and_inputs()

									(
									    (
									    UpperCAmelCase__
									) ,      (
									    UpperCAmelCase__
									) ,      (
									    UpperCAmelCase__
									) ,      (
									    UpperCAmelCase__
									) ,      (
									    UpperCAmelCase__
									) ,      (
									    UpperCAmelCase__
									) ,      (
									    UpperCAmelCase__
									) ,      (
									    UpperCAmelCase__
									) ,      (
									    UpperCAmelCase__
									) ,      
									)			 =				config_and_inputs

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






@require_tf
class 							snake_case			(					__snake_case ,       __snake_case ,       unittest.TestCase							):




					"""simple docstring"""

					snake_case__							       =					(
					    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
					    if is_tf_available()
					    else ()
					)
					snake_case__							       =					(
					    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
					)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
					snake_case__							       =					(
					    {
					        "feature-extraction": TFOpenAIGPTModel,
					        "text-classification": TFOpenAIGPTForSequenceClassification,
					        "text-generation": TFOpenAIGPTLMHeadModel,
					        "zero-shot": TFOpenAIGPTForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)
					snake_case__							       =					False
					snake_case__							       =					False







					def 						__lowerCAmelCase		(							self  :      Dict       ,lowerCamelCase__  :      str       ,lowerCamelCase__  :      Optional[Any]       ,lowerCamelCase__  :      List[Any]       ,lowerCamelCase__  :      int       ,lowerCamelCase__  :      Tuple     ):

									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]     ):
									UpperCAmelCase__			 =				TFOpenAIGPTModelTester(self     )
									UpperCAmelCase__			 =				ConfigTester(self       ,config_class=__A       ,n_embd=37     )







					def 						__lowerCAmelCase		(							self  :      Tuple     ):
									self.config_tester.run_common_tests()







					def 						__lowerCAmelCase		(							self  :      str     ):
									UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*__A     )







					def 						__lowerCAmelCase		(							self  :      Optional[Any]     ):
									UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_lm_head(*__A     )







					def 						__lowerCAmelCase		(							self  :      str     ):
									UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_double_head(*__A     )







					def 						__lowerCAmelCase		(							self  :      Dict     ):
									UpperCAmelCase__			 =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__A     )







					@slow
					def 						__lowerCAmelCase		(							self  :      int     ):

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






@require_tf
class 							snake_case			(					unittest.TestCase							):




					"""simple docstring"""

					@slow
					def 						__lowerCAmelCase		(							self  :      List[str]     ):
									UpperCAmelCase__			 =				TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'     )
									UpperCAmelCase__			 =				tf.convert_to_tensor([[481, 4_735, 544]]       ,dtype=tf.intaa     )  # the president is
									UpperCAmelCase__			 =				[
									    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__			 =				model.generate(__A       ,do_sample=__A     )
									self.assertListEqual(output_ids[0].numpy().tolist()       ,__A     )











"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__a							:						int   							=			{
    'configuration_markuplm': ['MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MarkupLMConfig'],
    'feature_extraction_markuplm': ['MarkupLMFeatureExtractor'],
    'processing_markuplm': ['MarkupLMProcessor'],
    'tokenization_markuplm': ['MarkupLMTokenizer'],
}

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__a							:						List[Any]   							=			['MarkupLMTokenizerFast']

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__a							:						List[Any]   							=			[
	    'MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'MarkupLMForQuestionAnswering',
	    'MarkupLMForSequenceClassification',
	    'MarkupLMForTokenClassification',
	    'MarkupLMModel',
	    'MarkupLMPreTrainedModel',
	]


if TYPE_CHECKING:
	from .configuration_markuplm import MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP, MarkupLMConfig
	from .feature_extraction_markuplm import MarkupLMFeatureExtractor
	from .processing_markuplm import MarkupLMProcessor
	from .tokenization_markuplm import MarkupLMTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_markuplm_fast import MarkupLMTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_markuplm import (
		    MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    MarkupLMForQuestionAnswering,
		    MarkupLMForSequenceClassification,
		    MarkupLMForTokenClassification,
		    MarkupLMModel,
		    MarkupLMPreTrainedModel,
		)


else:
	import sys

	__a							:						Tuple   							=			_LazyModule(__name__, globals()['__file__'], _import_structure)


