


"""simple docstring"""







import argparse

import torch
from datasets import load_dataset
from donut import DonutModel

from transformers import (
    DonutImageProcessor,
    DonutProcessor,
    DonutSwinConfig,
    DonutSwinModel,
    MBartConfig,
    MBartForCausalLM,
    VisionEncoderDecoderModel,
    XLMRobertaTokenizerFast,
)
def _A						(						lowercase		):







    """simple docstring"""
    a 		=model.config

    a 		=DonutSwinConfig(
        image_size=original_config.input_size       ,    patch_size=4       ,    depths=original_config.encoder_layer       ,    num_heads=[4, 8, 16, 32]       ,    window_size=original_config.window_size       ,    embed_dim=1_28       ,    )
    a 		=MBartConfig(
        is_decoder=lowercase       ,    is_encoder_decoder=lowercase       ,    add_cross_attention=lowercase       ,    decoder_layers=original_config.decoder_layer       ,    max_position_embeddings=original_config.max_position_embeddings       ,    vocab_size=len(
            model.decoder.tokenizer		)       ,    scale_embedding=lowercase       ,    add_final_layer_norm=lowercase       ,    )

    return encoder_config, decoder_config
def _A						(						lowercase		):







    """simple docstring"""



    if "encoder.model" in name:
        a 		=name.replace('''encoder.model'''       ,    '''encoder'''		)
    if "decoder.model" in name:
        a 		=name.replace('''decoder.model'''       ,    '''decoder'''		)
    if "patch_embed.proj" in name:
        a 		=name.replace('''patch_embed.proj'''       ,    '''embeddings.patch_embeddings.projection'''		)
    if "patch_embed.norm" in name:
        a 		=name.replace('''patch_embed.norm'''       ,    '''embeddings.norm'''		)
    if name.startswith('''encoder'''		):
        if "layers" in name:
            a 		="""encoder.""" + name
        if "attn.proj" in name:
            a 		=name.replace('''attn.proj'''       ,    '''attention.output.dense'''		)
        if "attn" in name and "mask" not in name:
            a 		=name.replace('''attn'''       ,    '''attention.self'''		)
        if "norm1" in name:
            a 		=name.replace('''norm1'''       ,    '''layernorm_before'''		)
        if "norm2" in name:
            a 		=name.replace('''norm2'''       ,    '''layernorm_after'''		)
        if "mlp.fc1" in name:
            a 		=name.replace('''mlp.fc1'''       ,    '''intermediate.dense'''		)
        if "mlp.fc2" in name:
            a 		=name.replace('''mlp.fc2'''       ,    '''output.dense'''		)

        if name == "encoder.norm.weight":
            a 		="""encoder.layernorm.weight"""
        if name == "encoder.norm.bias":
            a 		="""encoder.layernorm.bias"""

    return name
def _A						(						lowercase       ,    lowercase		):







    """simple docstring"""



    for key in orig_state_dict.copy().keys():
        a 		=orig_state_dict.pop(lowercase		)

        if "qkv" in key:
            a 		=key.split('''.'''		)
            a 		=int(key_split[3]		)
            a 		=int(key_split[5]		)
            a 		=model.encoder.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size

            if "weight" in key:
                a 		=val[:dim, :]
                a 		=val[dim : dim * 2, :]
                a 		=val[-dim:, :]
            else:
                a 		=val[:dim]
                a 		=val[dim : dim * 2]
                a 		=val[-dim:]
        elif "attn_mask" in key or key in ["encoder.model.norm.weight", "encoder.model.norm.bias"]:
            # HuggingFace implementation doesn't use attn_mask buffer
            # and model doesn't use final LayerNorms for the encoder
            pass
        else:
            a 		=val

    return orig_state_dict
