


import argparse
import json
import os

import fairseq
import torch
from fairseq.data import Dictionary

# Register SEW's fairseq modules
from sew_asapp import tasks  # noqa: F401

from transformers import (
    SEWConfig,
    SEWForCTC,
    SEWModel,
    WavaVecaCTCTokenizer,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
    logging,
)


logging.set_verbosity_info()
_A		:				Tuple									=       logging.get_logger(__name__)

_A		:				int									=       {
    "post_extract_proj": "feature_projection",
    "encoder.pos_conv.0": "encoder.pos_conv_embed.conv",
    "self_attn.k_proj": "encoder.layers.*.attention.k_proj",
    "self_attn.v_proj": "encoder.layers.*.attention.v_proj",
    "self_attn.q_proj": "encoder.layers.*.attention.q_proj",
    "self_attn.out_proj": "encoder.layers.*.attention.out_proj",
    "self_attn_layer_norm": "encoder.layers.*.layer_norm",
    "fc1": "encoder.layers.*.feed_forward.intermediate_dense",
    "fc2": "encoder.layers.*.feed_forward.output_dense",
    "final_layer_norm": "encoder.layers.*.final_layer_norm",
    "encoder.upsample.0": "encoder.upsample.projection",
    "encoder.layer_norm": "encoder.layer_norm",
    "w2v_model.layer_norm": "layer_norm",
    "w2v_encoder.proj": "lm_head",
    "mask_emb": "masked_spec_embed",
}



def     __lowerCAmelCase						(							snake_case	:    str						,      snake_case	:    Tuple						,      snake_case	:    List[Any]						,      snake_case	:    Dict						,      snake_case	:    str )       ->							int:


   for attribute in key.split(""".""" ):
      __lowerCamelCase:				List[Any]     =	getattr(_UpperCamelCase						,      _UpperCamelCase )

   if weight_type is not None:
      __lowerCamelCase:				str     =	getattr(_UpperCamelCase						,      _UpperCamelCase ).shape
   else:
      __lowerCamelCase:				List[Any]     =	hf_pointer.shape

   assert hf_shape == value.shape, (
       f'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be'
       f' {value.shape} for {full_name}'
   )

   if weight_type == "weight":
      __lowerCamelCase:				Optional[int]     =	value
   elif weight_type == "weight_g":
      __lowerCamelCase:				List[Any]     =	value
   elif weight_type == "weight_v":
      __lowerCamelCase:				int     =	value
   elif weight_type == "bias":
      __lowerCamelCase:				Optional[int]     =	value
   else:
      __lowerCamelCase:				List[Any]     =	value

   logger.info(f'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.' )



def     __lowerCAmelCase						(							snake_case	:    Tuple						,      snake_case	:    List[str]						,      snake_case	:    Dict )       ->							Tuple:
   __lowerCamelCase:				List[Any]     =	[]
   __lowerCamelCase:				Tuple     =	fairseq_model.state_dict()

   __lowerCamelCase:				Dict     =	hf_model.sew.feature_extractor if is_finetuned else hf_model.feature_extractor

   for name, value in fairseq_dict.items():
      __lowerCamelCase:				int     =	False
      if "conv_layers" in name:
         load_conv_layer(
             _UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      hf_model.config.feat_extract_norm == """group"""						,      )
         __lowerCamelCase:				int     =	True
      else:
         for key, mapped_key in MAPPING.items():
            __lowerCamelCase:				Dict     =	"""sew.""" + mapped_key if (is_finetuned and mapped_key != """lm_head""") else mapped_key

            if key in name or key.split("""w2v_model.""" )[-1] == name.split(""".""" )[0]:
               __lowerCamelCase:				List[Any]     =	True
               if "*" in mapped_key:
                  __lowerCamelCase:				str     =	name.split(_UpperCamelCase )[0].split(""".""" )[-2]
                  __lowerCamelCase:				Union[str, Any]     =	mapped_key.replace("""*"""						,      _UpperCamelCase )
               if "weight_g" in name:
                  __lowerCamelCase:				Union[str, Any]     =	"""weight_g"""
               elif "weight_v" in name:
                  __lowerCamelCase:				Optional[int]     =	"""weight_v"""
               elif "weight" in name:
                  __lowerCamelCase:				str     =	"""weight"""
               elif "bias" in name:
                  __lowerCamelCase:				Any     =	"""bias"""
               else:
                  __lowerCamelCase:				str     =	None
               set_recursively(_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase )
            continue
      if not is_used:
         unused_weights.append(_UpperCamelCase )

   logger.warning(f'Unused weights: {unused_weights}' )



