





"""simple docstring"""





import argparse

import torch
from torch import nn

from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration


def       lowerCamelCase_       (   UpperCAmelCase_  )						->List[str]:





  """simple docstring"""


  __UpperCAmelCase    :						Optional[Any]       =						[
      '''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(UpperCAmelCase_  ,			UpperCAmelCase_  )


def       lowerCamelCase_       (   UpperCAmelCase_  )						->Optional[int]:





  """simple docstring"""


  __UpperCAmelCase    :						Tuple       =						list(s_dict.keys()  )
  for key in keys:
    if "transformer_layers" in key:
      __UpperCAmelCase    :						Tuple       =						s_dict.pop(UpperCAmelCase_  )
    elif "subsample" in key:
      __UpperCAmelCase    :						List[str]       =						s_dict.pop(UpperCAmelCase_  )


def       lowerCamelCase_       (   UpperCAmelCase_  )						->Tuple:





  """simple docstring"""


  __UpperCAmelCase    :						List[Any]       =						emb.weight.shape
  __UpperCAmelCase    :						Dict       =						nn.Linear(UpperCAmelCase_  ,			UpperCAmelCase_  ,			bias=UpperCAmelCase_  )
  __UpperCAmelCase    :						List[str]       =						emb.weight.data
  return lin_layer


def       lowerCamelCase_       (   UpperCAmelCase_  ,			UpperCAmelCase_  )						->Tuple:





  """simple docstring"""


  __UpperCAmelCase    :						List[str]       =						torch.load(UpperCAmelCase_  ,			map_location='''cpu'''  )
  __UpperCAmelCase    :						Dict       =						mam_aaa['''args''']
  __UpperCAmelCase    :						Optional[int]       =						mam_aaa['''model''']
  __UpperCAmelCase    :						Any       =						state_dict['''decoder.output_projection.weight''']

  remove_ignore_keys_(UpperCAmelCase_  )
  rename_keys(UpperCAmelCase_  )

  __UpperCAmelCase    :						Optional[Any]       =						state_dict['''decoder.embed_tokens.weight'''].shape[0]

  __UpperCAmelCase    :						str       =						args.share_decoder_input_output_embed

  __UpperCAmelCase    :						Tuple       =						[int(UpperCAmelCase_  ) for i in args.conv_kernel_sizes.split(''','''  )]
  __UpperCAmelCase    :						Any       =						SpeechaTextConfig(
      vocab_size=UpperCAmelCase_  ,			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(UpperCAmelCase_  )  ,			conv_channels=args.conv_channels  ,			conv_kernel_sizes=UpperCAmelCase_  ,			input_feat_per_channel=args.input_feat_per_channel  ,			input_channels=args.input_channels  ,			tie_word_embeddings=UpperCAmelCase_  ,			num_beams=5  ,			max_length=2_00  ,			use_cache=UpperCAmelCase_  ,			decoder_start_token_id=2  ,			early_stopping=UpperCAmelCase_  ,			)

  __UpperCAmelCase    :						Any       =						SpeechaTextForConditionalGeneration(UpperCAmelCase_  )
  __UpperCAmelCase    :						Any       =						model.model.load_state_dict(UpperCAmelCase_  ,			strict=UpperCAmelCase_  )
  if len(UpperCAmelCase_  ) > 0 and not set(UpperCAmelCase_  ) <= {
      "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:
    __UpperCAmelCase    :						Union[str, Any]       =						make_linear_from_emb(model.model.decoder.embed_tokens  )
  else:
    __UpperCAmelCase    :						int       =						lm_head_weights

  model.save_pretrained(UpperCAmelCase_  )


if __name__ == "__main__":
  lowercase__				:Optional[Any]				   =       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__				:Dict				   =       parser.parse_args()
  convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)






"""simple docstring"""





import argparse

import torch

from transformers import LxmertConfig, LxmertForPreTraining, load_tf_weights_in_lxmert
from transformers.utils import logging


logging.set_verbosity_info()


def       lowerCamelCase_       (   UpperCAmelCase_  ,			UpperCAmelCase_  ,			UpperCAmelCase_  )						->Optional[int]:





  """simple docstring"""


  __UpperCAmelCase    :						Dict       =						LxmertConfig.from_json_file(UpperCAmelCase_  )
  print(f'''Building PyTorch model from configuration: {config}'''  )
  __UpperCAmelCase    :						Tuple       =						LxmertForPreTraining(UpperCAmelCase_  )

  # Load weights from tf checkpoint
  load_tf_weights_in_lxmert(UpperCAmelCase_  ,			UpperCAmelCase_  ,			UpperCAmelCase_  )

  # Save pytorch-model
  print(f'''Save PyTorch model to {pytorch_dump_path}'''  )
  torch.save(model.state_dict()  ,			UpperCAmelCase_  )


if __name__ == "__main__":
  lowercase__				:Optional[int]				   =       argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
  )
  parser.add_argument(
      '--config_file',
      default=None,
      type=str,
      required=True,
      help='The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.',
  )
  parser.add_argument(
      '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
  )
  lowercase__				:Union[str, Any]				   =       parser.parse_args()
  convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)