

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






				"""simple docstring"""



				def __init__(    self,     _lowercase,     _lowercase=13,     _lowercase=7,     _lowercase=True,     _lowercase=True,     _lowercase=True,     _lowercase=99,     _lowercase=32,     _lowercase=5,     _lowercase=4,     _lowercase=37,     _lowercase="gelu",     _lowercase=0.1,     _lowercase=0.1,     _lowercase=512,     _lowercase=16,     _lowercase=2,     _lowercase=0.02,     _lowercase=3,     _lowercase=4,     _lowercase=None,     )     ->							List[Any]:
											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   a__					(    self       )     ->							List[str]:
											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   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     *_lowercase       )     ->							Optional[Any]:
											SCREAMING_SNAKE_CASE_				=				OpenAIGPTModel(config=_lowercase       )
											model.to(_lowercase       )
											model.eval()

											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     token_type_ids=_lowercase,     head_mask=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     token_type_ids=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				model(_lowercase       )

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



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     *_lowercase       )     ->							int:
											SCREAMING_SNAKE_CASE_				=				OpenAIGPTLMHeadModel(_lowercase       )
											model.to(_lowercase       )
											model.eval()

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



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     *_lowercase       )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				OpenAIGPTDoubleHeadsModel(_lowercase       )
											model.to(_lowercase       )
											model.eval()

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



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase,     _lowercase,     *_lowercase       )     ->							str:
											SCREAMING_SNAKE_CASE_				=				self.num_labels
											SCREAMING_SNAKE_CASE_				=				OpenAIGPTForSequenceClassification(_lowercase       )
											model.to(_lowercase       )
											model.eval()

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





				def   a__					(    self       )     ->							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    snake_case     (      lowercase_,						lowercase_,						lowercase_,						unittest.TestCase		):






				"""simple docstring"""
				_a    	=     (
				    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
				    if is_torch_available()
				    else ()
				)
				_a    	=     (
				    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
				)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
				_a    	=     (
				    {
				        """feature-extraction""": OpenAIGPTModel,
				        """text-classification""": OpenAIGPTForSequenceClassification,
				        """text-generation""": OpenAIGPTLMHeadModel,
				        """zero-shot""": OpenAIGPTForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)



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

											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=_lowercase,     )
																									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=_lowercase,     )
																									SCREAMING_SNAKE_CASE_				=				torch.zeros(
																									    self.model_tester.batch_size,     dtype=torch.long,     device=_lowercase       )
											return inputs_dict



				def   a__					(    self       )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				OpenAIGPTModelTester(self       )
											SCREAMING_SNAKE_CASE_				=				ConfigTester(self,     config_class=_lowercase,     n_embd=37       )



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



				def   a__					(    self       )     ->							Optional[int]:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_model(*_lowercase       )



				def   a__					(    self       )     ->							int:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_lm_head_model(*_lowercase       )



				def   a__					(    self       )     ->							Optional[Any]:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_double_lm_head_model(*_lowercase       )



				def   a__					(    self       )     ->							Any:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowercase       )





				@slow
				def   a__					(    self       )     ->							Any:
											for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE_				=				OpenAIGPTModel.from_pretrained(_lowercase       )
																		self.assertIsNotNone(_lowercase       )






