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






					def __init__(       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE=13    ,							__SCREAMING_SNAKE_CASE=7    ,							__SCREAMING_SNAKE_CASE=True    ,							__SCREAMING_SNAKE_CASE=True    ,							__SCREAMING_SNAKE_CASE=True    ,							__SCREAMING_SNAKE_CASE=99    ,							__SCREAMING_SNAKE_CASE=32    ,							__SCREAMING_SNAKE_CASE=5    ,							__SCREAMING_SNAKE_CASE=4    ,							__SCREAMING_SNAKE_CASE=37    ,							__SCREAMING_SNAKE_CASE="gelu"    ,							__SCREAMING_SNAKE_CASE=0.1    ,							__SCREAMING_SNAKE_CASE=0.1    ,							__SCREAMING_SNAKE_CASE=5_12    ,							__SCREAMING_SNAKE_CASE=16    ,							__SCREAMING_SNAKE_CASE=2    ,							__SCREAMING_SNAKE_CASE=0.02    ,							__SCREAMING_SNAKE_CASE=3    ,							__SCREAMING_SNAKE_CASE=4    ,							__SCREAMING_SNAKE_CASE=None    ,							):


											"""simple docstring"""





											lowercase_     :					Union[str, Any]													=	parent
											lowercase_     :					Optional[Any]													=	batch_size
											lowercase_     :					Union[str, Any]													=	seq_length
											lowercase_     :					Union[str, Any]													=	is_training
											lowercase_     :					Tuple													=	use_token_type_ids
											lowercase_     :					Any													=	use_labels
											lowercase_     :					Tuple													=	vocab_size
											lowercase_     :					Optional[Any]													=	hidden_size
											lowercase_     :					str													=	num_hidden_layers
											lowercase_     :					Dict													=	num_attention_heads
											lowercase_     :					Any													=	intermediate_size
											lowercase_     :					List[str]													=	hidden_act
											lowercase_     :					Optional[int]													=	hidden_dropout_prob
											lowercase_     :					Optional[int]													=	attention_probs_dropout_prob
											lowercase_     :					List[str]													=	max_position_embeddings
											lowercase_     :					int													=	type_vocab_size
											lowercase_     :					Optional[Any]													=	type_sequence_label_size
											lowercase_     :					Union[str, Any]													=	initializer_range
											lowercase_     :					Any													=	num_labels
											lowercase_     :					Optional[Any]													=	num_choices
											lowercase_     :					Optional[int]													=	scope
											lowercase_     :					List[Any]													=	self.vocab_size - 1






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					int													=	ids_tensor([self.batch_size, self.seq_length]    ,							self.vocab_size		)

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

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

											lowercase_     :					Optional[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    ,							)

											lowercase_     :					str													=	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      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							*__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					int													=	OpenAIGPTModel(config=__SCREAMING_SNAKE_CASE		)
											model.to(__SCREAMING_SNAKE_CASE		)
											model.eval()

											lowercase_     :					str													=	model(__SCREAMING_SNAKE_CASE    ,							token_type_ids=__SCREAMING_SNAKE_CASE    ,							head_mask=__SCREAMING_SNAKE_CASE		)
											lowercase_     :					List[Any]													=	model(__SCREAMING_SNAKE_CASE    ,							token_type_ids=__SCREAMING_SNAKE_CASE		)
											lowercase_     :					Optional[Any]													=	model(__SCREAMING_SNAKE_CASE		)

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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							*__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					List[Any]													=	OpenAIGPTLMHeadModel(__SCREAMING_SNAKE_CASE		)
											model.to(__SCREAMING_SNAKE_CASE		)
											model.eval()

											lowercase_     :					List[str]													=	model(__SCREAMING_SNAKE_CASE    ,							token_type_ids=__SCREAMING_SNAKE_CASE    ,							labels=__SCREAMING_SNAKE_CASE		)
											self.parent.assertEqual(result.loss.shape    ,							()		)
											self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.seq_length, self.vocab_size)		)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							*__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					int													=	OpenAIGPTDoubleHeadsModel(__SCREAMING_SNAKE_CASE		)
											model.to(__SCREAMING_SNAKE_CASE		)
											model.eval()

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






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							*__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					Any													=	self.num_labels
											lowercase_     :					List[str]													=	OpenAIGPTForSequenceClassification(__SCREAMING_SNAKE_CASE		)
											model.to(__SCREAMING_SNAKE_CASE		)
											model.eval()

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






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Optional[Any]													=	self.prepare_config_and_inputs()
											(
											    (
											    lowercase_
											)						,			(
											    lowercase_
											)						,			(
											    lowercase_
											)						,			(
											    lowercase_
											)						,			(
											    lowercase_
											)						,			(
											    lowercase_
											)						,			(
											    lowercase_
											)						,			
											)     :					Optional[int]													=	config_and_inputs
											lowercase_     :					Union[str, Any]													=	{
											    '''input_ids''': input_ids,
											    '''token_type_ids''': token_type_ids,
											    '''head_mask''': head_mask,
											}

											return config, inputs_dict







