





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    OpenAIGPTConfig,
				    OpenAIGPTDoubleHeadsModel,
				    OpenAIGPTForSequenceClassification,
				    OpenAIGPTLMHeadModel,
				    OpenAIGPTModel,
				)

class 	A_       :
	def __init__(    self							,					_A							,					_A=1_3							,					_A=7							,					_A=True							,					_A=True							,					_A=True							,					_A=9_9							,					_A=3_2							,					_A=5							,					_A=4							,					_A=3_7							,					_A="gelu"							,					_A=0.1							,					_A=0.1							,					_A=5_1_2							,					_A=1_6							,					_A=2							,					_A=0.02							,					_A=3							,					_A=4							,					_A=None							,					):






				'''simple docstring'''







				UpperCAmelCase   		=				parent
				UpperCAmelCase   		=				batch_size
				UpperCAmelCase   		=				seq_length
				UpperCAmelCase   		=				is_training
				UpperCAmelCase   		=				use_token_type_ids
				UpperCAmelCase   		=				use_labels
				UpperCAmelCase   		=				vocab_size
				UpperCAmelCase   		=				hidden_size
				UpperCAmelCase   		=				num_hidden_layers
				UpperCAmelCase   		=				num_attention_heads
				UpperCAmelCase   		=				intermediate_size
				UpperCAmelCase   		=				hidden_act
				UpperCAmelCase   		=				hidden_dropout_prob
				UpperCAmelCase   		=				attention_probs_dropout_prob
				UpperCAmelCase   		=				max_position_embeddings
				UpperCAmelCase   		=				type_vocab_size
				UpperCAmelCase   		=				type_sequence_label_size
				UpperCAmelCase   		=				initializer_range
				UpperCAmelCase   		=				num_labels
				UpperCAmelCase   		=				num_choices
				UpperCAmelCase   		=				scope
				UpperCAmelCase   		=				self.vocab_size - 1
	def      _lowercase				(    self		):






				'''simple docstring'''







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

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

				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,
				    head_mask,
				    token_type_ids,
				    sequence_labels,
				    token_labels,
				    choice_labels,
				)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					_A							,					*_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				OpenAIGPTModel(config=_A		)
				model.to(_A		)
				model.eval()

				UpperCAmelCase   		=				model(_A							,					token_type_ids=_A							,					head_mask=_A		)
				UpperCAmelCase   		=				model(_A							,					token_type_ids=_A		)
				UpperCAmelCase   		=				model(_A		)

				self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size)		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					_A							,					*_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				OpenAIGPTLMHeadModel(_A		)
				model.to(_A		)
				model.eval()

				UpperCAmelCase   		=				model(_A							,					token_type_ids=_A							,					labels=_A		)
				self.parent.assertEqual(result.loss.shape							,					()		)
				self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.seq_length, self.vocab_size)		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					_A							,					*_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				OpenAIGPTDoubleHeadsModel(_A		)
				model.to(_A		)
				model.eval()

				UpperCAmelCase   		=				model(_A							,					token_type_ids=_A							,					labels=_A		)
				self.parent.assertEqual(result.loss.shape							,					()		)
				self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.seq_length, self.vocab_size)		)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					_A							,					*_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.num_labels
				UpperCAmelCase   		=				OpenAIGPTForSequenceClassification(_A		)
				model.to(_A		)
				model.eval()

				UpperCAmelCase   		=				ids_tensor([self.batch_size]							,					self.type_sequence_label_size		)
				UpperCAmelCase   		=				model(_A							,					token_type_ids=_A							,					labels=_A		)
				self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_labels)		)







	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.prepare_config_and_inputs()
				(
				    UpperCAmelCase			
				)   		=				config_and_inputs
				UpperCAmelCase   		=				{
				    """input_ids""": input_ids,
				    """token_type_ids""": token_type_ids,
				    """head_mask""": head_mask,
				}

				return config, inputs_dict

