



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





	def __init__(  self		,		A		,		A=13		,		A=7		,		A=True		,		A=True		,		A=True		,		A=99		,		A=32		,		A=5		,		A=4		,		A=37		,		A="gelu"		,		A=0.1		,		A=0.1		,		A=512		,		A=16		,		A=2		,		A=0.0_2		,		A=3		,		A=4		,		A=None		,		)			->			Dict:
								UpperCAmelCase     :		int							     =		parent
								UpperCAmelCase     :		Any							     =		batch_size
								UpperCAmelCase     :		List[Any]							     =		seq_length
								UpperCAmelCase     :		Optional[Any]							     =		is_training
								UpperCAmelCase     :		Optional[int]							     =		use_token_type_ids
								UpperCAmelCase     :		Optional[Any]							     =		use_labels
								UpperCAmelCase     :		Optional[Any]							     =		vocab_size
								UpperCAmelCase     :		Optional[Any]							     =		hidden_size
								UpperCAmelCase     :		Optional[Any]							     =		num_hidden_layers
								UpperCAmelCase     :		List[str]							     =		num_attention_heads
								UpperCAmelCase     :		Union[str, Any]							     =		intermediate_size
								UpperCAmelCase     :		int							     =		hidden_act
								UpperCAmelCase     :		Union[str, Any]							     =		hidden_dropout_prob
								UpperCAmelCase     :		List[Any]							     =		attention_probs_dropout_prob
								UpperCAmelCase     :		int							     =		max_position_embeddings
								UpperCAmelCase     :		List[str]							     =		type_vocab_size
								UpperCAmelCase     :		Union[str, Any]							     =		type_sequence_label_size
								UpperCAmelCase     :		Union[str, Any]							     =		initializer_range
								UpperCAmelCase     :		List[str]							     =		num_labels
								UpperCAmelCase     :		Optional[Any]							     =		num_choices
								UpperCAmelCase     :		List[str]							     =		scope
								UpperCAmelCase     :		Optional[Any]							     =		self.vocab_size - 1





	def  _lowercase(  self    )			->			Union[str, Any]:
								UpperCAmelCase     :		Tuple							     =		ids_tensor([self.batch_size, self.seq_length]		,		self.vocab_size    )

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

								UpperCAmelCase     :		Dict							     =		None
								UpperCAmelCase     :		List[Any]							     =		None
								UpperCAmelCase     :		Dict							     =		None
								if self.use_labels:
															UpperCAmelCase     :		str							     =		ids_tensor([self.batch_size]		,		self.type_sequence_label_size    )
															UpperCAmelCase     :		List[Any]							     =		ids_tensor([self.batch_size, self.seq_length]		,		self.num_labels    )
															UpperCAmelCase     :		List[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     :		int							     =		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  _lowercase(  self		,		A		,		A		,		A		,		A		,		*A    )			->			List[Any]:
								UpperCAmelCase     :		str							     =		OpenAIGPTModel(config=A    )
								model.to(A    )
								model.eval()

								UpperCAmelCase     :		Any							     =		model(A		,		token_type_ids=A		,		head_mask=A    )
								UpperCAmelCase     :		Tuple							     =		model(A		,		token_type_ids=A    )
								UpperCAmelCase     :		List[str]							     =		model(A    )

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





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		*A    )			->			Dict:
								UpperCAmelCase     :		int							     =		OpenAIGPTLMHeadModel(A    )
								model.to(A    )
								model.eval()

								UpperCAmelCase     :		Tuple							     =		model(A		,		token_type_ids=A		,		labels=A    )
								self.parent.assertEqual(result.loss.shape		,		()    )
								self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.seq_length, self.vocab_size)    )





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		*A    )			->			Dict:
								UpperCAmelCase     :		Dict							     =		OpenAIGPTDoubleHeadsModel(A    )
								model.to(A    )
								model.eval()

								UpperCAmelCase     :		Tuple							     =		model(A		,		token_type_ids=A		,		labels=A    )
								self.parent.assertEqual(result.loss.shape		,		()    )
								self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.seq_length, self.vocab_size)    )





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		*A    )			->			Optional[int]:
								UpperCAmelCase     :		Union[str, Any]							     =		self.num_labels
								UpperCAmelCase     :		str							     =		OpenAIGPTForSequenceClassification(A    )
								model.to(A    )
								model.eval()

								UpperCAmelCase     :		Dict							     =		ids_tensor([self.batch_size]		,		self.type_sequence_label_size    )
								UpperCAmelCase     :		Optional[int]							     =		model(A		,		token_type_ids=A		,		labels=A    )
								self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.num_labels)    )






	def  _lowercase(  self    )			->			Any:
								UpperCAmelCase     :		Dict							     =		self.prepare_config_and_inputs()
								(
								    (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     
								)     :		List[str]							     =		config_and_inputs
								UpperCAmelCase     :		Tuple							     =		{
								    """input_ids""": input_ids,
								    """token_type_ids""": token_type_ids,
								    """head_mask""": head_mask,
								}

								return config, inputs_dict




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





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		A    )			->			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  _lowercase(  self		,		A		,		A		,		A=False    )			->			int:
								UpperCAmelCase     :		List[str]							     =		super()._prepare_for_class(A		,		A		,		return_labels=A    )

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





	def  _lowercase(  self    )			->			List[str]:
								UpperCAmelCase     :		Optional[int]							     =		OpenAIGPTModelTester(self    )
								UpperCAmelCase     :		Tuple							     =		ConfigTester(self		,		config_class=A		,		n_embd=37    )





	def  _lowercase(  self    )			->			str:
								self.config_tester.run_common_tests()





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





	def  _lowercase(  self    )			->			Tuple:
								UpperCAmelCase     :		Tuple							     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*A    )





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





	def  _lowercase(  self    )			->			Tuple:
								UpperCAmelCase     :		Tuple							     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*A    )






	@slow
	def  _lowercase(  self    )			->			int:
								for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															UpperCAmelCase     :		List[str]							     =		OpenAIGPTModel.from_pretrained(A    )
															self.assertIsNotNone(A    )




