





"""simple docstring"""




import argparse
import json
import os

import fairseq
import torch
from fairseq.data import Dictionary

# Register SEW's fairseq modules
from sew_asapp import tasks  # noqa: F401

from transformers import (
    SEWConfig,
    SEWForCTC,
    SEWModel,
    WavaVecaCTCTokenizer,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
    logging,
)


logging.set_verbosity_info()
UpperCamelCase_									=logging.get_logger(__name__)

UpperCamelCase_									={
    """post_extract_proj""": """feature_projection""",
    """encoder.pos_conv.0""": """encoder.pos_conv_embed.conv""",
    """self_attn.k_proj""": """encoder.layers.*.attention.k_proj""",
    """self_attn.v_proj""": """encoder.layers.*.attention.v_proj""",
    """self_attn.q_proj""": """encoder.layers.*.attention.q_proj""",
    """self_attn.out_proj""": """encoder.layers.*.attention.out_proj""",
    """self_attn_layer_norm""": """encoder.layers.*.layer_norm""",
    """fc1""": """encoder.layers.*.feed_forward.intermediate_dense""",
    """fc2""": """encoder.layers.*.feed_forward.output_dense""",
    """final_layer_norm""": """encoder.layers.*.final_layer_norm""",
    """encoder.upsample.0""": """encoder.upsample.projection""",
    """encoder.layer_norm""": """encoder.layer_norm""",
    """w2v_model.layer_norm""": """layer_norm""",
    """w2v_encoder.proj""": """lm_head""",
    """mask_emb""": """masked_spec_embed""",
}





def 	a_    (							_lowercase	,						_lowercase	,						_lowercase	,						_lowercase	,						_lowercase       ):
						for attribute in key.split('''.'''       ):
												_UpperCamelCase      : Dict         =   getattr(_lowercase	,						_lowercase       )

						if weight_type is not None:
												_UpperCamelCase      : str         =   getattr(_lowercase	,						_lowercase       ).shape
						else:
												_UpperCamelCase      : List[str]         =   hf_pointer.shape

						assert hf_shape == value.shape, (
						    F"""Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be"""
						    F""" {value.shape} for {full_name}"""
						)

						if weight_type == "weight":
												_UpperCamelCase      : Dict         =   value
						elif weight_type == "weight_g":
												_UpperCamelCase      : int         =   value
						elif weight_type == "weight_v":
												_UpperCamelCase      : List[str]         =   value
						elif weight_type == "bias":
												_UpperCamelCase      : List[str]         =   value
						else:
												_UpperCamelCase      : Union[str, Any]         =   value

						logger.info(F"""{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}."""       )





def 	a_    (							_lowercase	,						_lowercase	,						_lowercase       ):
						_UpperCamelCase      : str         =   []
						_UpperCamelCase      : List[str]         =   fairseq_model.state_dict()

						_UpperCamelCase      : Optional[int]         =   hf_model.sew.feature_extractor if is_finetuned else hf_model.feature_extractor

						for name, value in fairseq_dict.items():
												_UpperCamelCase      : Dict         =   False
												if "conv_layers" in name:
																		load_conv_layer(
																		    _lowercase	,						_lowercase	,						_lowercase	,						_lowercase	,						hf_model.config.feat_extract_norm == '''group'''	,						)
																		_UpperCamelCase      : Optional[Any]         =   True
												else:
																		for key, mapped_key in MAPPING.items():
																								_UpperCamelCase      : str         =   '''sew.''' + mapped_key if (is_finetuned and mapped_key != '''lm_head''') else mapped_key

																								if key in name or key.split('''w2v_model.'''       )[-1] == name.split('''.'''       )[0]:
																														_UpperCamelCase      : Tuple         =   True
																														if "*" in mapped_key:
																																				_UpperCamelCase      : str         =   name.split(_lowercase       )[0].split('''.'''       )[-2]
																																				_UpperCamelCase      : Optional[Any]         =   mapped_key.replace('''*'''	,						_lowercase       )
																														if "weight_g" in name:
																																				_UpperCamelCase      : Tuple         =   '''weight_g'''
																														elif "weight_v" in name:
																																				_UpperCamelCase      : List[str]         =   '''weight_v'''
																														elif "weight" in name:
																																				_UpperCamelCase      : int         =   '''weight'''
																														elif "bias" in name:
																																				_UpperCamelCase      : List[Any]         =   '''bias'''
																														else:
																																				_UpperCamelCase      : Optional[int]         =   None
																														set_recursively(_lowercase	,						_lowercase	,						_lowercase	,						_lowercase	,						_lowercase       )
																								continue
												if not is_used:
																		unused_weights.append(_lowercase       )

						logger.warning(F"""Unused weights: {unused_weights}"""       )





