




'''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 ,		lowerCamelCase__ ,		)   -> Tuple:

           """simple docstring"""



           __lowercase          =						parent
           __lowercase          =						13
           __lowercase          =						7
           __lowercase          =						True
           __lowercase          =						True
           __lowercase          =						True
           __lowercase          =						True
           __lowercase          =						True
           __lowercase          =						99
           __lowercase          =						32
           __lowercase          =						2
           __lowercase          =						4
           __lowercase          =						37
           __lowercase          =						'gelu'
           __lowercase          =						0.1
           __lowercase          =						0.1
           __lowercase          =						512
           __lowercase          =						16
           __lowercase          =						2
           __lowercase          =						0.02
           __lowercase          =						3
           __lowercase          =						4
           __lowercase          =						None
           __lowercase          =						self.vocab_size - 1

       def    snake_case__      (   self					)   -> Dict:

           """simple docstring"""



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

           __lowercase          =						None
           if self.use_input_mask:
               __lowercase          =						random_attention_mask([self.batch_size, self.seq_length]					)

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

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

           __lowercase          =						None
           __lowercase          =						None
           __lowercase          =						None
           if self.use_labels:
               __lowercase          =						ids_tensor([self.batch_size] ,		self.type_sequence_label_size					)
               __lowercase          =						ids_tensor([self.batch_size, self.seq_length] ,		self.num_labels					)
               __lowercase          =						ids_tensor([self.batch_size] ,		self.num_choices					)

           __lowercase          =						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 ,		)

           __lowercase          =						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    snake_case__      (   self ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		*lowerCamelCase__					)   -> List[str]:

           """simple docstring"""



           __lowercase          =						TFOpenAIGPTModel(config=__UpperCamelCase					)
           __lowercase          =						{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           __lowercase          =						model(__UpperCamelCase					)

           __lowercase          =						[input_ids, input_mask]
           __lowercase          =						model(__UpperCamelCase					)

           __lowercase          =						model(__UpperCamelCase					)

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

       def    snake_case__      (   self ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		*lowerCamelCase__					)   -> Optional[int]:

           """simple docstring"""



           __lowercase          =						TFOpenAIGPTLMHeadModel(config=__UpperCamelCase					)
           __lowercase          =						{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           __lowercase          =						model(__UpperCamelCase					)
           self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.seq_length, self.vocab_size)					)

       def    snake_case__      (   self ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		*lowerCamelCase__					)   -> Tuple:

           """simple docstring"""



           __lowercase          =						TFOpenAIGPTDoubleHeadsModel(config=__UpperCamelCase					)

           __lowercase          =						tf.tile(tf.expand_dims(__UpperCamelCase ,		1					) ,		(1, self.num_choices, 1)					)
           __lowercase          =						tf.tile(tf.expand_dims(__UpperCamelCase ,		1					) ,		(1, self.num_choices, 1)					)
           __lowercase          =						tf.tile(tf.expand_dims(__UpperCamelCase ,		1					) ,		(1, self.num_choices, 1)					)

           __lowercase          =						{
               '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,
           }
           __lowercase          =						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    snake_case__      (   self ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		*lowerCamelCase__					)   -> Tuple:

           """simple docstring"""



           __lowercase          =						self.num_labels
           __lowercase          =						ids_tensor([self.batch_size] ,		self.type_sequence_label_size					)
           __lowercase          =						{
               'input_ids': input_ids,
               'attention_mask': input_mask,
               'token_type_ids': token_type_ids,
               'labels': sequence_labels,
           }
           __lowercase          =						TFOpenAIGPTForSequenceClassification(__UpperCamelCase					)
           __lowercase          =						model(__UpperCamelCase					)
           self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.num_labels)					)




       def    snake_case__      (   self					)   -> List[str]:

           """simple docstring"""



           __lowercase          =						self.prepare_config_and_inputs()

           (
               __lowercase  
           )          =						config_and_inputs

           __lowercase          =						{'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					):
       snake_case_			:					List[Any]   = (
           (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
           if is_tf_available()
           else ()
       )
       snake_case_			:					Tuple   = (
           (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
       )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
       snake_case_			:					Optional[int]   = (
           {
               """feature-extraction""": TFOpenAIGPTModel,
               """text-classification""": TFOpenAIGPTForSequenceClassification,
               """text-generation""": TFOpenAIGPTLMHeadModel,
               """zero-shot""": TFOpenAIGPTForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       snake_case_			:					Optional[Any]   = False
       snake_case_			:					Any   = False

       def    snake_case__      (   self ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__ ,		lowerCamelCase__					)   -> 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    snake_case__      (   self					)   -> Any:

           """simple docstring"""



           __lowercase          =						TFOpenAIGPTModelTester(self					)
           __lowercase          =						ConfigTester(self ,		config_class=__UpperCamelCase ,		n_embd=37					)

       def    snake_case__      (   self					)   -> int:

           """simple docstring"""



           self.config_tester.run_common_tests()

       def    snake_case__      (   self					)   -> Dict:

           """simple docstring"""



           __lowercase          =						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*__UpperCamelCase					)

       def    snake_case__      (   self					)   -> int:

           """simple docstring"""



           __lowercase          =						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_lm_head(*__UpperCamelCase					)

       def    snake_case__      (   self					)   -> List[Any]:

           """simple docstring"""



           __lowercase          =						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_double_head(*__UpperCamelCase					)

       def    snake_case__      (   self					)   -> Dict:

           """simple docstring"""



           __lowercase          =						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__UpperCamelCase					)




       @slow
       def    snake_case__      (   self					)   -> Union[str, Any]:

           """simple docstring"""



           for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __lowercase          =						TFOpenAIGPTModel.from_pretrained(__UpperCamelCase					)
               self.assertIsNotNone(__UpperCamelCase					)






@require_tf
class 	SCREAMING_SNAKE_CASE(    unittest.TestCase					):

       @slow
       def    snake_case__      (   self					)   -> Union[str, Any]:

           """simple docstring"""



           __lowercase          =						TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""					)
           __lowercase          =						tf.convert_to_tensor([[481, 4735, 544]] ,		dtype=tf.intaa					)  # the president is
           __lowercase          =						[
               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

           __lowercase          =						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_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


A							:				Tuple			     =	{
    """configuration_dpr""": ["""DPR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DPRConfig"""],
    """tokenization_dpr""": [
        """DPRContextEncoderTokenizer""",
        """DPRQuestionEncoderTokenizer""",
        """DPRReaderOutput""",
        """DPRReaderTokenizer""",
    ],
}


try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  A							:				str			     =	[
      """DPRContextEncoderTokenizerFast""",
      """DPRQuestionEncoderTokenizerFast""",
      """DPRReaderTokenizerFast""",
  ]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  A							:				Tuple			     =	[
      """DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """DPRContextEncoder""",
      """DPRPretrainedContextEncoder""",
      """DPRPreTrainedModel""",
      """DPRPretrainedQuestionEncoder""",
      """DPRPretrainedReader""",
      """DPRQuestionEncoder""",
      """DPRReader""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  A							:				Tuple			     =	[
      """TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFDPRContextEncoder""",
      """TFDPRPretrainedContextEncoder""",
      """TFDPRPretrainedQuestionEncoder""",
      """TFDPRPretrainedReader""",
      """TFDPRQuestionEncoder""",
      """TFDPRReader""",
  ]


if TYPE_CHECKING:
  from .configuration_dpr import DPR_PRETRAINED_CONFIG_ARCHIVE_MAP, DPRConfig
  from .tokenization_dpr import (
      DPRContextEncoderTokenizer,
      DPRQuestionEncoderTokenizer,
      DPRReaderOutput,
      DPRReaderTokenizer,
  )

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_dpr_fast import (
        DPRContextEncoderTokenizerFast,
        DPRQuestionEncoderTokenizerFast,
        DPRReaderTokenizerFast,
    )

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_dpr import (
        DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
        DPRContextEncoder,
        DPRPretrainedContextEncoder,
        DPRPreTrainedModel,
        DPRPretrainedQuestionEncoder,
        DPRPretrainedReader,
        DPRQuestionEncoder,
        DPRReader,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_dpr import (
        TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFDPRContextEncoder,
        TFDPRPretrainedContextEncoder,
        TFDPRPretrainedQuestionEncoder,
        TFDPRPretrainedReader,
        TFDPRQuestionEncoder,
        TFDPRReader,
    )

else:
  import sys

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




