

'''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							(					__lowerCAmelCase		:   Tuple  ):

		"""simple docstring"""

		snake_case__    :							Dict							    =	model.config

		snake_case__    :							Optional[Any]							    =	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 ,				)
		snake_case__    :							List[str]							    =	MBartConfig(
		    is_decoder=_lowerCAmelCase ,				is_encoder_decoder=_lowerCAmelCase ,				add_cross_attention=_lowerCAmelCase ,				decoder_layers=original_config.decoder_layer ,				max_position_embeddings=original_config.max_position_embeddings ,				vocab_size=len(
		        model.decoder.tokenizer  ) ,				scale_embedding=_lowerCAmelCase ,				add_final_layer_norm=_lowerCAmelCase ,				)

		return encoder_config, decoder_config




def 						_a							(					__lowerCAmelCase		:   Optional[Any]  ):

		"""simple docstring"""

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

				if name == "encoder.norm.weight":
						snake_case__    :							List[Any]							    =	'''encoder.layernorm.weight'''
				if name == "encoder.norm.bias":
						snake_case__    :							str							    =	'''encoder.layernorm.bias'''

		return name




def 						_a							(					__lowerCAmelCase		:   Tuple ,				__lowerCAmelCase		:   Tuple  ):

		"""simple docstring"""

		for key in orig_state_dict.copy().keys():
				snake_case__    :							Optional[Any]							    =	orig_state_dict.pop(_lowerCAmelCase  )

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

						if "weight" in key:
								snake_case__    :							str							    =	val[:dim, :]
								snake_case__    :							int							    =	val[dim : dim * 2, :]
								snake_case__    :							int							    =	val[-dim:, :]
						else:
								snake_case__    :							Optional[Any]							    =	val[:dim]
								snake_case__    :							Union[str, Any]							    =	val[dim : dim * 2]
								snake_case__    :							List[Any]							    =	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:
						snake_case__    :							List[str]							    =	val

		return orig_state_dict




def 						_a							(					__lowerCAmelCase		:   Tuple ,				__lowerCAmelCase		:   Union[str, Any]=None ,				__lowerCAmelCase		:   Dict=False  ):

		"""simple docstring"""

		snake_case__    :							Tuple							    =	DonutModel.from_pretrained(_lowerCAmelCase  ).eval()

		# load HuggingFace model
		snake_case__    ,							snake_case__    :							Optional[Any]							    =	get_configs(_lowerCAmelCase  )
		snake_case__    :							Dict							    =	DonutSwinModel(_lowerCAmelCase  )
		snake_case__    :							str							    =	MBartForCausalLM(_lowerCAmelCase  )
		snake_case__    :							Dict							    =	VisionEncoderDecoderModel(encoder=_lowerCAmelCase ,				decoder=_lowerCAmelCase  )
		model.eval()

		snake_case__    :							List[Any]							    =	original_model.state_dict()
		snake_case__    :							Tuple							    =	convert_state_dict(_lowerCAmelCase ,				_lowerCAmelCase  )
		model.load_state_dict(_lowerCAmelCase  )

		# verify results on scanned document
		snake_case__    :							List[str]							    =	load_dataset('''hf-internal-testing/example-documents'''  )
		snake_case__    :							Union[str, Any]							    =	dataset['''test'''][0]['''image'''].convert('''RGB'''  )

		snake_case__    :							Optional[Any]							    =	XLMRobertaTokenizerFast.from_pretrained(_lowerCAmelCase ,				from_slow=_lowerCAmelCase  )
		snake_case__    :							Optional[int]							    =	DonutImageProcessor(
		    do_align_long_axis=original_model.config.align_long_axis ,				size=original_model.config.input_size[::-1]  )
		snake_case__    :							str							    =	DonutProcessor(_lowerCAmelCase ,				_lowerCAmelCase  )
		snake_case__    :							Dict							    =	processor(_lowerCAmelCase ,				return_tensors='''pt'''  ).pixel_values

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

		snake_case__    :							Optional[int]							    =	original_model.encoder.model.patch_embed(_lowerCAmelCase  )
		snake_case__    ,							snake_case__    :							List[Any]							    =	model.encoder.embeddings(_lowerCAmelCase  )
		assert torch.allclose(_lowerCAmelCase ,				_lowerCAmelCase ,				atol=1e-3  )

		# verify encoder hidden states
		snake_case__    :							int							    =	original_model.encoder(_lowerCAmelCase  )
		snake_case__    :							Optional[int]							    =	model.encoder(_lowerCAmelCase  ).last_hidden_state
		assert torch.allclose(_lowerCAmelCase ,				_lowerCAmelCase ,				atol=1e-2  )

		# verify decoder hidden states
		snake_case__    :							Union[str, Any]							    =	original_model(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase  ).logits
		snake_case__    :							List[Any]							    =	model(_lowerCAmelCase ,				decoder_input_ids=_lowerCAmelCase  ).logits
		assert torch.allclose(_lowerCAmelCase ,				_lowerCAmelCase ,				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(_lowerCAmelCase  )
				processor.save_pretrained(_lowerCAmelCase  )

		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__    :							Any					    =    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__    :							Optional[int]					    =    parser.parse_args()
					convert_donut_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)







def 						_lowerCAmelCase							(     _lowerCAmelCase = 1000			)    ->						int:


						'''simple docstring'''






						__snake_case				=		2**power
						__snake_case				=		str(_lowerCAmelCase			)
						__snake_case				=		list(_lowerCAmelCase			)
						__snake_case				=		0

						for i in list_num:
												sum_of_num += int(_lowerCAmelCase			)

						return sum_of_num


if __name__ == "__main__":
	A				:       Optional[Any]      =  int(input('Enter the power of 2: ').strip())
	print('2 ^ ', power, ' = ', 2**power)
	A				:       List[Any]      =  solution(power)
	print('Sum of the digits is: ', result)





