


'''simple docstring'''



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    :


     """simple docstring"""






     def __init__(    self					:		Union[str, Any]							,			snake_case					:		List[Any]							,			snake_case					:		Optional[int]=13							,			snake_case					:		Optional[Any]=7							,			snake_case					:		List[Any]=True							,			snake_case					:		int=True							,			snake_case					:		Optional[int]=True							,			snake_case					:		Union[str, Any]=99							,			snake_case					:		List[str]=32							,			snake_case					:		Any=5							,			snake_case					:		str=4							,			snake_case					:		Tuple=37							,			snake_case					:		List[Any]="gelu"							,			snake_case					:		List[Any]=0.1							,			snake_case					:		Tuple=0.1							,			snake_case					:		int=512							,			snake_case					:		str=16							,			snake_case					:		str=2							,			snake_case					:		Union[str, Any]=0.02							,			snake_case					:		Optional[Any]=3							,			snake_case					:		Union[str, Any]=4							,			snake_case					:		Optional[int]=None							,			)						->							Union[str, Any]:
          __UpperCAmelCase     :			Any  							=      parent
          __UpperCAmelCase     :			int  							=      batch_size
          __UpperCAmelCase     :			Optional[int]  							=      seq_length
          __UpperCAmelCase     :			Any  							=      is_training
          __UpperCAmelCase     :			Any  							=      use_token_type_ids
          __UpperCAmelCase     :			Optional[Any]  							=      use_labels
          __UpperCAmelCase     :			int  							=      vocab_size
          __UpperCAmelCase     :			Optional[int]  							=      hidden_size
          __UpperCAmelCase     :			Dict  							=      num_hidden_layers
          __UpperCAmelCase     :			Optional[int]  							=      num_attention_heads
          __UpperCAmelCase     :			Dict  							=      intermediate_size
          __UpperCAmelCase     :			Optional[Any]  							=      hidden_act
          __UpperCAmelCase     :			Any  							=      hidden_dropout_prob
          __UpperCAmelCase     :			Optional[int]  							=      attention_probs_dropout_prob
          __UpperCAmelCase     :			Dict  							=      max_position_embeddings
          __UpperCAmelCase     :			List[Any]  							=      type_vocab_size
          __UpperCAmelCase     :			Optional[int]  							=      type_sequence_label_size
          __UpperCAmelCase     :			List[str]  							=      initializer_range
          __UpperCAmelCase     :			Optional[int]  							=      num_labels
          __UpperCAmelCase     :			Union[str, Any]  							=      num_choices
          __UpperCAmelCase     :			Optional[Any]  							=      scope
          __UpperCAmelCase     :			Union[str, Any]  							=      self.vocab_size - 1

     def 			lowerCamelCase__			(    self					:		Tuple      )						->							Optional[int]:
          __UpperCAmelCase     :			Optional[int]  							=      ids_tensor([self.batch_size, self.seq_length]							,			self.vocab_size      )

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

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

          __UpperCAmelCase     :			List[Any]  							=      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     :			Any  							=      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 			lowerCamelCase__			(    self					:		Dict							,			snake_case					:		List[Any]							,			snake_case					:		List[str]							,			snake_case					:		Any							,			snake_case					:		int							,			*snake_case					:		Optional[int]      )						->							Optional[Any]:
          __UpperCAmelCase     :			Tuple  							=      OpenAIGPTModel(config=snake_case      )
          model.to(snake_case      )
          model.eval()

          __UpperCAmelCase     :			Union[str, Any]  							=      model(snake_case							,			token_type_ids=snake_case							,			head_mask=snake_case      )
          __UpperCAmelCase     :			List[Any]  							=      model(snake_case							,			token_type_ids=snake_case      )
          __UpperCAmelCase     :			Any  							=      model(snake_case      )

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

     def 			lowerCamelCase__			(    self					:		str							,			snake_case					:		Optional[Any]							,			snake_case					:		Any							,			snake_case					:		List[str]							,			snake_case					:		Tuple							,			*snake_case					:		Tuple      )						->							Union[str, Any]:
          __UpperCAmelCase     :			Optional[Any]  							=      OpenAIGPTLMHeadModel(snake_case      )
          model.to(snake_case      )
          model.eval()

          __UpperCAmelCase     :			Union[str, Any]  							=      model(snake_case							,			token_type_ids=snake_case							,			labels=snake_case      )
          self.parent.assertEqual(result.loss.shape							,			()      )
          self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length, self.vocab_size)      )

     def 			lowerCamelCase__			(    self					:		List[Any]							,			snake_case					:		Tuple							,			snake_case					:		Dict							,			snake_case					:		str							,			snake_case					:		int							,			*snake_case					:		Dict      )						->							Any:
          __UpperCAmelCase     :			Tuple  							=      OpenAIGPTDoubleHeadsModel(snake_case      )
          model.to(snake_case      )
          model.eval()

          __UpperCAmelCase     :			Optional[int]  							=      model(snake_case							,			token_type_ids=snake_case							,			labels=snake_case      )
          self.parent.assertEqual(result.loss.shape							,			()      )
          self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length, self.vocab_size)      )

     def 			lowerCamelCase__			(    self					:		str							,			snake_case					:		int							,			snake_case					:		List[str]							,			snake_case					:		Any							,			snake_case					:		Union[str, Any]							,			*snake_case					:		Any      )						->							int:
          __UpperCAmelCase     :			Dict  							=      self.num_labels
          __UpperCAmelCase     :			Dict  							=      OpenAIGPTForSequenceClassification(snake_case      )
          model.to(snake_case      )
          model.eval()

          __UpperCAmelCase     :			Union[str, Any]  							=      ids_tensor([self.batch_size]							,			self.type_sequence_label_size      )
          __UpperCAmelCase     :			List[str]  							=      model(snake_case							,			token_type_ids=snake_case							,			labels=snake_case      )
          self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_labels)      )






     def 			lowerCamelCase__			(    self					:		int      )						->							Optional[int]:
          __UpperCAmelCase     :			Dict  							=      self.prepare_config_and_inputs()
          (
              __UpperCAmelCase       
          )     :			Dict  							=      config_and_inputs
          __UpperCAmelCase     :			str  							=      {
              '''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	):


     """simple docstring"""






     SCREAMING_SNAKE_CASE	:			Dict							       =       (
         (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
         if is_torch_available()
         else ()
     )
     SCREAMING_SNAKE_CASE	:			Union[str, Any]							       =       (
         (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     SCREAMING_SNAKE_CASE	:			List[Any]							       =       (
         {
             "feature-extraction": OpenAIGPTModel,
             "text-classification": OpenAIGPTForSequenceClassification,
             "text-generation": OpenAIGPTLMHeadModel,
             "zero-shot": OpenAIGPTForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )

     def 			lowerCamelCase__			(    self					:		Tuple							,			snake_case					:		Dict							,			snake_case					:		Any							,			snake_case					:		Union[str, Any]							,			snake_case					:		Optional[int]							,			snake_case					:		str      )						->							Any:
          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]							,			snake_case					:		List[Any]							,			snake_case					:		Optional[Any]							,			snake_case					:		Tuple=False      )						->							Tuple:
          __UpperCAmelCase     :			int  							=      super()._prepare_for_class(snake_case							,			snake_case							,			return_labels=snake_case      )

          if return_labels:
               if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                    __UpperCAmelCase     :			Optional[Any]  							=      torch.zeros(
                        (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)							,			dtype=torch.long							,			device=snake_case							,			)
                    __UpperCAmelCase     :			Dict  							=      inputs_dict['''labels''']
                    __UpperCAmelCase     :			Optional[int]  							=      inputs_dict['''labels''']
                    __UpperCAmelCase     :			Optional[Any]  							=      torch.zeros(
                        (self.model_tester.batch_size, self.model_tester.num_choices)							,			dtype=torch.long							,			device=snake_case							,			)
                    __UpperCAmelCase     :			Optional[int]  							=      torch.zeros(
                        self.model_tester.batch_size							,			dtype=torch.long							,			device=snake_case      )
          return inputs_dict

     def 			lowerCamelCase__			(    self					:		Dict      )						->							str:
          __UpperCAmelCase     :			int  							=      OpenAIGPTModelTester(self      )
          __UpperCAmelCase     :			Tuple  							=      ConfigTester(self							,			config_class=snake_case							,			n_embd=37      )

     def 			lowerCamelCase__			(    self					:		Any      )						->							Dict:
          self.config_tester.run_common_tests()

     def 			lowerCamelCase__			(    self					:		str      )						->							List[str]:
          __UpperCAmelCase     :			str  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_model(*snake_case      )

     def 			lowerCamelCase__			(    self					:		Dict      )						->							Any:
          __UpperCAmelCase     :			Optional[Any]  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(*snake_case      )

     def 			lowerCamelCase__			(    self					:		Any      )						->							Optional[int]:
          __UpperCAmelCase     :			str  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_double_lm_head_model(*snake_case      )

     def 			lowerCamelCase__			(    self					:		Any      )						->							Optional[Any]:
          __UpperCAmelCase     :			Dict  							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case      )






     @slow
     def 			lowerCamelCase__			(    self					:		List[str]      )						->							Dict:
          for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __UpperCAmelCase     :			List[Any]  							=      OpenAIGPTModel.from_pretrained(snake_case      )
               self.assertIsNotNone(snake_case      )



@require_torch
class      a    (		unittest.TestCase	):


     """simple docstring"""






     @slow
     def 			lowerCamelCase__			(    self					:		Union[str, Any]      )						->							str:
          __UpperCAmelCase     :			Any  							=      OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''      )
          model.to(snake_case      )
          __UpperCAmelCase     :			Union[str, Any]  							=      torch.tensor([[481, 4735, 544]]							,			dtype=torch.long							,			device=snake_case      )  # the president is
          __UpperCAmelCase     :			str  							=      [
              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

          __UpperCAmelCase     :			int  							=      model.generate(snake_case							,			do_sample=snake_case      )
          self.assertListEqual(output_ids[0].tolist()							,			snake_case      )



'''simple docstring'''



from typing import TYPE_CHECKING

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


__UpperCAmelCase      :List[Any]			    =       {
    "configuration_lxmert": ["LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "LxmertConfig"],
    "tokenization_lxmert": ["LxmertTokenizer"],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase      :List[Any]			    =       ["LxmertTokenizerFast"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase      :List[Any]			    =       [
          "LxmertEncoder",
          "LxmertForPreTraining",
          "LxmertForQuestionAnswering",
          "LxmertModel",
          "LxmertPreTrainedModel",
          "LxmertVisualFeatureEncoder",
          "LxmertXLayer",
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __UpperCAmelCase      :List[str]			    =       [
          "TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST",
          "TFLxmertForPreTraining",
          "TFLxmertMainLayer",
          "TFLxmertModel",
          "TFLxmertPreTrainedModel",
          "TFLxmertVisualFeatureEncoder",
      ]


if TYPE_CHECKING:
      from .configuration_lxmert import LXMERT_PRETRAINED_CONFIG_ARCHIVE_MAP, LxmertConfig
      from .tokenization_lxmert import LxmertTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_lxmert_fast import LxmertTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_lxmert import (
                LxmertEncoder,
                LxmertForPreTraining,
                LxmertForQuestionAnswering,
                LxmertModel,
                LxmertPreTrainedModel,
                LxmertVisualFeatureEncoder,
                LxmertXLayer,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_lxmert import (
                TF_LXMERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFLxmertForPreTraining,
                TFLxmertMainLayer,
                TFLxmertModel,
                TFLxmertPreTrainedModel,
                TFLxmertVisualFeatureEncoder,
            )

else:
      import sys

      __UpperCAmelCase      :int			    =       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)