




import argparse

import requests
import torch
from PIL import Image

from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor







def 				lowerCAmelCase_	(     snake_case_     ):
      if "cls_token" in name:
            _A							:     Optional[Any]			=						name.replace("""cls_token""","""vit.embeddings.cls_token"""     )
      if "mask_token" in name:
            _A							:     int			=						name.replace("""mask_token""","""decoder.mask_token"""     )
      if "decoder_pos_embed" in name:
            _A							:     int			=						name.replace("""decoder_pos_embed""","""decoder.decoder_pos_embed"""     )
      if "pos_embed" in name and "decoder" not in name:
            _A							:     Optional[int]			=						name.replace("""pos_embed""","""vit.embeddings.position_embeddings"""     )
      if "patch_embed.proj" in name:
            _A							:     Union[str, Any]			=						name.replace("""patch_embed.proj""","""vit.embeddings.patch_embeddings.projection"""     )
      if "patch_embed.norm" in name:
            _A							:     Tuple			=						name.replace("""patch_embed.norm""","""vit.embeddings.norm"""     )
      if "decoder_blocks" in name:
            _A							:     Any			=						name.replace("""decoder_blocks""","""decoder.decoder_layers"""     )
      if "blocks" in name:
            _A							:     int			=						name.replace("""blocks""","""vit.encoder.layer"""     )
      if "attn.proj" in name:
            _A							:     Any			=						name.replace("""attn.proj""","""attention.output.dense"""     )
      if "attn" in name:
            _A							:     Any			=						name.replace("""attn""","""attention.self"""     )
      if "norm1" in name:
            _A							:     Optional[int]			=						name.replace("""norm1""","""layernorm_before"""     )
      if "norm2" in name:
            _A							:     Dict			=						name.replace("""norm2""","""layernorm_after"""     )
      if "mlp.fc1" in name:
            _A							:     Dict			=						name.replace("""mlp.fc1""","""intermediate.dense"""     )
      if "mlp.fc2" in name:
            _A							:     Union[str, Any]			=						name.replace("""mlp.fc2""","""output.dense"""     )
      if "decoder_embed" in name:
            _A							:     Optional[Any]			=						name.replace("""decoder_embed""","""decoder.decoder_embed"""     )
      if "decoder_norm" in name:
            _A							:     Optional[Any]			=						name.replace("""decoder_norm""","""decoder.decoder_norm"""     )
      if "decoder_pred" in name:
            _A							:     Optional[Any]			=						name.replace("""decoder_pred""","""decoder.decoder_pred"""     )
      if "norm.weight" in name and "decoder" not in name:
            _A							:     Any			=						name.replace("""norm.weight""","""vit.layernorm.weight"""     )
      if "norm.bias" in name and "decoder" not in name:
            _A							:     Optional[Any]			=						name.replace("""norm.bias""","""vit.layernorm.bias"""     )

      return name







def 				lowerCAmelCase_	(     snake_case_,snake_case_     ):
      for key in orig_state_dict.copy().keys():
            _A							:     Dict			=						orig_state_dict.pop(snake_case_     )

            if "qkv" in key:
                  _A							:     Any			=						key.split("""."""     )
                  _A							:     Optional[Any]			=						int(key_split[1]     )
                  if "decoder_blocks" in key:
                        _A							:     int			=						config.decoder_hidden_size
                        _A							:     Tuple			=						"""decoder.decoder_layers."""
                        if "weight" in key:
                              _A							:     Tuple			=						val[:dim, :]
                              _A							:     str			=						val[dim : dim * 2, :]
                              _A							:     int			=						val[-dim:, :]
                        elif "bias" in key:
                              _A							:     Optional[Any]			=						val[:dim]
                              _A							:     List[Any]			=						val[dim : dim * 2]
                              _A							:     List[Any]			=						val[-dim:]
                  else:
                        _A							:     Optional[Any]			=						config.hidden_size
                        _A							:     Optional[int]			=						"""vit.encoder.layer."""
                        if "weight" in key:
                              _A							:     str			=						val[:dim, :]
                              _A							:     Any			=						val[dim : dim * 2, :]
                              _A							:     Union[str, Any]			=						val[-dim:, :]
                        elif "bias" in key:
                              _A							:     Union[str, Any]			=						val[:dim]
                              _A							:     Any			=						val[dim : dim * 2]
                              _A							:     int			=						val[-dim:]

            else:
                  _A							:     Optional[Any]			=						val

      return orig_state_dict