def 	a_    (							_lowercase	,						_lowercase	,						_lowercase	,						_lowercase	,						_lowercase       ):
						_UpperCamelCase      : int         =   full_name.split('''conv_layers.'''       )[-1]
						_UpperCamelCase      : Optional[int]         =   name.split('''.'''       )
						_UpperCamelCase      : str         =   int(items[0]       )
						_UpperCamelCase      : Any         =   int(items[1]       )

						if type_id == 0:
												if "bias" in name:
																		assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
																		    F"""{full_name} has size {value.shape}, but"""
																		    F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found."""
																		)
																		_UpperCamelCase      : List[Any]         =   value
																		logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}."""       )
												elif "weight" in name:
																		assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
																		    F"""{full_name} has size {value.shape}, but"""
																		    F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found."""
																		)
																		_UpperCamelCase      : Optional[int]         =   value
																		logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}."""       )
						elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
												if "bias" in name:
																		assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
																		    F"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was"""
																		    " found."
																		)
																		_UpperCamelCase      : Optional[Any]         =   value
																		logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."""       )
												elif "weight" in name:
																		assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
																		    F"""{full_name} has size {value.shape}, but"""
																		    F""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found."""
																		)
																		_UpperCamelCase      : List[str]         =   value
																		logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."""       )
						else:
												unused_weights.append(_lowercase       )





def 	a_    (							_lowercase	,						_lowercase       ):
						_UpperCamelCase      : Optional[int]         =   SEWConfig()
						if is_finetuned:
												_UpperCamelCase      : Optional[int]         =   model.wav_encoder.wav_model.cfg
						else:
												_UpperCamelCase      : Union[str, Any]         =   model.cfg

						_UpperCamelCase      : int         =   fs_config.conv_bias
						_UpperCamelCase      : int         =   eval(fs_config.conv_feature_layers       )
						_UpperCamelCase      : Any         =   [x[0] for x in conv_layers]
						_UpperCamelCase      : List[Any]         =   [x[1] for x in conv_layers]
						_UpperCamelCase      : Union[str, Any]         =   [x[2] for x in conv_layers]
						_UpperCamelCase      : List[Any]         =   '''gelu'''
						_UpperCamelCase      : Optional[Any]         =   '''layer''' if fs_config.extractor_mode == '''layer_norm''' else '''group'''
						_UpperCamelCase      : List[str]         =   0.0
						_UpperCamelCase      : Optional[Any]         =   fs_config.activation_fn.name
						_UpperCamelCase      : Union[str, Any]         =   fs_config.encoder_embed_dim
						_UpperCamelCase      : Dict         =   0.02
						_UpperCamelCase      : Optional[Any]         =   fs_config.encoder_ffn_embed_dim
						_UpperCamelCase      : Any         =   1E-5
						_UpperCamelCase      : List[Any]         =   fs_config.encoder_layerdrop
						_UpperCamelCase      : Optional[Any]         =   fs_config.encoder_attention_heads
						_UpperCamelCase      : List[str]         =   fs_config.conv_pos_groups
						_UpperCamelCase      : Union[str, Any]         =   fs_config.conv_pos
						_UpperCamelCase      : Union[str, Any]         =   len(_lowercase       )
						_UpperCamelCase      : Any         =   fs_config.encoder_layers
						_UpperCamelCase      : Tuple         =   fs_config.squeeze_factor

						# take care of any params that are overridden by the Wav2VecCtc model
						if is_finetuned:
												_UpperCamelCase      : str         =   model.cfg
												_UpperCamelCase      : Optional[int]         =   fs_config.final_dropout
												_UpperCamelCase      : Any         =   fs_config.layerdrop
						_UpperCamelCase      : List[str]         =   fs_config.activation_dropout
						_UpperCamelCase      : Optional[Any]         =   fs_config.mask_prob > 0 or fs_config.mask_channel_prob > 0
						_UpperCamelCase      : Union[str, Any]         =   fs_config.attention_dropout
						_UpperCamelCase      : Any         =   fs_config.dropout_input
						_UpperCamelCase      : Optional[int]         =   fs_config.dropout
						_UpperCamelCase      : Tuple         =   fs_config.mask_channel_length
						_UpperCamelCase      : Union[str, Any]         =   fs_config.mask_channel_prob
						_UpperCamelCase      : List[str]         =   fs_config.mask_length
						_UpperCamelCase      : Optional[Any]         =   fs_config.mask_prob

						_UpperCamelCase      : Dict         =   '''Wav2Vec2FeatureExtractor'''
						_UpperCamelCase      : Any         =   '''Wav2Vec2CTCTokenizer'''

						return config





@torch.no_grad()
def 	a_    (							_lowercase	,						_lowercase	,						_lowercase=None	,						_lowercase=None	,						_lowercase=True       ):

						if is_finetuned:
												_UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase      : str         =   fairseq.checkpoint_utils.load_model_ensemble_and_task(
												    [checkpoint_path]	,						arg_overrides={'''data''': '''/'''.join(dict_path.split('''/'''       )[:-1]       )}       )
						else:
												_UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase      : int         =   fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path]       )

						if config_path is not None:
												_UpperCamelCase      : Union[str, Any]         =   SEWConfig.from_pretrained(_lowercase       )
						else:
												_UpperCamelCase      : int         =   convert_config(model[0]	,						_lowercase       )
						_UpperCamelCase      : int         =   model[0].eval()

						_UpperCamelCase      : Any         =   True if config.feat_extract_norm == '''layer''' else False
						_UpperCamelCase      : str         =   WavaVecaFeatureExtractor(
						    feature_size=1	,						sampling_rate=1_6000	,						padding_value=0	,						do_normalize=_lowercase	,						return_attention_mask=_lowercase	,						)

						if is_finetuned:
												if dict_path:
																		_UpperCamelCase      : Optional[int]         =   Dictionary.load(_lowercase       )

																		# important change bos & pad token id since CTC symbol is <pad> and
																		# not <s> as in fairseq
																		_UpperCamelCase      : Dict         =   target_dict.pad_index
																		_UpperCamelCase      : Optional[int]         =   target_dict.bos_index
																		_UpperCamelCase      : int         =   target_dict.pad_index
																		_UpperCamelCase      : Union[str, Any]         =   target_dict.bos_index
																		_UpperCamelCase      : Tuple         =   target_dict.eos_index
																		_UpperCamelCase      : Union[str, Any]         =   len(target_dict.symbols       )
																		_UpperCamelCase      : Dict         =   os.path.join(_lowercase	,						'''vocab.json'''       )
																		if not os.path.isdir(_lowercase       ):
																								logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(_lowercase       )       )
																								return
																		os.makedirs(_lowercase	,						exist_ok=_lowercase       )
																		with open(_lowercase	,						'''w'''	,						encoding='''utf-8'''       ) as vocab_handle:
																								json.dump(target_dict.indices	,						_lowercase       )
																		_UpperCamelCase      : Optional[Any]         =   WavaVecaCTCTokenizer(
																		    _lowercase	,						unk_token=target_dict.unk_word	,						pad_token=target_dict.pad_word	,						bos_token=target_dict.bos_word	,						eos_token=target_dict.eos_word	,						word_delimiter_token='''|'''	,						do_lower_case=_lowercase	,						)
																		_UpperCamelCase      : Union[str, Any]         =   WavaVecaProcessor(feature_extractor=_lowercase	,						tokenizer=_lowercase       )
																		processor.save_pretrained(_lowercase       )

												_UpperCamelCase      : Union[str, Any]         =   SEWForCTC(_lowercase       )
						else:
												_UpperCamelCase      : Any         =   SEWModel(_lowercase       )
												feature_extractor.save_pretrained(_lowercase       )

						recursively_load_weights(_lowercase	,						_lowercase	,						_lowercase       )

						hf_model.save_pretrained(_lowercase       )


if __name__ == "__main__":
						UpperCamelCase_									=argparse.ArgumentParser()
						parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
						parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to fairseq checkpoint""")
						parser.add_argument("""--dict_path""", default=None, type=str, help="""Path to dict of fine-tuned model""")
						parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
						parser.add_argument(
						    """--is_finetuned""", action="""store_true""", help="""Whether the model to convert is a fine-tuned model or not"""
						)
						UpperCamelCase_									=parser.parse_args()
						convert_sew_checkpoint(
						    args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, args.is_finetuned
						)










