





'''simple docstring'''







import numpy as np
from scipy.spatial.distance import cdist
from sklearn.metrics import fa_score

import datasets


_lowercase       						=			"""\
    @inproceedings{kakwani2020indicnlpsuite,
    title={{IndicNLPSuite: Monolingual Corpora, Evaluation Benchmarks and Pre-trained Multilingual Language Models for Indian Languages}},
    author={Divyanshu Kakwani and Anoop Kunchukuttan and Satish Golla and Gokul N.C. and Avik Bhattacharyya and Mitesh M. Khapra and Pratyush Kumar},
    year={2020},
    booktitle={Findings of EMNLP},
}
"""

_lowercase       						=			"""\
    IndicGLUE is a natural language understanding benchmark for Indian languages. It contains a wide
    variety of tasks and covers 11 major Indian languages - as, bn, gu, hi, kn, ml, mr, or, pa, ta, te.
"""

_lowercase       						=			"""
Compute IndicGLUE evaluation metric associated to each IndicGLUE dataset.
Args:
    predictions: list of predictions to score (as int64),
        except for 'cvit-mkb-clsr' where each prediction is a vector (of float32).
    references: list of ground truth labels corresponding to the predictions (as int64),
        except for 'cvit-mkb-clsr' where each reference is a vector (of float32).
Returns: depending on the IndicGLUE subset, one or several of:
    \"accuracy\": Accuracy
    \"f1\": F1 score
    \"precision\": Precision@10
Examples:

    >>> indic_glue_metric = datasets.load_metric('indic_glue', 'wnli')  # 'wnli' or any of [\"copa\", \"sna\", \"csqa\", \"wstp\", \"inltkh\", \"bbca\", \"iitp-mr\", \"iitp-pr\", \"actsa-sc\", \"md\"]
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0}

    >>> indic_glue_metric = datasets.load_metric('indic_glue', 'wiki-ner')
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'accuracy': 1.0, 'f1': 1.0}

    >>> indic_glue_metric = datasets.load_metric('indic_glue', 'cvit-mkb-clsr')
    >>> references = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]
    >>> predictions = [[0.5, 0.5, 0.5], [0.1, 0.2, 0.3]]
    >>> results = indic_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'precision@10': 1.0}

"""



def    __UpperCamelCase	(	a    : int					,   a    : Optional[int]     ) ->int:
 return float((preds == labels).mean()     )



def    __UpperCamelCase	(	a    : str					,   a    : Union[str, Any]     ) ->str:
 snake_case				=			simple_accuracy(UpperCamelCase__					,   UpperCamelCase__     )
 snake_case				=			float(fa_score(y_true=UpperCamelCase__					,   y_pred=UpperCamelCase__     )     )
 return {
     "accuracy": acc,
     "f1": fa,
 }



def    __UpperCamelCase	(	a    : Any					,   a    : Dict     ) ->Dict:
 snake_case				=			np.array(UpperCamelCase__     )
 snake_case				=			np.array(UpperCamelCase__     )
 snake_case				=			en_sentvecs.shape[0]

 # mean centering
 snake_case				=			en_sentvecs - np.mean(UpperCamelCase__					,   axis=0     )
 snake_case				=			in_sentvecs - np.mean(UpperCamelCase__					,   axis=0     )

 snake_case				=			cdist(UpperCamelCase__					,   UpperCamelCase__					,   '''cosine'''     )
 snake_case				=			np.array(range(UpperCamelCase__     )     )
 snake_case				=			sim.argsort(axis=1     )[:, :10]
 snake_case				=			np.any(preds == actual[:, None]					,   axis=1     )
 return float(matches.mean()     )






@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION				,		_KWARGS_DESCRIPTION					)
class 			_lowercase						(       datasets.Metric					):







     def     UpperCamelCase			( self    ) ->	Optional[int]:
      if self.config_name not in [
          "wnli",
          "copa",
          "sna",
          "csqa",
          "wstp",
          "inltkh",
          "bbca",
          "cvit-mkb-clsr",
          "iitp-mr",
          "iitp-pr",
          "actsa-sc",
          "md",
          "wiki-ner",
      ]:
       raise KeyError(
           '''You should supply a configuration name selected in '''
           '''["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", '''
           '''"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", '''
           '''"wiki-ner"]'''    )
      return datasets.MetricInfo(
          description=_DESCRIPTION    ,   citation=_CITATION    ,   inputs_description=_KWARGS_DESCRIPTION    ,   features=datasets.Features(
              {
                  '''predictions''': datasets.Value('''int64'''    )
                  if self.config_name != '''cvit-mkb-clsr'''
                  else datasets.Sequence(datasets.Value('''float32'''    )    ),
                  '''references''': datasets.Value('''int64'''    )
                  if self.config_name != '''cvit-mkb-clsr'''
                  else datasets.Sequence(datasets.Value('''float32'''    )    ),
              }    )    ,   codebase_urls=[]    ,   reference_urls=[]    ,   format='''numpy''' if self.config_name != '''cvit-mkb-clsr''' else None    ,   )







     def     UpperCamelCase			( self    ,   A__    ,   A__    ) ->	Tuple:
      if self.config_name == "cvit-mkb-clsr":
       return {"precision@10": precision_at_aa(A__    ,   A__    )}
      elif self.config_name in ["wiki-ner"]:
       return acc_and_fa(A__    ,   A__    )
      elif self.config_name in [
          "wnli",
          "copa",
          "sna",
          "csqa",
          "wstp",
          "inltkh",
          "bbca",
          "iitp-mr",
          "iitp-pr",
          "actsa-sc",
          "md",
      ]:
       return {"accuracy": simple_accuracy(A__    ,   A__    )}
      else:
       raise KeyError(
           '''You should supply a configuration name selected in '''
           '''["wnli", "copa", "sna", "csqa", "wstp", "inltkh", "bbca", '''
           '''"cvit-mkb-clsr", "iitp-mr", "iitp-pr", "actsa-sc", "md", '''
           '''"wiki-ner"]'''    )








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

