
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			)       ->  Dict:
		'''simple docstring'''

		UpperCamelCase          =					model.config

		UpperCamelCase          =					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=128   ,	)
		UpperCamelCase          =					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			)       ->  Optional[Any]:
		'''simple docstring'''

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

				if name == "encoder.norm.weight":
						UpperCamelCase          =					'encoder.layernorm.weight'
				if name == "encoder.norm.bias":
						UpperCamelCase          =					'encoder.layernorm.bias'

		return name


def 				A	( lowercase   ,	lowercase			)       ->  List[str]:
		'''simple docstring'''

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

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

						if "weight" in key:
								UpperCamelCase          =					val[:dim, :]
								UpperCamelCase          =					val[dim : dim * 2, :]
								UpperCamelCase          =					val[-dim:, :]
						else:
								UpperCamelCase          =					val[:dim]
								UpperCamelCase          =					val[dim : dim * 2]
								UpperCamelCase          =					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:
						UpperCamelCase          =					val

		return orig_state_dict


def 				A	( lowercase   ,	lowercase=None   ,	lowercase=False			)       ->  List[Any]:
		'''simple docstring'''

		UpperCamelCase          =					DonutModel.from_pretrained(lowercase			).eval()

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

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

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

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

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

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

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

		# verify decoder hidden states
		UpperCamelCase          =					original_model(lowercase   ,	lowercase   ,	lowercase			).logits
		UpperCamelCase          =					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__":
					_UpperCAmelCase :			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.",
					)

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


import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
					    MODEL_FOR_QUESTION_ANSWERING_MAPPING,
					    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
					    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
					    LayoutLMvaConfig,
					    LayoutLMvaForQuestionAnswering,
					    LayoutLMvaForSequenceClassification,
					    LayoutLMvaForTokenClassification,
					    LayoutLMvaModel,
					)
					from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
					from PIL import Image

					from transformers import LayoutLMvaImageProcessor