@require_torch
class 			lowerCAmelCase__       (  lowerCamelCase_   ,						lowerCamelCase_   ,						lowerCamelCase_   ,						unittest.TestCase					):
					lowerCAmelCase_					       =					(
					    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
					    if is_torch_available()
					    else ()
					)
					lowerCAmelCase_					       =					(
					    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
					)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
					lowerCAmelCase_					       =					(
					    {
					        '''feature-extraction''': OpenAIGPTModel,
					        '''text-classification''': OpenAIGPTForSequenceClassification,
					        '''text-generation''': OpenAIGPTLMHeadModel,
					        '''zero-shot''': OpenAIGPTForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


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


											"""simple docstring"""





											lowercase_     :					int													=	super()._prepare_for_class(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							return_labels=__SCREAMING_SNAKE_CASE		)

											if return_labels:
																	if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																							lowercase_     :					int													=	torch.zeros(
																							    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)    ,							dtype=torch.long    ,							device=__SCREAMING_SNAKE_CASE    ,							)
																							lowercase_     :					Optional[Any]													=	inputs_dict['''labels''']
																							lowercase_     :					Tuple													=	inputs_dict['''labels''']
																							lowercase_     :					Union[str, Any]													=	torch.zeros(
																							    (self.model_tester.batch_size, self.model_tester.num_choices)    ,							dtype=torch.long    ,							device=__SCREAMING_SNAKE_CASE    ,							)
																							lowercase_     :					Tuple													=	torch.zeros(
																							    self.model_tester.batch_size    ,							dtype=torch.long    ,							device=__SCREAMING_SNAKE_CASE		)
											return inputs_dict






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Tuple													=	OpenAIGPTModelTester(self		)
											lowercase_     :					Dict													=	ConfigTester(self    ,							config_class=__SCREAMING_SNAKE_CASE    ,							n_embd=37		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											self.config_tester.run_common_tests()






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Optional[Any]													=	self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_model(*__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Optional[Any]													=	self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_lm_head_model(*__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					List[Any]													=	self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_double_lm_head_model(*__SCREAMING_SNAKE_CASE		)






					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Tuple													=	self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__SCREAMING_SNAKE_CASE		)






					@slow
					def      _snake_case   (       self		):


											"""simple docstring"""





											for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase_     :					str													=	OpenAIGPTModel.from_pretrained(__SCREAMING_SNAKE_CASE		)
																	self.assertIsNotNone(__SCREAMING_SNAKE_CASE		)







@require_torch
class 			lowerCAmelCase__       (  unittest.TestCase					):






					@slow
					def      _snake_case   (       self		):


											"""simple docstring"""





											lowercase_     :					Any													=	OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''		)
											model.to(__SCREAMING_SNAKE_CASE		)
											lowercase_     :					List[Any]													=	torch.tensor([[4_81, 47_35, 5_44]]    ,							dtype=torch.long    ,							device=__SCREAMING_SNAKE_CASE		)  # the president is
											lowercase_     :					List[Any]													=	[
											    4_81,
											    47_35,
											    5_44,
											    2_46,
											    9_63,
											    8_70,
											    7_62,
											    2_39,
											    2_44,
											    4_04_77,
											    2_44,
											    2_49,
											    7_19,
											    8_81,
											    4_87,
											    5_44,
											    2_40,
											    2_44,
											    6_03,
											    4_81,
											]  # the president is a very good man. " \n " i\'m sure he is, " said the

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





'''simple docstring'''







import logging
import os

from .state import PartialState






class 			lowerCAmelCase__       (  logging.LoggerAdapter					):






					@staticmethod
					def      _snake_case   (       __SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					Optional[Any]													=	PartialState()
											return not main_process_only or (main_process_only and state.is_main_process)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							*__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											if PartialState._shared_state == {}:
																	raise RuntimeError(
																	    '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.'''		)
											lowercase_     :					Tuple													=	kwargs.pop('''main_process_only'''    ,							__SCREAMING_SNAKE_CASE		)
											lowercase_     :					Optional[int]													=	kwargs.pop('''in_order'''    ,							__SCREAMING_SNAKE_CASE		)

											if self.isEnabledFor(__SCREAMING_SNAKE_CASE		):
																	if self._should_log(__SCREAMING_SNAKE_CASE		):
																							lowercase_						,			lowercase_     :					Optional[Any]													=	self.process(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)
																							self.logger.log(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							*__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		)

																	elif in_order:
																							lowercase_     :					Optional[Any]													=	PartialState()
																							for i in range(state.num_processes		):
																													if i == state.process_index:
																																			lowercase_						,			lowercase_     :					Optional[int]													=	self.process(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)
																																			self.logger.log(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							*__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		)
																													state.wait_for_everyone()







def  snake_case_		(    __SCREAMING_SNAKE_CASE :			str				,				__SCREAMING_SNAKE_CASE :			str = None							):



						"""simple docstring"""





						if log_level is None:
												lowercase_     :					Any													=	os.environ.get('''ACCELERATE_LOG_LEVEL'''				,				__SCREAMING_SNAKE_CASE							)
						lowercase_     :					List[str]													=	logging.getLogger(__SCREAMING_SNAKE_CASE							)
						if log_level is not None:
												logger.setLevel(log_level.upper()							)
												logger.root.setLevel(log_level.upper()							)
						return MultiProcessAdapter(__SCREAMING_SNAKE_CASE				,				{}							)