@require_torch
class    snake_case     (      unittest.TestCase		):






				"""simple docstring"""



				@slow
				def   a__					(    self       )     ->							str:
											SCREAMING_SNAKE_CASE_				=				OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'       )
											model.to(_lowercase       )
											SCREAMING_SNAKE_CASE_				=				torch.tensor([[481, 4735, 544]],     dtype=torch.long,     device=_lowercase       )  # the president is
											SCREAMING_SNAKE_CASE_				=				[
											    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

											SCREAMING_SNAKE_CASE_				=				model.generate(_lowercase,     do_sample=_lowercase       )
											self.assertListEqual(output_ids[0].tolist(),     _lowercase       )








'''simple docstring'''

import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils"))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


SCREAMING_SNAKE_CASE							:				int     	=			get_tests_dir("fixtures")
SCREAMING_SNAKE_CASE							:				str     	=			get_tests_dir("fixtures/dummy_feature_extractor_config.json")
SCREAMING_SNAKE_CASE							:				str     	=			get_tests_dir("fixtures/dummy-config.json")






class    snake_case     (      unittest.TestCase		):






				"""simple docstring"""



				def   a__					(    self       )     ->							str:
											SCREAMING_SNAKE_CASE_				=				0



				def   a__					(    self       )     ->							Tuple:
											SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained('facebook/wav2vec2-base-960h'       )
											self.assertIsInstance(_lowercase,     _lowercase       )



				def   a__					(    self       )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase       )
											self.assertIsInstance(_lowercase,     _lowercase       )



				def   a__					(    self       )     ->							str:
											with tempfile.TemporaryDirectory() as tmpdirname:
																		SCREAMING_SNAKE_CASE_				=				WavaVecaConfig()

																		# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase       ).to_dict()

																		config_dict.pop('feature_extractor_type'       )
																		SCREAMING_SNAKE_CASE_				=				WavaVecaFeatureExtractor(**_lowercase       )

																		# save in new folder
																		model_config.save_pretrained(_lowercase       )
																		config.save_pretrained(_lowercase       )

																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase       )

																		# make sure private variable is not incorrectly saved
																		SCREAMING_SNAKE_CASE_				=				json.loads(config.to_json_string()       )
																		self.assertTrue('_processor_class' not in dict_as_saved       )

											self.assertIsInstance(_lowercase,     _lowercase       )



				def   a__					(    self       )     ->							int:
											SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase       )
											self.assertIsInstance(_lowercase,     _lowercase       )



				def   a__					(    self       )     ->							Any:
											with self.assertRaisesRegex(
											    _lowercase,     'bert-base is not a local folder and is not a valid model identifier'       ):
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained('bert-base'       )



				def   a__					(    self       )     ->							List[Any]:
											with self.assertRaisesRegex(
											    _lowercase,     R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'       ):
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase,     revision='aaaaaa'       )



				def   a__					(    self       )     ->							List[Any]:
											with self.assertRaisesRegex(
											    _lowercase,     'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.',     ):
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained('hf-internal-testing/config-no-model'       )



				def   a__					(    self       )     ->							List[str]:
											# If remote code is not set, we will time out when asking whether to load the model.
											with self.assertRaises(_lowercase       ):
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_feature_extractor'       )
											# If remote code is disabled, we can't load this config.
											with self.assertRaises(_lowercase       ):
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_feature_extractor',     trust_remote_code=_lowercase       )

											SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
											    'hf-internal-testing/test_dynamic_feature_extractor',     trust_remote_code=_lowercase       )
											self.assertEqual(feature_extractor.__class__.__name__,     'NewFeatureExtractor'       )

											# Test feature extractor can be reloaded.
											with tempfile.TemporaryDirectory() as tmp_dir:
																		feature_extractor.save_pretrained(_lowercase       )
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase,     trust_remote_code=_lowercase       )
											self.assertEqual(reloaded_feature_extractor.__class__.__name__,     'NewFeatureExtractor'       )



				def   a__					(    self       )     ->							int:
											try:
																		AutoConfig.register('custom',     _lowercase       )
																		AutoFeatureExtractor.register(_lowercase,     _lowercase       )
																		# Trying to register something existing in the Transformers library will raise an error
																		with self.assertRaises(_lowercase       ):
																									AutoFeatureExtractor.register(_lowercase,     _lowercase       )

																		# Now that the config is registered, it can be used as any other config with the auto-API
																		SCREAMING_SNAKE_CASE_				=				CustomFeatureExtractor.from_pretrained(_lowercase       )
																		with tempfile.TemporaryDirectory() as tmp_dir:
																									feature_extractor.save_pretrained(_lowercase       )
																									SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(_lowercase       )
																									self.assertIsInstance(_lowercase,     _lowercase       )

											finally:
																		if "custom" in CONFIG_MAPPING._extra_content:
																									del CONFIG_MAPPING._extra_content["custom"]
																		if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
																									del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]





				def   a__					(    self       )     ->							str:






											class    snake_case     (      lowercase_		):






															"""simple docstring"""
															_a    	=     True

											try:
																		AutoConfig.register('custom',     _lowercase       )
																		AutoFeatureExtractor.register(_lowercase,     _lowercase       )
																		# If remote code is not set, the default is to use local
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_feature_extractor'       )
																		self.assertEqual(feature_extractor.__class__.__name__,     'NewFeatureExtractor'       )
																		self.assertTrue(feature_extractor.is_local       )

																		# If remote code is disabled, we load the local one.
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_feature_extractor',     trust_remote_code=_lowercase       )
																		self.assertEqual(feature_extractor.__class__.__name__,     'NewFeatureExtractor'       )
																		self.assertTrue(feature_extractor.is_local       )

																		# If remote is enabled, we load from the Hub
																		SCREAMING_SNAKE_CASE_				=				AutoFeatureExtractor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_feature_extractor',     trust_remote_code=_lowercase       )
																		self.assertEqual(feature_extractor.__class__.__name__,     'NewFeatureExtractor'       )
																		self.assertTrue(not hasattr(_lowercase,     'is_local'       )       )

											finally:
																		if "custom" in CONFIG_MAPPING._extra_content:
																									del CONFIG_MAPPING._extra_content["custom"]
																		if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
																									del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]