"""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 			_a						:






				def __init__(			self		:   Dict,			lowerCAmelCase__		:   Optional[Any],			lowerCAmelCase__		:   Optional[int]=1_3,			lowerCAmelCase__		:   Optional[Any]=7,			lowerCAmelCase__		:   Optional[Any]=True,			lowerCAmelCase__		:   Any=True,			lowerCAmelCase__		:   str=True,			lowerCAmelCase__		:   Any=9_9,			lowerCAmelCase__		:   Dict=3_2,			lowerCAmelCase__		:   List[Any]=5,			lowerCAmelCase__		:   Tuple=4,			lowerCAmelCase__		:   List[Any]=3_7,			lowerCAmelCase__		:   Tuple="gelu",			lowerCAmelCase__		:   Any=0.1,			lowerCAmelCase__		:   Optional[Any]=0.1,			lowerCAmelCase__		:   Dict=5_1_2,			lowerCAmelCase__		:   List[str]=1_6,			lowerCAmelCase__		:   Tuple=2,			lowerCAmelCase__		:   int=0.02,			lowerCAmelCase__		:   int=3,			lowerCAmelCase__		:   Optional[Any]=4,			lowerCAmelCase__		:   Dict=None,			)     ->				int:




										'''simple docstring'''



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






				def     snake_case     (			self		:   int							)     ->				Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase      : Dict         =   ids_tensor([self.batch_size, self.seq_length],			self.vocab_size							)

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

										_UpperCamelCase      : Optional[int]         =   None
										_UpperCamelCase      : str         =   None
										_UpperCamelCase      : List[str]         =   None
										if self.use_labels:
																_UpperCamelCase      : int         =   ids_tensor([self.batch_size],			self.type_sequence_label_size							)
																_UpperCamelCase      : Dict         =   ids_tensor([self.batch_size, self.seq_length],			self.num_labels							)
																_UpperCamelCase      : Dict         =   ids_tensor([self.batch_size],			self.num_choices							)

										_UpperCamelCase      : str         =   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      : List[Any]         =   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		:   Union[str, Any],			lowerCAmelCase__		:   Optional[int],			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   List[str],			*lowerCAmelCase__		:   List[Any]							)     ->				Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase      : Dict         =   OpenAIGPTModel(config=lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()

										_UpperCamelCase      : List[str]         =   model(lowerCAmelCase__,			token_type_ids=lowerCAmelCase__,			head_mask=lowerCAmelCase__							)
										_UpperCamelCase      : Any         =   model(lowerCAmelCase__,			token_type_ids=lowerCAmelCase__							)
										_UpperCamelCase      : List[str]         =   model(lowerCAmelCase__							)

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






				def     snake_case     (			self		:   Any,			lowerCAmelCase__		:   Tuple,			lowerCAmelCase__		:   Union[str, Any],			lowerCAmelCase__		:   Any,			lowerCAmelCase__		:   Optional[Any],			*lowerCAmelCase__		:   Union[str, Any]							)     ->				Any:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   OpenAIGPTLMHeadModel(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()

										_UpperCamelCase      : Tuple         =   model(lowerCAmelCase__,			token_type_ids=lowerCAmelCase__,			labels=lowerCAmelCase__							)
										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		:   Optional[int],			lowerCAmelCase__		:   str,			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   Any,			lowerCAmelCase__		:   List[Any],			*lowerCAmelCase__		:   Any							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : Tuple         =   OpenAIGPTDoubleHeadsModel(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()

										_UpperCamelCase      : Optional[int]         =   model(lowerCAmelCase__,			token_type_ids=lowerCAmelCase__,			labels=lowerCAmelCase__							)
										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		:   List[str],			lowerCAmelCase__		:   Dict,			lowerCAmelCase__		:   Dict,			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   Optional[Any],			*lowerCAmelCase__		:   List[str]							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   self.num_labels
										_UpperCamelCase      : Optional[int]         =   OpenAIGPTForSequenceClassification(lowerCAmelCase__							)
										model.to(lowerCAmelCase__							)
										model.eval()

										_UpperCamelCase      : str         =   ids_tensor([self.batch_size],			self.type_sequence_label_size							)
										_UpperCamelCase      : Union[str, Any]         =   model(lowerCAmelCase__,			token_type_ids=lowerCAmelCase__,			labels=lowerCAmelCase__							)
										self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.num_labels)							)






				def     snake_case     (			self		:   str							)     ->				Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   self.prepare_config_and_inputs()
										(
										    (
										    _UpperCamelCase
										)       ,							(
										    _UpperCamelCase
										)       ,							(
										    _UpperCamelCase
										)       ,							(
										    _UpperCamelCase
										)       ,							(
										    _UpperCamelCase
										)       ,							(
										    _UpperCamelCase
										)       ,							(
										    _UpperCamelCase
										)       ,							
										)      : Tuple         =   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 			_a						(				_lowerCAmelCase     ,    _lowerCAmelCase     ,    _lowerCAmelCase     ,    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     snake_case     (			self		:   Union[str, Any],			lowerCAmelCase__		:   Any,			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   str,			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   List[str]							)     ->				List[str]:




										'''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		:   str,			lowerCAmelCase__		:   Optional[int],			lowerCAmelCase__		:   List[str],			lowerCAmelCase__		:   Optional[int]=False							)     ->				Tuple:




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   super()._prepare_for_class(lowerCAmelCase__,			lowerCAmelCase__,			return_labels=lowerCAmelCase__							)

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






				def     snake_case     (			self		:   List[str]							)     ->				List[str]:




										'''simple docstring'''



										_UpperCamelCase      : Optional[Any]         =   OpenAIGPTModelTester(self							)
										_UpperCamelCase      : int         =   ConfigTester(self,			config_class=lowerCAmelCase__,			n_embd=3_7							)






				def     snake_case     (			self		:   Optional[int]							)     ->				str:




										'''simple docstring'''



										self.config_tester.run_common_tests()






				def     snake_case     (			self		:   Optional[int]							)     ->				Any:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__							)






				def     snake_case     (			self		:   Any							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__							)






				def     snake_case     (			self		:   int							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : Union[str, Any]         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__							)






				def     snake_case     (			self		:   List[str]							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__							)






				@slow
				def     snake_case     (			self		:   Optional[Any]							)     ->				Optional[Any]:




										'''simple docstring'''



										for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_UpperCamelCase      : int         =   OpenAIGPTModel.from_pretrained(lowerCAmelCase__							)
																self.assertIsNotNone(lowerCAmelCase__							)




@require_torch
class 			_a						(				unittest.TestCase    ):






				@slow
				def     snake_case     (			self		:   Union[str, Any]							)     ->				Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase      : int         =   OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''							)
										model.to(lowerCAmelCase__							)
										_UpperCamelCase      : str         =   torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]],			dtype=torch.long,			device=lowerCAmelCase__							)  # the president is
										_UpperCamelCase      : Optional[int]         =   [
										    4_8_1,
										    4_7_3_5,
										    5_4_4,
										    2_4_6,
										    9_6_3,
										    8_7_0,
										    7_6_2,
										    2_3_9,
										    2_4_4,
										    4_0_4_7_7,
										    2_4_4,
										    2_4_9,
										    7_1_9,
										    8_8_1,
										    4_8_7,
										    5_4_4,
										    2_4_0,
										    2_4_4,
										    6_0_3,
										    4_8_1,
										]  # the president is a very good man. " \n " i\'m sure he is, " said the

										_UpperCamelCase      : Union[str, Any]         =   model.generate(lowerCAmelCase__,			do_sample=lowerCAmelCase__							)
										self.assertListEqual(output_ids[0].tolist(),			lowerCAmelCase__							)