def _A						(						lowercase       ,    lowercase=None       ,    lowercase=False		):







    """simple docstring"""
    a 		=DonutModel.from_pretrained(lowercase		).eval()

    # load HuggingFace model
    a 		=get_configs(lowercase		)
    a 		=DonutSwinModel(lowercase		)
    a 		=MBartForCausalLM(lowercase		)
    a 		=VisionEncoderDecoderModel(encoder=lowercase       ,    decoder=lowercase		)
    model.eval()

    a 		=original_model.state_dict()
    a 		=convert_state_dict(lowercase       ,    lowercase		)
    model.load_state_dict(lowercase		)

    # verify results on scanned document
    a 		=load_dataset('''hf-internal-testing/example-documents'''		)
    a 		=dataset["""test"""][0]["""image"""].convert('''RGB'''		)

    a 		=XLMRobertaTokenizerFast.from_pretrained(lowercase       ,    from_slow=lowercase		)
    a 		=DonutImageProcessor(
        do_align_long_axis=original_model.config.align_long_axis       ,    size=original_model.config.input_size[::-1]		)
    a 		=DonutProcessor(lowercase       ,    lowercase		)
    a 		=processor(lowercase       ,    return_tensors='''pt'''		).pixel_values

    if model_name == "naver-clova-ix/donut-base-finetuned-docvqa":
        a 		="""<s_docvqa><s_question>{user_input}</s_question><s_answer>"""
        a 		="""When is the coffee break?"""
        a 		=task_prompt.replace('''{user_input}'''       ,    lowercase		)
    elif model_name == "naver-clova-ix/donut-base-finetuned-rvlcdip":
        a 		="""<s_rvlcdip>"""
    elif model_name in [
        "naver-clova-ix/donut-base-finetuned-cord-v1",
        "naver-clova-ix/donut-base-finetuned-cord-v1-2560",
    ]:
        a 		="""<s_cord>"""
    elif model_name == "naver-clova-ix/donut-base-finetuned-cord-v2":
        a 		="""s_cord-v2>"""
    elif model_name == "naver-clova-ix/donut-base-finetuned-zhtrainticket":
        a 		="""<s_zhtrainticket>"""
    elif model_name in ["naver-clova-ix/donut-proto", "naver-clova-ix/donut-base"]:
        # use a random prompt
        a 		="""hello world"""
    else:
        raise ValueError('''Model name not supported'''		)
    a 		=original_model.decoder.tokenizer(lowercase       ,    add_special_tokens=lowercase       ,    return_tensors='''pt'''		)[
        """input_ids"""
    ]

    a 		=original_model.encoder.model.patch_embed(lowercase		)
    a 		=model.encoder.embeddings(lowercase		)
    assert torch.allclose(lowercase       ,    lowercase       ,    atol=1E-3		)

    # verify encoder hidden states
    a 		=original_model.encoder(lowercase		)
    a 		=model.encoder(lowercase		).last_hidden_state
    assert torch.allclose(lowercase       ,    lowercase       ,    atol=1E-2		)

    # verify decoder hidden states
    a 		=original_model(lowercase       ,    lowercase       ,    lowercase		).logits
    a 		=model(lowercase       ,    decoder_input_ids=lowercase		).logits
    assert torch.allclose(lowercase       ,    lowercase       ,    atol=1E-3		)
    print('''Looks ok!'''		)

    if pytorch_dump_folder_path is not None:
        print(f'''Saving model and processor to {pytorch_dump_folder_path}'''		)
        model.save_pretrained(lowercase		)
        processor.save_pretrained(lowercase		)

    if push_to_hub:
        model.push_to_hub('''nielsr/''' + model_name.split('''/'''		)[-1]       ,    commit_message='''Update model'''		)
        processor.push_to_hub('''nielsr/''' + model_name.split('''/'''		)[-1]       ,    commit_message='''Update model'''		)


if __name__ == "__main__":
       lowerCamelCase_   :   int									=  argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           """--model_name""",
           default="""naver-clova-ix/donut-base-finetuned-docvqa""",
           required=False,
           type=str,
           help="""Name of the original model you\'d like to convert.""",
       )
       parser.add_argument(
           """--pytorch_dump_folder_path""",
           default=None,
           required=False,
           type=str,
           help="""Path to the output PyTorch model directory.""",
       )
       parser.add_argument(
           """--push_to_hub""",
           action="""store_true""",
           help="""Whether or not to push the converted model and processor to the 🤗 hub.""",
       )

       lowerCamelCase_   :   List[Any]									=  parser.parse_args()
       convert_donut_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)







import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()