class     lowercase :



		def __init__(			self    ,				A_    ,				A_=2    ,				A_=3    ,				A_=4    ,				A_=2    ,				A_=7    ,				A_=True    ,				A_=True    ,				A_=True    ,				A_=True    ,				A_=99    ,				A_=36    ,				A_=3    ,				A_=4    ,				A_=37    ,				A_="gelu"    ,				A_=0.1    ,				A_=0.1    ,				A_=512    ,				A_=16    ,				A_=2    ,				A_=0.02    ,				A_=6    ,				A_=6    ,				A_=3    ,				A_=4    ,				A_=None    ,				A_=1_000    ,				)  -> str:
				"""simple docstring"""
				UpperCamelCase          =					parent
				UpperCamelCase          =					batch_size
				UpperCamelCase          =					num_channels
				UpperCamelCase          =					image_size
				UpperCamelCase          =					patch_size
				UpperCamelCase          =					text_seq_length
				UpperCamelCase          =					is_training
				UpperCamelCase          =					use_input_mask
				UpperCamelCase          =					use_token_type_ids
				UpperCamelCase          =					use_labels
				UpperCamelCase          =					vocab_size
				UpperCamelCase          =					hidden_size
				UpperCamelCase          =					num_hidden_layers
				UpperCamelCase          =					num_attention_heads
				UpperCamelCase          =					intermediate_size
				UpperCamelCase          =					hidden_act
				UpperCamelCase          =					hidden_dropout_prob
				UpperCamelCase          =					attention_probs_dropout_prob
				UpperCamelCase          =					max_position_embeddings
				UpperCamelCase          =					type_vocab_size
				UpperCamelCase          =					type_sequence_label_size
				UpperCamelCase          =					initializer_range
				UpperCamelCase          =					coordinate_size
				UpperCamelCase          =					shape_size
				UpperCamelCase          =					num_labels
				UpperCamelCase          =					num_choices
				UpperCamelCase          =					scope
				UpperCamelCase          =					range_bbox

				# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
				UpperCamelCase          =					text_seq_length
				UpperCamelCase          =					(image_size // patch_size) ** 2 + 1
				UpperCamelCase          =					self.text_seq_length + self.image_seq_length



		def __UpperCamelCase			(			self	)  -> Optional[int]:
				"""simple docstring"""
				UpperCamelCase          =					ids_tensor([self.batch_size, self.text_seq_length]    ,				self.vocab_size	)

				UpperCamelCase          =					ids_tensor([self.batch_size, self.text_seq_length, 4]    ,				self.range_bbox	)
				# Ensure that bbox is legal
				for i in range(bbox.shape[0]	):
						for j in range(bbox.shape[1]	):
								if bbox[i, j, 3] < bbox[i, j, 1]:
										UpperCamelCase          =					bbox[i, j, 3]
										UpperCamelCase          =					bbox[i, j, 1]
										UpperCamelCase          =					t
								if bbox[i, j, 2] < bbox[i, j, 0]:
										UpperCamelCase          =					bbox[i, j, 2]
										UpperCamelCase          =					bbox[i, j, 0]
										UpperCamelCase          =					t

				UpperCamelCase          =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

				UpperCamelCase          =					None
				if self.use_input_mask:
						UpperCamelCase          =					random_attention_mask([self.batch_size, self.text_seq_length]	)

				UpperCamelCase          =					None
				if self.use_token_type_ids:
						UpperCamelCase          =					ids_tensor([self.batch_size, self.text_seq_length]    ,				self.type_vocab_size	)

				UpperCamelCase          =					None
				UpperCamelCase          =					None
				if self.use_labels:
						UpperCamelCase          =					ids_tensor([self.batch_size]    ,				self.type_sequence_label_size	)
						UpperCamelCase          =					ids_tensor([self.batch_size, self.text_seq_length]    ,				self.num_labels	)

				UpperCamelCase          =					LayoutLMvaConfig(
				    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    ,				initializer_range=self.initializer_range    ,				coordinate_size=self.coordinate_size    ,				shape_size=self.shape_size    ,				input_size=self.image_size    ,				patch_size=self.patch_size    ,				)

				return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_	)  -> int:
				"""simple docstring"""
				UpperCamelCase          =					LayoutLMvaModel(config=A_	)
				model.to(A_	)
				model.eval()

				# text + image
				UpperCamelCase          =					model(A_    ,				pixel_values=A_	)
				UpperCamelCase          =					model(
				    A_    ,				bbox=A_    ,				pixel_values=A_    ,				attention_mask=A_    ,				token_type_ids=A_	)
				UpperCamelCase          =					model(A_    ,				bbox=A_    ,				pixel_values=A_    ,				token_type_ids=A_	)
				UpperCamelCase          =					model(A_    ,				bbox=A_    ,				pixel_values=A_	)

				self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.hidden_size)	)

				# text only
				UpperCamelCase          =					model(A_	)
				self.parent.assertEqual(
				    result.last_hidden_state.shape    ,				(self.batch_size, self.text_seq_length, self.hidden_size)	)

				# image only
				UpperCamelCase          =					model(pixel_values=A_	)
				self.parent.assertEqual(
				    result.last_hidden_state.shape    ,				(self.batch_size, self.image_seq_length, self.hidden_size)	)



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_	)  -> Optional[Any]:
				"""simple docstring"""
				UpperCamelCase          =					self.num_labels
				UpperCamelCase          =					LayoutLMvaForSequenceClassification(A_	)
				model.to(A_	)
				model.eval()
				UpperCamelCase          =					model(
				    A_    ,				bbox=A_    ,				pixel_values=A_    ,				attention_mask=A_    ,				token_type_ids=A_    ,				labels=A_    ,				)
				self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.num_labels)	)



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_	)  -> Dict:
				"""simple docstring"""
				UpperCamelCase          =					self.num_labels
				UpperCamelCase          =					LayoutLMvaForTokenClassification(config=A_	)
				model.to(A_	)
				model.eval()
				UpperCamelCase          =					model(
				    A_    ,				bbox=A_    ,				pixel_values=A_    ,				attention_mask=A_    ,				token_type_ids=A_    ,				labels=A_    ,				)
				self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.text_seq_length, self.num_labels)	)



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_	)  -> List[str]:
				"""simple docstring"""
				UpperCamelCase          =					LayoutLMvaForQuestionAnswering(config=A_	)
				model.to(A_	)
				model.eval()
				UpperCamelCase          =					model(
				    A_    ,				bbox=A_    ,				pixel_values=A_    ,				attention_mask=A_    ,				token_type_ids=A_    ,				start_positions=A_    ,				end_positions=A_    ,				)
				self.parent.assertEqual(result.start_logits.shape    ,				(self.batch_size, self.seq_length)	)
				self.parent.assertEqual(result.end_logits.shape    ,				(self.batch_size, self.seq_length)	)



		def __UpperCamelCase			(			self	)  -> int:
				"""simple docstring"""
				UpperCamelCase          =					self.prepare_config_and_inputs()
				(
				    (
				    UpperCamelCase
				)     ,  (
				    UpperCamelCase
				)     ,  (
				    UpperCamelCase
				)     ,  (
				    UpperCamelCase
				)     ,  (
				    UpperCamelCase
				)     ,  (
				    UpperCamelCase
				)     ,  (
				    UpperCamelCase
				)     ,  (
				    UpperCamelCase
				)     ,  
				)          =					config_and_inputs
				UpperCamelCase          =					{
				    'input_ids': input_ids,
				    'bbox': bbox,
				    'pixel_values': pixel_values,
				    'token_type_ids': token_type_ids,
				    'attention_mask': input_mask,
				}
				return config, inputs_dict






