





'''simple docstring'''







import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
   from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
   import torch_xla.core.xla_model as xm

def    __UpperCamelCase	(	a    : List[Any]     ) ->int:
 if is_torch_version('''<'''					,   '''2.0.0'''     ) or not hasattr(a					,   '''_dynamo'''     ):
  return False
 return isinstance(a					,   torch._dynamo.eval_frame.OptimizedModule     )

def    __UpperCamelCase	(	a    : List[Any]					,   a    : bool = True     ) ->Tuple:
 snake_case				=			(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

 snake_case				=			is_compiled_module(a     )
 if is_compiled:
  snake_case				=			model
  snake_case				=			model._orig_mod

 if is_deepspeed_available():
  options += (DeepSpeedEngine,)

 while isinstance(a					,   a     ):
  snake_case				=			model.module

 if not keep_fpaa_wrapper:
  snake_case				=			getattr(a					,   '''forward'''     )
  snake_case				=			model.__dict__.pop('''_original_forward'''					,   a     )
  if original_forward is not None:
   while hasattr(a					,   '''__wrapped__'''     ):
    snake_case				=			forward.__wrapped__
    if forward == original_forward:
     break
   snake_case				=			forward
  if getattr(a					,   '''_converted_to_transformer_engine'''					,   a     ):
   convert_model(a					,   to_transformer_engine=a     )

 if is_compiled:
  snake_case				=			model
  snake_case				=			compiled_model

 return model

def    __UpperCamelCase	(	) ->Dict:
 PartialState().wait_for_everyone()

def    __UpperCamelCase	(	a    : str					,   a    : Tuple     ) ->Optional[int]:
 if PartialState().distributed_type == DistributedType.TPU:
  xm.save(a					,   a     )
 elif PartialState().local_process_index == 0:
  torch.save(a					,   a     )

@contextmanager
def    __UpperCamelCase	(	**a    : Optional[int]     ) ->List[Any]:
 for key, value in kwargs.items():
  snake_case				=			str(a     )

 yield

 for key in kwargs:
  if key.upper() in os.environ:
   del os.environ[key.upper()]

def    __UpperCamelCase	(	a    : Optional[Any]     ) ->List[Any]:
 if not hasattr(a					,   '''__qualname__'''     ) and not hasattr(a					,   '''__name__'''     ):
  snake_case				=			getattr(a					,   '''__class__'''					,   a     )
 if hasattr(a					,   '''__qualname__'''     ):
  return obj.__qualname__
 if hasattr(a					,   '''__name__'''     ):
  return obj.__name__
 return str(a     )

def    __UpperCamelCase	(	a    : List[Any]					,   a    : List[Any]     ) ->str:
 for key, value in source.items():
  if isinstance(a					,   a     ):
   snake_case				=			destination.setdefault(a					,   {}     )
   merge_dicts(a					,   a     )
  else:
   snake_case				=			value

 return destination






def    __UpperCamelCase	(	a    : int = None     ) ->bool:
 if port is None:
  snake_case				=			2_9500
 with socket.socket(socket.AF_INET					,   socket.SOCK_STREAM     ) as s:
  return s.connect_ex(('''localhost''', port)     ) == 0








'''simple docstring'''







import argparse

import torch
from torch import nn

from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration

def    __UpperCamelCase	(	a    : Optional[int]     ) ->Dict:
 snake_case				=			[
     '''encoder.version''',
     '''decoder.version''',
     '''model.encoder.version''',
     '''model.decoder.version''',
     '''decoder.output_projection.weight''',
     '''_float_tensor''',
     '''encoder.embed_positions._float_tensor''',
     '''decoder.embed_positions._float_tensor''',
 ]
 for k in ignore_keys:
  state_dict.pop(a					,   a     )

def    __UpperCamelCase	(	a    : Optional[Any]     ) ->int:
 snake_case				=			list(s_dict.keys()     )
 for key in keys:
  if "transformer_layers" in key:
   snake_case				=			s_dict.pop(a     )
  elif "subsample" in key:
   snake_case				=			s_dict.pop(a     )

def    __UpperCamelCase	(	a    : Optional[int]     ) ->Optional[int]:
 snake_case				,		snake_case				=			emb.weight.shape
 snake_case				=			nn.Linear(a					,   a					,   bias=a     )
 snake_case				=			emb.weight.data
 return lin_layer

def    __UpperCamelCase	(	a    : Any					,   a    : Tuple     ) ->Tuple:
 snake_case				=			torch.load(a					,   map_location='''cpu'''     )
 snake_case				=			mam_aaa['''args''']
 snake_case				=			mam_aaa['''model''']
 snake_case				=			state_dict['''decoder.output_projection.weight''']

 remove_ignore_keys_(a     )
 rename_keys(a     )

 snake_case				=			state_dict['''decoder.embed_tokens.weight'''].shape[0]

 snake_case				=			args.share_decoder_input_output_embed

 snake_case				=			[int(a     ) for i in args.conv_kernel_sizes.split(''','''     )]
 snake_case				=			SpeechaTextConfig(
     vocab_size=a					,   max_source_positions=args.max_source_positions					,   max_target_positions=args.max_target_positions					,   encoder_layers=args.encoder_layers					,   decoder_layers=args.decoder_layers					,   encoder_attention_heads=args.encoder_attention_heads					,   decoder_attention_heads=args.decoder_attention_heads					,   encoder_ffn_dim=args.encoder_ffn_embed_dim					,   decoder_ffn_dim=args.decoder_ffn_embed_dim					,   d_model=args.encoder_embed_dim					,   dropout=args.dropout					,   attention_dropout=args.attention_dropout					,   activation_dropout=args.activation_dropout					,   activation_function='''relu'''					,   num_conv_layers=len(a     )					,   conv_channels=args.conv_channels					,   conv_kernel_sizes=a					,   input_feat_per_channel=args.input_feat_per_channel					,   input_channels=args.input_channels					,   tie_word_embeddings=a					,   num_beams=5					,   max_length=200					,   use_cache=a					,   decoder_start_token_id=2					,   early_stopping=a					,   )

 snake_case				=			SpeechaTextForConditionalGeneration(a     )
 snake_case				,		snake_case				=			model.model.load_state_dict(a					,   strict=a     )
 if len(a     ) > 0 and not set(a     ) <= {
     "encoder.embed_positions.weights",
     "decoder.embed_positions.weights",
 }:
  raise ValueError(
      '''Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights`  are allowed to be missing,'''
      f""" but all the following weights are missing {missing}"""     )

 if tie_embeds:
  snake_case				=			make_linear_from_emb(model.model.decoder.embed_tokens     )
 else:
  snake_case				=			lm_head_weights

 model.save_pretrained(a     )


if __name__ == "__main__":
   _lowercase       						=			argparse.ArgumentParser()
   # Required parameters
   parser.add_argument('--fairseq_path', type=str, help='Path to the fairseq model (.pt) file.')
   parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
   _lowercase       						=			parser.parse_args()
   convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)

