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      :     str ,			_A      :     Tuple ,			_A      :     Tuple=13 ,			_A      :     Union[str, Any]=7 ,			_A      :     Tuple=True ,			_A      :     Dict=True ,			_A      :     Optional[int]=True ,			_A      :     Any=99 ,			_A      :     str=32 ,			_A      :     List[str]=5 ,			_A      :     List[Any]=4 ,			_A      :     Tuple=37 ,			_A      :     Dict="gelu" ,			_A      :     int=0.1 ,			_A      :     List[Any]=0.1 ,			_A      :     Dict=512 ,			_A      :     int=16 ,			_A      :     List[Any]=2 ,			_A      :     Union[str, Any]=0.02 ,			_A      :     int=3 ,			_A      :     Union[str, Any]=4 ,			_A      :     List[Any]=None ,			):
												_UpperCamelCase			     =     parent
												_UpperCamelCase			     =     batch_size
												_UpperCamelCase			     =     seq_length
												_UpperCamelCase			     =     is_training
												_UpperCamelCase			     =     use_token_type_ids
												_UpperCamelCase			     =     use_labels
												_UpperCamelCase			     =     vocab_size
												_UpperCamelCase			     =     hidden_size
												_UpperCamelCase			     =     num_hidden_layers
												_UpperCamelCase			     =     num_attention_heads
												_UpperCamelCase			     =     intermediate_size
												_UpperCamelCase			     =     hidden_act
												_UpperCamelCase			     =     hidden_dropout_prob
												_UpperCamelCase			     =     attention_probs_dropout_prob
												_UpperCamelCase			     =     max_position_embeddings
												_UpperCamelCase			     =     type_vocab_size
												_UpperCamelCase			     =     type_sequence_label_size
												_UpperCamelCase			     =     initializer_range
												_UpperCamelCase			     =     num_labels
												_UpperCamelCase			     =     num_choices
												_UpperCamelCase			     =     scope
												_UpperCamelCase			     =     self.vocab_size - 1







							def  UpperCamelCase_						(   self      :     List[str]		):
												_UpperCamelCase			     =     ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size		)

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

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

												_UpperCamelCase			     =     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			     =     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      :     Tuple ,			_A      :     Union[str, Any] ,			_A      :     Union[str, Any] ,			_A      :     Union[str, Any] ,			_A      :     Union[str, Any] ,			*_A      :     List[str]		):
												_UpperCamelCase			     =     OpenAIGPTModel(config=_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A ,			token_type_ids=_A ,			head_mask=_A		)
												_UpperCamelCase			     =     model(_A ,			token_type_ids=_A		)
												_UpperCamelCase			     =     model(_A		)

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







							def  UpperCamelCase_						(   self      :     Optional[int] ,			_A      :     List[Any] ,			_A      :     int ,			_A      :     str ,			_A      :     int ,			*_A      :     Optional[Any]		):
												_UpperCamelCase			     =     OpenAIGPTLMHeadModel(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     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  UpperCamelCase_						(   self      :     Union[str, Any] ,			_A      :     Tuple ,			_A      :     str ,			_A      :     Optional[int] ,			_A      :     List[str] ,			*_A      :     List[str]		):
												_UpperCamelCase			     =     OpenAIGPTDoubleHeadsModel(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     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  UpperCamelCase_						(   self      :     Tuple ,			_A      :     Optional[int] ,			_A      :     Union[str, Any] ,			_A      :     List[str] ,			_A      :     Optional[Any] ,			*_A      :     List[Any]		):
												_UpperCamelCase			     =     self.num_labels
												_UpperCamelCase			     =     OpenAIGPTForSequenceClassification(_A		)
												model.to(_A		)
												model.eval()

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







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase			     =     self.prepare_config_and_inputs()
												(
												    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    
												)			     =     config_and_inputs
												_UpperCamelCase			     =     {
												    '''input_ids''': input_ids,
												    '''token_type_ids''': token_type_ids,
												    '''head_mask''': head_mask,
												}

												return config, inputs_dict




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







							def  UpperCamelCase_						(   self      :     Optional[Any] ,			_A      :     Tuple ,			_A      :     Optional[int] ,			_A      :     List[Any] ,			_A      :     Optional[Any] ,			_A      :     Optional[int]		):
												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      :     Dict ,			_A      :     int ,			_A      :     Any ,			_A      :     Dict=False		):
												_UpperCamelCase			     =     super()._prepare_for_class(_A ,			_A ,			return_labels=_A		)

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







							def  UpperCamelCase_						(   self      :     List[Any]		):
												_UpperCamelCase			     =     OpenAIGPTModelTester(self		)
												_UpperCamelCase			     =     ConfigTester(self ,			config_class=_A ,			n_embd=37		)







							def  UpperCamelCase_						(   self      :     Dict		):
												self.config_tester.run_common_tests()







							def  UpperCamelCase_						(   self      :     List[Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_model(*_A		)







							def  UpperCamelCase_						(   self      :     List[str]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_lm_head_model(*_A		)







							def  UpperCamelCase_						(   self      :     int		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_double_lm_head_model(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_A		)







							@slow
							def  UpperCamelCase_						(   self      :     List[str]		):
												for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_UpperCamelCase			     =     OpenAIGPTModel.from_pretrained(_A		)
																	self.assertIsNotNone(_A		)




@require_torch
class     lowerCAmelCase_     (       unittest.TestCase ):







							@slow
							def  UpperCamelCase_						(   self      :     Dict		):
												_UpperCamelCase			     =     OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''		)
												model.to(_A		)
												_UpperCamelCase			     =     torch.tensor([[481, 4735, 544]] ,			dtype=torch.long ,			device=_A		)  # the president is
												_UpperCamelCase			     =     [
												    481,
												    4735,
												    544,
												    246,
												    963,
												    870,
												    762,
												    239,
												    244,
												    4_0477,
												    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			     =     model.generate(_A ,			do_sample=_A		)
												self.assertListEqual(output_ids[0].tolist() ,			_A		)

import os
import re
import shutil
import sys
import tempfile
import unittest

import black


_lowerCAmelCase       =		os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, "utils"))

import check_copies  # noqa: E402


# This is the reference code that will be used in the tests.
# If DDPMSchedulerOutput is changed in scheduling_ddpm.py, this code needs to be manually updated.
_lowerCAmelCase       =		"    \"\"\"\n    Output class for the scheduler's step function output.\n\n    Args:\n        prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n            Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the\n            denoising loop.\n        pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):\n            The predicted denoised sample (x_{0}) based on the model output from the current timestep.\n            `pred_original_sample` can be used to preview progress or for guidance.\n    \"\"\"\n\n    prev_sample: torch.FloatTensor\n    pred_original_sample: Optional[torch.FloatTensor] = None\n"
class     lowerCAmelCase_     (       unittest.TestCase ):







							def  UpperCamelCase_						(   self      :     List[Any]		):
												_UpperCamelCase			     =     tempfile.mkdtemp()
												os.makedirs(os.path.join(self.diffusers_dir ,			'''schedulers/'''		)		)
												_UpperCamelCase			     =     self.diffusers_dir
												shutil.copy(
												    os.path.join(_A ,			'''src/diffusers/schedulers/scheduling_ddpm.py'''		) ,			os.path.join(self.diffusers_dir ,			'''schedulers/scheduling_ddpm.py'''		) ,			)







							def  UpperCamelCase_						(   self      :     List[str]		):
												_UpperCamelCase			     =     '''src/diffusers'''
												shutil.rmtree(self.diffusers_dir		)







							def  UpperCamelCase_						(   self      :     str ,			_A      :     List[str] ,			_A      :     Optional[Any] ,			_A      :     List[str] ,			_A      :     Optional[int]=None		):
												_UpperCamelCase			     =     comment + F"""\nclass {class_name}(nn.Module):\n""" + class_code
												if overwrite_result is not None:
																	_UpperCamelCase			     =     comment + F"""\nclass {class_name}(nn.Module):\n""" + overwrite_result
												_UpperCamelCase			     =     black.Mode(target_versions={black.TargetVersion.PYaa} ,			line_length=119		)
												_UpperCamelCase			     =     black.format_str(_A ,			mode=_A		)
												_UpperCamelCase			     =     os.path.join(self.diffusers_dir ,			'''new_code.py'''		)
												with open(_A ,			'''w''' ,			newline='''\n'''		) as f:
																	f.write(_A		)
												if overwrite_result is None:
																	self.assertTrue(len(check_copies.is_copy_consistent(_A		)		) == 0		)
												else:
																	check_copies.is_copy_consistent(f.name ,			overwrite=_A		)
																	with open(_A ,			'''r'''		) as f:
																						self.assertTrue(f.read() ,			_A		)







							def  UpperCamelCase_						(   self      :     Any		):
												_UpperCamelCase			     =     check_copies.find_code_in_diffusers('''schedulers.scheduling_ddpm.DDPMSchedulerOutput'''		)
												self.assertEqual(_A ,			_A		)







							def  UpperCamelCase_						(   self      :     List[str]		):
												# Base copy consistency
												self.check_copy_consistency(
												    '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' ,			'''DDPMSchedulerOutput''' ,			REFERENCE_CODE + '''\n''' ,			)

												# With no empty line at the end
												self.check_copy_consistency(
												    '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput''' ,			'''DDPMSchedulerOutput''' ,			_A ,			)

												# Copy consistency with rename
												self.check_copy_consistency(
												    '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' ,			'''TestSchedulerOutput''' ,			re.sub('''DDPM''' ,			'''Test''' ,			_A		) ,			)

												# Copy consistency with a really long name
												_UpperCamelCase			     =     '''TestClassWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason'''
												self.check_copy_consistency(
												    F"""# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->{long_class_name}""" ,			F"""{long_class_name}SchedulerOutput""" ,			re.sub('''Bert''' ,			_A ,			_A		) ,			)

												# Copy consistency with overwrite
												self.check_copy_consistency(
												    '''# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->Test''' ,			'''TestSchedulerOutput''' ,			_A ,			overwrite_result=re.sub('''DDPM''' ,			'''Test''' ,			_A		) ,			)