@require_torch
class     lowercase (		_SCREAMING_SNAKE_CASE  ,       _SCREAMING_SNAKE_CASE  ,       unittest.TestCase       ):
		__lowercase	:	Tuple     					=    False
		__lowercase	:	List[Any]     					=    False
		__lowercase	:	str     					=    False

		__lowercase	:	Tuple     					=    (
		    (
		        LayoutLMvaModel,
		        LayoutLMvaForSequenceClassification,
		        LayoutLMvaForTokenClassification,
		        LayoutLMvaForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		__lowercase	:	List[str]     					=    (
		    {"document-question-answering": LayoutLMvaForQuestionAnswering, "feature-extraction": LayoutLMvaModel}
		    if is_torch_available()
		    else {}
		)



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				A_    ,				A_    ,				A_	)  -> int:
				"""simple docstring"""
				# `DocumentQuestionAnsweringPipeline` is expected to work with this model, but it combines the text and visual
				# embedding along the sequence dimension (dim 1), which causes an error during post-processing as `p_mask` has
				# the sequence dimension of the text embedding only.
				# (see the line `embedding_output = torch.cat([embedding_output, visual_embeddings], dim=1)`)
				return True



		def __UpperCamelCase			(			self	)  -> Any:
				"""simple docstring"""
				UpperCamelCase          =					LayoutLMvaModelTester(self	)
				UpperCamelCase          =					ConfigTester(self    ,				config_class=A_    ,				hidden_size=37	)



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				A_=False	)  -> int:
				"""simple docstring"""
				UpperCamelCase          =					copy.deepcopy(A_	)
				if model_class in get_values(A_	):
						UpperCamelCase          =					{
						    k: v.unsqueeze(1	).expand(-1    ,				self.model_tester.num_choices    ,				-1	).contiguous()
						    if isinstance(A_    ,				torch.Tensor	) and v.ndim > 1
						    else v
						    for k, v in inputs_dict.items()
						}
				if return_labels:
						if model_class in get_values(A_	):
								UpperCamelCase          =					torch.ones(self.model_tester.batch_size    ,				dtype=torch.long    ,				device=A_	)
						elif model_class in get_values(A_	):
								UpperCamelCase          =					torch.zeros(
								    self.model_tester.batch_size    ,				dtype=torch.long    ,				device=A_	)
								UpperCamelCase          =					torch.zeros(
								    self.model_tester.batch_size    ,				dtype=torch.long    ,				device=A_	)
						elif model_class in [
						    *get_values(A_	),
						]:
								UpperCamelCase          =					torch.zeros(
								    self.model_tester.batch_size    ,				dtype=torch.long    ,				device=A_	)
						elif model_class in [
						    *get_values(A_	),
						]:
								UpperCamelCase          =					torch.zeros(
								    (self.model_tester.batch_size, self.model_tester.text_seq_length)    ,				dtype=torch.long    ,				device=A_    ,				)

				return inputs_dict



		def __UpperCamelCase			(			self	)  -> Optional[int]:
				"""simple docstring"""
				self.config_tester.run_common_tests()



		def __UpperCamelCase			(			self	)  -> str:
				"""simple docstring"""
				UpperCamelCase          =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*A_	)



		def __UpperCamelCase			(			self	)  -> Any:
				"""simple docstring"""
				UpperCamelCase          =					self.model_tester.prepare_config_and_inputs()
				for type in ["absolute", "relative_key", "relative_key_query"]:
						UpperCamelCase          =					type
						self.model_tester.create_and_check_model(*A_	)



		def __UpperCamelCase			(			self	)  -> List[Any]:
				"""simple docstring"""
				UpperCamelCase          =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*A_	)



		def __UpperCamelCase			(			self	)  -> Optional[Any]:
				"""simple docstring"""
				UpperCamelCase          =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*A_	)



		def __UpperCamelCase			(			self	)  -> Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase          =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*A_	)



		@slow
		def __UpperCamelCase			(			self	)  -> Optional[int]:
				"""simple docstring"""
				for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						UpperCamelCase          =					LayoutLMvaModel.from_pretrained(A_	)
						self.assertIsNotNone(A_	)






def 				A	( )       ->  int:
		'''simple docstring'''

		UpperCamelCase          =					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
		return image


@require_torch
class     lowercase (		unittest.TestCase       ):



		@cached_property
		def __UpperCamelCase			(			self	)  -> Any:
				"""simple docstring"""
				return LayoutLMvaImageProcessor(apply_ocr=A_	) if is_vision_available() else None



		@slow
		def __UpperCamelCase			(			self	)  -> Optional[int]:
				"""simple docstring"""
				UpperCamelCase          =					LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'	).to(A_	)

				UpperCamelCase          =					self.default_image_processor
				UpperCamelCase          =					prepare_img()
				UpperCamelCase          =					image_processor(images=A_    ,				return_tensors='pt'	).pixel_values.to(A_	)

				UpperCamelCase          =					torch.tensor([[1, 2]]	)
				UpperCamelCase          =					torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]	).unsqueeze(0	)

				# forward pass
				UpperCamelCase          =					model(
				    input_ids=input_ids.to(A_	)    ,				bbox=bbox.to(A_	)    ,				pixel_values=pixel_values.to(A_	)    ,				)

				# verify the logits
				UpperCamelCase          =					torch.Size((1, 199, 768)	)
				self.assertEqual(outputs.last_hidden_state.shape    ,				A_	)

				UpperCamelCase          =					torch.tensor(
				    [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]	).to(A_	)

				self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]    ,				A_    ,				atol=1e-4	)	)
