





import unittest

from transformers import BigBirdConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
       import jax

       from transformers.models.big_bird.modeling_flax_big_bird import (
           FlaxBigBirdForCausalLM,
           FlaxBigBirdForMaskedLM,
           FlaxBigBirdForMultipleChoice,
           FlaxBigBirdForPreTraining,
           FlaxBigBirdForQuestionAnswering,
           FlaxBigBirdForSequenceClassification,
           FlaxBigBirdForTokenClassification,
           FlaxBigBirdModel,
       )




class        SCREAMING_SNAKE_CASE__					(		unittest.TestCase ):



 def __init__(				self,__lowerCamelCase,__lowerCamelCase=2,__lowerCamelCase=56,__lowerCamelCase=True,__lowerCamelCase=True,__lowerCamelCase=True,__lowerCamelCase=True,__lowerCamelCase=99,__lowerCamelCase=32,__lowerCamelCase=2,__lowerCamelCase=2,__lowerCamelCase=7,__lowerCamelCase="gelu_new",__lowerCamelCase=0.1,__lowerCamelCase=0.1,__lowerCamelCase=512,__lowerCamelCase=16,__lowerCamelCase=2,__lowerCamelCase=0.02,__lowerCamelCase=4,__lowerCamelCase="block_sparse",__lowerCamelCase=True,__lowerCamelCase=False,__lowerCamelCase=2,__lowerCamelCase=3,):
        A__		   =		parent
        A__		   =		batch_size
        A__		   =		seq_length
        A__		   =		is_training
        A__		   =		use_attention_mask
        A__		   =		use_token_type_ids
        A__		   =		use_labels
        A__		   =		vocab_size
        A__		   =		hidden_size
        A__		   =		num_hidden_layers
        A__		   =		num_attention_heads
        A__		   =		intermediate_size
        A__		   =		hidden_act
        A__		   =		hidden_dropout_prob
        A__		   =		attention_probs_dropout_prob
        A__		   =		max_position_embeddings
        A__		   =		type_vocab_size
        A__		   =		type_sequence_label_size
        A__		   =		initializer_range
        A__		   =		num_choices

        A__		   =		rescale_embeddings
        A__		   =		attention_type
        A__		   =		use_bias
        A__		   =		block_size
        A__		   =		num_random_blocks



 def 							UpperCamelCase     (				self					):
        A__		   =		ids_tensor([self.batch_size, self.seq_length],self.vocab_size					)

        A__		   =		None
        if self.use_attention_mask:
               A__		   =		random_attention_mask([self.batch_size, self.seq_length]					)

        A__		   =		None
        if self.use_token_type_ids:
               A__		   =		ids_tensor([self.batch_size, self.seq_length],self.type_vocab_size					)

        A__		   =		BigBirdConfig(
            vocab_size=self.vocab_size,hidden_size=self.hidden_size,num_hidden_layers=self.num_hidden_layers,num_attention_heads=self.num_attention_heads,intermediate_size=self.intermediate_size,hidden_act=self.hidden_act,hidden_dropout_prob=self.hidden_dropout_prob,attention_probs_dropout_prob=self.attention_probs_dropout_prob,max_position_embeddings=self.max_position_embeddings,type_vocab_size=self.type_vocab_size,is_decoder=__snake_case,initializer_range=self.initializer_range,attention_type=self.attention_type,block_size=self.block_size,num_random_blocks=self.num_random_blocks,use_bias=self.use_bias,rescale_embeddings=self.rescale_embeddings,)

        return config, input_ids, token_type_ids, attention_mask




 def 							UpperCamelCase     (				self					):
        A__		   =		self.prepare_config_and_inputs()
        A__   ,      A__   ,      A__   ,      A__		   =		config_and_inputs
        A__		   =		{
            '''input_ids''': input_ids,
            '''token_type_ids''': token_type_ids,
            '''attention_mask''': attention_mask,
        }
        return config, inputs_dict




