


"""simple docstring"""




import os
import unittest

import pytest
from parameterized import parameterized

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


# skipping in unittest tests

UpperCAmelCase__            =							[(1,)]
def     UpperCAmelCase_ (							)					->      List[Any]:







 """simple docstring"""


 _lowercase											=bool(os.getenv('''RUN_SLOW'''					,						0						)						)
 if run_slow:
  assert True
 else:
  assert False, "should have been skipped"
def     UpperCAmelCase_ (							)					->      str:







 """simple docstring"""


 _lowercase											=bool(os.getenv('''RUN_SLOW'''					,						0						)						)
 if run_slow and torch_device == "cuda":
  assert True
 else:
  assert False, "should have been skipped"







@require_torch
class 				lowerCamelCase__       (	unittest.TestCase):





     @slow
     @require_torch_gpu
     def 					__A (self	)					->       Tuple:
      check_slow_torch_cuda()





     @require_torch_gpu
     @slow
     def 					__A (self	)					->       int:
      check_slow_torch_cuda()





     @parameterized.expand(lowerCamelCase__	)
     @slow
     def 					__A (self		,					UpperCAmelCase=None	)					->       Optional[int]:
      check_slow()



@slow
@require_torch_gpu
def     UpperCAmelCase_ (							)					->      str:







 """simple docstring"""


 check_slow_torch_cuda()
@require_torch_gpu
@slow
def     UpperCAmelCase_ (							)					->      Optional[int]:







 """simple docstring"""


 check_slow_torch_cuda()
@slow
@pytest.mark.parametrize('''param'''					,						[1]						)
def     UpperCAmelCase_ (							__snake_case						)					->      Union[str, Any]:







 """simple docstring"""


 check_slow()



@pytest.mark.parametrize('''param'''					,						[1]						)
@slow
def     UpperCAmelCase_ (							__snake_case						)					->      Tuple:







 """simple docstring"""


 check_slow()





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		,					UpperCAmelCase		,					)					->       str:
      _lowercase											=parent
      _lowercase											=1_3
      _lowercase											=7
      _lowercase											=True
      _lowercase											=True
      _lowercase											=True
      _lowercase											=True
      _lowercase											=True
      _lowercase											=9_9
      _lowercase											=3_2
      _lowercase											=2
      _lowercase											=4
      _lowercase											=3_7
      _lowercase											='''gelu'''
      _lowercase											=0.1
      _lowercase											=0.1
      _lowercase											=5_1_2
      _lowercase											=1_6
      _lowercase											=2
      _lowercase											=0.02
      _lowercase											=3
      _lowercase											=4
      _lowercase											=None
      _lowercase											=self.vocab_size - 1





     def 					__A (self	)					->       str:
      _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 					__A (self		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					*UpperCAmelCase	)					->       List[Any]:
      _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 					__A (self		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					*UpperCAmelCase	)					->       Optional[Any]:
      _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 					__A (self		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					*UpperCAmelCase	)					->       str:
      _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 					__A (self		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					*UpperCAmelCase	)					->       Any:
      _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 					__A (self	)					->       List[str]:
      _lowercase											=self.prepare_config_and_inputs()

      (
          (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _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 				lowerCamelCase__       (	lowerCAmelCase       ,							lowerCAmelCase       ,							unittest.TestCase):
     SCREAMING_SNAKE_CASE__				      =      (
         (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
         if is_tf_available()
         else ()
     )
     SCREAMING_SNAKE_CASE__				      =      (
         (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     SCREAMING_SNAKE_CASE__				      =      (
         {
             '''feature-extraction''': TFOpenAIGPTModel,
             '''text-classification''': TFOpenAIGPTForSequenceClassification,
             '''text-generation''': TFOpenAIGPTLMHeadModel,
             '''zero-shot''': TFOpenAIGPTForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     SCREAMING_SNAKE_CASE__				      =      False
     SCREAMING_SNAKE_CASE__				      =      False





     def 					__A (self		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase	)					->       Optional[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 					__A (self	)					->       List[str]:
      _lowercase											=TFOpenAIGPTModelTester(self	)
      _lowercase											=ConfigTester(self		,					config_class=UpperCAmelCase		,					n_embd=3_7	)





     def 					__A (self	)					->       Any:
      self.config_tester.run_common_tests()





     def 					__A (self	)					->       Optional[int]:
      _lowercase											=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase	)





     def 					__A (self	)					->       Union[str, Any]:
      _lowercase											=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_lm_head(*UpperCAmelCase	)





     def 					__A (self	)					->       Tuple:
      _lowercase											=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_double_head(*UpperCAmelCase	)





     def 					__A (self	)					->       Union[str, Any]:
      _lowercase											=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase	)





     @slow
     def 					__A (self	)					->       Union[str, Any]:
      for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _lowercase											=TFOpenAIGPTModel.from_pretrained(UpperCAmelCase	)
       self.assertIsNotNone(UpperCAmelCase	)







@require_tf
class 				lowerCamelCase__       (	unittest.TestCase):





     @slow
     def 					__A (self	)					->       str:
      _lowercase											=TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''	)
      _lowercase											=tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]		,					dtype=tf.intaa	)  # the president is
      _lowercase											=[
          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

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

