"""simple docstring"""






import json
import os
import re
import shutil
import tempfile
import unittest
from typing import Tuple

from transformers import AddedToken, BatchEncoding, PerceiverTokenizer
from transformers.utils import cached_property, is_tf_available, is_torch_available

from ...test_tokenization_common import TokenizerTesterMixin


if is_torch_available():
		A 			=  "pt"
elif is_tf_available():
		A 			=  "tf"
else:
		A 			=  "jax"




class 				__lowercase			(			lowercase__   ,      unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  PerceiverTokenizer
					__lowerCAmelCase  =  False







					def  _lowerCamelCase		(						self     ):
									super().setUp()
									__a :						Tuple			       =  PerceiverTokenizer()
									tokenizer.save_pretrained(self.tmpdirname     )







					@cached_property
					def  _lowerCamelCase		(						self     ):
									return PerceiverTokenizer.from_pretrained('''deepmind/language-perceiver'''     )







					def  _lowerCamelCase		(						self	,			**_UpperCAmelCase     ):
									return self.tokenizer_class.from_pretrained(self.tmpdirname	,			**__lowerCamelCase     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase=False	,			_UpperCAmelCase=20	,			_UpperCAmelCase=5     ):
									__a :						Tuple			       =  []
									for i in range(len(__lowerCamelCase     )     ):
													try:
																	__a :						Any			       =  tokenizer.decode([i]	,			clean_up_tokenization_spaces=__lowerCamelCase     )
													except UnicodeDecodeError:
																	pass
													toks.append((i, tok)     )

									__a :						Union[str, Any]			       =  list(filter(lambda _UpperCAmelCase     : re.match(R'''^[ a-zA-Z]+$'''	,			t[1]     )	,			__lowerCamelCase     )     )
									__a :						List[str]			       =  list(filter(lambda _UpperCAmelCase     : [t[0]] == tokenizer.encode(t[1]	,			add_special_tokens=__lowerCamelCase     )	,			__lowerCamelCase     )     )
									if max_length is not None and len(__lowerCamelCase     ) > max_length:
													__a :						Union[str, Any]			       =  toks[:max_length]
									if min_length is not None and len(__lowerCamelCase     ) < min_length and len(__lowerCamelCase     ) > 0:
													while len(__lowerCamelCase     ) < min_length:
																	__a :						Dict			       =  toks + toks
        # toks_str = [t[1] for t in toks]
									__a :						Tuple			       =  [t[0] for t in toks]

									# Ensure consistency
									__a :						Optional[int]			       =  tokenizer.decode(__lowerCamelCase	,			clean_up_tokenization_spaces=__lowerCamelCase     )
									if " " not in output_txt and len(__lowerCamelCase     ) > 1:
													__a :						Any			       =  (
													    tokenizer.decode([toks_ids[0]]	,			clean_up_tokenization_spaces=__lowerCamelCase     )
													    + " "
													    + tokenizer.decode(toks_ids[1:]	,			clean_up_tokenization_spaces=__lowerCamelCase     )
													)
									if with_prefix_space:
													__a :						Union[str, Any]			       =  " " + output_txt
									__a :						List[Any]			       =  tokenizer.encode(__lowerCamelCase	,			add_special_tokens=__lowerCamelCase     )
									return output_txt, output_ids







					def  _lowerCamelCase		(						self     ):
									__a :						Optional[int]			       =  self.perceiver_tokenizer
									__a :						Optional[Any]			       =  "Unicode €."
									__a :						Dict			       =  tokenizer(__lowerCamelCase     )
									__a :						List[str]			       =  [4, 91, 116, 111, 105, 117, 106, 107, 38, 232, 136, 178, 52, 5]
									self.assertEqual(encoded['''input_ids''']	,			__lowerCamelCase     )

									# decoding
									__a :						Any			       =  tokenizer.decode(__lowerCamelCase     )
									self.assertEqual(__lowerCamelCase	,			'''[CLS]Unicode €.[SEP]'''     )

									__a :						int			       =  tokenizer('''e è é ê ë'''     )
									__a :						List[Any]			       =  [4, 107, 38, 201, 174, 38, 201, 175, 38, 201, 176, 38, 201, 177, 5]
									self.assertEqual(encoded['''input_ids''']	,			__lowerCamelCase     )
									# decoding
									__a :						str			       =  tokenizer.decode(__lowerCamelCase     )
									self.assertEqual(__lowerCamelCase	,			'''[CLS]e è é ê ë[SEP]'''     )

									# encode/decode, but with `encode` instead of `__call__`
									self.assertEqual(tokenizer.decode(tokenizer.encode('''e è é ê ë'''     )     )	,			'''[CLS]e è é ê ë[SEP]'''     )







					def  _lowerCamelCase		(						self     ):
									__a :						Union[str, Any]			       =  self.perceiver_tokenizer
									__a :						Optional[int]			       =  ["A long paragraph for summarization.", "Another paragraph for summarization."]
									# fmt: off
									__a :						Any			       =  [4, 71, 38, 114, 117, 116, 109, 38, 118, 103, 120, 103, 109, 120, 103, 118, 110, 38, 108, 117, 120, 38, 121, 123, 115, 115, 103, 120, 111, 128, 103, 122, 111, 117, 116, 52, 5, 0]
									# fmt: on
									__a :						str			       =  tokenizer(__lowerCamelCase	,			padding=__lowerCamelCase	,			return_tensors=__lowerCamelCase     )
									self.assertIsInstance(__lowerCamelCase	,			__lowerCamelCase     )

									if FRAMEWORK != "jax":
													__a :						Union[str, Any]			       =  list(batch.input_ids.numpy()[0]     )
									else:
													__a :						int			       =  list(batch.input_ids.tolist()[0]     )

									self.assertListEqual(__lowerCamelCase	,			__lowerCamelCase     )

									self.assertEqual((2, 38)	,			batch.input_ids.shape     )
									self.assertEqual((2, 38)	,			batch.attention_mask.shape     )







					def  _lowerCamelCase		(						self     ):
									__a :						Union[str, Any]			       =  self.perceiver_tokenizer
									__a :						Optional[int]			       =  ["A long paragraph for summarization.", "Another paragraph for summarization."]
									__a :						List[Any]			       =  tokenizer(__lowerCamelCase	,			padding=__lowerCamelCase	,			return_tensors=__lowerCamelCase     )
									# check if input_ids are returned and no decoder_input_ids
									self.assertIn('''input_ids'''	,			__lowerCamelCase     )
									self.assertIn('''attention_mask'''	,			__lowerCamelCase     )
									self.assertNotIn('''decoder_input_ids'''	,			__lowerCamelCase     )
									self.assertNotIn('''decoder_attention_mask'''	,			__lowerCamelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  self.perceiver_tokenizer
									__a :						Tuple			       =  [
									    "Summary of the text.",
									    "Another summary.",
									]
									__a :						List[Any]			       =  tokenizer(
									    text_target=__lowerCamelCase	,			max_length=32	,			padding='''max_length'''	,			truncation=__lowerCamelCase	,			return_tensors=__lowerCamelCase     )
									self.assertEqual(32	,			targets['''input_ids'''].shape[1]     )







					def  _lowerCamelCase		(						self     ):
									__a :						Union[str, Any]			       =  self.get_tokenizers()
									for tokenizer in tokenizers:
													with self.subTest(f"""{tokenizer.__class__.__name__}"""     ):
																	self.assertNotEqual(tokenizer.model_max_length	,			42     )

        # Now let's start the test
									__a :						List[str]			       =  self.get_tokenizers()
									for tokenizer in tokenizers:
													with self.subTest(f"""{tokenizer.__class__.__name__}"""     ):
																	# Isolate this from the other tests because we save additional tokens/etc
																	__a :						int			       =  tempfile.mkdtemp()

																	__a :						List[Any]			       =  " He is very happy, UNwant\u00E9d,running"
																	__a :						Any			       =  tokenizer.encode(__lowerCamelCase	,			add_special_tokens=__lowerCamelCase     )
																	tokenizer.save_pretrained(__lowerCamelCase     )

																	__a :						Optional[int]			       =  tokenizer.__class__.from_pretrained(__lowerCamelCase     )
																	__a :						str			       =  after_tokenizer.encode(__lowerCamelCase	,			add_special_tokens=__lowerCamelCase     )
																	self.assertListEqual(__lowerCamelCase	,			__lowerCamelCase     )

																	shutil.rmtree(__lowerCamelCase     )

									__a :						int			       =  self.get_tokenizers(model_max_length=42     )
									for tokenizer in tokenizers:
													with self.subTest(f"""{tokenizer.__class__.__name__}"""     ):
																	# Isolate this from the other tests because we save additional tokens/etc
																	__a :						str			       =  tempfile.mkdtemp()

																	__a :						Optional[Any]			       =  " He is very happy, UNwant\u00E9d,running"
																	tokenizer.add_tokens(['''bim''', '''bambam''']     )
																	__a :						List[str]			       =  tokenizer.additional_special_tokens
																	additional_special_tokens.append('''new_additional_special_token'''     )
																	tokenizer.add_special_tokens({'''additional_special_tokens''': additional_special_tokens}     )
																	__a :						Union[str, Any]			       =  tokenizer.encode(__lowerCamelCase	,			add_special_tokens=__lowerCamelCase     )
																	tokenizer.save_pretrained(__lowerCamelCase     )

																	__a :						Union[str, Any]			       =  tokenizer.__class__.from_pretrained(__lowerCamelCase     )
																	__a :						Union[str, Any]			       =  after_tokenizer.encode(__lowerCamelCase	,			add_special_tokens=__lowerCamelCase     )
																	self.assertListEqual(__lowerCamelCase	,			__lowerCamelCase     )
																	self.assertIn('''new_additional_special_token'''	,			after_tokenizer.additional_special_tokens     )
																	self.assertEqual(after_tokenizer.model_max_length	,			42     )

																	__a :						Any			       =  tokenizer.__class__.from_pretrained(__lowerCamelCase	,			model_max_length=43     )
																	self.assertEqual(tokenizer.model_max_length	,			43     )

																	shutil.rmtree(__lowerCamelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Optional[Any]			       =  []
									if self.test_slow_tokenizer:
													tokenizer_list.append((self.tokenizer_class, self.get_tokenizer())     )

									if self.test_rust_tokenizer:
													tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer())     )

									for tokenizer_class, tokenizer_utils in tokenizer_list:
													with tempfile.TemporaryDirectory() as tmp_dir:
																	tokenizer_utils.save_pretrained(__lowerCamelCase     )

																	with open(os.path.join(__lowerCamelCase	,			'''special_tokens_map.json'''     )	,			encoding='''utf-8'''     ) as json_file:
																					__a :						Union[str, Any]			       =  json.load(__lowerCamelCase     )

																	with open(os.path.join(__lowerCamelCase	,			'''tokenizer_config.json'''     )	,			encoding='''utf-8'''     ) as json_file:
																					__a :						List[str]			       =  json.load(__lowerCamelCase     )

																	__a :						int			       =  [f"""<extra_id_{i}>""" for i in range(125     )]

																	__a :						List[Any]			       =  added_tokens_extra_ids + [
																	    "an_additional_special_token"
																	]
																	__a :						List[str]			       =  added_tokens_extra_ids + [
																	    "an_additional_special_token"
																	]

																	with open(os.path.join(__lowerCamelCase	,			'''special_tokens_map.json'''     )	,			'''w'''	,			encoding='''utf-8'''     ) as outfile:
																					json.dump(__lowerCamelCase	,			__lowerCamelCase     )
																	with open(os.path.join(__lowerCamelCase	,			'''tokenizer_config.json'''     )	,			'''w'''	,			encoding='''utf-8'''     ) as outfile:
																					json.dump(__lowerCamelCase	,			__lowerCamelCase     )

																	# the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes
																	# into account the new value of additional_special_tokens given in the "tokenizer_config.json" and
																	# "special_tokens_map.json" files
																	__a :						List[Any]			       =  tokenizer_class.from_pretrained(
																	    __lowerCamelCase	,			)
																	self.assertIn(
																	    '''an_additional_special_token'''	,			tokenizer_without_change_in_init.additional_special_tokens     )
																	self.assertEqual(
																	    ['''an_additional_special_token''']	,			tokenizer_without_change_in_init.convert_ids_to_tokens(
																	        tokenizer_without_change_in_init.convert_tokens_to_ids(['''an_additional_special_token''']     )     )	,			)

																	# Now we test that we can change the value of additional_special_tokens in the from_pretrained
																	__a :						int			       =  added_tokens_extra_ids + [AddedToken('''a_new_additional_special_token'''	,			lstrip=__lowerCamelCase     )]
																	__a :						Any			       =  tokenizer_class.from_pretrained(
																	    __lowerCamelCase	,			additional_special_tokens=__lowerCamelCase	,			)

																	self.assertIn('''a_new_additional_special_token'''	,			tokenizer.additional_special_tokens     )
																	self.assertEqual(
																	    ['''a_new_additional_special_token''']	,			tokenizer.convert_ids_to_tokens(
																	        tokenizer.convert_tokens_to_ids(['''a_new_additional_special_token''']     )     )	,			)







					def  _lowerCamelCase		(						self     ):
									__a :						Optional[Any]			       =  self.perceiver_tokenizer
									self.assertEqual(tokenizer.decode([178]     )	,			'''�'''     )







					def  _lowerCamelCase		(						self     ):
									pass







					def  _lowerCamelCase		(						self     ):
									pass







					def  _lowerCamelCase		(						self     ):
									pass







					def  _lowerCamelCase		(						self     ):
									pass







					def  _lowerCamelCase		(						self     ):
									__a :						Optional[int]			       =  self.get_tokenizers(fast=__lowerCamelCase	,			do_lower_case=__lowerCamelCase     )
									for tokenizer in tokenizers:
													with self.subTest(f"""{tokenizer.__class__.__name__}"""     ):
																	__a :						Union[str, Any]			       =  ["[CLS]", "t", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "s", "t", "[SEP]"]
																	__a :						str			       =  tokenizer.convert_tokens_to_string(__lowerCamelCase     )

																	self.assertIsInstance(__lowerCamelCase	,			__lowerCamelCase     )






"""simple docstring"""






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			:







					'''simple docstring'''




					def __init__(						self	,			_UpperCAmelCase	,			_UpperCAmelCase=2	,			_UpperCAmelCase=3	,			_UpperCAmelCase=4	,			_UpperCAmelCase=2	,			_UpperCAmelCase=7	,			_UpperCAmelCase=True	,			_UpperCAmelCase=True	,			_UpperCAmelCase=True	,			_UpperCAmelCase=True	,			_UpperCAmelCase=99	,			_UpperCAmelCase=36	,			_UpperCAmelCase=3	,			_UpperCAmelCase=4	,			_UpperCAmelCase=37	,			_UpperCAmelCase="gelu"	,			_UpperCAmelCase=0.1	,			_UpperCAmelCase=0.1	,			_UpperCAmelCase=512	,			_UpperCAmelCase=16	,			_UpperCAmelCase=2	,			_UpperCAmelCase=0.0_2	,			_UpperCAmelCase=6	,			_UpperCAmelCase=6	,			_UpperCAmelCase=3	,			_UpperCAmelCase=4	,			_UpperCAmelCase=None	,			_UpperCAmelCase=1000	,			):
									__a :						Dict			       =  parent
									__a :						Optional[int]			       =  batch_size
									__a :						Optional[int]			       =  num_channels
									__a :						List[Any]			       =  image_size
									__a :						int			       =  patch_size
									__a :						Tuple			       =  text_seq_length
									__a :						Dict			       =  is_training
									__a :						str			       =  use_input_mask
									__a :						Optional[int]			       =  use_token_type_ids
									__a :						List[Any]			       =  use_labels
									__a :						Tuple			       =  vocab_size
									__a :						str			       =  hidden_size
									__a :						Any			       =  num_hidden_layers
									__a :						List[str]			       =  num_attention_heads
									__a :						str			       =  intermediate_size
									__a :						int			       =  hidden_act
									__a :						List[Any]			       =  hidden_dropout_prob
									__a :						Tuple			       =  attention_probs_dropout_prob
									__a :						Tuple			       =  max_position_embeddings
									__a :						List[Any]			       =  type_vocab_size
									__a :						int			       =  type_sequence_label_size
									__a :						str			       =  initializer_range
									__a :						Dict			       =  coordinate_size
									__a :						int			       =  shape_size
									__a :						int			       =  num_labels
									__a :						Optional[int]			       =  num_choices
									__a :						Any			       =  scope
									__a :						Any			       =  range_bbox

									# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
									__a :						Optional[int]			       =  text_seq_length
									__a :						str			       =  (image_size // patch_size) ** 2 + 1
									__a :						Tuple			       =  self.text_seq_length + self.image_seq_length







					def  _lowerCamelCase		(						self     ):
									__a :						List[Any]			       =  ids_tensor([self.batch_size, self.text_seq_length]	,			self.vocab_size     )

									__a :						Union[str, Any]			       =  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]:
																					__a :						Optional[Any]			       =  bbox[i, j, 3]
																					__a :						Union[str, Any]			       =  bbox[i, j, 1]
																					__a :						Tuple			       =  t
																	if bbox[i, j, 2] < bbox[i, j, 0]:
																					__a :						Optional[int]			       =  bbox[i, j, 2]
																					__a :						Optional[int]			       =  bbox[i, j, 0]
																					__a :						str			       =  t

									__a :						str			       =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

									__a :						Union[str, Any]			       =  None
									if self.use_input_mask:
													__a :						int			       =  random_attention_mask([self.batch_size, self.text_seq_length]     )

									__a :						int			       =  None
									if self.use_token_type_ids:
													__a :						Union[str, Any]			       =  ids_tensor([self.batch_size, self.text_seq_length]	,			self.type_vocab_size     )

									__a :						Optional[int]			       =  None
									__a :						Dict			       =  None
									if self.use_labels:
													__a :						Optional[Any]			       =  ids_tensor([self.batch_size]	,			self.type_sequence_label_size     )
													__a :						Optional[int]			       =  ids_tensor([self.batch_size, self.text_seq_length]	,			self.num_labels     )

									__a :						int			       =  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  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Union[str, Any]			       =  LayoutLMvaModel(config=_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()

									# text + image
									__a :						Union[str, Any]			       =  model(_UpperCAmelCase	,			pixel_values=_UpperCAmelCase     )
									__a :						List[Any]			       =  model(
									    _UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			token_type_ids=_UpperCAmelCase     )
									__a :						Optional[int]			       =  model(_UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase	,			token_type_ids=_UpperCAmelCase     )
									__a :						List[str]			       =  model(_UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase     )

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

									# text only
									__a :						int			       =  model(_UpperCAmelCase     )
									self.parent.assertEqual(
									    result.last_hidden_state.shape	,			(self.batch_size, self.text_seq_length, self.hidden_size)     )

									# image only
									__a :						Tuple			       =  model(pixel_values=_UpperCAmelCase     )
									self.parent.assertEqual(
									    result.last_hidden_state.shape	,			(self.batch_size, self.image_seq_length, self.hidden_size)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						Optional[Any]			       =  self.num_labels
									__a :						Optional[Any]			       =  LayoutLMvaForSequenceClassification(_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()
									__a :						Union[str, Any]			       =  model(
									    _UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			token_type_ids=_UpperCAmelCase	,			labels=_UpperCAmelCase	,			)
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.num_labels)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						List[str]			       =  self.num_labels
									__a :						int			       =  LayoutLMvaForTokenClassification(config=_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()
									__a :						List[str]			       =  model(
									    _UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			token_type_ids=_UpperCAmelCase	,			labels=_UpperCAmelCase	,			)
									self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.text_seq_length, self.num_labels)     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									__a :						List[Any]			       =  LayoutLMvaForQuestionAnswering(config=_UpperCAmelCase     )
									model.to(_UpperCAmelCase     )
									model.eval()
									__a :						List[str]			       =  model(
									    _UpperCAmelCase	,			bbox=_UpperCAmelCase	,			pixel_values=_UpperCAmelCase	,			attention_mask=_UpperCAmelCase	,			token_type_ids=_UpperCAmelCase	,			start_positions=_UpperCAmelCase	,			end_positions=_UpperCAmelCase	,			)
									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  _lowerCamelCase		(						self     ):
									__a :						Optional[int]			       =  self.prepare_config_and_inputs()
									(
									    (
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						(
									    __a
									)       ,						
									) :						List[Any]			       =  config_and_inputs
									__a :						Optional[int]			       =  {
									    '''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			(			_UpperCamelCase   ,      _UpperCamelCase   ,      unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  False
					__lowerCAmelCase  =  False
					__lowerCAmelCase  =  False

					__lowerCAmelCase  =  (
					    (
					        LayoutLMvaModel,
					        LayoutLMvaForSequenceClassification,
					        LayoutLMvaForTokenClassification,
					        LayoutLMvaForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					__lowerCAmelCase  =  (
					    {'''document-question-answering''': LayoutLMvaForQuestionAnswering, '''feature-extraction''': LayoutLMvaModel}
					    if is_torch_available()
					    else {}
					)







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase     ):
									# `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  _lowerCamelCase		(						self     ):
									__a :						str			       =  LayoutLMvaModelTester(self     )
									__a :						int			       =  ConfigTester(self	,			config_class=_UpperCAmelCase	,			hidden_size=37     )







					def  _lowerCamelCase		(						self	,			_UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase=False     ):
									__a :						int			       =  copy.deepcopy(_UpperCAmelCase     )
									if model_class in get_values(_UpperCAmelCase     ):
													__a :						List[str]			       =  {
													    k: v.unsqueeze(1     ).expand(-1	,			self.model_tester.num_choices	,			-1     ).contiguous()
													    if isinstance(_UpperCAmelCase	,			torch.Tensor     ) and v.ndim > 1
													    else v
													    for k, v in inputs_dict.items()
													}
									if return_labels:
													if model_class in get_values(_UpperCAmelCase     ):
																	__a :						Dict			       =  torch.ones(self.model_tester.batch_size	,			dtype=torch.long	,			device=_UpperCAmelCase     )
													elif model_class in get_values(_UpperCAmelCase     ):
																	__a :						int			       =  torch.zeros(
																	    self.model_tester.batch_size	,			dtype=torch.long	,			device=_UpperCAmelCase     )
																	__a :						Any			       =  torch.zeros(
																	    self.model_tester.batch_size	,			dtype=torch.long	,			device=_UpperCAmelCase     )
													elif model_class in [
													    *get_values(_UpperCAmelCase     ),
													]:
																	__a :						Any			       =  torch.zeros(
																	    self.model_tester.batch_size	,			dtype=torch.long	,			device=_UpperCAmelCase     )
													elif model_class in [
													    *get_values(_UpperCAmelCase     ),
													]:
																	__a :						int			       =  torch.zeros(
																	    (self.model_tester.batch_size, self.model_tester.text_seq_length)	,			dtype=torch.long	,			device=_UpperCAmelCase	,			)

									return inputs_dict







					def  _lowerCamelCase		(						self     ):
									self.config_tester.run_common_tests()







					def  _lowerCamelCase		(						self     ):
									__a :						List[str]			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Optional[int]			       =  self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
													__a :						Optional[Any]			       =  type
													self.model_tester.create_and_check_model(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						Tuple			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase     )







					def  _lowerCamelCase		(						self     ):
									__a :						int			       =  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase     )







					@slow
					def  _lowerCamelCase		(						self     ):
									for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a :						Tuple			       =  LayoutLMvaModel.from_pretrained(_UpperCAmelCase     )
													self.assertIsNotNone(_UpperCAmelCase     )






def     __A      (			)		->		Dict:
				__a :						Tuple			       =  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
				return image






@require_torch
class 				__lowercase			(			unittest.TestCase							):







					'''simple docstring'''




					@cached_property
					def  _lowerCamelCase		(						self     ):
									return LayoutLMvaImageProcessor(apply_ocr=_UpperCAmelCase     ) if is_vision_available() else None







					@slow
					def  _lowerCamelCase		(						self     ):
									__a :						List[Any]			       =  LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''     ).to(_UpperCAmelCase     )

									__a :						Dict			       =  self.default_image_processor
									__a :						Tuple			       =  prepare_img()
									__a :						Optional[Any]			       =  image_processor(images=_UpperCAmelCase	,			return_tensors='''pt'''     ).pixel_values.to(_UpperCAmelCase     )

									__a :						Tuple			       =  torch.tensor([[1, 2]]     )
									__a :						Dict			       =  torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]     ).unsqueeze(0     )

									# forward pass
									__a :						Tuple			       =  model(
									    input_ids=input_ids.to(_UpperCAmelCase     )	,			bbox=bbox.to(_UpperCAmelCase     )	,			pixel_values=pixel_values.to(_UpperCAmelCase     )	,			)

									# verify the logits
									__a :						Any			       =  torch.Size((1, 199, 768)     )
									self.assertEqual(outputs.last_hidden_state.shape	,			_UpperCAmelCase     )

									__a :						Union[str, Any]			       =  torch.tensor(
									    [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]     ).to(_UpperCAmelCase     )

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