@require_flax
class        SCREAMING_SNAKE_CASE__					(		A__							,   unittest.TestCase ):
 __SCREAMING_SNAKE_CASE							=      (
     (
         FlaxBigBirdForCausalLM,
         FlaxBigBirdModel,
         FlaxBigBirdForPreTraining,
         FlaxBigBirdForMaskedLM,
         FlaxBigBirdForMultipleChoice,
         FlaxBigBirdForQuestionAnswering,
         FlaxBigBirdForSequenceClassification,
         FlaxBigBirdForTokenClassification,
     )
     if is_flax_available()
     else ()
 )

 __SCREAMING_SNAKE_CASE							=      False
 __SCREAMING_SNAKE_CASE							=      False



 def 							UpperCamelCase     (				self					):
        A__		   =		FlaxBigBirdModelTester(self					)



 @slow
 # copied from `test_modeling_flax_common` because it takes much longer than other models
 def 							UpperCamelCase     (				self					):
        super().test_from_pretrained_save_pretrained()



 @slow
 # copied from `test_modeling_flax_common` because it takes much longer than other models
 def 							UpperCamelCase     (				self					):
        super().test_from_pretrained_with_no_automatic_init()



 @slow
 # copied from `test_modeling_flax_common` because it takes much longer than other models
 def 							UpperCamelCase     (				self					):
        super().test_no_automatic_init()



 @slow
 # copied from `test_modeling_flax_common` because it takes much longer than other models
 def 							UpperCamelCase     (				self					):
        super().test_hidden_states_output()



 @slow
 def 							UpperCamelCase     (				self					):
        for model_class_name in self.all_model_classes:
               A__		   =		model_class_name.from_pretrained('''google/bigbird-roberta-base'''					)
               self.assertIsNotNone(__snake_case					)



 def 							UpperCamelCase     (				self					):
        if self.test_attn_probs:
               super().test_attention_outputs()



 @slow
 # copied from `test_modeling_flax_common` because it takes much longer than other models
 def 							UpperCamelCase     (				self					):
        A__   ,      A__		   =		self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               with self.subTest(model_class.__name__					):
                      A__		   =		self._prepare_for_class(__snake_case,__snake_case					)
                      A__		   =		model_class(__snake_case					)

                      @jax.jit
                      def model_jitted(__lowerCamelCase,__lowerCamelCase=None,**__lowerCamelCase					):
                             return model(input_ids=__snake_case,attention_mask=__snake_case,**__snake_case					)

                      with self.subTest('''JIT Enabled'''					):
                             A__		   =		model_jitted(**__snake_case					).to_tuple()

                      with self.subTest('''JIT Disabled'''					):
                             with jax.disable_jit():
                                    A__		   =		model_jitted(**__snake_case					).to_tuple()

                      self.assertEqual(len(__snake_case					),len(__snake_case					)					)
                      for jitted_output, output in zip(__snake_case,__snake_case					):
                             self.assertEqual(jitted_output.shape,output.shape					)




 def 							UpperCamelCase     (				self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase=1E-5,__lowerCamelCase="outputs",__lowerCamelCase=None					):
        # `bigbird_block_sparse_attention` in `FlaxBigBird` returns `attention_probs = None`, while in PyTorch version,
        # an effort was done to return `attention_probs` (yet to be verified).
        if name.startswith('''outputs.attentions'''					):
               return
        else:
               super().check_pt_flax_outputs(__snake_case,__snake_case,__snake_case,__snake_case,__snake_case,__snake_case					)












'''simple docstring'''




import argparse
import json
from pathlib import Path

import requests
import timm
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import DeiTImageProcessor, ViTConfig, ViTForImageClassification, ViTImageProcessor, ViTModel
from transformers.utils import logging


logging.set_verbosity_info()
_SCREAMING_SNAKE_CASE							     =			logging.get_logger(__name__)





