import argparse
from argparse import Namespace

import torch
from torch import nn

from transformers import XGLMConfig, XGLMForCausalLM



def      __SCREAMING_SNAKE_CASE  ( __UpperCamelCase   :	Optional[int]     )							-> str:







		"""simple docstring"""





		SCREAMING_SNAKE_CASE__      							=				[
		    'decoder.version',
		    'decoder.output_projection.weight',
		    '_float_tensor',
		    'decoder.embed_positions._float_tensor',
		]
		for k in ignore_keys:
				state_dict.pop(_UpperCAmelCase			,					_UpperCAmelCase     )



def      __SCREAMING_SNAKE_CASE  ( __UpperCamelCase   :	List[Any]     )							-> List[str]:







		"""simple docstring"""





		SCREAMING_SNAKE_CASE__      							=				emb.weight.shape
		SCREAMING_SNAKE_CASE__      							=				nn.Linear(_UpperCAmelCase			,					_UpperCAmelCase			,					bias=_UpperCAmelCase     )
		SCREAMING_SNAKE_CASE__      							=				emb.weight.data
		return lin_layer



def      __SCREAMING_SNAKE_CASE  ( __UpperCamelCase   :	int     )							-> Union[str, Any]:







		"""simple docstring"""





		SCREAMING_SNAKE_CASE__      							=				torch.load(_UpperCAmelCase			,					map_location="""cpu"""     )
		SCREAMING_SNAKE_CASE__      							=				Namespace(**checkpoint["""cfg"""]["""model"""]     )
		SCREAMING_SNAKE_CASE__      							=				checkpoint['model']
		remove_ignore_keys_(_UpperCAmelCase     )
		SCREAMING_SNAKE_CASE__      							=				state_dict['decoder.embed_tokens.weight'].shape[0]

		SCREAMING_SNAKE_CASE__      							=				{key.replace("""decoder"""			,					"""model"""     ): val for key, val in state_dict.items()}

		SCREAMING_SNAKE_CASE__      							=				XGLMConfig(
		    vocab_size=_UpperCAmelCase			,					max_position_embeddings=args.max_target_positions			,					num_layers=args.decoder_layers			,					attention_heads=args.decoder_attention_heads			,					ffn_dim=args.decoder_ffn_embed_dim			,					d_model=args.decoder_embed_dim			,					layerdrop=args.decoder_layerdrop			,					dropout=args.dropout			,					attention_dropout=args.attention_dropout			,					activation_dropout=args.activation_dropout			,					activation_function="""gelu"""			,					scale_embedding=not args.no_scale_embedding			,					tie_word_embeddings=args.share_decoder_input_output_embed			,					)

		SCREAMING_SNAKE_CASE__      							=				XGLMForCausalLM(_UpperCAmelCase     )
		SCREAMING_SNAKE_CASE__      							=				model.load_state_dict(_UpperCAmelCase			,					strict=_UpperCAmelCase     )
		print(_UpperCAmelCase     )
		SCREAMING_SNAKE_CASE__      							=				make_linear_from_emb(model.model.embed_tokens     )

		return model


if __name__ == "__main__":
			__lowerCamelCase      :			int							       =		argparse.ArgumentParser()
			# Required parameters
			parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''')
			parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
			__lowerCamelCase      :			Any							       =		parser.parse_args()
			__lowerCamelCase      :			int							       =		convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path)
			model.save_pretrained(args.pytorch_dump_folder_path)

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__lowerCamelCase      :			Optional[int]							       =		{
    '''configuration_whisper''': ['''WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''WhisperConfig''', '''WhisperOnnxConfig'''],
    '''feature_extraction_whisper''': ['''WhisperFeatureExtractor'''],
    '''processing_whisper''': ['''WhisperProcessor'''],
    '''tokenization_whisper''': ['''WhisperTokenizer'''],
}

try:
   if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase      :			Optional[Any]							       =		['''WhisperTokenizerFast''']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase      :			Any							       =		[
       '''WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''WhisperForConditionalGeneration''',
       '''WhisperModel''',
       '''WhisperPreTrainedModel''',
       '''WhisperForAudioClassification''',
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase      :			Tuple							       =		[
       '''TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''TFWhisperForConditionalGeneration''',
       '''TFWhisperModel''',
       '''TFWhisperPreTrainedModel''',
   ]

try:
   if not is_flax_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __lowerCamelCase      :			List[Any]							       =		[
       '''FlaxWhisperForConditionalGeneration''',
       '''FlaxWhisperModel''',
       '''FlaxWhisperPreTrainedModel''',
       '''FlaxWhisperForAudioClassification''',
   ]


if TYPE_CHECKING:
   from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig
   from .feature_extraction_whisper import WhisperFeatureExtractor
   from .processing_whisper import WhisperProcessor
   from .tokenization_whisper import WhisperTokenizer

   try:
      if not is_tokenizers_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_whisper_fast import WhisperTokenizerFast

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_whisper import (
          WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
          WhisperForAudioClassification,
          WhisperForConditionalGeneration,
          WhisperModel,
          WhisperPreTrainedModel,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_whisper import (
          TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFWhisperForConditionalGeneration,
          TFWhisperModel,
          TFWhisperPreTrainedModel,
      )

   try:
      if not is_flax_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_flax_whisper import (
          FlaxWhisperForAudioClassification,
          FlaxWhisperForConditionalGeneration,
          FlaxWhisperModel,
          FlaxWhisperPreTrainedModel,
      )

else:
   import sys

   __lowerCamelCase      :			Tuple							       =		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