def     __lowerCAmelCase						(							snake_case	:    int						,      snake_case	:    str						,      snake_case	:    Dict						,      snake_case	:    Optional[int]						,      snake_case	:    Tuple )       ->							str:
   __lowerCamelCase:				Optional[Any]     =	full_name.split("""conv_layers.""" )[-1]
   __lowerCamelCase:				int     =	name.split(""".""" )
   __lowerCamelCase:				Optional[Any]     =	int(items[0] )
   __lowerCamelCase:				Dict     =	int(items[1] )

   if type_id == 0:
      if "bias" in name:
         assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
             f'{full_name} has size {value.shape}, but'
             f' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.'
         )
         __lowerCamelCase:				List[str]     =	value
         logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.' )
      elif "weight" in name:
         assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
             f'{full_name} has size {value.shape}, but'
             f' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.'
         )
         __lowerCamelCase:				Dict     =	value
         logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.' )
   elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
      if "bias" in name:
         assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
             f'{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was'
             " found."
         )
         __lowerCamelCase:				Optional[Any]     =	value
         logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' )
      elif "weight" in name:
         assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
             f'{full_name} has size {value.shape}, but'
             f' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.'
         )
         __lowerCamelCase:				Union[str, Any]     =	value
         logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.' )
   else:
      unused_weights.append(_UpperCamelCase )



def     __lowerCAmelCase						(							snake_case	:    Tuple						,      snake_case	:    List[str] )       ->							Any:
   __lowerCamelCase:				List[str]     =	SEWConfig()
   if is_finetuned:
      __lowerCamelCase:				Any     =	model.wav_encoder.wav_model.cfg
   else:
      __lowerCamelCase:				Tuple     =	model.cfg

   __lowerCamelCase:				Tuple     =	fs_config.conv_bias
   __lowerCamelCase:				Optional[int]     =	eval(fs_config.conv_feature_layers )
   __lowerCamelCase:				Tuple     =	[x[0] for x in conv_layers]
   __lowerCamelCase:				int     =	[x[1] for x in conv_layers]
   __lowerCamelCase:				Optional[Any]     =	[x[2] for x in conv_layers]
   __lowerCamelCase:				List[str]     =	"""gelu"""
   __lowerCamelCase:				Optional[int]     =	"""layer""" if fs_config.extractor_mode == """layer_norm""" else """group"""
   __lowerCamelCase:				str     =	0.0
   __lowerCamelCase:				Tuple     =	fs_config.activation_fn.name
   __lowerCamelCase:				Tuple     =	fs_config.encoder_embed_dim
   __lowerCamelCase:				str     =	0.02
   __lowerCamelCase:				str     =	fs_config.encoder_ffn_embed_dim
   __lowerCamelCase:				Dict     =	1e-5
   __lowerCamelCase:				str     =	fs_config.encoder_layerdrop
   __lowerCamelCase:				Tuple     =	fs_config.encoder_attention_heads
   __lowerCamelCase:				Any     =	fs_config.conv_pos_groups
   __lowerCamelCase:				Tuple     =	fs_config.conv_pos
   __lowerCamelCase:				List[str]     =	len(_UpperCamelCase )
   __lowerCamelCase:				int     =	fs_config.encoder_layers
   __lowerCamelCase:				Dict     =	fs_config.squeeze_factor

   # take care of any params that are overridden by the Wav2VecCtc model
   if is_finetuned:
      __lowerCamelCase:				List[str]     =	model.cfg
      __lowerCamelCase:				Optional[int]     =	fs_config.final_dropout
      __lowerCamelCase:				Optional[int]     =	fs_config.layerdrop
   __lowerCamelCase:				Union[str, Any]     =	fs_config.activation_dropout
   __lowerCamelCase:				Optional[int]     =	fs_config.mask_prob > 0 or fs_config.mask_channel_prob > 0
   __lowerCamelCase:				int     =	fs_config.attention_dropout
   __lowerCamelCase:				List[Any]     =	fs_config.dropout_input
   __lowerCamelCase:				Union[str, Any]     =	fs_config.dropout
   __lowerCamelCase:				Optional[Any]     =	fs_config.mask_channel_length
   __lowerCamelCase:				Optional[int]     =	fs_config.mask_channel_prob
   __lowerCamelCase:				Tuple     =	fs_config.mask_length
   __lowerCamelCase:				int     =	fs_config.mask_prob

   __lowerCamelCase:				List[Any]     =	"""Wav2Vec2FeatureExtractor"""
   __lowerCamelCase:				Union[str, Any]     =	"""Wav2Vec2CTCTokenizer"""

   return config