class     _SCREAMING_SNAKE_CASE       (			_a		,   _a		,   unittest.TestCase      ):
	snake_case__							:     Any     	=  StableDiffusionXLImgaImgPipeline
	snake_case__							:     Tuple     	=  TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width"""}
	snake_case__							:     Tuple     	=  PipelineTesterMixin.required_optional_params - {"""latents"""}
	snake_case__							:     Any     	=  TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
	snake_case__							:     List[str]     	=  IMAGE_TO_IMAGE_IMAGE_PARAMS
	snake_case__							:     Tuple     	=  IMAGE_TO_IMAGE_IMAGE_PARAMS





	def 						_A (		self				:					int	):
		torch.manual_seed(0	)
		UpperCamelCase     :Any          =	UNetaDConditionModel(
		    block_out_channels=(32, 64) ,  layers_per_block=2 ,  sample_size=32 ,  in_channels=4 ,  out_channels=4 ,  down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""") ,  up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""") ,  attention_head_dim=(2, 4) ,  use_linear_projection=__lowerCamelCase ,  addition_embed_type="""text_time""" ,  addition_time_embed_dim=8 ,  transformer_layers_per_block=(1, 2) ,  projection_class_embeddings_input_dim=80 ,  cross_attention_dim=64 ,  )
		UpperCamelCase     :Tuple          =	EulerDiscreteScheduler(
		    beta_start=0.00085 ,  beta_end=0.012 ,  steps_offset=1 ,  beta_schedule="""scaled_linear""" ,  timestep_spacing="""leading""" ,  )
		torch.manual_seed(0	)
		UpperCamelCase     :Union[str, Any]          =	AutoencoderKL(
		    block_out_channels=[32, 64] ,  in_channels=3 ,  out_channels=3 ,  down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""] ,  up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""] ,  latent_channels=4 ,  sample_size=128 ,  )
		torch.manual_seed(0	)
		UpperCamelCase     :Optional[int]          =	CLIPTextConfig(
		    bos_token_id=0 ,  eos_token_id=2 ,  hidden_size=32 ,  intermediate_size=37 ,  layer_norm_eps=1E-05 ,  num_attention_heads=4 ,  num_hidden_layers=5 ,  pad_token_id=1 ,  vocab_size=1_000 ,  hidden_act="""gelu""" ,  projection_dim=32 ,  )
		UpperCamelCase     :Any          =	CLIPTextModel(__lowerCamelCase	)
		UpperCamelCase     :List[Any]          =	CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ,  local_files_only=__lowerCamelCase	)

		UpperCamelCase     :List[Any]          =	CLIPTextModelWithProjection(__lowerCamelCase	)
		UpperCamelCase     :int          =	CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip""" ,  local_files_only=__lowerCamelCase	)

		UpperCamelCase     :Union[str, Any]          =	{
		    """unet""": unet,
		    """scheduler""": scheduler,
		    """vae""": vae,
		    """text_encoder""": text_encoder,
		    """tokenizer""": tokenizer,
		    """text_encoder_2""": text_encoder_a,
		    """tokenizer_2""": tokenizer_a,
		    # "safety_checker": None,
		    # "feature_extractor": None,
		}
		return components





	def 						_A (		self				:					Tuple ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					Optional[Any]=0	):
		UpperCamelCase     :Tuple          =	floats_tensor((1, 3, 32, 32) ,  rng=random.Random(__lowerCamelCase	)	).to(__lowerCamelCase	)
		UpperCamelCase     :List[str]          =	image / 2 + 0.5
		if str(__lowerCamelCase	).startswith("""mps"""	):
			UpperCamelCase     :Any          =	torch.manual_seed(__lowerCamelCase	)
		else:
			UpperCamelCase     :List[Any]          =	torch.Generator(device=__lowerCamelCase	).manual_seed(__lowerCamelCase	)
		UpperCamelCase     :str          =	{
		    """prompt""": """A painting of a squirrel eating a burger""",
		    """image""": image,
		    """generator""": generator,
		    """num_inference_steps""": 2,
		    """guidance_scale""": 5.0,
		    """output_type""": """numpy""",
		    """strength""": 0.75,
		}
		return inputs





	def 						_A (		self				:					str	):
		UpperCamelCase     :List[str]          =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
		UpperCamelCase     :Optional[Any]          =	self.get_dummy_components()
		UpperCamelCase     :List[Any]          =	StableDiffusionXLImgaImgPipeline(**__lowerCamelCase	)
		UpperCamelCase     :Any          =	sd_pipe.to(__lowerCamelCase	)
		sd_pipe.set_progress_bar_config(disable=__lowerCamelCase	)

		UpperCamelCase     :Optional[Any]          =	self.get_dummy_inputs(__lowerCamelCase	)
		UpperCamelCase     :Union[str, Any]          =	sd_pipe(**__lowerCamelCase	).images
		UpperCamelCase     :Union[str, Any]          =	image[0, -3:, -3:, -1]

		assert image.shape == (1, 32, 32, 3)

		UpperCamelCase     :List[Any]          =	np.array([0.4656, 0.4840, 0.4439, 0.6698, 0.5574, 0.4524, 0.5799, 0.5943, 0.5165]	)

		assert np.abs(image_slice.flatten() - expected_slice	).max() < 1E-2





	def 						_A (		self				:					Dict	):
		super().test_attention_slicing_forward_pass(expected_max_diff=3E-3	)





	def 						_A (		self				:					Optional[Any]	):
		super().test_inference_batch_single_identical(expected_max_diff=3E-3	)





	def 						_A (		self				:					Union[str, Any]	):
		pass





	def 						_A (		self				:					Optional[int]	):
		UpperCamelCase     :Union[str, Any]          =	self.get_dummy_components()
		UpperCamelCase     :Dict          =	StableDiffusionXLImgaImgPipeline(**__lowerCamelCase	)
		UpperCamelCase     :List[Any]          =	sd_pipe.to(__lowerCamelCase	)
		UpperCamelCase     :List[str]          =	sd_pipe.to(__lowerCamelCase	)
		sd_pipe.set_progress_bar_config(disable=__lowerCamelCase	)

		# forward without prompt embeds
		UpperCamelCase     :List[Any]          =	self.get_dummy_inputs(__lowerCamelCase	)
		UpperCamelCase     :int          =	3 * ["""this is a negative prompt"""]
		UpperCamelCase     :Union[str, Any]          =	negative_prompt
		UpperCamelCase     :Union[str, Any]          =	3 * [inputs["""prompt"""]]

		UpperCamelCase     :Dict          =	sd_pipe(**__lowerCamelCase	)
		UpperCamelCase     :Union[str, Any]          =	output.images[0, -3:, -3:, -1]

		# forward with prompt embeds
		UpperCamelCase     :Union[str, Any]          =	self.get_dummy_inputs(__lowerCamelCase	)
		UpperCamelCase     :Optional[int]          =	3 * ["""this is a negative prompt"""]
		UpperCamelCase     :Union[str, Any]          =	3 * [inputs.pop("""prompt"""	)]

		(
		    (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      
		)     :Union[str, Any]          =	sd_pipe.encode_prompt(__lowerCamelCase ,  negative_prompt=__lowerCamelCase	)

		UpperCamelCase     :Dict          =	sd_pipe(
		    **__lowerCamelCase ,  prompt_embeds=__lowerCamelCase ,  negative_prompt_embeds=__lowerCamelCase ,  pooled_prompt_embeds=__lowerCamelCase ,  negative_pooled_prompt_embeds=__lowerCamelCase ,  )
		UpperCamelCase     :Union[str, Any]          =	output.images[0, -3:, -3:, -1]

		# make sure that it's equal
		assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()	).max() < 1E-4

