


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






						"""simple docstring"""




						def __init__(   self				,							a_				,							):
							lowerCamelCase_			:							Optional[Any]      =       parent
							lowerCamelCase_			:							Optional[Any]      =       13
							lowerCamelCase_			:							str      =       7
							lowerCamelCase_			:							Any      =       True
							lowerCamelCase_			:							Dict      =       True
							lowerCamelCase_			:							Union[str, Any]      =       True
							lowerCamelCase_			:							Tuple      =       True
							lowerCamelCase_			:							List[Any]      =       True
							lowerCamelCase_			:							Tuple      =       99
							lowerCamelCase_			:							str      =       32
							lowerCamelCase_			:							List[str]      =       2
							lowerCamelCase_			:							Dict      =       4
							lowerCamelCase_			:							int      =       37
							lowerCamelCase_			:							Union[str, Any]      =       "gelu"
							lowerCamelCase_			:							Any      =       0.1
							lowerCamelCase_			:							List[str]      =       0.1
							lowerCamelCase_			:							List[Any]      =       512
							lowerCamelCase_			:							int      =       16
							lowerCamelCase_			:							Dict      =       2
							lowerCamelCase_			:							Any      =       0.02
							lowerCamelCase_			:							List[str]      =       3
							lowerCamelCase_			:							Optional[Any]      =       4
							lowerCamelCase_			:							str      =       None
							lowerCamelCase_			:							Union[str, Any]      =       self.vocab_size - 1




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							Union[str, Any]      =       ids_tensor([self.batch_size, self.seq_length]				,							self.vocab_size     )

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

							lowerCamelCase_			:							Optional[int]      =       None
							if self.use_token_type_ids:
								lowerCamelCase_			:							List[Any]      =       ids_tensor([self.batch_size, self.seq_length]				,							self.type_vocab_size     )

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

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

							lowerCamelCase_			:							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				,							)

							lowerCamelCase_			:							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 					_UpperCamelCase			(   self				,							a_				,							a_				,							a_				,							a_				,							a_				,							*a_     ):
							lowerCamelCase_			:							Any      =       TFOpenAIGPTModel(config=a_     )
							lowerCamelCase_			:							int      =       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							lowerCamelCase_			:							Dict      =       model(a_     )

							lowerCamelCase_			:							Optional[Any]      =       [input_ids, input_mask]
							lowerCamelCase_			:							Optional[int]      =       model(a_     )

							lowerCamelCase_			:							List[str]      =       model(a_     )

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




						def 					_UpperCamelCase			(   self				,							a_				,							a_				,							a_				,							a_				,							a_				,							*a_     ):
							lowerCamelCase_			:							Any      =       TFOpenAIGPTLMHeadModel(config=a_     )
							lowerCamelCase_			:							Optional[int]      =       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							lowerCamelCase_			:							Union[str, Any]      =       model(a_     )
							self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.seq_length, self.vocab_size)     )




						def 					_UpperCamelCase			(   self				,							a_				,							a_				,							a_				,							a_				,							a_				,							a_				,							*a_     ):
							lowerCamelCase_			:							List[str]      =       TFOpenAIGPTDoubleHeadsModel(config=a_     )

							lowerCamelCase_			:							List[Any]      =       tf.tile(tf.expand_dims(a_				,							1     )				,							(1, self.num_choices, 1)     )
							lowerCamelCase_			:							List[Any]      =       tf.tile(tf.expand_dims(a_				,							1     )				,							(1, self.num_choices, 1)     )
							lowerCamelCase_			:							Any      =       tf.tile(tf.expand_dims(a_				,							1     )				,							(1, self.num_choices, 1)     )

							lowerCamelCase_			:							List[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,
							}
							lowerCamelCase_			:							Tuple      =       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 					_UpperCamelCase			(   self				,							a_				,							a_				,							a_				,							a_				,							a_				,							*a_     ):
							lowerCamelCase_			:							Union[str, Any]      =       self.num_labels
							lowerCamelCase_			:							List[str]      =       ids_tensor([self.batch_size]				,							self.type_sequence_label_size     )
							lowerCamelCase_			:							Tuple      =       {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							    "labels": sequence_labels,
							}
							lowerCamelCase_			:							List[Any]      =       TFOpenAIGPTForSequenceClassification(a_     )
							lowerCamelCase_			:							Optional[Any]      =       model(a_     )
							self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.num_labels)     )




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							List[Any]      =       self.prepare_config_and_inputs()

							(
							    lowerCamelCase_  
							)			:							Union[str, Any]      =       config_and_inputs

							lowerCamelCase_			:							List[str]      =       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict





@require_tf
class    lowerCAmelCase__				(			snake_case__,    snake_case__,    unittest.TestCase       ):






						"""simple docstring"""
						__UpperCAmelCase   :		int  					=	(
						    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
						    if is_tf_available()
						    else ()
						)
						__UpperCAmelCase   :		Optional[Any]  					=	(
						    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
						)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
						__UpperCAmelCase   :		int  					=	(
						    {
						        "feature-extraction": TFOpenAIGPTModel,
						        "text-classification": TFOpenAIGPTForSequenceClassification,
						        "text-generation": TFOpenAIGPTLMHeadModel,
						        "zero-shot": TFOpenAIGPTForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						__UpperCAmelCase   :		str  					=	False
						__UpperCAmelCase   :		Dict  					=	False




						def 					_UpperCamelCase			(   self				,							a_				,							a_				,							a_				,							a_				,							a_     ):
							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 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							List[str]      =       TFOpenAIGPTModelTester(self     )
							lowerCamelCase_			:							List[str]      =       ConfigTester(self				,							config_class=a_				,							n_embd=37     )




						def 					_UpperCamelCase			(   self     ):
							self.config_tester.run_common_tests()




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							Any      =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_model(*a_     )




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							Optional[Any]      =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_lm_head(*a_     )




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							int      =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_double_head(*a_     )




						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							int      =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a_     )




						@slow
						def 					_UpperCamelCase			(   self     ):
							for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								lowerCamelCase_			:							int      =       TFOpenAIGPTModel.from_pretrained(a_     )
								self.assertIsNotNone(a_     )





@require_tf
class    lowerCAmelCase__				(			unittest.TestCase       ):






						"""simple docstring"""




						@slow
						def 					_UpperCamelCase			(   self     ):
							lowerCamelCase_			:							Optional[int]      =       TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"     )
							lowerCamelCase_			:							List[str]      =       tf.convert_to_tensor([[481, 4735, 544]]				,							dtype=tf.intaa     )  # the president is
							lowerCamelCase_			:							int      =       [
							    481,
							    4735,
							    544,
							    246,
							    963,
							    870,
							    762,
							    239,
							    244,
							    4_0477,
							    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

							lowerCamelCase_			:							Optional[int]      =       model.generate(a_				,							do_sample=a_     )
							self.assertListEqual(output_ids[0].numpy().tolist()				,							a_     )





from typing import TYPE_CHECKING

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


__magic_name__       					=      {'''configuration_levit''': ['''LEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''LevitConfig''', '''LevitOnnxConfig''']}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__magic_name__       					=      ['''LevitFeatureExtractor''']
							__magic_name__       					=      ['''LevitImageProcessor''']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__magic_name__       					=      [
							    '''LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''LevitForImageClassification''',
							    '''LevitForImageClassificationWithTeacher''',
							    '''LevitModel''',
							    '''LevitPreTrainedModel''',
							]


if TYPE_CHECKING:
							from .configuration_levit import LEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, LevitConfig, LevitOnnxConfig

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_levit import LevitFeatureExtractor
														from .image_processing_levit import LevitImageProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_levit import (
														    LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    LevitForImageClassification,
														    LevitForImageClassificationWithTeacher,
														    LevitModel,
														    LevitPreTrainedModel,
														)
else:
							import sys

							__magic_name__       					=      _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