@torch.no_grad()
def     __lowerCAmelCase						(							snake_case	:    Any						,      snake_case	:    Dict						,      snake_case	:    List[Any]=None						,      snake_case	:    Any=None						,      snake_case	:    str=True )       ->							Any:


   if is_finetuned:
      __lowerCamelCase   ,     __lowerCamelCase   ,     __lowerCamelCase:				Dict     =	fairseq.checkpoint_utils.load_model_ensemble_and_task(
          [checkpoint_path]						,      arg_overrides={"""data""": """/""".join(dict_path.split("""/""" )[:-1] )} )
   else:
      __lowerCamelCase   ,     __lowerCamelCase   ,     __lowerCamelCase:				Optional[int]     =	fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path] )

   if config_path is not None:
      __lowerCamelCase:				Dict     =	SEWConfig.from_pretrained(_UpperCamelCase )
   else:
      __lowerCamelCase:				str     =	convert_config(model[0]						,      _UpperCamelCase )
   __lowerCamelCase:				Union[str, Any]     =	model[0].eval()

   __lowerCamelCase:				Any     =	True if config.feat_extract_norm == """layer""" else False
   __lowerCamelCase:				Union[str, Any]     =	WavaVecaFeatureExtractor(
       feature_size=1						,      sampling_rate=16000						,      padding_value=0						,      do_normalize=_UpperCamelCase						,      return_attention_mask=_UpperCamelCase						,      )

   if is_finetuned:
      if dict_path:
         __lowerCamelCase:				List[Any]     =	Dictionary.load(_UpperCamelCase )

         # important change bos & pad token id since CTC symbol is <pad> and
         # not <s> as in fairseq
         __lowerCamelCase:				Dict     =	target_dict.pad_index
         __lowerCamelCase:				List[Any]     =	target_dict.bos_index
         __lowerCamelCase:				Optional[int]     =	target_dict.pad_index
         __lowerCamelCase:				List[str]     =	target_dict.bos_index
         __lowerCamelCase:				int     =	target_dict.eos_index
         __lowerCamelCase:				str     =	len(target_dict.symbols )
         __lowerCamelCase:				Optional[Any]     =	os.path.join(_UpperCamelCase						,      """vocab.json""" )
         if not os.path.isdir(_UpperCamelCase ):
            logger.error("""--pytorch_dump_folder_path ({}) should be a directory""".format(_UpperCamelCase ) )
            return
         os.makedirs(_UpperCamelCase						,      exist_ok=_UpperCamelCase )
         with open(_UpperCamelCase						,      """w"""						,      encoding="""utf-8""" ) as vocab_handle:
            json.dump(target_dict.indices						,      _UpperCamelCase )
         __lowerCamelCase:				Dict     =	WavaVecaCTCTokenizer(
             _UpperCamelCase						,      unk_token=target_dict.unk_word						,      pad_token=target_dict.pad_word						,      bos_token=target_dict.bos_word						,      eos_token=target_dict.eos_word						,      word_delimiter_token="""|"""						,      do_lower_case=_UpperCamelCase						,      )
         __lowerCamelCase:				Dict     =	WavaVecaProcessor(feature_extractor=_UpperCamelCase						,      tokenizer=_UpperCamelCase )
         processor.save_pretrained(_UpperCamelCase )

      __lowerCamelCase:				List[str]     =	SEWForCTC(_UpperCamelCase )
   else:
      __lowerCamelCase:				Optional[int]     =	SEWModel(_UpperCamelCase )
      feature_extractor.save_pretrained(_UpperCamelCase )

   recursively_load_weights(_UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase )

   hf_model.save_pretrained(_UpperCamelCase )