@require_torch
class 			UpperCamelCase_ (							unittest.TestCase			):





	@slow
	def  _lowercase(  self    )			->			Dict:
								UpperCAmelCase     :		Dict							     =		OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""    )
								model.to(A    )
								UpperCAmelCase     :		Union[str, Any]							     =		torch.tensor([[481, 4735, 544]]		,		dtype=torch.long		,		device=A    )  # the president is
								UpperCAmelCase     :		int							     =		[
								    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     :		Optional[int]							     =		model.generate(A		,		do_sample=A    )
								self.assertListEqual(output_ids[0].tolist()		,		A    )






'''simple docstring'''




import time
from contextlib import contextmanager
from pathlib import Path

import pytest
import requests
from huggingface_hub.hf_api import HfApi, HfFolder


a   :			List[Any]					  =    """__DUMMY_TRANSFORMERS_USER__"""
a   :			Tuple					  =    """Dummy User"""
a   :			Optional[Any]					  =    """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"""

a   :			Optional[Any]					  =    """https://hub-ci.huggingface.co"""
a   :			List[Any]					  =    CI_HUB_ENDPOINT + """/datasets/{repo_id}/resolve/{revision}/{path}"""
a   :			Tuple					  =    CI_HUB_ENDPOINT + """/{repo_id}/resolve/{revision}/{filename}"""
a   :			str					  =    Path("""~/.huggingface/hub_ci_token""").expanduser()



@pytest.fixture
def       __lowerCamelCase  (					_lowercase		)      ->			Optional[int]:
							monkeypatch.setattr(
							    """huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE"""	,				_lowercase		)



@pytest.fixture
def       __lowerCamelCase  (					_lowercase		)      ->			List[str]:
							monkeypatch.setattr("""datasets.config.HF_ENDPOINT"""	,				_lowercase		)
							monkeypatch.setattr("""datasets.config.HUB_DATASETS_URL"""	,				_lowercase		)



@pytest.fixture
def       __lowerCamelCase  (					_lowercase		)      ->			Any:
							monkeypatch.setattr("""huggingface_hub.hf_api.HfFolder.path_token"""	,				_lowercase		)



@pytest.fixture
def       __lowerCamelCase  (					_lowercase	,				_lowercase		)      ->			Optional[Any]:
							HfFolder.save_token(_lowercase		)
							yield
							HfFolder.delete_token()



@pytest.fixture(scope="""session"""		)
def       __lowerCamelCase  (					)      ->			str:
							return HfApi(endpoint=_lowercase		)



@pytest.fixture(scope="""session"""		)
def       __lowerCamelCase  (					_lowercase		)      ->			Union[str, Any]:
							UpperCAmelCase     :		str							     =		HfFolder.get_token()
							HfFolder.save_token(_lowercase		)
							yield CI_HUB_USER_TOKEN
							if previous_token is not None:
														HfFolder.save_token(_lowercase		)



@pytest.fixture
def       __lowerCamelCase  (					_lowercase		)      ->			Any:
							def _cleanup_repo(_lowercase		):
														hf_api.delete_repo(_lowercase	,				token=_lowercase	,				repo_type="""dataset"""		)

							return _cleanup_repo



@pytest.fixture
def       __lowerCamelCase  (					_lowercase		)      ->			List[str]:
							@contextmanager
							def _temporary_repo(_lowercase		):
														try:
																					yield repo_id
														finally:
																					cleanup_repo(_lowercase		)

							return _temporary_repo



@pytest.fixture(scope="""session"""		)
def       __lowerCamelCase  (					_lowercase	,				_lowercase	,				_lowercase		)      ->			List[str]:
							UpperCAmelCase     :		str							     =		F'''repo_txt_data-{int(time.time() * 10e3		)}'''
							UpperCAmelCase     :		List[Any]							     =		F'''{CI_HUB_USER}/{repo_name}'''
							hf_api.create_repo(_lowercase	,				token=_lowercase	,				repo_type="""dataset"""	,				private=_lowercase		)
							hf_api.upload_file(
							    token=_lowercase	,				path_or_fileobj=str(_lowercase		)	,				path_in_repo="""data/text_data.txt"""	,				repo_id=_lowercase	,				repo_type="""dataset"""	,				)
							yield repo_id
							try:
														hf_api.delete_repo(_lowercase	,				token=_lowercase	,				repo_type="""dataset"""		)
							except (requests.exceptions.HTTPError, ValueError):  # catch http error and token invalid error
														pass



@pytest.fixture()
def       __lowerCamelCase  (					_lowercase	,				_lowercase	,				_lowercase		)      ->			List[Any]:
							return hf_private_dataset_repo_txt_data_



@pytest.fixture(scope="""session"""		)
def       __lowerCamelCase  (					_lowercase	,				_lowercase	,				_lowercase		)      ->			Optional[int]:
							UpperCAmelCase     :		Optional[int]							     =		F'''repo_zipped_txt_data-{int(time.time() * 10e3		)}'''
							UpperCAmelCase     :		Optional[int]							     =		F'''{CI_HUB_USER}/{repo_name}'''
							hf_api.create_repo(_lowercase	,				token=_lowercase	,				repo_type="""dataset"""	,				private=_lowercase		)
							hf_api.upload_file(
							    token=_lowercase	,				path_or_fileobj=str(_lowercase		)	,				path_in_repo="""data.zip"""	,				repo_id=_lowercase	,				repo_type="""dataset"""	,				)
							yield repo_id
							try:
														hf_api.delete_repo(_lowercase	,				token=_lowercase	,				repo_type="""dataset"""		)
							except (requests.exceptions.HTTPError, ValueError):  # catch http error and token invalid error
														pass



@pytest.fixture()
def       __lowerCamelCase  (					_lowercase	,				_lowercase	,				_lowercase		)      ->			List[str]:
							return hf_private_dataset_repo_zipped_txt_data_



@pytest.fixture(scope="""session"""		)
def       __lowerCamelCase  (					_lowercase	,				_lowercase	,				_lowercase		)      ->			Tuple:
							UpperCAmelCase     :		List[Any]							     =		F'''repo_zipped_img_data-{int(time.time() * 10e3		)}'''
							UpperCAmelCase     :		List[str]							     =		F'''{CI_HUB_USER}/{repo_name}'''
							hf_api.create_repo(_lowercase	,				token=_lowercase	,				repo_type="""dataset"""	,				private=_lowercase		)
							hf_api.upload_file(
							    token=_lowercase	,				path_or_fileobj=str(_lowercase		)	,				path_in_repo="""data.zip"""	,				repo_id=_lowercase	,				repo_type="""dataset"""	,				)
							yield repo_id
							try:
														hf_api.delete_repo(_lowercase	,				token=_lowercase	,				repo_type="""dataset"""		)
							except (requests.exceptions.HTTPError, ValueError):  # catch http error and token invalid error
														pass



@pytest.fixture()
def       __lowerCamelCase  (					_lowercase	,				_lowercase	,				_lowercase		)      ->			List[Any]:
							return hf_private_dataset_repo_zipped_img_data_

