




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        __lowerCAmelCase					:

			"""simple docstring"""







			def __init__( self     ,		lowerCamelCase__     ,		lowerCamelCase__=2     ,		lowerCamelCase__=3     ,		lowerCamelCase__=4     ,		lowerCamelCase__=2     ,		lowerCamelCase__=7     ,		lowerCamelCase__=True     ,		lowerCamelCase__=True     ,		lowerCamelCase__=True     ,		lowerCamelCase__=True     ,		lowerCamelCase__=99     ,		lowerCamelCase__=36     ,		lowerCamelCase__=3     ,		lowerCamelCase__=4     ,		lowerCamelCase__=37     ,		lowerCamelCase__="gelu"     ,		lowerCamelCase__=0.1     ,		lowerCamelCase__=0.1     ,		lowerCamelCase__=512     ,		lowerCamelCase__=16     ,		lowerCamelCase__=2     ,		lowerCamelCase__=0.02     ,		lowerCamelCase__=6     ,		lowerCamelCase__=6     ,		lowerCamelCase__=3     ,		lowerCamelCase__=4     ,		lowerCamelCase__=None     ,		lowerCamelCase__=1_000     ,		)       -> Optional[Any]:



							'''simple docstring'''
							__lowerCamelCase									=       parent
							__lowerCamelCase									=       batch_size
							__lowerCamelCase									=       num_channels
							__lowerCamelCase									=       image_size
							__lowerCamelCase									=       patch_size
							__lowerCamelCase									=       text_seq_length
							__lowerCamelCase									=       is_training
							__lowerCamelCase									=       use_input_mask
							__lowerCamelCase									=       use_token_type_ids
							__lowerCamelCase									=       use_labels
							__lowerCamelCase									=       vocab_size
							__lowerCamelCase									=       hidden_size
							__lowerCamelCase									=       num_hidden_layers
							__lowerCamelCase									=       num_attention_heads
							__lowerCamelCase									=       intermediate_size
							__lowerCamelCase									=       hidden_act
							__lowerCamelCase									=       hidden_dropout_prob
							__lowerCamelCase									=       attention_probs_dropout_prob
							__lowerCamelCase									=       max_position_embeddings
							__lowerCamelCase									=       type_vocab_size
							__lowerCamelCase									=       type_sequence_label_size
							__lowerCamelCase									=       initializer_range
							__lowerCamelCase									=       coordinate_size
							__lowerCamelCase									=       shape_size
							__lowerCamelCase									=       num_labels
							__lowerCamelCase									=       num_choices
							__lowerCamelCase									=       scope
							__lowerCamelCase									=       range_bbox

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


			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							__lowerCamelCase									=       ids_tensor([self.batch_size, self.text_seq_length]     ,		self.vocab_size				)

							__lowerCamelCase									=       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]:
																			__lowerCamelCase									=       bbox[i, j, 3]
																			__lowerCamelCase									=       bbox[i, j, 1]
																			__lowerCamelCase									=       t
															if bbox[i, j, 2] < bbox[i, j, 0]:
																			__lowerCamelCase									=       bbox[i, j, 2]
																			__lowerCamelCase									=       bbox[i, j, 0]
																			__lowerCamelCase									=       t

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

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

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

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

							__lowerCamelCase									=       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  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> List[str]:



							'''simple docstring'''
							__lowerCamelCase									=       LayoutLMvaModel(config=lowerCamelCase__				)
							model.to(lowerCamelCase__				)
							model.eval()

							# text + image
							__lowerCamelCase									=       model(lowerCamelCase__     ,		pixel_values=lowerCamelCase__				)
							__lowerCamelCase									=       model(
							    lowerCamelCase__     ,		bbox=lowerCamelCase__     ,		pixel_values=lowerCamelCase__     ,		attention_mask=lowerCamelCase__     ,		token_type_ids=lowerCamelCase__				)
							__lowerCamelCase									=       model(lowerCamelCase__     ,		bbox=lowerCamelCase__     ,		pixel_values=lowerCamelCase__     ,		token_type_ids=lowerCamelCase__				)
							__lowerCamelCase									=       model(lowerCamelCase__     ,		bbox=lowerCamelCase__     ,		pixel_values=lowerCamelCase__				)

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

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

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


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> Optional[int]:



							'''simple docstring'''
							__lowerCamelCase									=       self.num_labels
							__lowerCamelCase									=       LayoutLMvaForSequenceClassification(lowerCamelCase__				)
							model.to(lowerCamelCase__				)
							model.eval()
							__lowerCamelCase									=       model(
							    lowerCamelCase__     ,		bbox=lowerCamelCase__     ,		pixel_values=lowerCamelCase__     ,		attention_mask=lowerCamelCase__     ,		token_type_ids=lowerCamelCase__     ,		labels=lowerCamelCase__     ,		)
							self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_labels)				)


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> str:



							'''simple docstring'''
							__lowerCamelCase									=       self.num_labels
							__lowerCamelCase									=       LayoutLMvaForTokenClassification(config=lowerCamelCase__				)
							model.to(lowerCamelCase__				)
							model.eval()
							__lowerCamelCase									=       model(
							    lowerCamelCase__     ,		bbox=lowerCamelCase__     ,		pixel_values=lowerCamelCase__     ,		attention_mask=lowerCamelCase__     ,		token_type_ids=lowerCamelCase__     ,		labels=lowerCamelCase__     ,		)
							self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.text_seq_length, self.num_labels)				)


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> str:



							'''simple docstring'''
							__lowerCamelCase									=       LayoutLMvaForQuestionAnswering(config=lowerCamelCase__				)
							model.to(lowerCamelCase__				)
							model.eval()
							__lowerCamelCase									=       model(
							    lowerCamelCase__     ,		bbox=lowerCamelCase__     ,		pixel_values=lowerCamelCase__     ,		attention_mask=lowerCamelCase__     ,		token_type_ids=lowerCamelCase__     ,		start_positions=lowerCamelCase__     ,		end_positions=lowerCamelCase__     ,		)
							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  lowercase_       ( self				)       -> Tuple:



							'''simple docstring'''
							__lowerCamelCase									=       self.prepare_config_and_inputs()
							(
							    (
							    __lowerCamelCase
							)			,		(
							    __lowerCamelCase
							)			,		(
							    __lowerCamelCase
							)			,		(
							    __lowerCamelCase
							)			,		(
							    __lowerCamelCase
							)			,		(
							    __lowerCamelCase
							)			,		(
							    __lowerCamelCase
							)			,		(
							    __lowerCamelCase
							)			,		
							)									=       config_and_inputs
							__lowerCamelCase									=       {
							    '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        __lowerCAmelCase					(			__magic_name__     ,							__magic_name__     ,							unittest.TestCase					):

			"""simple docstring"""







			snake_case_  						=			False
			snake_case_  						=			False
			snake_case_  						=			False

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


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__				)       -> Any:



							'''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  lowercase_       ( self				)       -> str:



							'''simple docstring'''
							__lowerCamelCase									=       LayoutLMvaModelTester(self				)
							__lowerCamelCase									=       ConfigTester(self     ,		config_class=lowerCamelCase__     ,		hidden_size=37				)


			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__     ,		lowerCamelCase__=False				)       -> Tuple:



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

							return inputs_dict


			def  lowercase_       ( self				)       -> Union[str, Any]:



							'''simple docstring'''
							self.config_tester.run_common_tests()


			def  lowercase_       ( self				)       -> Optional[int]:



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__				)


			def  lowercase_       ( self				)       -> List[Any]:



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
											__lowerCamelCase									=       type
											self.model_tester.create_and_check_model(*lowerCamelCase__				)


			def  lowercase_       ( self				)       -> List[str]:



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__				)


			def  lowercase_       ( self				)       -> Union[str, Any]:



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__				)


			def  lowercase_       ( self				)       -> Optional[int]:



							'''simple docstring'''
							__lowerCamelCase									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__				)






			@slow
			def  lowercase_       ( self				)       -> Optional[int]:



							'''simple docstring'''
							for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCamelCase									=       LayoutLMvaModel.from_pretrained(lowerCamelCase__				)
											self.assertIsNotNone(lowerCamelCase__				)







def      lowerCamelCase_						(			)	->			Union[str, Any]:





				"""simple docstring"""


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







@require_torch
class        __lowerCAmelCase					(			unittest.TestCase					):

			"""simple docstring"""







			@cached_property
			def  lowercase_       ( self				)       -> Optional[int]:



							'''simple docstring'''
							return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase__				) if is_vision_available() else None






			@slow
			def  lowercase_       ( self				)       -> str:



							'''simple docstring'''
							__lowerCamelCase									=       LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'				).to(lowerCamelCase__				)

							__lowerCamelCase									=       self.default_image_processor
							__lowerCamelCase									=       prepare_img()
							__lowerCamelCase									=       image_processor(images=lowerCamelCase__     ,		return_tensors='pt'				).pixel_values.to(lowerCamelCase__				)

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

							# forward pass
							__lowerCamelCase									=       model(
							    input_ids=input_ids.to(lowerCamelCase__				)     ,		bbox=bbox.to(lowerCamelCase__				)     ,		pixel_values=pixel_values.to(lowerCamelCase__				)     ,		)

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

							__lowerCamelCase									=       torch.tensor(
							    [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]]				).to(lowerCamelCase__				)

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








import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
					import torch

					from transformers.activations import gelu_new, gelu_python, get_activation







@require_torch
class        __lowerCAmelCase					(			unittest.TestCase					):

			"""simple docstring"""







			def  lowercase_       ( self				)       -> int:



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertTrue(torch.allclose(gelu_python(lowerCamelCase__				)     ,		torch_builtin(lowerCamelCase__				)				)				)
							self.assertFalse(torch.allclose(gelu_python(lowerCamelCase__				)     ,		gelu_new(lowerCamelCase__				)				)				)


			def  lowercase_       ( self				)       -> Optional[int]:



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       get_activation('gelu_10'				)

							__lowerCamelCase									=       torch_builtin(lowerCamelCase__				)
							__lowerCamelCase									=       geluaa(lowerCamelCase__				)

							__lowerCamelCase									=       torch.where(y_gelu_aa < 10.0     ,		1     ,		0				)

							self.assertTrue(torch.max(lowerCamelCase__				).item() == 10.0				)
							self.assertTrue(torch.allclose(y_gelu * clipped_mask     ,		y_gelu_aa * clipped_mask				)				)


			def  lowercase_       ( self				)       -> Any:



							'''simple docstring'''
							get_activation('gelu'				)
							get_activation('gelu_10'				)
							get_activation('gelu_fast'				)
							get_activation('gelu_new'				)
							get_activation('gelu_python'				)
							get_activation('gelu_pytorch_tanh'				)
							get_activation('linear'				)
							get_activation('mish'				)
							get_activation('quick_gelu'				)
							get_activation('relu'				)
							get_activation('sigmoid'				)
							get_activation('silu'				)
							get_activation('swish'				)
							get_activation('tanh'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation('bogus'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation(lowerCamelCase__				)






			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       1
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertEqual(acta.a     ,		1				)
							with self.assertRaises(lowerCamelCase__				):
											__lowerCamelCase									=       acta.a