def 	__lowerCamelCase			(      __lowerCAmelCase       :							Optional[int]      ,    __lowerCAmelCase       :							List[Any]=False		)		->  List[str]:
   snake_case 						=   []
   for i in range(config.num_hidden_layers		):
      # encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
      rename_keys.append((F'''blocks.{i}.norm1.weight''', F'''vit.encoder.layer.{i}.layernorm_before.weight''')		)
      rename_keys.append((F'''blocks.{i}.norm1.bias''', F'''vit.encoder.layer.{i}.layernorm_before.bias''')		)
      rename_keys.append((F'''blocks.{i}.attn.proj.weight''', F'''vit.encoder.layer.{i}.attention.output.dense.weight''')		)
      rename_keys.append((F'''blocks.{i}.attn.proj.bias''', F'''vit.encoder.layer.{i}.attention.output.dense.bias''')		)
      rename_keys.append((F'''blocks.{i}.norm2.weight''', F'''vit.encoder.layer.{i}.layernorm_after.weight''')		)
      rename_keys.append((F'''blocks.{i}.norm2.bias''', F'''vit.encoder.layer.{i}.layernorm_after.bias''')		)
      rename_keys.append((F'''blocks.{i}.mlp.fc1.weight''', F'''vit.encoder.layer.{i}.intermediate.dense.weight''')		)
      rename_keys.append((F'''blocks.{i}.mlp.fc1.bias''', F'''vit.encoder.layer.{i}.intermediate.dense.bias''')		)
      rename_keys.append((F'''blocks.{i}.mlp.fc2.weight''', F'''vit.encoder.layer.{i}.output.dense.weight''')		)
      rename_keys.append((F'''blocks.{i}.mlp.fc2.bias''', F'''vit.encoder.layer.{i}.output.dense.bias''')		)

   # projection layer + position embeddings
   rename_keys.extend(
       [
           ("""cls_token""", """vit.embeddings.cls_token"""),
           ("""patch_embed.proj.weight""", """vit.embeddings.patch_embeddings.projection.weight"""),
           ("""patch_embed.proj.bias""", """vit.embeddings.patch_embeddings.projection.bias"""),
           ("""pos_embed""", """vit.embeddings.position_embeddings"""),
       ]		)

   if base_model:
      # layernorm + pooler
      rename_keys.extend(
          [
              ("""norm.weight""", """layernorm.weight"""),
              ("""norm.bias""", """layernorm.bias"""),
              ("""pre_logits.fc.weight""", """pooler.dense.weight"""),
              ("""pre_logits.fc.bias""", """pooler.dense.bias"""),
          ]		)

      # if just the base model, we should remove "vit" from all keys that start with "vit"
      snake_case 						=   [(pair[0], pair[1][4:]) if pair[1].startswith("""vit"""		) else pair for pair in rename_keys]
   else:
      # layernorm + classification head
      rename_keys.extend(
          [
              ("""norm.weight""", """vit.layernorm.weight"""),
              ("""norm.bias""", """vit.layernorm.bias"""),
              ("""head.weight""", """classifier.weight"""),
              ("""head.bias""", """classifier.bias"""),
          ]		)

   return rename_keys





def 	__lowerCamelCase			(      __lowerCAmelCase       :							Dict      ,    __lowerCAmelCase       :							Optional[Any]      ,    __lowerCAmelCase       :							int=False		)		->  Union[str, Any]:
   for i in range(config.num_hidden_layers		):
      if base_model:
         snake_case 						=   """"""
      else:
         snake_case 						=   """vit."""
      # read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
      snake_case 						=   state_dict.pop(F'''blocks.{i}.attn.qkv.weight'''		)
      snake_case 						=   state_dict.pop(F'''blocks.{i}.attn.qkv.bias'''		)
      # next, add query, keys and values (in that order) to the state dict
      snake_case 						=   in_proj_weight[
          : config.hidden_size, :
      ]
      snake_case 						=   in_proj_bias[: config.hidden_size]
      snake_case 						=   in_proj_weight[
          config.hidden_size : config.hidden_size * 2, :
      ]
      snake_case 						=   in_proj_bias[
          config.hidden_size : config.hidden_size * 2
      ]
      snake_case 						=   in_proj_weight[
          -config.hidden_size :, :
      ]
      snake_case 						=   in_proj_bias[-config.hidden_size :]





def 	__lowerCamelCase			(      __lowerCAmelCase       :							List[str]		)		->  List[str]:
   snake_case 						=   ["""head.weight""", """head.bias"""]
   for k in ignore_keys:
      state_dict.pop(__lowerCAmelCase      ,    __lowerCAmelCase		)





def 	__lowerCamelCase			(      __lowerCAmelCase       :							Optional[Any]      ,    __lowerCAmelCase       :							int      ,    __lowerCAmelCase       :							Tuple		)		->  int:
   snake_case 						=   dct.pop(__lowerCAmelCase		)
   snake_case 						=   val





def 	__lowerCamelCase			(      )		->  Optional[Any]:
   snake_case 						=   """http://images.cocodataset.org/val2017/000000039769.jpg"""
   snake_case 						=   Image.open(requests.get(__lowerCAmelCase      ,    stream=__lowerCAmelCase		).raw		)
   return im





