

from typing import Optional, Tuple, Union

import flax
import flax.linen as nn
import jax
import jax.numpy as jnp
from flax.core.frozen_dict import FrozenDict

from ..configuration_utils import ConfigMixin, flax_register_to_config
from ..utils import BaseOutput
from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps
from .modeling_flax_utils import FlaxModelMixin
from .unet_ad_blocks_flax import (
    FlaxCrossAttnDownBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
)



@flax.struct.dataclass
class SCREAMING_SNAKE_CASE							(  _snake_case			):
  """simple docstring"""






  lowerCamelCase   :      Optional[Any]										=42
  lowerCamelCase   :      List[str]										=42






class SCREAMING_SNAKE_CASE							(  nn.Module			):
  """simple docstring"""






  lowerCamelCase   :      Optional[Any]										=42
  lowerCamelCase   :      Union[str, Any]										=(16, 32, 96, 256)
  lowerCamelCase   :      Union[str, Any]										=jnp.floataa







  def 	SCREAMING_SNAKE_CASE   (      self			:   Dict   )							->     Any:






     """simple docstring"""







     __lowerCAmelCase  :      str    =						nn.Conv(
         self.block_out_channels[0]	,							kernel_size=(3, 3)	,							padding=((1, 1), (1, 1))	,							dtype=self.dtype	,							)

     __lowerCAmelCase  :      Any    =						[]
     for i in range(len(self.block_out_channels   ) - 1   ):
        __lowerCAmelCase  :      List[Any]    =						self.block_out_channels[i]
        __lowerCAmelCase  :      str    =						self.block_out_channels[i + 1]
        __lowerCAmelCase  :      Optional[Any]    =						nn.Conv(
            UpperCamelCase__	,							kernel_size=(3, 3)	,							padding=((1, 1), (1, 1))	,							dtype=self.dtype	,							)
        blocks.append(UpperCamelCase__   )
        __lowerCAmelCase  :      List[Any]    =						nn.Conv(
            UpperCamelCase__	,							kernel_size=(3, 3)	,							strides=(2, 2)	,							padding=((1, 1), (1, 1))	,							dtype=self.dtype	,							)
        blocks.append(UpperCamelCase__   )
     __lowerCAmelCase  :      Optional[Any]    =						blocks

     __lowerCAmelCase  :      str    =						nn.Conv(
         self.conditioning_embedding_channels	,							kernel_size=(3, 3)	,							padding=((1, 1), (1, 1))	,							kernel_init=nn.initializers.zeros_init()	,							bias_init=nn.initializers.zeros_init()	,							dtype=self.dtype	,							)







  def __call__(      self			:   Any	,							lowerCAmelCase			:   Dict   )							->     int:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[Any]    =						self.conv_in(UpperCamelCase__   )
     __lowerCAmelCase  :      str    =						nn.silu(UpperCamelCase__   )

     for block in self.blocks:
        __lowerCAmelCase  :      List[Any]    =						block(UpperCamelCase__   )
        __lowerCAmelCase  :      List[str]    =						nn.silu(UpperCamelCase__   )

     __lowerCAmelCase  :      Any    =						self.conv_out(UpperCamelCase__   )

     return embedding