def 				lowerCAmelCase_	(     snake_case_,snake_case_     ):
      _A							:     Optional[Any]			=						ViTMAEConfig()
      if "large" in checkpoint_url:
            _A							:     List[str]			=						1024
            _A							:     Any			=						4096
            _A							:     List[Any]			=						24
            _A							:     Union[str, Any]			=						16
      elif "huge" in checkpoint_url:
            _A							:     Optional[int]			=						14
            _A							:     List[str]			=						1280
            _A							:     Any			=						5120
            _A							:     Union[str, Any]			=						32
            _A							:     Optional[Any]			=						16

      _A							:     Union[str, Any]			=						ViTMAEForPreTraining(snake_case_     )

      _A							:     Dict			=						torch.hub.load_state_dict_from_url(snake_case_,map_location="""cpu"""     )["""model"""]

      _A							:     Dict			=						ViTMAEImageProcessor(size=config.image_size     )

      _A							:     Union[str, Any]			=						convert_state_dict(snake_case_,snake_case_     )

      model.load_state_dict(snake_case_     )
      model.eval()

      _A							:     Any			=						"""https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg"""

      _A							:     List[str]			=						Image.open(requests.get(snake_case_,stream=snake_case_     ).raw     )
      _A							:     str			=						ViTMAEImageProcessor(size=config.image_size     )
      _A							:     Union[str, Any]			=						image_processor(images=snake_case_,return_tensors="""pt"""     )

      # forward pass
      torch.manual_seed(2     )
      _A							:     str			=						model(**snake_case_     )
      _A							:     Optional[int]			=						outputs.logits

      if "large" in checkpoint_url:
            _A							:     int			=						torch.tensor(
                [[-0.73_09, -0.71_28, -1.01_69], [-1.01_61, -0.90_58, -1.18_78], [-1.04_78, -0.94_11, -1.19_11]]     )
      elif "huge" in checkpoint_url:
            _A							:     List[str]			=						torch.tensor(
                [[-1.15_99, -0.91_99, -1.22_21], [-1.19_52, -0.92_69, -1.23_07], [-1.21_43, -0.93_37, -1.22_62]]     )
      else:
            _A							:     List[str]			=						torch.tensor(
                [[-0.91_92, -0.84_81, -1.12_59], [-1.13_49, -1.00_34, -1.25_99], [-1.17_57, -1.04_29, -1.27_26]]     )

      # verify logits
      assert torch.allclose(logits[0, :3, :3],snake_case_,atol=1e-4     )

      print(f'''Saving model to {pytorch_dump_folder_path}'''     )
      model.save_pretrained(snake_case_     )

      print(f'''Saving image processor to {pytorch_dump_folder_path}'''     )
      image_processor.save_pretrained(snake_case_     )


if __name__ == "__main__":
  _snake_case										=       argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      "--checkpoint_url",
      default="https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth",
      type=str,
      help="URL of the checkpoint you'd like to convert.",
  )
  parser.add_argument(
      "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
  )

  _snake_case										=       parser.parse_args()
  convert_vit_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)







import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
  import torch







