





'''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)








'''simple docstring'''







import argparse
from collections import OrderedDict
from pathlib import Path

import torch

from transformers import (
    VisualBertConfig,
    VisualBertForMultipleChoice,
    VisualBertForPreTraining,
    VisualBertForQuestionAnswering,
    VisualBertForVisualReasoning,
)
from transformers.utils import logging


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

_lowercase       						=			[
    ('bert.bert', 'visual_bert'),
    ('bert.cls', 'cls'),
    ('bert.classifier', 'cls'),
    ('token_type_embeddings_visual', 'visual_token_type_embeddings'),
    ('position_embeddings_visual', 'visual_position_embeddings'),
    ('projection', 'visual_projection'),
]

_lowercase       						=			[
    'nlvr2_coco_pre_trained.th',
    'nlvr2_fine_tuned.th',
    'nlvr2_pre_trained.th',
    'vcr_coco_pre_train.th',
    'vcr_fine_tune.th',
    'vcr_pre_train.th',
    'vqa_coco_pre_trained.th',
    'vqa_fine_tuned.th',
    'vqa_pre_trained.th',
]

def    __UpperCamelCase	(	a    : List[str]     ) ->Optional[int]:
 snake_case				=			torch.load(a					,   map_location='''cpu'''     )
 return sd

def    __UpperCamelCase	(	a    : Optional[int]					,   a    : Union[str, Any]					,   a    : int=rename_keys_prefix     ) ->Tuple:
 snake_case				=			OrderedDict()
 snake_case				=			torch.arange(config.max_position_embeddings     ).expand((1, -1)     )
 # detector_d = OrderedDict()
 for key in d:
  if "detector" in key:
   # detector_d[key.replace('detector.','')] = d[key]
   continue
  snake_case				=			key
  for name_pair in rename_keys_prefix:
   snake_case				=			new_key.replace(name_pair[0]					,   name_pair[1]     )
  snake_case				=			d[key]
  if key == "bert.cls.predictions.decoder.weight":
   # Old bert code didn't have `decoder.bias`, but was added separately
   snake_case				=			new_d['''cls.predictions.bias''']
 return new_d

@torch.no_grad()
def    __UpperCamelCase	(	a    : Optional[int]					,   a    : int     ) ->Union[str, Any]:
 assert (
     checkpoint_path.split('''/'''     )[-1] in ACCEPTABLE_CHECKPOINTS
 ), f"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}."""

 # Get Config
 if "pre" in checkpoint_path:
  snake_case				=			'''pretraining'''
  if "vcr" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 512}
  elif "vqa_advanced" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 2048}
  elif "vqa" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 2048}
  elif "nlvr" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 1024}
  else:
   raise NotImplementedError(f"""No implementation found for `{checkpoint_path}`."""     )
 else:
  if "vcr" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 512}
   snake_case				=			'''multichoice'''
  elif "vqa_advanced" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 2048}
   snake_case				=			'''vqa_advanced'''
  elif "vqa" in checkpoint_path:
   snake_case				=			{'''visual_embedding_dim''': 2048, '''num_labels''': 3129}
   snake_case				=			'''vqa'''
  elif "nlvr" in checkpoint_path:
   snake_case				=			{
       '''visual_embedding_dim''': 1024,
       '''num_labels''': 2,
   }
   snake_case				=			'''nlvr'''

 snake_case				=			VisualBertConfig(**a     )

 # Load State Dict
 snake_case				=			load_state_dict(a     )

 snake_case				=			get_new_dict(a					,   a     )

 if model_type == "pretraining":
  snake_case				=			VisualBertForPreTraining(a     )
 elif model_type == "vqa":
  snake_case				=			VisualBertForQuestionAnswering(a     )
 elif model_type == "nlvr":
  snake_case				=			VisualBertForVisualReasoning(a     )
 elif model_type == "multichoice":
  snake_case				=			VisualBertForMultipleChoice(a     )

 model.load_state_dict(a     )
 # Save Checkpoints
 Path(a     ).mkdir(exist_ok=a     )
 model.save_pretrained(a     )


if __name__ == "__main__":
   _lowercase       						=			argparse.ArgumentParser()
   # Required parameters
   parser.add_argument('orig_checkpoint_path', type=str, help='A path to .th on local filesystem.')
   parser.add_argument('pytorch_dump_folder_path', type=str, help='Path to the output PyTorch model.')
   _lowercase       						=			parser.parse_args()
   convert_visual_bert_checkpoint(args.orig_checkpoint_path, args.pytorch_dump_folder_path)