@flax_register_to_config
class SCREAMING_SNAKE_CASE							(  nn.Module       ,							_snake_case       ,							_snake_case			):
  """simple docstring"""






  lowerCamelCase   :      str										=32
  lowerCamelCase   :      int										=4
  lowerCamelCase   :      Optional[Any]										=(
      "CrossAttnDownBlock2D",
      "CrossAttnDownBlock2D",
      "CrossAttnDownBlock2D",
      "DownBlock2D",
  )
  lowerCamelCase   :      List[str]										=False
  lowerCamelCase   :      Union[str, Any]										=(320, 640, 1280, 1280)
  lowerCamelCase   :      Dict										=2
  lowerCamelCase   :      str										=8
  lowerCamelCase   :      Dict										=None
  lowerCamelCase   :      Any										=1280
  lowerCamelCase   :      Union[str, Any]										=0.0
  lowerCamelCase   :      Optional[Any]										=False
  lowerCamelCase   :      Optional[Any]										=jnp.floataa
  lowerCamelCase   :      Union[str, Any]										=True
  lowerCamelCase   :      Dict										=0
  lowerCamelCase   :      List[Any]										="rgb"
  lowerCamelCase   :      List[Any]										=(16, 32, 96, 256)







  def 	SCREAMING_SNAKE_CASE   (      self			:   int	,							lowerCAmelCase			:   Dict   )							->     FrozenDict:






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						(1, self.in_channels, self.sample_size, self.sample_size)
     __lowerCAmelCase  :      Optional[int]    =						jnp.zeros(UpperCamelCase__	,							dtype=jnp.floataa   )
     __lowerCAmelCase  :      Optional[int]    =						jnp.ones((1,)	,							dtype=jnp.intaa   )
     __lowerCAmelCase  :      Optional[int]    =						jnp.zeros((1, 1, self.cross_attention_dim)	,							dtype=jnp.floataa   )
     __lowerCAmelCase  :      Optional[int]    =						(1, 3, self.sample_size * 8, self.sample_size * 8)
     __lowerCAmelCase  :      int    =						jnp.zeros(UpperCamelCase__	,							dtype=jnp.floataa   )

     __lowerCAmelCase   ,__lowerCAmelCase  :      Union[str, Any]    =						jax.random.split(UpperCamelCase__   )
     __lowerCAmelCase  :      List[Any]    =						{"""params""": params_rng, """dropout""": dropout_rng}

     return self.init(UpperCamelCase__	,							UpperCamelCase__	,							UpperCamelCase__	,							UpperCamelCase__	,							UpperCamelCase__   )["params"]







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     List[str]:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						self.block_out_channels
     __lowerCAmelCase  :      int    =						block_out_channels[0] * 4

     # If `num_attention_heads` is not defined (which is the case for most models)
     # it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
     # The reason for this behavior is to correct for incorrectly named variables that were introduced
     # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
     # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
     # which is why we correct for the naming here.
     __lowerCAmelCase  :      str    =						self.num_attention_heads or self.attention_head_dim

     # input
     __lowerCAmelCase  :      Dict    =						nn.Conv(
         block_out_channels[0]	,							kernel_size=(3, 3)	,							strides=(1, 1)	,							padding=((1, 1), (1, 1))	,							dtype=self.dtype	,							)

     # time
     __lowerCAmelCase  :      Union[str, Any]    =						FlaxTimesteps(
         block_out_channels[0]	,							flip_sin_to_cos=self.flip_sin_to_cos	,							freq_shift=self.config.freq_shift   )
     __lowerCAmelCase  :      Dict    =						FlaxTimestepEmbedding(UpperCamelCase__	,							dtype=self.dtype   )

     __lowerCAmelCase  :      Any    =						FlaxControlNetConditioningEmbedding(
         conditioning_embedding_channels=block_out_channels[0]	,							block_out_channels=self.conditioning_embedding_out_channels	,							)

     __lowerCAmelCase  :      Any    =						self.only_cross_attention
     if isinstance(UpperCamelCase__	,							UpperCamelCase__   ):
        __lowerCAmelCase  :      Any    =						(only_cross_attention,) * len(self.down_block_types   )

     if isinstance(UpperCamelCase__	,							UpperCamelCase__   ):
        __lowerCAmelCase  :      List[str]    =						(num_attention_heads,) * len(self.down_block_types   )

     # down
     __lowerCAmelCase  :      List[Any]    =						[]
     __lowerCAmelCase  :      Optional[Any]    =						[]

     __lowerCAmelCase  :      Any    =						block_out_channels[0]

     __lowerCAmelCase  :      Union[str, Any]    =						nn.Conv(
         UpperCamelCase__	,							kernel_size=(1, 1)	,							padding="""VALID"""	,							kernel_init=nn.initializers.zeros_init()	,							bias_init=nn.initializers.zeros_init()	,							dtype=self.dtype	,							)
     controlnet_down_blocks.append(UpperCamelCase__   )

     for i, down_block_type in enumerate(self.down_block_types   ):
        __lowerCAmelCase  :      List[Any]    =						output_channel
        __lowerCAmelCase  :      List[str]    =						block_out_channels[i]
        __lowerCAmelCase  :      int    =						i == len(UpperCamelCase__   ) - 1

        if down_block_type == "CrossAttnDownBlock2D":
           __lowerCAmelCase  :      str    =						FlaxCrossAttnDownBlockaD(
               in_channels=UpperCamelCase__	,							out_channels=UpperCamelCase__	,							dropout=self.dropout	,							num_layers=self.layers_per_block	,							num_attention_heads=num_attention_heads[i]	,							add_downsample=not is_final_block	,							use_linear_projection=self.use_linear_projection	,							only_cross_attention=only_cross_attention[i]	,							dtype=self.dtype	,							)
        else:
           __lowerCAmelCase  :      Union[str, Any]    =						FlaxDownBlockaD(
               in_channels=UpperCamelCase__	,							out_channels=UpperCamelCase__	,							dropout=self.dropout	,							num_layers=self.layers_per_block	,							add_downsample=not is_final_block	,							dtype=self.dtype	,							)

        down_blocks.append(UpperCamelCase__   )

        for _ in range(self.layers_per_block   ):
           __lowerCAmelCase  :      int    =						nn.Conv(
               UpperCamelCase__	,							kernel_size=(1, 1)	,							padding="""VALID"""	,							kernel_init=nn.initializers.zeros_init()	,							bias_init=nn.initializers.zeros_init()	,							dtype=self.dtype	,							)
           controlnet_down_blocks.append(UpperCamelCase__   )

        if not is_final_block:
           __lowerCAmelCase  :      str    =						nn.Conv(
               UpperCamelCase__	,							kernel_size=(1, 1)	,							padding="""VALID"""	,							kernel_init=nn.initializers.zeros_init()	,							bias_init=nn.initializers.zeros_init()	,							dtype=self.dtype	,							)
           controlnet_down_blocks.append(UpperCamelCase__   )

     __lowerCAmelCase  :      int    =						down_blocks
     __lowerCAmelCase  :      Any    =						controlnet_down_blocks

     # mid
     __lowerCAmelCase  :      List[str]    =						block_out_channels[-1]
     __lowerCAmelCase  :      int    =						FlaxUNetMidBlockaDCrossAttn(
         in_channels=UpperCamelCase__	,							dropout=self.dropout	,							num_attention_heads=num_attention_heads[-1]	,							use_linear_projection=self.use_linear_projection	,							dtype=self.dtype	,							)

     __lowerCAmelCase  :      List[str]    =						nn.Conv(
         UpperCamelCase__	,							kernel_size=(1, 1)	,							padding="""VALID"""	,							kernel_init=nn.initializers.zeros_init()	,							bias_init=nn.initializers.zeros_init()	,							dtype=self.dtype	,							)







  def __call__(      self			:   Any	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   int = 1.0	,							lowerCAmelCase			:   int = True	,							lowerCAmelCase			:   List[Any] = False	,							)							->     Union[FlaxControlNetOutput, Tuple]:






     """simple docstring"""







     __lowerCAmelCase  :      Tuple    =						self.controlnet_conditioning_channel_order
     if channel_order == "bgr":
        __lowerCAmelCase  :      Optional[int]    =						jnp.flip(UpperCamelCase__	,							axis=1   )

     # 1. time
     if not isinstance(UpperCamelCase__	,							jnp.ndarray   ):
        __lowerCAmelCase  :      Dict    =						jnp.array([timesteps]	,							dtype=jnp.intaa   )
     elif isinstance(UpperCamelCase__	,							jnp.ndarray   ) and len(timesteps.shape   ) == 0:
        __lowerCAmelCase  :      List[str]    =						timesteps.astype(dtype=jnp.floataa   )
        __lowerCAmelCase  :      Tuple    =						jnp.expand_dims(UpperCamelCase__	,							0   )

     __lowerCAmelCase  :      List[Any]    =						self.time_proj(UpperCamelCase__   )
     __lowerCAmelCase  :      Tuple    =						self.time_embedding(UpperCamelCase__   )

     # 2. pre-process
     __lowerCAmelCase  :      Dict    =						jnp.transpose(UpperCamelCase__	,							(0, 2, 3, 1)   )
     __lowerCAmelCase  :      int    =						self.conv_in(UpperCamelCase__   )

     __lowerCAmelCase  :      Union[str, Any]    =						jnp.transpose(UpperCamelCase__	,							(0, 2, 3, 1)   )
     __lowerCAmelCase  :      Union[str, Any]    =						self.controlnet_cond_embedding(UpperCamelCase__   )
     sample += controlnet_cond

     # 3. down
     __lowerCAmelCase  :      Union[str, Any]    =						(sample,)
     for down_block in self.down_blocks:
        if isinstance(UpperCamelCase__	,							UpperCamelCase__   ):
           __lowerCAmelCase   ,__lowerCAmelCase  :      str    =						down_block(UpperCamelCase__	,							UpperCamelCase__	,							UpperCamelCase__	,							deterministic=not train   )
        else:
           __lowerCAmelCase   ,__lowerCAmelCase  :      Any    =						down_block(UpperCamelCase__	,							UpperCamelCase__	,							deterministic=not train   )
        down_block_res_samples += res_samples

     # 4. mid
     __lowerCAmelCase  :      Any    =						self.mid_block(UpperCamelCase__	,							UpperCamelCase__	,							UpperCamelCase__	,							deterministic=not train   )

     # 5. contronet blocks
     __lowerCAmelCase  :      List[str]    =						()
     for down_block_res_sample, controlnet_block in zip(UpperCamelCase__	,							self.controlnet_down_blocks   ):
        __lowerCAmelCase  :      Optional[Any]    =						controlnet_block(UpperCamelCase__   )
        controlnet_down_block_res_samples += (down_block_res_sample,)

     __lowerCAmelCase  :      Optional[int]    =						controlnet_down_block_res_samples

     __lowerCAmelCase  :      Tuple    =						self.controlnet_mid_block(UpperCamelCase__   )

     # 6. scaling
     __lowerCAmelCase  :      Any    =						[sample * conditioning_scale for sample in down_block_res_samples]
     mid_block_res_sample *= conditioning_scale

     if not return_dict:
        return (down_block_res_samples, mid_block_res_sample)

     return FlaxControlNetOutput(
         down_block_res_samples=UpperCamelCase__	,							mid_block_res_sample=UpperCamelCase__   )





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





