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







				def __init__(    self    :Union[str, Any]      ,__UpperCAmelCase    :List[str]      ,)      ->					Any:




									"""simple docstring"""



									lowerCamelCase__		:   Dict  				=					parent
									lowerCamelCase__		:   Union[str, Any]  				=					13
									lowerCamelCase__		:   List[Any]  				=					7
									lowerCamelCase__		:   List[Any]  				=					True
									lowerCamelCase__		:   int  				=					True
									lowerCamelCase__		:   Union[str, Any]  				=					True
									lowerCamelCase__		:   Tuple  				=					True
									lowerCamelCase__		:   Union[str, Any]  				=					True
									lowerCamelCase__		:   List[str]  				=					99
									lowerCamelCase__		:   List[str]  				=					32
									lowerCamelCase__		:   str  				=					2
									lowerCamelCase__		:   str  				=					4
									lowerCamelCase__		:   Optional[int]  				=					37
									lowerCamelCase__		:   Tuple  				=					'''gelu'''
									lowerCamelCase__		:   Optional[Any]  				=					0.1
									lowerCamelCase__		:   Tuple  				=					0.1
									lowerCamelCase__		:   Optional[Any]  				=					5_12
									lowerCamelCase__		:   str  				=					16
									lowerCamelCase__		:   Optional[int]  				=					2
									lowerCamelCase__		:   Union[str, Any]  				=					0.02
									lowerCamelCase__		:   str  				=					3
									lowerCamelCase__		:   Optional[Any]  				=					4
									lowerCamelCase__		:   List[Any]  				=					None
									lowerCamelCase__		:   Optional[int]  				=					self.vocab_size - 1







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




									"""simple docstring"""



									lowerCamelCase__		:   Any  				=					ids_tensor([self.batch_size, self.seq_length]      ,self.vocab_size  )

									lowerCamelCase__		:   int  				=					None
									if self.use_input_mask:
														lowerCamelCase__		:   Dict  				=					random_attention_mask([self.batch_size, self.seq_length]  )

									lowerCamelCase__		:   Dict  				=					None
									if self.use_token_type_ids:
														lowerCamelCase__		:   List[str]  				=					ids_tensor([self.batch_size, self.seq_length]      ,self.type_vocab_size  )

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

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

									lowerCamelCase__		:   List[str]  				=					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   lowercase_		(    self    :int      ,__UpperCAmelCase    :Tuple      ,__UpperCAmelCase    :Any      ,__UpperCAmelCase    :Optional[int]      ,__UpperCAmelCase    :str      ,__UpperCAmelCase    :Any      ,*__UpperCAmelCase    :Optional[int]  )      ->					Any:




									"""simple docstring"""



									lowerCamelCase__		:   Union[str, Any]  				=					TFOpenAIGPTModel(config=UpperCAmelCase__  )
									lowerCamelCase__		:   List[str]  				=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									lowerCamelCase__		:   Union[str, Any]  				=					model(UpperCAmelCase__  )

									lowerCamelCase__		:   str  				=					[input_ids, input_mask]
									lowerCamelCase__		:   Any  				=					model(UpperCAmelCase__  )

									lowerCamelCase__		:   List[Any]  				=					model(UpperCAmelCase__  )

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







				def   lowercase_		(    self    :Tuple      ,__UpperCAmelCase    :Dict      ,__UpperCAmelCase    :Tuple      ,__UpperCAmelCase    :Tuple      ,__UpperCAmelCase    :int      ,__UpperCAmelCase    :List[str]      ,*__UpperCAmelCase    :List[str]  )      ->					str:




									"""simple docstring"""



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







				def   lowercase_		(    self    :Union[str, Any]      ,__UpperCAmelCase    :List[str]      ,__UpperCAmelCase    :Optional[int]      ,__UpperCAmelCase    :Optional[Any]      ,__UpperCAmelCase    :Tuple      ,__UpperCAmelCase    :Union[str, Any]      ,__UpperCAmelCase    :Optional[Any]      ,*__UpperCAmelCase    :Optional[Any]  )      ->					Any:




									"""simple docstring"""



									lowerCamelCase__		:   Dict  				=					TFOpenAIGPTDoubleHeadsModel(config=UpperCAmelCase__  )

									lowerCamelCase__		:   List[Any]  				=					tf.tile(tf.expand_dims(UpperCAmelCase__      ,1  )      ,(1, self.num_choices, 1)  )
									lowerCamelCase__		:   Optional[int]  				=					tf.tile(tf.expand_dims(UpperCAmelCase__      ,1  )      ,(1, self.num_choices, 1)  )
									lowerCamelCase__		:   Dict  				=					tf.tile(tf.expand_dims(UpperCAmelCase__      ,1  )      ,(1, self.num_choices, 1)  )

									lowerCamelCase__		:   Union[str, Any]  				=					{
									    '''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(UpperCAmelCase__  )
									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[int]      ,__UpperCAmelCase    :Any      ,__UpperCAmelCase    :Union[str, Any]      ,__UpperCAmelCase    :int      ,__UpperCAmelCase    :Optional[int]      ,__UpperCAmelCase    :str      ,*__UpperCAmelCase    :List[str]  )      ->					Union[str, Any]:




									"""simple docstring"""



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







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




									"""simple docstring"""



									lowerCamelCase__		:   Union[str, Any]  				=					self.prepare_config_and_inputs()

									(
									    (
									    lowerCamelCase__   
									)				,						(
									    lowerCamelCase__   
									)				,						(
									    lowerCamelCase__   
									)				,						(
									    lowerCamelCase__   
									)				,						(
									    lowerCamelCase__   
									)				,						(
									    lowerCamelCase__   
									)				,						(
									    lowerCamelCase__   
									)				,						(
									    lowerCamelCase__   
									)				,						(
									    lowerCamelCase__   
									)				,						   
									)		:   str  				=					config_and_inputs

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





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







				def   lowercase_		(    self    :Optional[Any]      ,__UpperCAmelCase    :int      ,__UpperCAmelCase    :List[str]      ,__UpperCAmelCase    :Tuple      ,__UpperCAmelCase    :Any      ,__UpperCAmelCase    :Tuple  )      ->					Optional[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   lowercase_		(    self    :Optional[Any]  )      ->					int:




									"""simple docstring"""



									lowerCamelCase__		:   Optional[Any]  				=					TFOpenAIGPTModelTester(self  )
									lowerCamelCase__		:   List[str]  				=					ConfigTester(self      ,config_class=UpperCAmelCase__      ,n_embd=37  )







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




									"""simple docstring"""



									self.config_tester.run_common_tests()







				def   lowercase_		(    self    :str  )      ->					Any:




									"""simple docstring"""



									lowerCamelCase__		:   Tuple  				=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase__  )







				def   lowercase_		(    self    :int  )      ->					Any:




									"""simple docstring"""



									lowerCamelCase__		:   List[Any]  				=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_lm_head(*UpperCAmelCase__  )







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




									"""simple docstring"""



									lowerCamelCase__		:   Dict  				=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_double_head(*UpperCAmelCase__  )







				def   lowercase_		(    self    :str  )      ->					Dict:




									"""simple docstring"""



									lowerCamelCase__		:   List[Any]  				=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase__  )







				@slow
				def   lowercase_		(    self    :List[Any]  )      ->					List[Any]:




									"""simple docstring"""



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





@require_tf
class        __SCREAMING_SNAKE_CASE     (   unittest.TestCase							):







				@slow
				def   lowercase_		(    self    :Any  )      ->					Dict:




									"""simple docstring"""



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

									lowerCamelCase__		:   Optional[Any]  				=					model.generate(UpperCAmelCase__      ,do_sample=UpperCAmelCase__  )
									self.assertListEqual(output_ids[0].numpy().tolist()      ,UpperCAmelCase__  )










"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


_lowerCamelCase  =	{
    'configuration_open_llama': ['OPEN_LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'OpenLlamaConfig'],
}

try:
	if not is_sentencepiece_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase  =	['LlamaTokenizer']

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase  =	['LlamaTokenizerFast']

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase  =	[
	    'OpenLlamaForCausalLM',
	    'OpenLlamaModel',
	    'OpenLlamaPreTrainedModel',
	    'OpenLlamaForSequenceClassification',
	]


if TYPE_CHECKING:
	from .configuration_open_llama import OPEN_LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, OpenLlamaConfig

	try:
		if not is_sentencepiece_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from transformers import LlamaTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from transformers import LlamaTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_open_llama import (
		    OpenLlamaForCausalLM,
		    OpenLlamaForSequenceClassification,
		    OpenLlamaModel,
		    OpenLlamaPreTrainedModel,
		)


else:
	import sys

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