




"""simple docstring"""

from __future__ import annotations






def 		_snake_case	(   _snake_case				:   int = 4				)       ->   list[list[int]]:






      '''simple docstring'''


      _A     =  abs(_snake_case				) or 4
      return [[1 + x + y * row_size for x in range(_snake_case				)] for y in range(_snake_case				)]






def 		_snake_case	(   _snake_case				:   list[list[int]]				)       ->   list[list[int]]:






      '''simple docstring'''


      return reverse_row(transpose(_snake_case				)				)
      # OR.. transpose(reverse_column(matrix))






def 		_snake_case	(   _snake_case				:   list[list[int]]				)       ->   list[list[int]]:






      '''simple docstring'''


      return reverse_row(reverse_column(_snake_case				)				)
      # OR.. reverse_column(reverse_row(matrix))






def 		_snake_case	(   _snake_case				:   list[list[int]]				)       ->   list[list[int]]:






      '''simple docstring'''


      return reverse_column(transpose(_snake_case				)				)
      # OR.. transpose(reverse_row(matrix))






def 		_snake_case	(   _snake_case				:   list[list[int]]				)       ->   list[list[int]]:






      '''simple docstring'''


      _A     =  [list(_snake_case				) for x in zip(*_snake_case				)]
      return matrix






def 		_snake_case	(   _snake_case				:   list[list[int]]				)       ->   list[list[int]]:






      '''simple docstring'''


      _A     =  matrix[::-1]
      return matrix






def 		_snake_case	(   _snake_case				:   list[list[int]]				)       ->   list[list[int]]:






      '''simple docstring'''


      _A     =  [x[::-1] for x in matrix]
      return matrix






def 		_snake_case	(   _snake_case				:   list[list[int]]				)       ->   None:






      '''simple docstring'''


      for i in matrix:
            print(*_snake_case				)


if __name__ == "__main__":
       a							      =     make_matrix()
       print('''\norigin:\n''')
       print_matrix(matrix)
       print('''\nrotate 90 counterclockwise:\n''')
       print_matrix(rotate_aa(matrix))

       a							      =     make_matrix()
       print('''\norigin:\n''')
       print_matrix(matrix)
       print('''\nrotate 180:\n''')
       print_matrix(rotate_aaa(matrix))

       a							      =     make_matrix()
       print('''\norigin:\n''')
       print_matrix(matrix)
       print('''\nrotate 270 counterclockwise:\n''')
       print_matrix(rotate_aaa(matrix))







"""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							      =     logging.get_logger(__name__)

a							      =     [
    ('''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							      =     [
    '''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 		_snake_case	(   _snake_case				:   Optional[Any]				)       ->   str:






      '''simple docstring'''


      _A     =  torch.load(_snake_case							,    map_location='cpu'				)
      return sd






def 		_snake_case	(   _snake_case				:   Union[str, Any]							,    _snake_case				:   str							,    _snake_case				:   Tuple=rename_keys_prefix				)       ->   List[str]:






      '''simple docstring'''


      _A     =  OrderedDict()
      _A     =  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
            _A     =  key
            for name_pair in rename_keys_prefix:
                  _A     =  new_key.replace(name_pair[0]							,    name_pair[1]				)
            _A     =  d[key]
            if key == "bert.cls.predictions.decoder.weight":
                  # Old bert code didn't have `decoder.bias`, but was added separately
                  _A     =  new_d['cls.predictions.bias']
      return new_d






@torch.no_grad()
def 		_snake_case	(   _snake_case				:   List[str]							,    _snake_case				:   Dict				)       ->   Dict:






      '''simple docstring'''


      assert (
          checkpoint_path.split('/'				)[-1] in ACCEPTABLE_CHECKPOINTS
      ), F'''The checkpoint provided must be in {ACCEPTABLE_CHECKPOINTS}.'''

      # Get Config
      if "pre" in checkpoint_path:
            _A     =  'pretraining'
            if "vcr" in checkpoint_path:
                  _A     =  {'visual_embedding_dim': 5_12}
            elif "vqa_advanced" in checkpoint_path:
                  _A     =  {'visual_embedding_dim': 20_48}
            elif "vqa" in checkpoint_path:
                  _A     =  {'visual_embedding_dim': 20_48}
            elif "nlvr" in checkpoint_path:
                  _A     =  {'visual_embedding_dim': 10_24}
            else:
                  raise NotImplementedError(F'''No implementation found for `{checkpoint_path}`.'''				)
      else:
            if "vcr" in checkpoint_path:
                  _A     =  {'visual_embedding_dim': 5_12}
                  _A     =  'multichoice'
            elif "vqa_advanced" in checkpoint_path:
                  _A     =  {'visual_embedding_dim': 20_48}
                  _A     =  'vqa_advanced'
            elif "vqa" in checkpoint_path:
                  _A     =  {'visual_embedding_dim': 20_48, 'num_labels': 31_29}
                  _A     =  'vqa'
            elif "nlvr" in checkpoint_path:
                  _A     =  {
                      'visual_embedding_dim': 10_24,
                      'num_labels': 2,
                  }
                  _A     =  'nlvr'

      _A     =  VisualBertConfig(**_snake_case				)

      # Load State Dict
      _A     =  load_state_dict(_snake_case				)

      _A     =  get_new_dict(_snake_case							,    _snake_case				)

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

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


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