@slow
@require_torch_gpu
class     _SCREAMING_SNAKE_CASE       (			unittest.TestCase      ):





	def 						_A (		self				:					Tuple	):
		super().tearDown()
		gc.collect()
		torch.cuda.empty_cache()





	def 						_A (		self				:					List[Any] ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					Dict="cpu" ,  __lowerCamelCase				:					List[Any]=torch.floataa ,  __lowerCamelCase				:					Tuple=0	):
		UpperCamelCase     :Optional[int]          =	torch.Generator(device=__lowerCamelCase	).manual_seed(__lowerCamelCase	)
		UpperCamelCase     :Optional[Any]          =	np.random.RandomState(__lowerCamelCase	).standard_normal((1, 4, 64, 64)	)
		UpperCamelCase     :Dict          =	torch.from_numpy(__lowerCamelCase	).to(device=__lowerCamelCase ,  dtype=__lowerCamelCase	)
		UpperCamelCase     :str          =	{
		    """prompt""": """a photograph of an astronaut riding a horse""",
		    """latents""": latents,
		    """generator""": generator,
		    """num_inference_steps""": 3,
		    """guidance_scale""": 7.5,
		    """output_type""": """numpy""",
		}
		return inputs





	def 						_A (		self				:					Optional[Any]	):
		UpperCamelCase     :Any          =	DiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-base"""	)
		pipe.to(__lowerCamelCase	)
		pipe.set_progress_bar_config(disable=__lowerCamelCase	)

		UpperCamelCase     :Optional[Any]          =	self.get_inputs(__lowerCamelCase	)
		UpperCamelCase     :Optional[int]          =	pipe(**__lowerCamelCase	).images
		UpperCamelCase     :Dict          =	image[0, -3:, -3:, -1].flatten()

		assert image.shape == (1, 512, 512, 3)
		UpperCamelCase     :Union[str, Any]          =	np.array([0.49493, 0.47896, 0.40798, 0.54214, 0.53212, 0.48202, 0.47656, 0.46329, 0.48506]	)
		assert np.abs(image_slice - expected_slice	).max() < 7E-3
