
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 			A_  :




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case=13						,       snake_case=7						,       snake_case=True						,       snake_case=True						,       snake_case=True						,       snake_case=99						,       snake_case=32						,       snake_case=5						,       snake_case=4						,       snake_case=37						,       snake_case="gelu"						,       snake_case=0.1						,       snake_case=0.1						,       snake_case=512						,       snake_case=16						,       snake_case=2						,       snake_case=0.02						,       snake_case=3						,       snake_case=4						,       snake_case=None						,       ):
		lowercase	    =       parent
		lowercase	    =       batch_size
		lowercase	    =       seq_length
		lowercase	    =       is_training
		lowercase	    =       use_token_type_ids
		lowercase	    =       use_labels
		lowercase	    =       vocab_size
		lowercase	    =       hidden_size
		lowercase	    =       num_hidden_layers
		lowercase	    =       num_attention_heads
		lowercase	    =       intermediate_size
		lowercase	    =       hidden_act
		lowercase	    =       hidden_dropout_prob
		lowercase	    =       attention_probs_dropout_prob
		lowercase	    =       max_position_embeddings
		lowercase	    =       type_vocab_size
		lowercase	    =       type_sequence_label_size
		lowercase	    =       initializer_range
		lowercase	    =       num_labels
		lowercase	    =       num_choices
		lowercase	    =       scope
		lowercase	    =       self.vocab_size - 1





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       ids_tensor([self.batch_size, self.seq_length]						,       self.vocab_size			)

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

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

		lowercase	    =       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	    =       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       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       *snake_case			):
		lowercase	    =       OpenAIGPTModel(config=snake_case			)
		model.to(snake_case			)
		model.eval()

		lowercase	    =       model(snake_case						,       token_type_ids=snake_case						,       head_mask=snake_case			)
		lowercase	    =       model(snake_case						,       token_type_ids=snake_case			)
		lowercase	    =       model(snake_case			)

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





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       *snake_case			):
		lowercase	    =       OpenAIGPTLMHeadModel(snake_case			)
		model.to(snake_case			)
		model.eval()

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





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       *snake_case			):
		lowercase	    =       OpenAIGPTDoubleHeadsModel(snake_case			)
		model.to(snake_case			)
		model.eval()

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





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       *snake_case			):
		lowercase	    =       self.num_labels
		lowercase	    =       OpenAIGPTForSequenceClassification(snake_case			)
		model.to(snake_case			)
		model.eval()

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





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.prepare_config_and_inputs()
		(
		    (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   
		)	    =       config_and_inputs
		lowercase	    =       {
		    'input_ids': input_ids,
		    'token_type_ids': token_type_ids,
		    'head_mask': head_mask,
		}

		return config, inputs_dict







@require_torch
class 			A_  (     __lowerCamelCase ,      __lowerCamelCase ,      __lowerCamelCase ,      unittest.TestCase  ):




	'''simple docstring'''


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





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		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       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case=False			):
		lowercase	    =       super()._prepare_for_class(snake_case						,       snake_case						,       return_labels=snake_case			)

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





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       OpenAIGPTModelTester(self			)
		lowercase	    =       ConfigTester(self						,       config_class=snake_case						,       n_embd=37			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		self.config_tester.run_common_tests()





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_openai_gpt_model(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_lm_head_model(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_double_lm_head_model(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowercase	    =       OpenAIGPTModel.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)







@require_torch
class 			A_  (     unittest.TestCase  ):




	'''simple docstring'''


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

		lowercase	    =       model.generate(snake_case						,       do_sample=snake_case			)
		self.assertListEqual(output_ids[0].tolist()						,       snake_case			)



import importlib
import json
import os
from collections import OrderedDict
from typing import Dict, Optional, Union

# Build the list of all feature extractors
from ...configuration_utils import PretrainedConfig
from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code
from ...feature_extraction_utils import FeatureExtractionMixin
from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging
from .auto_factory import _LazyAutoMapping
from .configuration_auto import (
    CONFIG_MAPPING_NAMES,
    AutoConfig,
    model_type_to_module_name,
    replace_list_option_in_docstrings,
)


__lowerCAmelCase     :	List[str]       			=  logging.get_logger(__name__)

__lowerCAmelCase     :	List[Any]       			=  OrderedDict(
    [
        ('audio-spectrogram-transformer', 'ASTFeatureExtractor'),
        ('beit', 'BeitFeatureExtractor'),
        ('chinese_clip', 'ChineseCLIPFeatureExtractor'),
        ('clap', 'ClapFeatureExtractor'),
        ('clip', 'CLIPFeatureExtractor'),
        ('clipseg', 'ViTFeatureExtractor'),
        ('conditional_detr', 'ConditionalDetrFeatureExtractor'),
        ('convnext', 'ConvNextFeatureExtractor'),
        ('cvt', 'ConvNextFeatureExtractor'),
        ('data2vec-audio', 'Wav2Vec2FeatureExtractor'),
        ('data2vec-vision', 'BeitFeatureExtractor'),
        ('deformable_detr', 'DeformableDetrFeatureExtractor'),
        ('deit', 'DeiTFeatureExtractor'),
        ('detr', 'DetrFeatureExtractor'),
        ('dinat', 'ViTFeatureExtractor'),
        ('donut-swin', 'DonutFeatureExtractor'),
        ('dpt', 'DPTFeatureExtractor'),
        ('encodec', 'EncodecFeatureExtractor'),
        ('flava', 'FlavaFeatureExtractor'),
        ('glpn', 'GLPNFeatureExtractor'),
        ('groupvit', 'CLIPFeatureExtractor'),
        ('hubert', 'Wav2Vec2FeatureExtractor'),
        ('imagegpt', 'ImageGPTFeatureExtractor'),
        ('layoutlmv2', 'LayoutLMv2FeatureExtractor'),
        ('layoutlmv3', 'LayoutLMv3FeatureExtractor'),
        ('levit', 'LevitFeatureExtractor'),
        ('maskformer', 'MaskFormerFeatureExtractor'),
        ('mctct', 'MCTCTFeatureExtractor'),
        ('mobilenet_v1', 'MobileNetV1FeatureExtractor'),
        ('mobilenet_v2', 'MobileNetV2FeatureExtractor'),
        ('mobilevit', 'MobileViTFeatureExtractor'),
        ('nat', 'ViTFeatureExtractor'),
        ('owlvit', 'OwlViTFeatureExtractor'),
        ('perceiver', 'PerceiverFeatureExtractor'),
        ('poolformer', 'PoolFormerFeatureExtractor'),
        ('regnet', 'ConvNextFeatureExtractor'),
        ('resnet', 'ConvNextFeatureExtractor'),
        ('segformer', 'SegformerFeatureExtractor'),
        ('sew', 'Wav2Vec2FeatureExtractor'),
        ('sew-d', 'Wav2Vec2FeatureExtractor'),
        ('speech_to_text', 'Speech2TextFeatureExtractor'),
        ('speecht5', 'SpeechT5FeatureExtractor'),
        ('swiftformer', 'ViTFeatureExtractor'),
        ('swin', 'ViTFeatureExtractor'),
        ('swinv2', 'ViTFeatureExtractor'),
        ('table-transformer', 'DetrFeatureExtractor'),
        ('timesformer', 'VideoMAEFeatureExtractor'),
        ('tvlt', 'TvltFeatureExtractor'),
        ('unispeech', 'Wav2Vec2FeatureExtractor'),
        ('unispeech-sat', 'Wav2Vec2FeatureExtractor'),
        ('van', 'ConvNextFeatureExtractor'),
        ('videomae', 'VideoMAEFeatureExtractor'),
        ('vilt', 'ViltFeatureExtractor'),
        ('vit', 'ViTFeatureExtractor'),
        ('vit_mae', 'ViTFeatureExtractor'),
        ('vit_msn', 'ViTFeatureExtractor'),
        ('wav2vec2', 'Wav2Vec2FeatureExtractor'),
        ('wav2vec2-conformer', 'Wav2Vec2FeatureExtractor'),
        ('wavlm', 'Wav2Vec2FeatureExtractor'),
        ('whisper', 'WhisperFeatureExtractor'),
        ('xclip', 'CLIPFeatureExtractor'),
        ('yolos', 'YolosFeatureExtractor'),
    ]
)

__lowerCAmelCase     :	Optional[Any]       			=  _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES)

def a__       (			A_   ):






  '''simple docstring'''





  for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items():
    if class_name in extractors:
      __magic_name__								=			model_type_to_module_name(A_   )

      __magic_name__								=			importlib.import_module(f'''.{module_name}''',	"""transformers.models"""   )
      try:
        return getattr(A_,	A_   )
      except AttributeError:
        continue

  for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items():
    if getattr(A_,	"""__name__""",	A_   ) == class_name:
      return extractor

    # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main
    # init and we return the proper dummy to get an appropriate error message.
  __magic_name__								=			importlib.import_module("""transformers"""   )
  if hasattr(A_,	A_   ):
    return getattr(A_,	A_   )

  return None

def a__       (			A_,	A_ = None,	A_ = False,	A_ = False,	A_ = None,	A_ = None,	A_ = None,	A_ = False,	**A_,	):






  '''simple docstring'''





  __magic_name__								=			get_file_from_repo(
      A_,	A_,	cache_dir=A_,	force_download=A_,	resume_download=A_,	proxies=A_,	use_auth_token=A_,	revision=A_,	local_files_only=A_,	)
  if resolved_config_file is None:
    logger.info(
        """Could not locate the feature extractor configuration file, will try to use the model config instead."""   )
    return {}

  with open(A_,	encoding="""utf-8"""   ) as reader:
    return json.load(A_   )





class 			UpperCAmelCase_     :


  '''simple docstring'''







  def __init__(					self     :					Dict       )     ->      Optional[int]:







    """simple docstring"""






    raise EnvironmentError(
        """AutoFeatureExtractor is designed to be instantiated """
        """using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method."""       )
  @classmethod
  @replace_list_option_in_docstrings(UpperCamelCase__       )
  def 				_lowercase				(					cls     :					Optional[int]     ,				UpperCamelCase__     :					Any     ,				**UpperCamelCase__     :					Optional[int]       )     ->      str:







    """simple docstring"""






    __magic_name__								=			kwargs.pop("""config"""     ,				UpperCamelCase__       )
    __magic_name__								=			kwargs.pop("""trust_remote_code"""     ,				UpperCamelCase__       )
    __magic_name__								=			True

    __magic_name__	,       __magic_name__								=			FeatureExtractionMixin.get_feature_extractor_dict(UpperCamelCase__     ,				**UpperCamelCase__       )
    __magic_name__								=			config_dict.get("""feature_extractor_type"""     ,				UpperCamelCase__       )
    __magic_name__								=			None
    if "AutoFeatureExtractor" in config_dict.get("""auto_map"""     ,				{}       ):
      __magic_name__								=			config_dict["""auto_map"""]["""AutoFeatureExtractor"""]

    # If we don't find the feature extractor class in the feature extractor config, let's try the model config.
    if feature_extractor_class is None and feature_extractor_auto_map is None:
      if not isinstance(UpperCamelCase__     ,				UpperCamelCase__       ):
        __magic_name__								=			AutoConfig.from_pretrained(UpperCamelCase__     ,				**UpperCamelCase__       )
      # It could be in `config.feature_extractor_type``
      __magic_name__								=			getattr(UpperCamelCase__     ,				"""feature_extractor_type"""     ,				UpperCamelCase__       )
      if hasattr(UpperCamelCase__     ,				"""auto_map"""       ) and "AutoFeatureExtractor" in config.auto_map:
        __magic_name__								=			config.auto_map["""AutoFeatureExtractor"""]

    if feature_extractor_class is not None:
      __magic_name__								=			feature_extractor_class_from_name(UpperCamelCase__       )

    __magic_name__								=			feature_extractor_auto_map is not None
    __magic_name__								=			feature_extractor_class is not None or type(UpperCamelCase__       ) in FEATURE_EXTRACTOR_MAPPING
    __magic_name__								=			resolve_trust_remote_code(
        UpperCamelCase__     ,				UpperCamelCase__     ,				UpperCamelCase__     ,				UpperCamelCase__       )

    if has_remote_code and trust_remote_code:
      __magic_name__								=			get_class_from_dynamic_module(
          UpperCamelCase__     ,				UpperCamelCase__     ,				**UpperCamelCase__       )
      __magic_name__								=			kwargs.pop("""code_revision"""     ,				UpperCamelCase__       )
      if os.path.isdir(UpperCamelCase__       ):
        feature_extractor_class.register_for_auto_class()
      return feature_extractor_class.from_dict(UpperCamelCase__     ,				**UpperCamelCase__       )
    elif feature_extractor_class is not None:
      return feature_extractor_class.from_dict(UpperCamelCase__     ,				**UpperCamelCase__       )
    # Last try: we use the FEATURE_EXTRACTOR_MAPPING.
    elif type(UpperCamelCase__       ) in FEATURE_EXTRACTOR_MAPPING:
      __magic_name__								=			FEATURE_EXTRACTOR_MAPPING[type(UpperCamelCase__       )]
      return feature_extractor_class.from_dict(UpperCamelCase__     ,				**UpperCamelCase__       )

    raise ValueError(
        F'''Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a '''
        F'''`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following '''
        F'''`model_type` keys in its {CONFIG_NAME}: {', '.join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys()       )}'''       )





  @staticmethod
  def 				_lowercase				(					UpperCamelCase__     :					Any     ,				UpperCamelCase__     :					Optional[Any]       )     ->      Dict:







    """simple docstring"""






    FEATURE_EXTRACTOR_MAPPING.register(UpperCamelCase__     ,				UpperCamelCase__       )