@torch.no_grad()
def 	__lowerCamelCase			(      __lowerCAmelCase       :							List[str]      ,    __lowerCAmelCase       :							Any		)		->  Dict:
   snake_case 						=   ViTConfig()
   snake_case 						=   False
   # dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size
   if vit_name[-5:] == "in21k":
      snake_case 						=   True
      snake_case 						=   int(vit_name[-12:-10]		)
      snake_case 						=   int(vit_name[-9:-6]		)
   else:
      snake_case 						=   10_00
      snake_case 						=   """huggingface/label-files"""
      snake_case 						=   """imagenet-1k-id2label.json"""
      snake_case 						=   json.load(open(hf_hub_download(__lowerCAmelCase      ,    __lowerCAmelCase      ,    repo_type="""dataset"""		)      ,    """r"""		)		)
      snake_case 						=   {int(__lowerCAmelCase		): v for k, v in idalabel.items()}
      snake_case 						=   idalabel
      snake_case 						=   {v: k for k, v in idalabel.items()}
      snake_case 						=   int(vit_name[-6:-4]		)
      snake_case 						=   int(vit_name[-3:]		)
   # size of the architecture
   if "deit" in vit_name:
      if vit_name[9:].startswith("""tiny"""		):
         snake_case 						=   1_92
         snake_case 						=   7_68
         snake_case 						=   12
         snake_case 						=   3
      elif vit_name[9:].startswith("""small"""		):
         snake_case 						=   3_84
         snake_case 						=   15_36
         snake_case 						=   12
         snake_case 						=   6
      else:
         pass
   else:
      if vit_name[4:].startswith("""small"""		):
         snake_case 						=   7_68
         snake_case 						=   23_04
         snake_case 						=   8
         snake_case 						=   8
      elif vit_name[4:].startswith("""base"""		):
         pass
      elif vit_name[4:].startswith("""large"""		):
         snake_case 						=   10_24
         snake_case 						=   40_96
         snake_case 						=   24
         snake_case 						=   16
      elif vit_name[4:].startswith("""huge"""		):
         snake_case 						=   12_80
         snake_case 						=   51_20
         snake_case 						=   32
         snake_case 						=   16

    # load original model from timm
   snake_case 						=   timm.create_model(__lowerCAmelCase      ,    pretrained=__lowerCAmelCase		)
   timm_model.eval()

   # load state_dict of original model, remove and rename some keys
   snake_case 						=   timm_model.state_dict()
   if base_model:
      remove_classification_head_(__lowerCAmelCase		)
   snake_case 						=   create_rename_keys(__lowerCAmelCase      ,    __lowerCAmelCase		)
   for src, dest in rename_keys:
      rename_key(__lowerCAmelCase      ,    __lowerCAmelCase      ,    __lowerCAmelCase		)
   read_in_q_k_v(__lowerCAmelCase      ,    __lowerCAmelCase      ,    __lowerCAmelCase		)

   # load HuggingFace model
   if vit_name[-5:] == "in21k":
      snake_case 						=   ViTModel(__lowerCAmelCase		).eval()
   else:
      snake_case 						=   ViTForImageClassification(__lowerCAmelCase		).eval()
   model.load_state_dict(__lowerCAmelCase		)

   # Check outputs on an image, prepared by ViTImageProcessor/DeiTImageProcessor
   if "deit" in vit_name:
      snake_case 						=   DeiTImageProcessor(size=config.image_size		)
   else:
      snake_case 						=   ViTImageProcessor(size=config.image_size		)
   snake_case 						=   image_processor(images=prepare_img()      ,    return_tensors="""pt"""		)
   snake_case 						=   encoding["""pixel_values"""]
   snake_case 						=   model(__lowerCAmelCase		)

   if base_model:
      snake_case 						=   timm_model.forward_features(__lowerCAmelCase		)
      assert timm_pooled_output.shape == outputs.pooler_output.shape
      assert torch.allclose(__lowerCAmelCase      ,    outputs.pooler_output      ,    atol=1e-3		)
   else:
      snake_case 						=   timm_model(__lowerCAmelCase		)
      assert timm_logits.shape == outputs.logits.shape
      assert torch.allclose(__lowerCAmelCase      ,    outputs.logits      ,    atol=1e-3		)

   Path(__lowerCAmelCase		).mkdir(exist_ok=__lowerCAmelCase		)
   print(F'''Saving model {vit_name} to {pytorch_dump_folder_path}'''		)
   model.save_pretrained(__lowerCAmelCase		)
   print(F'''Saving image processor to {pytorch_dump_folder_path}'''		)
   image_processor.save_pretrained(__lowerCAmelCase		)


if __name__ == "__main__":
 _SCREAMING_SNAKE_CASE							     =			argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     "--vit_name",
     default="vit_base_patch16_224",
     type=str,
     help="Name of the ViT timm model you'd like to convert.",
 )
 parser.add_argument(
     "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
 )

 _SCREAMING_SNAKE_CASE							     =			parser.parse_args()
 convert_vit_checkpoint(args.vit_name, args.pytorch_dump_folder_path)

