






'''simple docstring'''







import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import MaskFormerSwinBackbone
  from transformers.models.maskformer import MaskFormerSwinModel




class   __SCREAMING_SNAKE_CASE       :


  """simple docstring"""


  def __init__(			self   ,	UpperCamelCase__   ,	UpperCamelCase__=13   ,	UpperCamelCase__=32   ,	UpperCamelCase__=2   ,	UpperCamelCase__=3   ,	UpperCamelCase__=16   ,	UpperCamelCase__=[1, 2, 1]   ,	UpperCamelCase__=[2, 2, 4]   ,	UpperCamelCase__=2   ,	UpperCamelCase__=2.0   ,	UpperCamelCase__=True   ,	UpperCamelCase__=0.0   ,	UpperCamelCase__=0.0   ,	UpperCamelCase__=0.1   ,	UpperCamelCase__="gelu"   ,	UpperCamelCase__=False   ,	UpperCamelCase__=True   ,	UpperCamelCase__=0.02   ,	UpperCamelCase__=1e-5   ,	UpperCamelCase__=True   ,	UpperCamelCase__=None   ,	UpperCamelCase__=True   ,	UpperCamelCase__=10   ,	UpperCamelCase__=8   ,	UpperCamelCase__=["stage1", "stage2", "stage3"]   ,	UpperCamelCase__=[1, 2, 3]   ,	):






         """simple docstring"""


         a_						     =			parent
         a_						     =			batch_size
         a_						     =			image_size
         a_						     =			patch_size
         a_						     =			num_channels
         a_						     =			embed_dim
         a_						     =			depths
         a_						     =			num_heads
         a_						     =			window_size
         a_						     =			mlp_ratio
         a_						     =			qkv_bias
         a_						     =			hidden_dropout_prob
         a_						     =			attention_probs_dropout_prob
         a_						     =			drop_path_rate
         a_						     =			hidden_act
         a_						     =			use_absolute_embeddings
         a_						     =			patch_norm
         a_						     =			layer_norm_eps
         a_						     =			initializer_range
         a_						     =			is_training
         a_						     =			scope
         a_						     =			use_labels
         a_						     =			type_sequence_label_size
         a_						     =			encoder_stride
         a_						     =			out_features
         a_						     =			out_indices







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

         a_						     =			None
         if self.use_labels:
                a_						     =			ids_tensor([self.batch_size]   ,	self.type_sequence_label_size	)

         a_						     =			self.get_config()

         return config, pixel_values, labels







  def    _a       (			self	):






         """simple docstring"""


         return MaskFormerSwinConfig(
             image_size=self.image_size   ,	patch_size=self.patch_size   ,	num_channels=self.num_channels   ,	embed_dim=self.embed_dim   ,	depths=self.depths   ,	num_heads=self.num_heads   ,	window_size=self.window_size   ,	mlp_ratio=self.mlp_ratio   ,	qkv_bias=self.qkv_bias   ,	hidden_dropout_prob=self.hidden_dropout_prob   ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,	drop_path_rate=self.drop_path_rate   ,	hidden_act=self.hidden_act   ,	use_absolute_embeddings=self.use_absolute_embeddings   ,	path_norm=self.patch_norm   ,	layer_norm_eps=self.layer_norm_eps   ,	initializer_range=self.initializer_range   ,	encoder_stride=self.encoder_stride   ,	out_features=self.out_features   ,	out_indices=self.out_indices   ,	)







  def    _a       (			self   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__	):






         """simple docstring"""


         a_						     =			MaskFormerSwinModel(config=UpperCamelCase__	)
         model.to(UpperCamelCase__	)
         model.eval()
         a_						     =			model(UpperCamelCase__	)

         a_						     =			((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths	) - 1))
         a_						     =			int(config.embed_dim * 2 ** (len(config.depths	) - 1)	)

         self.parent.assertEqual(result.last_hidden_state.shape   ,	(self.batch_size, expected_seq_len, expected_dim)	)







  def    _a       (			self   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__	):






         """simple docstring"""


         a_						     =			MaskFormerSwinBackbone(config=UpperCamelCase__	)
         model.to(UpperCamelCase__	)
         model.eval()
         a_						     =			model(UpperCamelCase__	)

         # verify feature maps
         self.parent.assertEqual(len(result.feature_maps	)   ,	len(config.out_features	)	)
         self.parent.assertListEqual(list(result.feature_maps[0].shape	)   ,	[13, 16, 16, 16]	)

         # verify channels
         self.parent.assertEqual(len(model.channels	)   ,	len(config.out_features	)	)
         self.parent.assertListEqual(model.channels   ,	[16, 32, 64]	)

         # verify ValueError
         with self.parent.assertRaises(UpperCamelCase__	):
                a_						     =			['stem']
                a_						     =			MaskFormerSwinBackbone(config=UpperCamelCase__	)







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			self.prepare_config_and_inputs()
         a_     ,	a_     ,	a_						     =			config_and_inputs
         a_						     =			{'pixel_values': pixel_values}
         return config, inputs_dict