@is_pipeline_test
class 						lowercase							(   unittest.TestCase		):
     _a							 =						MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
     _a							 =						TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

     def 					a__	(  self		,     _a		,     _a		,     _a				)						->							int:
           _A							:     str			=						TextaTextGenerationPipeline(model=_a		,     tokenizer=_a				)
           return generator, ["Something to write", "Something else"]

     def 					a__	(  self		,     _a		,     _a				)						->							Dict:
           _A							:     Any			=						generator("""Something there"""				)
           self.assertEqual(_a		,     [{"""generated_text""": ANY(_a				)}]				)
           # These are encoder decoder, they don't just append to incoming string
           self.assertFalse(outputs[0]["""generated_text"""].startswith("""Something there"""				)				)

           _A							:     List[Any]			=						generator(["""This is great !""", """Something else"""]		,     num_return_sequences=2		,     do_sample=_a				)
           self.assertEqual(
               _a		,     [
                   [{"""generated_text""": ANY(_a				)}, {"""generated_text""": ANY(_a				)}],
                   [{"""generated_text""": ANY(_a				)}, {"""generated_text""": ANY(_a				)}],
               ]		,     )

           _A							:     Optional[int]			=						generator(
               ["""This is great !""", """Something else"""]		,     num_return_sequences=2		,     batch_size=2		,     do_sample=_a				)
           self.assertEqual(
               _a		,     [
                   [{"""generated_text""": ANY(_a				)}, {"""generated_text""": ANY(_a				)}],
                   [{"""generated_text""": ANY(_a				)}, {"""generated_text""": ANY(_a				)}],
               ]		,     )

           with self.assertRaises(_a				):
                 generator(4				)

     @require_torch
     def 					a__	(  self				)						->							List[str]:
           _A							:     Any			=						pipeline("""text2text-generation"""		,     model="""patrickvonplaten/t5-tiny-random"""		,     framework="""pt"""				)
           # do_sample=False necessary for reproducibility
           _A							:     Dict			=						generator("""Something there"""		,     do_sample=_a				)
           self.assertEqual(_a		,     [{"""generated_text""": """"""}]				)

           _A							:     Any			=						3
           _A							:     Any			=						generator(
               """Something there"""		,     num_return_sequences=_a		,     num_beams=_a		,     )
           _A							:     Optional[int]			=						[
               {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide Beide"""},
               {"""generated_text""": """Beide Beide Beide Beide Beide Beide Beide Beide"""},
               {"""generated_text""": """"""},
           ]
           self.assertEqual(_a		,     _a				)

           _A							:     Dict			=						generator("""This is a test"""		,     do_sample=_a		,     num_return_sequences=2		,     return_tensors=_a				)
           self.assertEqual(
               _a		,     [
                   {"""generated_token_ids""": ANY(torch.Tensor				)},
                   {"""generated_token_ids""": ANY(torch.Tensor				)},
               ]		,     )
           _A							:     Dict			=						generator.model.config.eos_token_id
           _A							:     List[str]			=						"""<pad>"""
           _A							:     Dict			=						generator(
               ["""This is a test""", """This is a second test"""]		,     do_sample=_a		,     num_return_sequences=2		,     batch_size=2		,     return_tensors=_a		,     )
           self.assertEqual(
               _a		,     [
                   [
                       {"""generated_token_ids""": ANY(torch.Tensor				)},
                       {"""generated_token_ids""": ANY(torch.Tensor				)},
                   ],
                   [
                       {"""generated_token_ids""": ANY(torch.Tensor				)},
                       {"""generated_token_ids""": ANY(torch.Tensor				)},
                   ],
               ]		,     )



     @require_tf
     def 					a__	(  self				)						->							int:
           _A							:     Optional[Any]			=						pipeline("""text2text-generation"""		,     model="""patrickvonplaten/t5-tiny-random"""		,     framework="""tf"""				)
           # do_sample=False necessary for reproducibility
           _A							:     str			=						generator("""Something there"""		,     do_sample=_a				)
           self.assertEqual(_a		,     [{"""generated_text""": """"""}]				)

