





import argparse

import torch
from torch import nn

from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration



def 		A_    (					_lowerCAmelCase					)						->			Union[str, Any]:
 UpperCamelCase     : List[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(_lowerCAmelCase							,       _lowerCAmelCase					)



def 		A_    (					_lowerCAmelCase					)						->			Union[str, Any]:
 UpperCamelCase     : int	     =       list(s_dict.keys()					)
 for key in keys:
  if "transformer_layers" in key:
   UpperCamelCase     : Optional[Any]	     =       s_dict.pop(_lowerCAmelCase					)
  elif "subsample" in key:
   UpperCamelCase     : Dict	     =       s_dict.pop(_lowerCAmelCase					)



def 		A_    (					_lowerCAmelCase					)						->			Optional[int]:
 UpperCamelCase     ,   UpperCamelCase     : Any	     =       emb.weight.shape
 UpperCamelCase     : Union[str, Any]	     =       nn.Linear(_lowerCAmelCase							,       _lowerCAmelCase							,       bias=_lowerCAmelCase					)
 UpperCamelCase     : str	     =       emb.weight.data
 return lin_layer



def 		A_    (					_lowerCAmelCase							,       _lowerCAmelCase					)						->			Union[str, Any]:
 UpperCamelCase     : str	     =       torch.load(_lowerCAmelCase							,       map_location="cpu"					)
 UpperCamelCase     : Optional[Any]	     =       mam_aaa["args"]
 UpperCamelCase     : str	     =       mam_aaa["model"]
 UpperCamelCase     : List[Any]	     =       state_dict["decoder.output_projection.weight"]

 remove_ignore_keys_(_lowerCAmelCase					)
 rename_keys(_lowerCAmelCase					)

 UpperCamelCase     : Optional[int]	     =       state_dict["decoder.embed_tokens.weight"].shape[0]

 UpperCamelCase     : str	     =       args.share_decoder_input_output_embed

 UpperCamelCase     : int	     =       [int(_lowerCAmelCase					) for i in args.conv_kernel_sizes.split(","					)]
 UpperCamelCase     : List[str]	     =       SpeechaTextConfig(
     vocab_size=_lowerCAmelCase							,       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(_lowerCAmelCase					)							,       conv_channels=args.conv_channels							,       conv_kernel_sizes=_lowerCAmelCase							,       input_feat_per_channel=args.input_feat_per_channel							,       input_channels=args.input_channels							,       tie_word_embeddings=_lowerCAmelCase							,       num_beams=5							,       max_length=200							,       use_cache=_lowerCAmelCase							,       decoder_start_token_id=2							,       early_stopping=_lowerCAmelCase							,       )

 UpperCamelCase     : Tuple	     =       SpeechaTextForConditionalGeneration(_lowerCAmelCase					)
 UpperCamelCase     ,   UpperCamelCase     : List[str]	     =       model.model.load_state_dict(_lowerCAmelCase							,       strict=_lowerCAmelCase					)
 if len(_lowerCAmelCase					) > 0 and not set(_lowerCAmelCase					) <= {
     "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     : Tuple	     =       lm_head_weights

 model.save_pretrained(_lowerCAmelCase					)


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







__lowerCamelCase	:					Tuple			  =					"""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"""



def 		A_    (					_lowerCAmelCase					)						->			bytes:
 # Make sure the supplied data is a bytes-like object
 if not isinstance(_lowerCAmelCase							,       _lowerCAmelCase					):
  UpperCamelCase     : str	     =       F"""a bytes-like object is required, not '{data.__class__.__name__}'"""
  raise TypeError(_lowerCAmelCase					)

 UpperCamelCase     : Optional[int]	     =       "".join(bin(_lowerCAmelCase					)[2:].zfill(8					) for byte in data					)

 UpperCamelCase     : str	     =       len(_lowerCAmelCase					) % 6 != 0

 if padding_needed:
  # The padding that will be added later
  UpperCamelCase     : Optional[Any]	     =       b"=" * ((6 - len(_lowerCAmelCase					) % 6) // 2)

  # Append binary_stream with arbitrary binary digits (0's by default) to make its
  # length a multiple of 6.
  binary_stream += "0" * (6 - len(_lowerCAmelCase					) % 6)
 else:
  UpperCamelCase     : List[Any]	     =       b""

 # Encode every 6 binary digits to their corresponding Base64 character
 return (
     "".join(
         B64_CHARSET[int(binary_stream[index : index + 6]							,       2					)]
         for index in range(0							,       len(_lowerCAmelCase					)							,       6					)					).encode()
     + padding
 )



def 		A_    (					_lowerCAmelCase					)						->			bytes:
 # Make sure encoded_data is either a string or a bytes-like object
 if not isinstance(_lowerCAmelCase							,       _lowerCAmelCase					) and not isinstance(_lowerCAmelCase							,       _lowerCAmelCase					):
  UpperCamelCase     : List[Any]	     =       (
      "argument should be a bytes-like object or ASCII string, "
      F"""not '{encoded_data.__class__.__name__}'"""
  )
  raise TypeError(_lowerCAmelCase					)

 # In case encoded_data is a bytes-like object, make sure it contains only
 # ASCII characters so we convert it to a string object
 if isinstance(_lowerCAmelCase							,       _lowerCAmelCase					):
  try:
   UpperCamelCase     : Any	     =       encoded_data.decode("utf-8"					)
  except UnicodeDecodeError:
   raise ValueError("base64 encoded data should only contain ASCII characters"					)

 UpperCamelCase     : Union[str, Any]	     =       encoded_data.count("="					)

 # Check if the encoded string contains non base64 characters
 if padding:
  assert all(
      char in B64_CHARSET for char in encoded_data[:-padding]					), "Invalid base64 character(s) found."
 else:
  assert all(
      char in B64_CHARSET for char in encoded_data					), "Invalid base64 character(s) found."

 # Check the padding
 assert len(_lowerCAmelCase					) % 4 == 0 and padding < 3, "Incorrect padding"

 if padding:
  # Remove padding if there is one
  UpperCamelCase     : List[str]	     =       encoded_data[:-padding]

  UpperCamelCase     : List[Any]	     =       "".join(
      bin(B64_CHARSET.index(_lowerCAmelCase					)					)[2:].zfill(6					) for char in encoded_data					)[: -padding * 2]
 else:
  UpperCamelCase     : List[Any]	     =       "".join(
      bin(B64_CHARSET.index(_lowerCAmelCase					)					)[2:].zfill(6					) for char in encoded_data					)

 UpperCamelCase     : Any	     =       [
     int(binary_stream[index : index + 8]							,       2					)
     for index in range(0							,       len(_lowerCAmelCase					)							,       8					)
 ]

 return bytes(_lowerCAmelCase					)


if __name__ == "__main__":
    import doctest

    doctest.testmod()
