




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


			def __init__(       self			,	SCREAMING_SNAKE_CASE			,	):



					"""simple docstring"""







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


			def 							lowerCamelCase_			(       self							):



					"""simple docstring"""







					snake_case						:  str        =			ids_tensor([self.batch_size, self.seq_length]			,	self.vocab_size							)

					snake_case						:  List[Any]        =			None
					if self.use_input_mask:
							snake_case						:  Optional[int]        =			random_attention_mask([self.batch_size, self.seq_length]							)

					snake_case						:  Union[str, Any]        =			None
					if self.use_token_type_ids:
							snake_case						:  Any        =			ids_tensor([self.batch_size, self.seq_length]			,	self.type_vocab_size							)

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

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

					snake_case						:  Tuple        =			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			,	)

					snake_case						:  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 							lowerCamelCase_			(       self			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	*SCREAMING_SNAKE_CASE							):



					"""simple docstring"""







					snake_case						:  List[str]        =			TFOpenAIGPTModel(config=SCREAMING_SNAKE_CASE							)
					snake_case						:  int        =			{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
					snake_case						:  int        =			model(SCREAMING_SNAKE_CASE							)

					snake_case						:  List[Any]        =			[input_ids, input_mask]
					snake_case						:  Dict        =			model(SCREAMING_SNAKE_CASE							)

					snake_case						:  Any        =			model(SCREAMING_SNAKE_CASE							)

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


			def 							lowerCamelCase_			(       self			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	*SCREAMING_SNAKE_CASE							):



					"""simple docstring"""







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


			def 							lowerCamelCase_			(       self			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	*SCREAMING_SNAKE_CASE							):



					"""simple docstring"""







					snake_case						:  List[Any]        =			TFOpenAIGPTDoubleHeadsModel(config=SCREAMING_SNAKE_CASE							)

					snake_case						:  Tuple        =			tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE			,	1							)			,	(1, self.num_choices, 1)							)
					snake_case						:  Dict        =			tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE			,	1							)			,	(1, self.num_choices, 1)							)
					snake_case						:  Tuple        =			tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE			,	1							)			,	(1, self.num_choices, 1)							)

					snake_case						:  List[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,
					}
					snake_case						:  Optional[Any]        =			model(SCREAMING_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 							lowerCamelCase_			(       self			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	*SCREAMING_SNAKE_CASE							):



					"""simple docstring"""







					snake_case						:  Union[str, Any]        =			self.num_labels
					snake_case						:  Tuple        =			ids_tensor([self.batch_size]			,	self.type_sequence_label_size							)
					snake_case						:  Dict        =			{
					    "input_ids": input_ids,
					    "attention_mask": input_mask,
					    "token_type_ids": token_type_ids,
					    "labels": sequence_labels,
					}
					snake_case						:  Union[str, Any]        =			TFOpenAIGPTForSequenceClassification(SCREAMING_SNAKE_CASE							)
					snake_case						:  Dict        =			model(SCREAMING_SNAKE_CASE							)
					self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.num_labels)							)



			def 							lowerCamelCase_			(       self							):



					"""simple docstring"""







					snake_case						:  Optional[Any]        =			self.prepare_config_and_inputs()

					(
					    (
					    snake_case
					)     ,			(
					    snake_case
					)     ,			(
					    snake_case
					)     ,			(
					    snake_case
					)     ,			(
					    snake_case
					)     ,			(
					    snake_case
					)     ,			(
					    snake_case
					)     ,			(
					    snake_case
					)     ,			(
					    snake_case
					)     ,			
					)						:  int        =			config_and_inputs

					snake_case						:  str        =			{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
					return config, inputs_dict







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


			def 							lowerCamelCase_			(       self			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE			,	SCREAMING_SNAKE_CASE							):



					"""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 							lowerCamelCase_			(       self							):



					"""simple docstring"""







					snake_case						:  Tuple        =			TFOpenAIGPTModelTester(self							)
					snake_case						:  List[Any]        =			ConfigTester(self			,	config_class=SCREAMING_SNAKE_CASE			,	n_embd=37							)


			def 							lowerCamelCase_			(       self							):



					"""simple docstring"""







					self.config_tester.run_common_tests()


			def 							lowerCamelCase_			(       self							):



					"""simple docstring"""







					snake_case						:  Union[str, Any]        =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_model(*SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_			(       self							):



					"""simple docstring"""







					snake_case						:  Optional[int]        =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_lm_head(*SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_			(       self							):



					"""simple docstring"""







					snake_case						:  Tuple        =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_double_head(*SCREAMING_SNAKE_CASE							)


			def 							lowerCamelCase_			(       self							):



					"""simple docstring"""







					snake_case						:  List[Any]        =			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*SCREAMING_SNAKE_CASE							)



			@slow
			def 							lowerCamelCase_			(       self							):



					"""simple docstring"""







					for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							snake_case						:  List[Any]        =			TFOpenAIGPTModel.from_pretrained(SCREAMING_SNAKE_CASE							)
							self.assertIsNotNone(SCREAMING_SNAKE_CASE							)







@require_tf
class      lowerCamelCase__      (					unittest.TestCase     ):


			@slow
			def 							lowerCamelCase_			(       self							):



					"""simple docstring"""







					snake_case						:  Union[str, Any]        =			TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"							)
					snake_case						:  str        =			tf.convert_to_tensor([[481, 4_735, 544]]			,	dtype=tf.intaa							)  # the president is
					snake_case						:  Optional[int]        =			[
					    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

					snake_case						:  List[str]        =			model.generate(SCREAMING_SNAKE_CASE			,	do_sample=SCREAMING_SNAKE_CASE							)
					self.assertListEqual(output_ids[0].numpy().tolist()			,	SCREAMING_SNAKE_CASE							)







"""simple docstring"""

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


__A							=     {"configuration_gptj": ["GPTJ_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTJConfig", "GPTJOnnxConfig"]}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__A							=     [
							    "GPTJ_PRETRAINED_MODEL_ARCHIVE_LIST",
							    "GPTJForCausalLM",
							    "GPTJForQuestionAnswering",
							    "GPTJForSequenceClassification",
							    "GPTJModel",
							    "GPTJPreTrainedModel",
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__A							=     [
							    "TFGPTJForCausalLM",
							    "TFGPTJForQuestionAnswering",
							    "TFGPTJForSequenceClassification",
							    "TFGPTJModel",
							    "TFGPTJPreTrainedModel",
							]

try:
							if not is_flax_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__A							=     [
							    "FlaxGPTJForCausalLM",
							    "FlaxGPTJModel",
							    "FlaxGPTJPreTrainedModel",
							]


if TYPE_CHECKING:
							from .configuration_gptj import GPTJ_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTJConfig, GPTJOnnxConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_gptj import (
														    GPTJ_PRETRAINED_MODEL_ARCHIVE_LIST,
														    GPTJForCausalLM,
														    GPTJForQuestionAnswering,
														    GPTJForSequenceClassification,
														    GPTJModel,
														    GPTJPreTrainedModel,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_gptj import (
														    TFGPTJForCausalLM,
														    TFGPTJForQuestionAnswering,
														    TFGPTJForSequenceClassification,
														    TFGPTJModel,
														    TFGPTJPreTrainedModel,
														)

							try:
														if not is_flax_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel, FlaxGPTJPreTrainedModel

else:
							import sys

							__A							=     _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