@require_torch
class 	A_       (a_     ,							a_     ,							a_     ,							unittest.TestCase			):
	UpperCAmelCase__					=			(
	    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
	    if is_torch_available()
	    else ()
	)
	UpperCAmelCase__					=			(
	    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
	)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
	UpperCAmelCase__					=			(
	    {
	        "feature-extraction": OpenAIGPTModel,
	        "text-classification": OpenAIGPTForSequenceClassification,
	        "text-generation": OpenAIGPTLMHeadModel,
	        "zero-shot": OpenAIGPTForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					_A							,					_A		):






				'''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							,					_A							,					_A							,					_A=False		):






				'''simple docstring'''







				UpperCAmelCase   		=				super()._prepare_for_class(_A							,					_A							,					return_labels=_A		)

				if return_labels:
							if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
										UpperCAmelCase   		=				torch.zeros(
										    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)							,					dtype=torch.long							,					device=_A							,					)
										UpperCAmelCase   		=				inputs_dict["""labels"""]
										UpperCAmelCase   		=				inputs_dict["""labels"""]
										UpperCAmelCase   		=				torch.zeros(
										    (self.model_tester.batch_size, self.model_tester.num_choices)							,					dtype=torch.long							,					device=_A							,					)
										UpperCAmelCase   		=				torch.zeros(
										    self.model_tester.batch_size							,					dtype=torch.long							,					device=_A		)
				return inputs_dict
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				OpenAIGPTModelTester(self		)
				UpperCAmelCase   		=				ConfigTester(self							,					config_class=_A							,					n_embd=3_7		)
	def      _lowercase				(    self		):






				'''simple docstring'''







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






				'''simple docstring'''







				UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_openai_gpt_model(*_A		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_lm_head_model(*_A		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_double_lm_head_model(*_A		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_A		)







	@slow
	def      _lowercase				(    self		):






				'''simple docstring'''







				for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase   		=				OpenAIGPTModel.from_pretrained(_A		)
							self.assertIsNotNone(_A		)

@require_torch
class 	A_       (unittest.TestCase			):
	@slow
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''		)
				model.to(_A		)
				UpperCAmelCase   		=				torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]							,					dtype=torch.long							,					device=_A		)  # the president is
				UpperCAmelCase   		=				[
				    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

				UpperCAmelCase   		=				model.generate(_A							,					do_sample=_A		)
				self.assertListEqual(output_ids[0].tolist()							,					_A		)





from typing import TYPE_CHECKING

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


lowercase  :					str						    =							{
    """configuration_funnel""": ["""FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP""", """FunnelConfig"""],
    """convert_funnel_original_tf_checkpoint_to_pytorch""": [],
    """tokenization_funnel""": ["""FunnelTokenizer"""],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase  :					Tuple						    =							["""FunnelTokenizerFast"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase  :					List[str]						    =							[
      """FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """FunnelBaseModel""",
      """FunnelForMaskedLM""",
      """FunnelForMultipleChoice""",
      """FunnelForPreTraining""",
      """FunnelForQuestionAnswering""",
      """FunnelForSequenceClassification""",
      """FunnelForTokenClassification""",
      """FunnelModel""",
      """FunnelPreTrainedModel""",
      """load_tf_weights_in_funnel""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase  :					Optional[Any]						    =							[
      """TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFFunnelBaseModel""",
      """TFFunnelForMaskedLM""",
      """TFFunnelForMultipleChoice""",
      """TFFunnelForPreTraining""",
      """TFFunnelForQuestionAnswering""",
      """TFFunnelForSequenceClassification""",
      """TFFunnelForTokenClassification""",
      """TFFunnelModel""",
      """TFFunnelPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig
  from .tokenization_funnel import FunnelTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_funnel_fast import FunnelTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_funnel import (
        FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
        FunnelBaseModel,
        FunnelForMaskedLM,
        FunnelForMultipleChoice,
        FunnelForPreTraining,
        FunnelForQuestionAnswering,
        FunnelForSequenceClassification,
        FunnelForTokenClassification,
        FunnelModel,
        FunnelPreTrainedModel,
        load_tf_weights_in_funnel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_funnel import (
        TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFFunnelBaseModel,
        TFFunnelForMaskedLM,
        TFFunnelForMultipleChoice,
        TFFunnelForPreTraining,
        TFFunnelForQuestionAnswering,
        TFFunnelForSequenceClassification,
        TFFunnelForTokenClassification,
        TFFunnelModel,
        TFFunnelPreTrainedModel,
    )

else:
  import sys

  lowercase  :					Any						    =							_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
