'''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()
__a:					Any			  =  logging.get_logger(__name__)

__a:					List[Any]			  =  [
    ('''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'''),
]

__a:					List[str]			  =  [
    '''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			(  UpperCAmelCase	):
 lowercase__							:       List[Any]										=   torch.load(_UpperCAmelCase						,	map_location='''cpu'''	)
 return sd
def      __UpperCamelCase			(  UpperCAmelCase						,	UpperCAmelCase						,	UpperCAmelCase=rename_keys_prefix	):
 lowercase__							:       List[Any]										=   OrderedDict()
 lowercase__							:       Any										=   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
  lowercase__							:       Any										=   key
  for name_pair in rename_keys_prefix:
   lowercase__							:       List[str]										=   new_key.replace(name_pair[0]						,	name_pair[1]	)
  lowercase__							:       Tuple										=   d[key]
  if key == "bert.cls.predictions.decoder.weight":
   # Old bert code didn't have `decoder.bias`, but was added separately
   lowercase__							:       Union[str, Any]										=   new_d['''cls.predictions.bias''']
 return new_d
@torch.no_grad()
def      __UpperCamelCase			(  UpperCAmelCase						,	UpperCAmelCase	):
 assert (
     checkpoint_path.split('''/'''	)[-1] in ACCEPTABLE_CHECKPOINTS
 ), F"""The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}."""

 # Get Config
 if "pre" in checkpoint_path:
  lowercase__							:       Optional[int]										=   '''pretraining'''
  if "vcr" in checkpoint_path:
   lowercase__							:       Optional[int]										=   {'''visual_embedding_dim''': 512}
  elif "vqa_advanced" in checkpoint_path:
   lowercase__							:       List[str]										=   {'''visual_embedding_dim''': 2048}
  elif "vqa" in checkpoint_path:
   lowercase__							:       str										=   {'''visual_embedding_dim''': 2048}
  elif "nlvr" in checkpoint_path:
   lowercase__							:       Any										=   {'''visual_embedding_dim''': 1024}
  else:
   raise NotImplementedError(F"""No implementation found for `{checkpoint_path}`."""	)
 else:
  if "vcr" in checkpoint_path:
   lowercase__							:       List[str]										=   {'''visual_embedding_dim''': 512}
   lowercase__							:       Any										=   '''multichoice'''
  elif "vqa_advanced" in checkpoint_path:
   lowercase__							:       Optional[int]										=   {'''visual_embedding_dim''': 2048}
   lowercase__							:       Tuple										=   '''vqa_advanced'''
  elif "vqa" in checkpoint_path:
   lowercase__							:       Optional[int]										=   {'''visual_embedding_dim''': 2048, '''num_labels''': 3129}
   lowercase__							:       Union[str, Any]										=   '''vqa'''
  elif "nlvr" in checkpoint_path:
   lowercase__							:       Tuple										=   {
       '''visual_embedding_dim''': 1024,
       '''num_labels''': 2,
   }
   lowercase__							:       int										=   '''nlvr'''

 lowercase__							:       Dict										=   VisualBertConfig(**_UpperCAmelCase	)

 # Load State Dict
 lowercase__							:       str										=   load_state_dict(_UpperCAmelCase	)

 lowercase__							:       Dict										=   get_new_dict(_UpperCAmelCase						,	_UpperCAmelCase	)

 if model_type == "pretraining":
  lowercase__							:       List[str]										=   VisualBertForPreTraining(_UpperCAmelCase	)
 elif model_type == "vqa":
  lowercase__							:       int										=   VisualBertForQuestionAnswering(_UpperCAmelCase	)
 elif model_type == "nlvr":
  lowercase__							:       List[str]										=   VisualBertForVisualReasoning(_UpperCAmelCase	)
 elif model_type == "multichoice":
  lowercase__							:       int										=   VisualBertForMultipleChoice(_UpperCAmelCase	)

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


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








"""simple docstring"""
import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import numpy as np
import pytest

from datasets.arrow_dataset import Dataset
from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex

from .utils import require_elasticsearch, require_faiss


__UpperCamelCase     :						Optional[int]						       =  pytest.mark.integration







