





import argparse

import requests
import torch
from PIL import Image

from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor




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

       return name




def  __UpperCAmelCase(   lowercase_			,     lowercase_   ):
       for key in orig_state_dict.copy().keys():
              _lowerCamelCase				:  List[str]						=		orig_state_dict.pop(lowercase_   )

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

              else:
                     _lowerCamelCase				:  str						=		val

       return orig_state_dict




def  __UpperCAmelCase(   lowercase_			,     lowercase_   ):
       _lowerCamelCase				:  Dict						=		ViTMAEConfig()
       if "large" in checkpoint_url:
              _lowerCamelCase				:  List[str]						=		10_24
              _lowerCamelCase				:  Optional[Any]						=		40_96
              _lowerCamelCase				:  Optional[int]						=		24
              _lowerCamelCase				:  str						=		16
       elif "huge" in checkpoint_url:
              _lowerCamelCase				:  List[str]						=		14
              _lowerCamelCase				:  Tuple						=		12_80
              _lowerCamelCase				:  Optional[Any]						=		51_20
              _lowerCamelCase				:  Any						=		32
              _lowerCamelCase				:  str						=		16

       _lowerCamelCase				:  List[str]						=		ViTMAEForPreTraining(lowercase_   )

       _lowerCamelCase				:  List[Any]						=		torch.hub.load_state_dict_from_url(lowercase_			,     map_location='''cpu'''   )['''model''']

       _lowerCamelCase				:  Optional[Any]						=		ViTMAEImageProcessor(size=config.image_size   )

       _lowerCamelCase				:  str						=		convert_state_dict(lowercase_			,     lowercase_   )

       model.load_state_dict(lowercase_   )
       model.eval()

       _lowerCamelCase				:  str						=		'''https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg'''

       _lowerCamelCase				:  Any						=		Image.open(requests.get(lowercase_			,     stream=lowercase_   ).raw   )
       _lowerCamelCase				:  Dict						=		ViTMAEImageProcessor(size=config.image_size   )
       _lowerCamelCase				:  Union[str, Any]						=		image_processor(images=lowercase_			,     return_tensors='''pt'''   )

       # forward pass
       torch.manual_seed(2   )
       _lowerCamelCase				:  Tuple						=		model(**lowercase_   )
       _lowerCamelCase				:  str						=		outputs.logits

       if "large" in checkpoint_url:
              _lowerCamelCase				:  Any						=		torch.tensor(
                  [[-0.7_3_0_9, -0.7_1_2_8, -1.0_1_6_9], [-1.0_1_6_1, -0.9_0_5_8, -1.1_8_7_8], [-1.0_4_7_8, -0.9_4_1_1, -1.1_9_1_1]]   )
       elif "huge" in checkpoint_url:
              _lowerCamelCase				:  Union[str, Any]						=		torch.tensor(
                  [[-1.1_5_9_9, -0.9_1_9_9, -1.2_2_2_1], [-1.1_9_5_2, -0.9_2_6_9, -1.2_3_0_7], [-1.2_1_4_3, -0.9_3_3_7, -1.2_2_6_2]]   )
       else:
              _lowerCamelCase				:  Tuple						=		torch.tensor(
                  [[-0.9_1_9_2, -0.8_4_8_1, -1.1_2_5_9], [-1.1_3_4_9, -1.0_0_3_4, -1.2_5_9_9], [-1.1_7_5_7, -1.0_4_2_9, -1.2_7_2_6]]   )

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

       print(F"""Saving model to {pytorch_dump_folder_path}"""   )
       model.save_pretrained(lowercase_   )

       print(F"""Saving image processor to {pytorch_dump_folder_path}"""   )
       image_processor.save_pretrained(lowercase_   )


if __name__ == "__main__":
       _lowerCamelCase      							=   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.'
       )

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








import json
import os
import subprocess
import unittest
from ast import literal_eval

import pytest
from parameterized import parameterized_class

from . import is_sagemaker_available


if is_sagemaker_available():
       from sagemaker import Session, TrainingJobAnalytics
       from sagemaker.huggingface import HuggingFace







@pytest.mark.skipif(
    literal_eval(os.getenv("""TEST_SAGEMAKER"""			,"""False"""	)	) is not True			,reason="""Skipping test because should only be run when releasing minor transformers version"""			,)
@pytest.mark.usefixtures("""sm_env"""	)
@parameterized_class(
    [
        {
            """framework""": """pytorch""",
            """script""": """run_glue.py""",
            """model_name_or_path""": """distilbert-base-cased""",
            """instance_type""": """ml.g4dn.xlarge""",
            """results""": {"""train_runtime""": 650, """eval_accuracy""": 0.6, """eval_loss""": 0.9},
        },
        {
            """framework""": """tensorflow""",
            """script""": """run_tf.py""",
            """model_name_or_path""": """distilbert-base-cased""",
            """instance_type""": """ml.g4dn.xlarge""",
            """results""": {"""train_runtime""": 600, """eval_accuracy""": 0.3, """eval_loss""": 0.9},
        },
    ]	)
class       __A							(   unittest.TestCase	):




      """simple docstring"""







      def 					__snake_case      (		self):



             """simple docstring"""





             if self.framework == "pytorch":
                    subprocess.run(
                        F"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split()   ,				encoding='''utf-8'''   ,				check=a__   ,				)
             assert hasattr(self   ,				'''env''')


      def 					__snake_case      (		self   ,				a__=1):



             """simple docstring"""





             return HuggingFace(
                 entry_point=self.script   ,				source_dir=self.env.test_path   ,				role=self.env.role   ,				image_uri=self.env.image_uri   ,				base_job_name=F"""{self.env.base_job_name}-single"""   ,				instance_count=a__   ,				instance_type=self.instance_type   ,				debugger_hook_config=a__   ,				hyperparameters={**self.env.hyperparameters, '''model_name_or_path''': self.model_name_or_path}   ,				metric_definitions=self.env.metric_definitions   ,				py_version='''py36'''   ,				)


      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





             TrainingJobAnalytics(a__).export_csv(F"""{self.env.test_path}/{job_name}_metrics.csv""")



      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  Dict						=		self.create_estimator()

             # run training
             estimator.fit()

             # result dataframe
             _lowerCamelCase				:  Tuple						=		TrainingJobAnalytics(estimator.latest_training_job.name).dataframe()

             # extract kpis
             _lowerCamelCase				:  Tuple						=		list(result_metrics_df[result_metrics_df.metric_name == '''eval_accuracy''']['''value'''])
             _lowerCamelCase				:  Dict						=		list(result_metrics_df[result_metrics_df.metric_name == '''eval_loss''']['''value'''])
             # get train time from SageMaker job, this includes starting, preprocessing, stopping
             _lowerCamelCase				:  Optional[Any]						=		(
                 Session().describe_training_job(estimator.latest_training_job.name).get('''TrainingTimeInSeconds'''   ,				99_9999)
             )

             # assert kpis
             assert train_runtime <= self.results["train_runtime"]
             assert all(t >= self.results['''eval_accuracy'''] for t in eval_accuracy)
             assert all(t <= self.results['''eval_loss'''] for t in eval_loss)

             # dump tests result into json file to share in PR
             with open(F"""{estimator.latest_training_job.name}.json"""   ,				'''w''') as outfile:
                    json.dump({'''train_time''': train_runtime, '''eval_accuracy''': eval_accuracy, '''eval_loss''': eval_loss}   ,				a__)