if __name__ == "__main__":
   _A		:				Optional[Any]									=       argparse.ArgumentParser()
   parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
   parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''')
   parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''')
   parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''')
   parser.add_argument(
       '''--is_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not'''
   )
   _A		:				Dict									=       parser.parse_args()
   convert_sew_checkpoint(
       args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, args.is_finetuned
   )






import argparse
from pathlib import Path

import requests
import torch
from PIL import Image

from transformers import (
    RobertaTokenizer,
    TrOCRConfig,
    TrOCRForCausalLM,
    TrOCRProcessor,
    VisionEncoderDecoderModel,
    ViTConfig,
    ViTImageProcessor,
    ViTModel,
)
from transformers.utils import logging


logging.set_verbosity_info()
_A		:				List[Any]									=       logging.get_logger(__name__)



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

   # cls token, position embeddings and patch embeddings of encoder
   rename_keys.extend(
       [
           ("""encoder.deit.cls_token""", """encoder.embeddings.cls_token"""),
           ("""encoder.deit.pos_embed""", """encoder.embeddings.position_embeddings"""),
           ("""encoder.deit.patch_embed.proj.weight""", """encoder.embeddings.patch_embeddings.projection.weight"""),
           ("""encoder.deit.patch_embed.proj.bias""", """encoder.embeddings.patch_embeddings.projection.bias"""),
           ("""encoder.deit.norm.weight""", """encoder.layernorm.weight"""),
           ("""encoder.deit.norm.bias""", """encoder.layernorm.bias"""),
       ] )

   return rename_keys



def     __lowerCAmelCase						(							snake_case	:    str						,      snake_case	:    Union[str, Any] )       ->							Tuple:
   for i in range(encoder_config.num_hidden_layers ):
      # queries, keys and values (only weights, no biases)
      __lowerCamelCase:				Union[str, Any]     =	state_dict.pop(f'encoder.deit.blocks.{i}.attn.qkv.weight' )

      __lowerCamelCase:				Any     =	in_proj_weight[
          : encoder_config.hidden_size, :
      ]
      __lowerCamelCase:				Dict     =	in_proj_weight[
          encoder_config.hidden_size : encoder_config.hidden_size * 2, :
      ]
      __lowerCamelCase:				Any     =	in_proj_weight[
          -encoder_config.hidden_size :, :
      ]



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



def     __lowerCAmelCase						(							snake_case	:    Dict )       ->							Any:
   if "handwritten" in checkpoint_url:
      __lowerCamelCase:				Tuple     =	"""https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg"""  # industry
      # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-12.jpg" # have
      # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02-10.jpg" # let
      # url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg"  #
      # url = "https://fki.tic.heia-fr.ch/static/img/a01-122.jpg"
   elif "printed" in checkpoint_url or "stage1" in checkpoint_url:
      __lowerCamelCase:				Union[str, Any]     =	"""https://www.researchgate.net/profile/Dinh-Sang/publication/338099565/figure/fig8/AS:840413229350922@1577381536857/An-receipt-example-in-the-SROIE-2019-dataset_Q640.jpg"""
   __lowerCamelCase:				Union[str, Any]     =	Image.open(requests.get(snake_case						,      stream=snake_case ).raw ).convert("""RGB""" )
   return im



@torch.no_grad()
def     __lowerCAmelCase						(							snake_case	:    List[Any]						,      snake_case	:    Any )       ->							List[str]:
   __lowerCamelCase:				Union[str, Any]     =	ViTConfig(image_size=384						,      qkv_bias=snake_case )
   __lowerCamelCase:				Optional[int]     =	TrOCRConfig()

   # size of the architecture
   if "base" in checkpoint_url:
      __lowerCamelCase:				Optional[int]     =	768
   elif "large" in checkpoint_url:
      # use ViT-large encoder
      __lowerCamelCase:				Optional[int]     =	1024
      __lowerCamelCase:				int     =	4096
      __lowerCamelCase:				Tuple     =	24
      __lowerCamelCase:				Optional[Any]     =	16
      __lowerCamelCase:				str     =	1024
   else:
      raise ValueError("""Should either find 'base' or 'large' in checkpoint URL""" )

   # the large-printed + stage1 checkpoints uses sinusoidal position embeddings, no layernorm afterwards
   if "large-printed" in checkpoint_url or "stage1" in checkpoint_url:
      __lowerCamelCase:				Any     =	False
      __lowerCamelCase:				Optional[Any]     =	"""relu"""
      __lowerCamelCase:				List[Any]     =	1024
      __lowerCamelCase:				List[str]     =	True
      __lowerCamelCase:				int     =	False
      __lowerCamelCase:				int     =	False

   # load HuggingFace model
   __lowerCamelCase:				Any     =	ViTModel(snake_case						,      add_pooling_layer=snake_case )
   __lowerCamelCase:				Dict     =	TrOCRForCausalLM(snake_case )
   __lowerCamelCase:				Any     =	VisionEncoderDecoderModel(encoder=snake_case						,      decoder=snake_case )
   model.eval()

   # load state_dict of original model, rename some keys
   __lowerCamelCase:				Any     =	torch.hub.load_state_dict_from_url(snake_case						,      map_location="""cpu"""						,      check_hash=snake_case )["""model"""]

   __lowerCamelCase:				Optional[Any]     =	create_rename_keys(snake_case						,      snake_case )
   for src, dest in rename_keys:
      rename_key(snake_case						,      snake_case						,      snake_case )
   read_in_q_k_v(snake_case						,      snake_case )

   # remove parameters we don't need
   del state_dict["encoder.deit.head.weight"]
   del state_dict["encoder.deit.head.bias"]
   del state_dict["decoder.version"]

   # add prefix to decoder keys
   for key, val in state_dict.copy().items():
      __lowerCamelCase:				int     =	state_dict.pop(snake_case )
      if key.startswith("""decoder""" ) and "output_projection" not in key:
         __lowerCamelCase:				Any     =	val
      else:
         __lowerCamelCase:				List[str]     =	val

    # load state dict
   model.load_state_dict(snake_case )

   # Check outputs on an image
   __lowerCamelCase:				Any     =	ViTImageProcessor(size=encoder_config.image_size )
   __lowerCamelCase:				Tuple     =	RobertaTokenizer.from_pretrained("""roberta-large""" )
   __lowerCamelCase:				List[Any]     =	TrOCRProcessor(snake_case						,      snake_case )

   __lowerCamelCase:				Optional[int]     =	processor(images=prepare_img(snake_case )						,      return_tensors="""pt""" ).pixel_values

   # verify logits
   __lowerCamelCase:				Tuple     =	torch.tensor([[model.config.decoder.decoder_start_token_id]] )
   __lowerCamelCase:				Dict     =	model(pixel_values=snake_case						,      decoder_input_ids=snake_case )
   __lowerCamelCase:				Any     =	outputs.logits

   __lowerCamelCase:				Dict     =	torch.Size([1, 1, 50265] )
   if "trocr-base-handwritten" in checkpoint_url:
      __lowerCamelCase:				Tuple     =	torch.tensor(
          [-1.4_502, -4.6_683, -0.5_347, -2.9_291, 9.1_435, -3.0_571, 8.9_764, 1.7_560, 8.7_358, -1.5_311] )
   elif "trocr-large-handwritten" in checkpoint_url:
      __lowerCamelCase:				Optional[int]     =	torch.tensor(
          [-2.6_437, -1.3_129, -2.2_596, -5.3_455, 6.3_539, 1.7_604, 5.4_991, 1.4_702, 5.6_113, 2.0_170] )
   elif "trocr-base-printed" in checkpoint_url:
      __lowerCamelCase:				Union[str, Any]     =	torch.tensor(
          [-5.6_816, -5.8_388, 1.1_398, -6.9_034, 6.8_505, -2.4_393, 1.2_284, -1.0_232, -1.9_661, -3.9_210] )
   elif "trocr-large-printed" in checkpoint_url:
      __lowerCamelCase:				Union[str, Any]     =	torch.tensor(
          [-6.0_162, -7.0_959, 4.4_155, -5.1_063, 7.0_468, -3.1_631, 2.6_466, -0.3_081, -0.8_106, -1.7_535] )

   if "stage1" not in checkpoint_url:
      assert logits.shape == expected_shape, "Shape of logits not as expected"
      assert torch.allclose(logits[0, 0, :10]						,      snake_case						,      atol=1e-3 ), "First elements of logits not as expected"

   Path(snake_case ).mkdir(exist_ok=snake_case )
   print(f'Saving model to {pytorch_dump_folder_path}' )
   model.save_pretrained(snake_case )
   print(f'Saving processor to {pytorch_dump_folder_path}' )
   processor.save_pretrained(snake_case )


if __name__ == "__main__":
   _A		:				int									=       argparse.ArgumentParser()

   parser.add_argument(
       '''--checkpoint_url''',
       default='''https://layoutlm.blob.core.windows.net/trocr/model_zoo/fairseq/trocr-base-handwritten.pt''',
       type=str,
       help='''URL to the original PyTorch checkpoint (.pth file).''',
   )
   parser.add_argument(
       '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the folder to output PyTorch model.'''
   )
   _A		:				Optional[int]									=       parser.parse_args()
   convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)