@require_faiss
class       a							(   a__  ):



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= Dataset.from_dict({'filename': ['my_name-train' + '_' + str(_snake_case     ) for x in np.arange(30     ).tolist()]}     )
  return dset



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  import faiss

  lowerCAmelCase    				= self._create_dummy_dataset()
  lowerCAmelCase    				= dset.map(
      lambda _snake_case				,       _snake_case     : {"vecs": i * np.ones(5				,       dtype=np.floataa     )}				,       with_indices=_snake_case				,       keep_in_memory=_snake_case     )
  lowerCAmelCase    				= dset.add_faiss_index('vecs'				,       batch_size=1_00				,       metric_type=faiss.METRIC_INNER_PRODUCT     )
  lowerCAmelCase				,lowerCAmelCase    				= dset.get_nearest_examples('vecs'				,       np.ones(5				,       dtype=np.floataa     )     )
  self.assertEqual(examples['filename'][0]				,       'my_name-train_29'     )
  dset.drop_index('vecs'     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  import faiss

  lowerCAmelCase    				= self._create_dummy_dataset()
  dset.add_faiss_index_from_external_arrays(
      external_arrays=np.ones((30, 5)     ) * np.arange(30     ).reshape(-1				,       1     )				,       index_name='vecs'				,       batch_size=1_00				,       metric_type=faiss.METRIC_INNER_PRODUCT				,       )
  lowerCAmelCase				,lowerCAmelCase    				= dset.get_nearest_examples('vecs'				,       np.ones(5				,       dtype=np.floataa     )     )
  self.assertEqual(examples['filename'][0]				,       'my_name-train_29'     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  import faiss

  lowerCAmelCase    				= self._create_dummy_dataset()
  dset.add_faiss_index_from_external_arrays(
      external_arrays=np.ones((30, 5)     ) * np.arange(30     ).reshape(-1				,       1     )				,       index_name='vecs'				,       metric_type=faiss.METRIC_INNER_PRODUCT				,       )

  # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
  # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
  # see https://bugs.python.org/issue14243 and
  # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
  with tempfile.NamedTemporaryFile(delete=_snake_case     ) as tmp_file:
   dset.save_faiss_index('vecs'				,       tmp_file.name     )
   dset.load_faiss_index('vecs2'				,       tmp_file.name     )
  os.unlink(tmp_file.name     )

  lowerCAmelCase				,lowerCAmelCase    				= dset.get_nearest_examples('vecs2'				,       np.ones(5				,       dtype=np.floataa     )     )
  self.assertEqual(examples['filename'][0]				,       'my_name-train_29'     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= self._create_dummy_dataset()
  dset.add_faiss_index_from_external_arrays(
      external_arrays=np.ones((30, 5)     ) * np.arange(30     ).reshape(-1				,       1     )				,       index_name='vecs'     )
  dset.drop_index('vecs'     )
  self.assertRaises(_snake_case				,       partial(dset.get_nearest_examples				,       'vecs2'				,       np.ones(5				,       dtype=np.floataa     )     )     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  from elasticsearch import Elasticsearch

  lowerCAmelCase    				= self._create_dummy_dataset()
  with patch('elasticsearch.Elasticsearch.search'     ) as mocked_search, patch(
      'elasticsearch.client.IndicesClient.create'     ) as mocked_index_create, patch('elasticsearch.helpers.streaming_bulk'     ) as mocked_bulk:
   lowerCAmelCase    				= {'acknowledged': True}
   mocked_bulk.return_value([(True, None)] * 30     )
   lowerCAmelCase    				= {'hits': {'hits': [{'_score': 1, '_id': 29}]}}
   lowerCAmelCase    				= Elasticsearch()

   dset.add_elasticsearch_index('filename'				,       es_client=_snake_case     )
   lowerCAmelCase				,lowerCAmelCase    				= dset.get_nearest_examples('filename'				,       'my_name-train_29'     )
   self.assertEqual(examples['filename'][0]				,       'my_name-train_29'     )







@require_faiss
class       a							(   a__  ):



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  import faiss

  lowerCAmelCase    				= FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT     )

  # add vectors
  index.add_vectors(np.eye(5				,       dtype=np.floataa     )     )
  self.assertIsNotNone(index.faiss_index     )
  self.assertEqual(index.faiss_index.ntotal				,       5     )
  index.add_vectors(np.zeros((5, 5)				,       dtype=np.floataa     )     )
  self.assertEqual(index.faiss_index.ntotal				,       10     )

  # single query
  lowerCAmelCase    				= np.zeros(5				,       dtype=np.floataa     )
  lowerCAmelCase    				= 1
  lowerCAmelCase				,lowerCAmelCase    				= index.search(_snake_case     )
  self.assertRaises(_snake_case				,       index.search				,       query.reshape(-1				,       1     )     )
  self.assertGreater(scores[0]				,       0     )
  self.assertEqual(indices[0]				,       1     )

  # batched queries
  lowerCAmelCase    				= np.eye(5				,       dtype=np.floataa     )[::-1]
  lowerCAmelCase				,lowerCAmelCase    				= index.search_batch(_snake_case     )
  self.assertRaises(_snake_case				,       index.search_batch				,       queries[0]     )
  lowerCAmelCase    				= [scores[0] for scores in total_scores]
  lowerCAmelCase    				= [indices[0] for indices in total_indices]
  self.assertGreater(np.min(_snake_case     )				,       0     )
  self.assertListEqual([4, 3, 2, 1, 0]				,       _snake_case     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  import faiss

  lowerCAmelCase    				= FaissIndex(string_factory='Flat'     )
  index.add_vectors(np.eye(5				,       dtype=np.floataa     )     )
  self.assertIsInstance(index.faiss_index				,       faiss.IndexFlat     )
  lowerCAmelCase    				= FaissIndex(string_factory='LSH'     )
  index.add_vectors(np.eye(5				,       dtype=np.floataa     )     )
  self.assertIsInstance(index.faiss_index				,       faiss.IndexLSH     )
  with self.assertRaises(_snake_case     ):
   lowerCAmelCase    				= FaissIndex(string_factory='Flat'				,       custom_index=faiss.IndexFlat(5     )     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  import faiss

  lowerCAmelCase    				= faiss.IndexFlat(5     )
  lowerCAmelCase    				= FaissIndex(custom_index=_snake_case     )
  index.add_vectors(np.eye(5				,       dtype=np.floataa     )     )
  self.assertIsInstance(index.faiss_index				,       faiss.IndexFlat     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  import faiss

  lowerCAmelCase    				= FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT     )
  index.add_vectors(np.eye(5				,       dtype=np.floataa     )     )

  # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
  # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
  # see https://bugs.python.org/issue14243 and
  # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
  with tempfile.NamedTemporaryFile(delete=_snake_case     ) as tmp_file:
   index.save(tmp_file.name     )
   lowerCAmelCase    				= FaissIndex.load(tmp_file.name     )
  os.unlink(tmp_file.name     )

  lowerCAmelCase    				= np.zeros(5				,       dtype=np.floataa     )
  lowerCAmelCase    				= 1
  lowerCAmelCase				,lowerCAmelCase    				= index.search(_snake_case     )
  self.assertGreater(scores[0]				,       0     )
  self.assertEqual(indices[0]				,       1     )


@require_faiss
def 		_SCREAMING_SNAKE_CASE			(_UpperCAmelCase					:    Dict			):
 import faiss

 lowerCAmelCase    				= FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT			)
 index.add_vectors(np.eye(5  ,     dtype=np.floataa			)			)

 lowerCAmelCase    				= 'index.faiss'
 lowerCAmelCase    				= F'mock://{index_name}'
 index.save(_UpperCAmelCase  ,     storage_options=mockfs.storage_options			)
 lowerCAmelCase    				= FaissIndex.load(_UpperCAmelCase  ,     storage_options=mockfs.storage_options			)

 lowerCAmelCase    				= np.zeros(5  ,     dtype=np.floataa			)
 lowerCAmelCase    				= 1
 lowerCAmelCase				,lowerCAmelCase    				= index.search(_UpperCAmelCase			)
 assert scores[0] > 0
 assert indices[0] == 1







@require_elasticsearch
class       a							(   a__  ):



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  from elasticsearch import Elasticsearch

  with patch('elasticsearch.Elasticsearch.search'     ) as mocked_search, patch(
      'elasticsearch.client.IndicesClient.create'     ) as mocked_index_create, patch('elasticsearch.helpers.streaming_bulk'     ) as mocked_bulk:
   lowerCAmelCase    				= Elasticsearch()
   lowerCAmelCase    				= {'acknowledged': True}
   lowerCAmelCase    				= ElasticSearchIndex(es_client=_snake_case     )
   mocked_bulk.return_value([(True, None)] * 3     )
   index.add_documents(['foo', 'bar', 'foobar']     )

   # single query
   lowerCAmelCase    				= 'foo'
   lowerCAmelCase    				= {'hits': {'hits': [{'_score': 1, '_id': 0}]}}
   lowerCAmelCase				,lowerCAmelCase    				= index.search(_snake_case     )
   self.assertEqual(scores[0]				,       1     )
   self.assertEqual(indices[0]				,       0     )

   # single query with timeout
   lowerCAmelCase    				= 'foo'
   lowerCAmelCase    				= {'hits': {'hits': [{'_score': 1, '_id': 0}]}}
   lowerCAmelCase				,lowerCAmelCase    				= index.search(_snake_case				,       request_timeout=30     )
   self.assertEqual(scores[0]				,       1     )
   self.assertEqual(indices[0]				,       0     )

   # batched queries
   lowerCAmelCase    				= ['foo', 'bar', 'foobar']
   lowerCAmelCase    				= {'hits': {'hits': [{'_score': 1, '_id': 1}]}}
   lowerCAmelCase				,lowerCAmelCase    				= index.search_batch(_snake_case     )
   lowerCAmelCase    				= [scores[0] for scores in total_scores]
   lowerCAmelCase    				= [indices[0] for indices in total_indices]
   self.assertGreater(np.min(_snake_case     )				,       0     )
   self.assertListEqual([1, 1, 1]				,       _snake_case     )

   # batched queries with timeout
   lowerCAmelCase    				= ['foo', 'bar', 'foobar']
   lowerCAmelCase    				= {'hits': {'hits': [{'_score': 1, '_id': 1}]}}
   lowerCAmelCase				,lowerCAmelCase    				= index.search_batch(_snake_case				,       request_timeout=30     )
   lowerCAmelCase    				= [scores[0] for scores in total_scores]
   lowerCAmelCase    				= [indices[0] for indices in total_indices]
   self.assertGreater(np.min(_snake_case     )				,       0     )
   self.assertListEqual([1, 1, 1]				,       _snake_case     )