@require_torch
class   __SCREAMING_SNAKE_CASE       (__A							,       __A							,       unittest.TestCase       ):


  """simple docstring"""


  _a  : Optional[Any]      			=     (
      (
          MaskFormerSwinModel,
          MaskFormerSwinBackbone,
      )
      if is_torch_available()
      else ()
  )
  _a  : List[str]      			=     {'''feature-extraction''': MaskFormerSwinModel} if is_torch_available() else {}
  _a  : List[str]      			=     False
  _a  : Tuple      			=     False
  _a  : Tuple      			=     False
  _a  : List[Any]      			=     False
  _a  : Optional[int]      			=     False







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			MaskFormerSwinModelTester(self	)
         a_						     =			ConfigTester(self   ,	config_class=UpperCamelCase__   ,	embed_dim=37	)







  @require_torch_multi_gpu
  @unittest.skip(
      reason=(
          '`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with'
          ' `nn.DataParallel`'
      )	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  def    _a       (			self	):






         """simple docstring"""


         self.create_and_test_config_common_properties()
         self.config_tester.create_and_test_config_to_json_string()
         self.config_tester.create_and_test_config_to_json_file()
         self.config_tester.create_and_test_config_from_and_save_pretrained()
         self.config_tester.create_and_test_config_with_num_labels()
         self.config_tester.check_config_can_be_init_without_params()
         self.config_tester.check_config_arguments_init()







  def    _a       (			self	):






         """simple docstring"""


         return







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase__	)







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_backbone(*UpperCamelCase__	)







  @unittest.skip('Swin does not use inputs_embeds'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  @unittest.skip('Swin does not support feedforward chunking'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                a_						     =			model_class(UpperCamelCase__	)
                self.assertIsInstance(model.get_input_embeddings()   ,	(nn.Module)	)
                a_						     =			model.get_output_embeddings()
                self.assertTrue(x is None or isinstance(UpperCamelCase__   ,	nn.Linear	)	)







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                a_						     =			model_class(UpperCamelCase__	)
                a_						     =			inspect.signature(model.forward	)
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                a_						     =			[*signature.parameters.keys()]

                a_						     =			['pixel_values']
                self.assertListEqual(arg_names[:1]   ,	UpperCamelCase__	)







  @unittest.skip(reason='MaskFormerSwin is only used as backbone and doesn\'t support output_attentions'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  @unittest.skip(reason='MaskFormerSwin is only used as an internal backbone'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  def    _a       (			self   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__	):






         """simple docstring"""


         a_						     =			model_class(UpperCamelCase__	)
         model.to(UpperCamelCase__	)
         model.eval()

         with torch.no_grad():
                a_						     =			model(**self._prepare_for_class(UpperCamelCase__   ,	UpperCamelCase__	)	)

         a_						     =			outputs.hidden_states

         a_						     =			getattr(
             self.model_tester   ,	'expected_num_hidden_layers'   ,	len(self.model_tester.depths	) + 1	)
         self.assertEqual(len(UpperCamelCase__	)   ,	UpperCamelCase__	)

         # Swin has a different seq_length
         a_						     =			(
             config.patch_size
             if isinstance(config.patch_size   ,	collections.abc.Iterable	)
             else (config.patch_size, config.patch_size)
         )

         a_						     =			(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

         self.assertListEqual(
             list(hidden_states[0].shape[-2:]	)   ,	[num_patches, self.model_tester.embed_dim]   ,	)







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs_for_common()

         a_						     =			(
             self.model_tester.image_size
             if isinstance(self.model_tester.image_size   ,	collections.abc.Iterable	)
             else (self.model_tester.image_size, self.model_tester.image_size)
         )

         for model_class in self.all_model_classes:
                a_						     =			True
                self.check_hidden_states_output(UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__	)

                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                a_						     =			True

                self.check_hidden_states_output(UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__	)







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs_for_common()
         a_						     =			3

         a_						     =			(
             self.model_tester.image_size
             if isinstance(self.model_tester.image_size   ,	collections.abc.Iterable	)
             else (self.model_tester.image_size, self.model_tester.image_size)
         )
         a_						     =			(
             config.patch_size
             if isinstance(config.patch_size   ,	collections.abc.Iterable	)
             else (config.patch_size, config.patch_size)
         )

         a_						     =			image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
         a_						     =			image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

         for model_class in self.all_model_classes:
                a_						     =			True
                self.check_hidden_states_output(UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	(padded_height, padded_width)	)

                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                a_						     =			True
                self.check_hidden_states_output(UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	(padded_height, padded_width)	)







  @unittest.skip(reason='MaskFormerSwin doesn\'t have pretrained checkpoints'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  @unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  @unittest.skip(reason='This will be fixed once MaskFormerSwin is replaced by native Swin'	)
  def    _a       (			self	):






         """simple docstring"""


         pass







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs_for_common()

         def set_nan_tensor_to_zero(UpperCamelCase__	):
                a_						     =			0
                return t

         def check_equivalence(UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__={}	):
                with torch.no_grad():
                       a_						     =			model(**UpperCamelCase__   ,	return_dict=UpperCamelCase__   ,	**UpperCamelCase__	)
                       a_						     =			model(**UpperCamelCase__   ,	return_dict=UpperCamelCase__   ,	**UpperCamelCase__	).to_tuple()

                       def recursive_check(UpperCamelCase__   ,	UpperCamelCase__	):
                              if isinstance(UpperCamelCase__   ,	(List, Tuple)	):
                                     for tuple_iterable_value, dict_iterable_value in zip(UpperCamelCase__   ,	UpperCamelCase__	):
                                            recursive_check(UpperCamelCase__   ,	UpperCamelCase__	)
                              elif isinstance(UpperCamelCase__   ,	UpperCamelCase__	):
                                     for tuple_iterable_value, dict_iterable_value in zip(
                                         tuple_object.values()   ,	dict_object.values()	):
                                            recursive_check(UpperCamelCase__   ,	UpperCamelCase__	)
                              elif tuple_object is None:
                                     return
                              else:
                                     self.assertTrue(
                                         torch.allclose(
                                             set_nan_tensor_to_zero(UpperCamelCase__	)   ,	set_nan_tensor_to_zero(UpperCamelCase__	)   ,	atol=1e-5	)   ,	msg=(
                                             'Tuple and dict output are not equal. Difference:'
                                             f' {torch.max(torch.abs(tuple_object - dict_object	)	)}. Tuple has `nan`:'
                                             f' {torch.isnan(UpperCamelCase__	).any()} and `inf`: {torch.isinf(UpperCamelCase__	)}. Dict has'
                                             f' `nan`: {torch.isnan(UpperCamelCase__	).any()} and `inf`: {torch.isinf(UpperCamelCase__	)}.'
                                         )   ,	)

                       recursive_check(UpperCamelCase__   ,	UpperCamelCase__	)

         for model_class in self.all_model_classes:
                a_						     =			model_class(UpperCamelCase__	)
                model.to(UpperCamelCase__	)
                model.eval()

                a_						     =			self._prepare_for_class(UpperCamelCase__   ,	UpperCamelCase__	)
                a_						     =			self._prepare_for_class(UpperCamelCase__   ,	UpperCamelCase__	)
                check_equivalence(UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__	)

                a_						     =			self._prepare_for_class(UpperCamelCase__   ,	UpperCamelCase__   ,	return_labels=UpperCamelCase__	)
                a_						     =			self._prepare_for_class(UpperCamelCase__   ,	UpperCamelCase__   ,	return_labels=UpperCamelCase__	)
                check_equivalence(UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__	)

                a_						     =			self._prepare_for_class(UpperCamelCase__   ,	UpperCamelCase__	)
                a_						     =			self._prepare_for_class(UpperCamelCase__   ,	UpperCamelCase__	)
                check_equivalence(UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	{'output_hidden_states': True}	)

                a_						     =			self._prepare_for_class(UpperCamelCase__   ,	UpperCamelCase__   ,	return_labels=UpperCamelCase__	)
                a_						     =			self._prepare_for_class(UpperCamelCase__   ,	UpperCamelCase__   ,	return_labels=UpperCamelCase__	)
                check_equivalence(UpperCamelCase__   ,	UpperCamelCase__   ,	UpperCamelCase__   ,	{'output_hidden_states': True}	)





@require_torch
class   __SCREAMING_SNAKE_CASE       (unittest.TestCase							,       __A       ):


  """simple docstring"""


  _a  : Any      			=     (MaskFormerSwinBackbone,) if is_torch_available() else ()
  _a  : List[Any]      			=     MaskFormerSwinConfig







  def    _a       (			self	):






         """simple docstring"""


         a_						     =			MaskFormerSwinModelTester(self	)







  def    _a       (			self	):






         """simple docstring"""


         a_     ,	a_						     =			self.model_tester.prepare_config_and_inputs_for_common()
         a_						     =			inputs_dict['pixel_values'].shape[0]

         for backbone_class in self.all_model_classes:
                a_						     =			backbone_class(UpperCamelCase__	)
                backbone.to(UpperCamelCase__	)
                backbone.eval()

                a_						     =			backbone(**UpperCamelCase__	)

                # Test default outputs and verify feature maps
                self.assertIsInstance(outputs.feature_maps   ,	UpperCamelCase__	)
                self.assertTrue(len(outputs.feature_maps	) == len(backbone.channels	)	)
                for feature_map, n_channels in zip(outputs.feature_maps   ,	backbone.channels	):
                       self.assertTrue(feature_map.shape[:2]   ,	(batch_size, n_channels)	)
                self.assertIsNone(outputs.hidden_states	)
                self.assertIsNone(outputs.attentions	)

                # Test output_hidden_states=True
                a_						     =			backbone(**UpperCamelCase__   ,	output_hidden_states=UpperCamelCase__	)
                self.assertIsNotNone(outputs.hidden_states	)
                self.assertTrue(len(outputs.hidden_states	)   ,	len(backbone.stage_names	)	)
                # We skip the stem layer
                for hidden_states, n_channels in zip(outputs.hidden_states[1:]   ,	backbone.channels	):
                       for hidden_state in hidden_states:
                              # Hidden states are in the format (batch_size, (height * width), n_channels)
                              a_     ,	a_     ,	a_						     =			hidden_state.shape
                              self.assertTrue((h_batch_size, h_n_channels)   ,	(batch_size, n_channels)	)

            # Test output_attentions=True
                if self.has_attentions:
                       a_						     =			backbone(**UpperCamelCase__   ,	output_attentions=UpperCamelCase__	)
                       self.assertIsNotNone(outputs.attentions	)












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

__lowerCAmelCase        =							[
    ("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"),
]

__lowerCAmelCase        =							[
    "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					(      lowercase_       :  int   ):
       """simple docstring"""




       a_						     =			torch.load(lowercase_   ,       map_location='cpu'   )
       return sd


def       __UpperCamelCase					(      lowercase_       :  List[str]   ,       lowercase_       :  Tuple   ,       lowercase_       :  Union[str, Any]=rename_keys_prefix   ):
       """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       __UpperCamelCase					(      lowercase_       :  Union[str, Any]   ,       lowercase_       :  Union[str, Any]   ):
       """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': 512}
              elif "vqa_advanced" in checkpoint_path:
                     a_						     =			{'visual_embedding_dim': 2_048}
              elif "vqa" in checkpoint_path:
                     a_						     =			{'visual_embedding_dim': 2_048}
              elif "nlvr" in checkpoint_path:
                     a_						     =			{'visual_embedding_dim': 1_024}
              else:
                     raise NotImplementedError(F'No implementation found for `{checkpoint_path}`.'   )
       else:
              if "vcr" in checkpoint_path:
                     a_						     =			{'visual_embedding_dim': 512}
                     a_						     =			'multichoice'
              elif "vqa_advanced" in checkpoint_path:
                     a_						     =			{'visual_embedding_dim': 2_048}
                     a_						     =			'vqa_advanced'
              elif "vqa" in checkpoint_path:
                     a_						     =			{'visual_embedding_dim': 2_048, 'num_labels': 3_129}
                     a_						     =			'vqa'
              elif "nlvr" in checkpoint_path:
                     a_						     =			{
                         'visual_embedding_dim': 1_024,
                         'num_labels': 2,
                     }
                     a_						     =			'nlvr'

       a_						     =			VisualBertConfig(**lowercase_   )

       # Load State Dict
       a_						     =			load_state_dict(lowercase_   )

       a_						     =			get_new_dict(lowercase_   ,       lowercase_   )

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

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


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




