



'''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:







 """simple docstring"""






 def __init__(						self      :					Optional[int]		,      __snake_case      :					int		,      )      ->		Union[str, Any]:
        UpperCAmelCase			:	Union[str, Any]										=		parent
        UpperCAmelCase			:	Tuple										=		13
        UpperCAmelCase			:	int										=		7
        UpperCAmelCase			:	Any										=		True
        UpperCAmelCase			:	Tuple										=		True
        UpperCAmelCase			:	int										=		True
        UpperCAmelCase			:	List[Any]										=		True
        UpperCAmelCase			:	int										=		True
        UpperCAmelCase			:	List[Any]										=		99
        UpperCAmelCase			:	Dict										=		32
        UpperCAmelCase			:	Any										=		2
        UpperCAmelCase			:	Optional[int]										=		4
        UpperCAmelCase			:	Dict										=		37
        UpperCAmelCase			:	Union[str, Any]										=		'''gelu'''
        UpperCAmelCase			:	Tuple										=		0.1
        UpperCAmelCase			:	List[Any]										=		0.1
        UpperCAmelCase			:	List[str]										=		512
        UpperCAmelCase			:	Tuple										=		16
        UpperCAmelCase			:	Tuple										=		2
        UpperCAmelCase			:	str										=		0.02
        UpperCAmelCase			:	Any										=		3
        UpperCAmelCase			:	Tuple										=		4
        UpperCAmelCase			:	Tuple										=		None
        UpperCAmelCase			:	Dict										=		self.vocab_size - 1







 def 					A     (						self      :					Optional[int]		)      ->		Dict:
        UpperCAmelCase			:	Any										=		ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size		)

        UpperCAmelCase			:	Dict										=		None
        if self.use_input_mask:
               UpperCAmelCase			:	Union[str, Any]										=		random_attention_mask([self.batch_size, self.seq_length]		)

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

        UpperCAmelCase			:	List[Any]										=		None
        if self.use_mc_token_ids:
               UpperCAmelCase			:	int										=		ids_tensor([self.batch_size, self.num_choices]		,      self.seq_length		)

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

        UpperCAmelCase			:	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		,      )

        UpperCAmelCase			:	List[str]										=		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      :					Union[str, Any]		,      __snake_case      :					List[Any]		,      __snake_case      :					Dict		,      __snake_case      :					Optional[Any]		,      __snake_case      :					str		,      __snake_case      :					Dict		,      *__snake_case      :					Union[str, Any]		)      ->		List[Any]:
        UpperCAmelCase			:	Any										=		TFOpenAIGPTModel(config=__snake_case		)
        UpperCAmelCase			:	Union[str, Any]										=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        UpperCAmelCase			:	List[Any]										=		model(__snake_case		)

        UpperCAmelCase			:	int										=		[input_ids, input_mask]
        UpperCAmelCase			:	Dict										=		model(__snake_case		)

        UpperCAmelCase			:	Tuple										=		model(__snake_case		)

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







 def 					A     (						self      :					Optional[Any]		,      __snake_case      :					str		,      __snake_case      :					str		,      __snake_case      :					Union[str, Any]		,      __snake_case      :					Optional[int]		,      __snake_case      :					Tuple		,      *__snake_case      :					List[Any]		)      ->		List[str]:
        UpperCAmelCase			:	Optional[Any]										=		TFOpenAIGPTLMHeadModel(config=__snake_case		)
        UpperCAmelCase			:	Dict										=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        UpperCAmelCase			:	Any										=		model(__snake_case		)
        self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.vocab_size)		)







 def 					A     (						self      :					Union[str, Any]		,      __snake_case      :					Union[str, Any]		,      __snake_case      :					str		,      __snake_case      :					str		,      __snake_case      :					Union[str, Any]		,      __snake_case      :					str		,      __snake_case      :					List[Any]		,      *__snake_case      :					Optional[Any]		)      ->		Optional[int]:
        UpperCAmelCase			:	List[str]										=		TFOpenAIGPTDoubleHeadsModel(config=__snake_case		)

        UpperCAmelCase			:	str										=		tf.tile(tf.expand_dims(__snake_case		,      1		)		,      (1, self.num_choices, 1)		)
        UpperCAmelCase			:	Any										=		tf.tile(tf.expand_dims(__snake_case		,      1		)		,      (1, self.num_choices, 1)		)
        UpperCAmelCase			:	List[str]										=		tf.tile(tf.expand_dims(__snake_case		,      1		)		,      (1, self.num_choices, 1)		)

        UpperCAmelCase			:	Optional[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,
        }
        UpperCAmelCase			:	int										=		model(__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 					A     (						self      :					str		,      __snake_case      :					Dict		,      __snake_case      :					Dict		,      __snake_case      :					Optional[Any]		,      __snake_case      :					Tuple		,      __snake_case      :					Any		,      *__snake_case      :					Optional[int]		)      ->		Optional[Any]:
        UpperCAmelCase			:	Dict										=		self.num_labels
        UpperCAmelCase			:	Optional[int]										=		ids_tensor([self.batch_size]		,      self.type_sequence_label_size		)
        UpperCAmelCase			:	Optional[Any]										=		{
            '''input_ids''': input_ids,
            '''attention_mask''': input_mask,
            '''token_type_ids''': token_type_ids,
            '''labels''': sequence_labels,
        }
        UpperCAmelCase			:	Optional[int]										=		TFOpenAIGPTForSequenceClassification(__snake_case		)
        UpperCAmelCase			:	Tuple										=		model(__snake_case		)
        self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels)		)







 def 					A     (						self      :					int		)      ->		List[str]:
        UpperCAmelCase			:	List[str]										=		self.prepare_config_and_inputs()

        (
            UpperCAmelCase		
        )			:	int										=		config_and_inputs

        UpperCAmelCase			:	Tuple										=		{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
        return config, inputs_dict

@require_tf
class     SCREAMING_SNAKE_CASE(							A__							,		A__							,		unittest.TestCase      ):







 """simple docstring"""






 lowerCamelCase__      			=	(
     (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
     if is_tf_available()
     else ()
 )
 lowerCamelCase__      			=	(
     (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 lowerCamelCase__      			=	(
     {
         """feature-extraction""": TFOpenAIGPTModel,
         """text-classification""": TFOpenAIGPTForSequenceClassification,
         """text-generation""": TFOpenAIGPTLMHeadModel,
         """zero-shot""": TFOpenAIGPTForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 lowerCamelCase__      			=	False
 lowerCamelCase__      			=	False







 def 					A     (						self      :					Any		,      __snake_case      :					int		,      __snake_case      :					Tuple		,      __snake_case      :					Optional[int]		,      __snake_case      :					List[Any]		,      __snake_case      :					List[Any]		)      ->		List[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      :					Tuple		)      ->		Optional[Any]:
        UpperCAmelCase			:	Tuple										=		TFOpenAIGPTModelTester(self		)
        UpperCAmelCase			:	str										=		ConfigTester(self		,      config_class=__snake_case		,      n_embd=37		)







 def 					A     (						self      :					Optional[int]		)      ->		Optional[int]:
        self.config_tester.run_common_tests()







 def 					A     (						self      :					Dict		)      ->		List[str]:
        UpperCAmelCase			:	Optional[Any]										=		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*__snake_case		)







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







 def 					A     (						self      :					Dict		)      ->		Optional[Any]:
        UpperCAmelCase			:	Tuple										=		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_double_head(*__snake_case		)







 def 					A     (						self      :					Optional[int]		)      ->		Dict:
        UpperCAmelCase			:	Tuple										=		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__snake_case		)







 @slow
 def 					A     (						self      :					str		)      ->		Optional[Any]:
        for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase			:	Optional[int]										=		TFOpenAIGPTModel.from_pretrained(__snake_case		)
               self.assertIsNotNone(__snake_case		)

@require_tf
class     SCREAMING_SNAKE_CASE(							unittest.TestCase      ):







 """simple docstring"""






 @slow
 def 					A     (						self      :					Optional[Any]		)      ->		str:
        UpperCAmelCase			:	Optional[int]										=		TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''		)
        UpperCAmelCase			:	Dict										=		tf.convert_to_tensor([[481, 4735, 544]]		,      dtype=tf.intaa		)  # the president is
        UpperCAmelCase			:	Union[str, Any]										=		[
            481,
            4735,
            544,
            246,
            963,
            870,
            762,
            239,
            244,
            40477,
            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			:	List[Any]										=		model.generate(__snake_case		,      do_sample=__snake_case		)
        self.assertListEqual(output_ids[0].numpy().tolist()		,      __snake_case		)










'''simple docstring'''






import importlib.metadata
import subprocess
import sys


def 			snake_case_					( _lowerCAmelCase  :						bool = False  )				->       str:
							UpperCAmelCase			:	List[str]										=		False
							if "IPython" in sys.modules:
														UpperCAmelCase			:	Optional[Any]										=		'''google.colab''' in str(sys.modules['''IPython'''].get_ipython()  )

							if in_colab:
														if upgrade:
																					UpperCAmelCase			:	List[Any]										=		['''pip''', '''install''', '''-U''', '''torch''']
																					subprocess.run(_lowerCAmelCase       ,				check=_lowerCAmelCase  )
														# get the current version of torch
														UpperCAmelCase			:	Dict										=		importlib.metadata.version('''torch'''  )
														UpperCAmelCase			:	int										=		torch_version[: torch_version.rindex('''.'''  )]
														UpperCAmelCase			:	Any										=		f"""https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl"""
														UpperCAmelCase			:	List[str]										=		['''pip''', '''install''', xla_wheel]
														subprocess.run(_lowerCAmelCase       ,				check=_lowerCAmelCase  )
							else:
														raise RuntimeError('''`install_xla` utility works only on google colab.'''  )





