


"""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 				UpperCamelCase_			:

	"""simple docstring"""





	def __init__(				self    :       Optional[Any]    ,    UpperCAmelCase__    :       Optional[int]    ,    UpperCAmelCase__    :       Tuple=1_3    ,    UpperCAmelCase__    :       int=7    ,    UpperCAmelCase__    :       str=True    ,    UpperCAmelCase__    :       Dict=True    ,    UpperCAmelCase__    :       Any=True    ,    UpperCAmelCase__    :       Optional[Any]=9_9    ,    UpperCAmelCase__    :       Dict=3_2    ,    UpperCAmelCase__    :       Union[str, Any]=5    ,    UpperCAmelCase__    :       Any=4    ,    UpperCAmelCase__    :       Dict=3_7    ,    UpperCAmelCase__    :       str="gelu"    ,    UpperCAmelCase__    :       List[Any]=0.1    ,    UpperCAmelCase__    :       Tuple=0.1    ,    UpperCAmelCase__    :       Union[str, Any]=5_1_2    ,    UpperCAmelCase__    :       Optional[Any]=1_6    ,    UpperCAmelCase__    :       Dict=2    ,    UpperCAmelCase__    :       Optional[int]=0.02    ,    UpperCAmelCase__    :       Union[str, Any]=3    ,    UpperCAmelCase__    :       List[Any]=4    ,    UpperCAmelCase__    :       Any=None    ,    ) ->			Dict:
						__SCREAMING_SNAKE_CASE		     =       parent
						__SCREAMING_SNAKE_CASE		     =       batch_size
						__SCREAMING_SNAKE_CASE		     =       seq_length
						__SCREAMING_SNAKE_CASE		     =       is_training
						__SCREAMING_SNAKE_CASE		     =       use_token_type_ids
						__SCREAMING_SNAKE_CASE		     =       use_labels
						__SCREAMING_SNAKE_CASE		     =       vocab_size
						__SCREAMING_SNAKE_CASE		     =       hidden_size
						__SCREAMING_SNAKE_CASE		     =       num_hidden_layers
						__SCREAMING_SNAKE_CASE		     =       num_attention_heads
						__SCREAMING_SNAKE_CASE		     =       intermediate_size
						__SCREAMING_SNAKE_CASE		     =       hidden_act
						__SCREAMING_SNAKE_CASE		     =       hidden_dropout_prob
						__SCREAMING_SNAKE_CASE		     =       attention_probs_dropout_prob
						__SCREAMING_SNAKE_CASE		     =       max_position_embeddings
						__SCREAMING_SNAKE_CASE		     =       type_vocab_size
						__SCREAMING_SNAKE_CASE		     =       type_sequence_label_size
						__SCREAMING_SNAKE_CASE		     =       initializer_range
						__SCREAMING_SNAKE_CASE		     =       num_labels
						__SCREAMING_SNAKE_CASE		     =       num_choices
						__SCREAMING_SNAKE_CASE		     =       scope
						__SCREAMING_SNAKE_CASE		     =       self.vocab_size - 1




	def 						UpperCAmelCase_   (				self    :       int     ) ->			Tuple:
						__SCREAMING_SNAKE_CASE		     =       ids_tensor([self.batch_size, self.seq_length]    ,    self.vocab_size     )

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

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

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

						__SCREAMING_SNAKE_CASE		     =       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 						UpperCAmelCase_   (				self    :       Optional[Any]    ,    UpperCAmelCase__    :       Any    ,    UpperCAmelCase__    :       Union[str, Any]    ,    UpperCAmelCase__    :       Optional[Any]    ,    UpperCAmelCase__    :       str    ,    *UpperCAmelCase__    :       List[str]     ) ->			Union[str, Any]:
						__SCREAMING_SNAKE_CASE		     =       OpenAIGPTModel(config=UpperCAmelCase__     )
						model.to(UpperCAmelCase__     )
						model.eval()

						__SCREAMING_SNAKE_CASE		     =       model(UpperCAmelCase__    ,    token_type_ids=UpperCAmelCase__    ,    head_mask=UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       model(UpperCAmelCase__    ,    token_type_ids=UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       model(UpperCAmelCase__     )

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




	def 						UpperCAmelCase_   (				self    :       Dict    ,    UpperCAmelCase__    :       Optional[int]    ,    UpperCAmelCase__    :       Tuple    ,    UpperCAmelCase__    :       Union[str, Any]    ,    UpperCAmelCase__    :       Dict    ,    *UpperCAmelCase__    :       Dict     ) ->			Any:
						__SCREAMING_SNAKE_CASE		     =       OpenAIGPTLMHeadModel(UpperCAmelCase__     )
						model.to(UpperCAmelCase__     )
						model.eval()

						__SCREAMING_SNAKE_CASE		     =       model(UpperCAmelCase__    ,    token_type_ids=UpperCAmelCase__    ,    labels=UpperCAmelCase__     )
						self.parent.assertEqual(result.loss.shape    ,    ()     )
						self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.seq_length, self.vocab_size)     )




	def 						UpperCAmelCase_   (				self    :       Union[str, Any]    ,    UpperCAmelCase__    :       Optional[int]    ,    UpperCAmelCase__    :       str    ,    UpperCAmelCase__    :       int    ,    UpperCAmelCase__    :       List[Any]    ,    *UpperCAmelCase__    :       Optional[int]     ) ->			List[Any]:
						__SCREAMING_SNAKE_CASE		     =       OpenAIGPTDoubleHeadsModel(UpperCAmelCase__     )
						model.to(UpperCAmelCase__     )
						model.eval()

						__SCREAMING_SNAKE_CASE		     =       model(UpperCAmelCase__    ,    token_type_ids=UpperCAmelCase__    ,    labels=UpperCAmelCase__     )
						self.parent.assertEqual(result.loss.shape    ,    ()     )
						self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.seq_length, self.vocab_size)     )




	def 						UpperCAmelCase_   (				self    :       Union[str, Any]    ,    UpperCAmelCase__    :       Optional[int]    ,    UpperCAmelCase__    :       List[Any]    ,    UpperCAmelCase__    :       Tuple    ,    UpperCAmelCase__    :       Any    ,    *UpperCAmelCase__    :       Dict     ) ->			List[str]:
						__SCREAMING_SNAKE_CASE		     =       self.num_labels
						__SCREAMING_SNAKE_CASE		     =       OpenAIGPTForSequenceClassification(UpperCAmelCase__     )
						model.to(UpperCAmelCase__     )
						model.eval()

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




	def 						UpperCAmelCase_   (				self    :       str     ) ->			List[Any]:
						__SCREAMING_SNAKE_CASE		     =       self.prepare_config_and_inputs()
						(
						    (
						    __SCREAMING_SNAKE_CASE
						)     ,						(
						    __SCREAMING_SNAKE_CASE
						)     ,						(
						    __SCREAMING_SNAKE_CASE
						)     ,						(
						    __SCREAMING_SNAKE_CASE
						)     ,						(
						    __SCREAMING_SNAKE_CASE
						)     ,						(
						    __SCREAMING_SNAKE_CASE
						)     ,						(
						    __SCREAMING_SNAKE_CASE
						)     ,						
						)		     =       config_and_inputs
						__SCREAMING_SNAKE_CASE		     =       {
						    "input_ids": input_ids,
						    "token_type_ids": token_type_ids,
						    "head_mask": head_mask,
						}

						return config, inputs_dict




@require_torch
class 				UpperCamelCase_			(				UpperCamelCase    ,						UpperCamelCase    ,						UpperCamelCase    ,						unittest.TestCase):

	"""simple docstring"""





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




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

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




	def 						UpperCAmelCase_   (				self    :       int     ) ->			Dict:
						__SCREAMING_SNAKE_CASE		     =       OpenAIGPTModelTester(self     )
						__SCREAMING_SNAKE_CASE		     =       ConfigTester(self    ,    config_class=UpperCAmelCase__    ,    n_embd=3_7     )




	def 						UpperCAmelCase_   (				self    :       List[str]     ) ->			Dict:
						self.config_tester.run_common_tests()




	def 						UpperCAmelCase_   (				self    :       Optional[int]     ) ->			str:
						__SCREAMING_SNAKE_CASE		     =       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			Union[str, Any]:
						__SCREAMING_SNAKE_CASE		     =       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       List[Any]     ) ->			Dict:
						__SCREAMING_SNAKE_CASE		     =       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_double_lm_head_model(*UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       Union[str, Any]     ) ->			Tuple:
						__SCREAMING_SNAKE_CASE		     =       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase__     )




	@slow
	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			Any:
						for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__SCREAMING_SNAKE_CASE		     =       OpenAIGPTModel.from_pretrained(UpperCAmelCase__     )
											self.assertIsNotNone(UpperCAmelCase__     )




@require_torch
class 				UpperCamelCase_			(				unittest.TestCase):

	"""simple docstring"""





	@slow
	def 						UpperCAmelCase_   (				self    :       List[str]     ) ->			List[Any]:
						__SCREAMING_SNAKE_CASE		     =       OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"     )
						model.to(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]    ,    dtype=torch.long    ,    device=UpperCAmelCase__     )  # the president is
						__SCREAMING_SNAKE_CASE		     =       [
						    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

						__SCREAMING_SNAKE_CASE		     =       model.generate(UpperCAmelCase__    ,    do_sample=UpperCAmelCase__     )
						self.assertListEqual(output_ids[0].tolist()    ,    UpperCAmelCase__     )




"""simple docstring"""







import platform
from argparse import ArgumentParser

import huggingface_hub

from .. import __version__ as version
from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available
from . import BaseDiffusersCLICommand




def      UpperCAmelCase__ (lowerCAmelCase_     ):




					'''simple docstring'''


					return EnvironmentCommand()




class 				UpperCamelCase_			(				UpperCamelCase):

	"""simple docstring"""





	@staticmethod
	def 						UpperCAmelCase_   (				UpperCAmelCase__    :       ArgumentParser     ) ->			Optional[Any]:
						__SCREAMING_SNAKE_CASE		     =       parser.add_parser("env"     )
						download_parser.set_defaults(func=UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       List[Any]     ) ->			List[str]:
						__SCREAMING_SNAKE_CASE		     =       huggingface_hub.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						__SCREAMING_SNAKE_CASE		     =       "NA"
						if is_torch_available():
											import torch

											__SCREAMING_SNAKE_CASE		     =       torch.__version__
											__SCREAMING_SNAKE_CASE		     =       torch.cuda.is_available()

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_transformers_available():
											import transformers

											__SCREAMING_SNAKE_CASE		     =       transformers.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_accelerate_available():
											import accelerate

											__SCREAMING_SNAKE_CASE		     =       accelerate.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_xformers_available():
											import xformers

											__SCREAMING_SNAKE_CASE		     =       xformers.__version__

						__SCREAMING_SNAKE_CASE		     =       {
						    "`diffusers` version": version,
						    "Platform": platform.platform(),
						    "Python version": platform.python_version(),
						    "PyTorch version (GPU?)": F"""{pt_version} ({pt_cuda_available})""",
						    "Huggingface_hub version": hub_version,
						    "Transformers version": transformers_version,
						    "Accelerate version": accelerate_version,
						    "xFormers version": xformers_version,
						    "Using GPU in script?": "<fill in>",
						    "Using distributed or parallel set-up in script?": "<fill in>",
						}

						print("\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n"     )
						print(self.format_dict(UpperCAmelCase__     )     )

						return info




	@staticmethod
	def 						UpperCAmelCase_   (				UpperCAmelCase__    :       Optional[Any]     ) ->			str:
						return "\n".join([F"""- {prop}: {val}""" for prop, val in d.items()]     ) + "\n"