def      snake_case_				(__A					:						Optional[int] ,       __A					:						Any					)      ->     Any:
   __lowerCAmelCase  :      Union[str, Any]    =						[]
   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      snake_case_				(__A					:						List[str] ,       __A					:						str					)      ->     Optional[Any]:
   for i in range(encoder_config.num_hidden_layers					):
      # queries, keys and values (only weights, no biases)
      __lowerCAmelCase  :      Optional[Any]    =						state_dict.pop(f'''encoder.deit.blocks.{i}.attn.qkv.weight'''					)

      __lowerCAmelCase  :      Tuple    =						in_proj_weight[
          : encoder_config.hidden_size, :
      ]
      __lowerCAmelCase  :      str    =						in_proj_weight[
          encoder_config.hidden_size : encoder_config.hidden_size * 2, :
      ]
      __lowerCAmelCase  :      str    =						in_proj_weight[
          -encoder_config.hidden_size :, :
      ]





def      snake_case_				(__A					:						Union[str, Any] ,       __A					:						str ,       __A					:						Optional[Any]					)      ->     Optional[Any]:
   __lowerCAmelCase  :      Any    =						dct.pop(__A					)
   __lowerCAmelCase  :      str    =						val





def      snake_case_				(__A					:						int					)      ->     Tuple:
   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  :      Optional[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  :      Dict    =						Image.open(requests.get(__A ,       stream=__A					).raw					).convert("""RGB"""					)
   return im





@torch.no_grad()
def      snake_case_				(__A					:						Any ,       __A					:						Union[str, Any]					)      ->     Optional[int]:
   __lowerCAmelCase  :      List[Any]    =						ViTConfig(image_size=3_8_4 ,       qkv_bias=__A					)
   __lowerCAmelCase  :      List[Any]    =						TrOCRConfig()

   # size of the architecture
   if "base" in checkpoint_url:
      __lowerCAmelCase  :      Union[str, Any]    =						7_6_8
   elif "large" in checkpoint_url:
      # use ViT-large encoder
      __lowerCAmelCase  :      Any    =						1_0_2_4
      __lowerCAmelCase  :      Any    =						4_0_9_6
      __lowerCAmelCase  :      Optional[int]    =						2_4
      __lowerCAmelCase  :      str    =						1_6
      __lowerCAmelCase  :      List[Any]    =						1_0_2_4
   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  :      Tuple    =						False
      __lowerCAmelCase  :      Union[str, Any]    =						"""relu"""
      __lowerCAmelCase  :      List[Any]    =						1_0_2_4
      __lowerCAmelCase  :      Any    =						True
      __lowerCAmelCase  :      List[Any]    =						False
      __lowerCAmelCase  :      Dict    =						False

   # load HuggingFace model
   __lowerCAmelCase  :      Dict    =						ViTModel(__A ,       add_pooling_layer=__A					)
   __lowerCAmelCase  :      Union[str, Any]    =						TrOCRForCausalLM(__A					)
   __lowerCAmelCase  :      Any    =						VisionEncoderDecoderModel(encoder=__A ,       decoder=__A					)
   model.eval()

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

   __lowerCAmelCase  :      Any    =						create_rename_keys(__A ,       __A					)
   for src, dest in rename_keys:
      rename_key(__A ,       __A ,       __A					)
   read_in_q_k_v(__A ,       __A					)

   # 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  :      Tuple    =						state_dict.pop(__A					)
      if key.startswith("""decoder"""					) and "output_projection" not in key:
         __lowerCAmelCase  :      str    =						val
      else:
         __lowerCAmelCase  :      Tuple    =						val

    # load state dict
   model.load_state_dict(__A					)

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

   __lowerCAmelCase  :      List[str]    =						processor(images=prepare_img(__A					) ,       return_tensors="""pt"""					).pixel_values

   # verify logits
   __lowerCAmelCase  :      List[str]    =						torch.tensor([[model.config.decoder.decoder_start_token_id]]					)
   __lowerCAmelCase  :      List[str]    =						model(pixel_values=__A ,       decoder_input_ids=__A					)
   __lowerCAmelCase  :      Optional[Any]    =						outputs.logits

   __lowerCAmelCase  :      Union[str, Any]    =						torch.Size([1, 1, 5_0_2_6_5]					)
   if "trocr-base-handwritten" in checkpoint_url:
      __lowerCAmelCase  :      Dict    =						torch.tensor(
          [-1.4502, -4.6683, -0.5347, -2.9291, 9.1435, -3.0571, 8.9764, 1.7560, 8.7358, -1.5311]					)
   elif "trocr-large-handwritten" in checkpoint_url:
      __lowerCAmelCase  :      List[Any]    =						torch.tensor(
          [-2.6437, -1.3129, -2.2596, -5.3455, 6.3539, 1.7604, 5.4991, 1.4702, 5.6113, 2.0170]					)
   elif "trocr-base-printed" in checkpoint_url:
      __lowerCAmelCase  :      Tuple    =						torch.tensor(
          [-5.6816, -5.8388, 1.1398, -6.9034, 6.8505, -2.4393, 1.2284, -1.0232, -1.9661, -3.9210]					)
   elif "trocr-large-printed" in checkpoint_url:
      __lowerCAmelCase  :      List[Any]    =						torch.tensor(
          [-6.0162, -7.0959, 4.4155, -5.1063, 7.0468, -3.1631, 2.6466, -0.3081, -0.8106, -1.7535]					)

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

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


if __name__ == "__main__":
   __UpperCAmelCase								=			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."""
   )
   __UpperCAmelCase								=			parser.parse_args()
   convert_tr_ocr_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)


