






"""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       snake_case_       :



	"""simple docstring"""

	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=1000  ,					):


					"""simple docstring"""

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

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


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       ids_tensor([self.batch_size, self.text_seq_length]  ,					self.vocab_size						)

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

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

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

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

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

					A__							 =       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						):


					"""simple docstring"""

					A__							 =       LayoutLMvaModel(config=__a						)
					model.to(__a						)
					model.eval()

					# text + image
					A__							 =       model(__a  ,					pixel_values=__a						)
					A__							 =       model(
					    __a  ,					bbox=__a  ,					pixel_values=__a  ,					attention_mask=__a  ,					token_type_ids=__a						)
					A__							 =       model(__a  ,					bbox=__a  ,					pixel_values=__a  ,					token_type_ids=__a						)
					A__							 =       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
					A__							 =       model(__a						)
					self.parent.assertEqual(
					    result.last_hidden_state.shape  ,					(self.batch_size, self.text_seq_length, self.hidden_size)						)

					# image only
					A__							 =       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						):


					"""simple docstring"""

					A__							 =       self.num_labels
					A__							 =       LayoutLMvaForSequenceClassification(__a						)
					model.to(__a						)
					model.eval()
					A__							 =       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						):


					"""simple docstring"""

					A__							 =       self.num_labels
					A__							 =       LayoutLMvaForTokenClassification(config=__a						)
					model.to(__a						)
					model.eval()
					A__							 =       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						):


					"""simple docstring"""

					A__							 =       LayoutLMvaForQuestionAnswering(config=__a						)
					model.to(__a						)
					model.eval()
					A__							 =       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						):


					"""simple docstring"""

					A__							 =       self.prepare_config_and_inputs()
					(
					    (
					    A__			
					) ,			(
					    A__			
					) ,			(
					    A__			
					) ,			(
					    A__			
					) ,			(
					    A__			
					) ,			(
					    A__			
					) ,			(
					    A__			
					) ,			(
					    A__			
					) ,						
					)							 =       config_and_inputs
					A__							 =       {
					    '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       snake_case_       (    _lowerCamelCase    ,							_lowerCamelCase    ,							unittest.TestCase       ):



	"""simple docstring"""

	SCREAMING_SNAKE_CASE_:							Optional[Any]   			=					False
	SCREAMING_SNAKE_CASE_:							Union[str, Any]   			=					False
	SCREAMING_SNAKE_CASE_:							Union[str, Any]   			=					False

	SCREAMING_SNAKE_CASE_:							Tuple   			=					(
	    (
	        LayoutLMvaModel,
	        LayoutLMvaForSequenceClassification,
	        LayoutLMvaForTokenClassification,
	        LayoutLMvaForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	SCREAMING_SNAKE_CASE_:							int   			=					(
	    {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel}
	    if is_torch_available()
	    else {}
	)


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a  ,					__a  ,					__a						):


					"""simple docstring"""

					return True


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       LayoutLMvaModelTester(self						)
					A__							 =       ConfigTester(self  ,					config_class=__a  ,					hidden_size=37						)


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a=False						):


					"""simple docstring"""

					A__							 =       copy.deepcopy(__a						)
					if model_class in get_values(__a						):
									A__							 =       {
									    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						):
													A__							 =       torch.ones(self.model_tester.batch_size  ,					dtype=torch.long  ,					device=__a						)
									elif model_class in get_values(__a						):
													A__							 =       torch.zeros(
													    self.model_tester.batch_size  ,					dtype=torch.long  ,					device=__a						)
													A__							 =       torch.zeros(
													    self.model_tester.batch_size  ,					dtype=torch.long  ,					device=__a						)
									elif model_class in [
									    *get_values(__a						),
									]:
													A__							 =       torch.zeros(
													    self.model_tester.batch_size  ,					dtype=torch.long  ,					device=__a						)
									elif model_class in [
									    *get_values(__a						),
									]:
													A__							 =       torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.text_seq_length)  ,					dtype=torch.long  ,					device=__a  ,					)

					return inputs_dict


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					self.config_tester.run_common_tests()


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
									A__							 =       type
									self.model_tester.create_and_check_model(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*__a						)






	@slow
	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									A__							 =       LayoutLMvaModel.from_pretrained(__a						)
									self.assertIsNotNone(__a						)





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







@require_torch
class       snake_case_       (    unittest.TestCase       ):



	"""simple docstring"""

	@cached_property
	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					return LayoutLMvaImageProcessor(apply_ocr=__a						) if is_vision_available() else None






	@slow
	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'						).to(__a						)

					A__							 =       self.default_image_processor
					A__							 =       prepare_img()
					A__							 =       image_processor(images=__a  ,					return_tensors='pt'						).pixel_values.to(__a						)

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

					# forward pass
					A__							 =       model(
					    input_ids=input_ids.to(__a						)  ,					bbox=bbox.to(__a						)  ,					pixel_values=pixel_values.to(__a						)  ,					)

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

					A__							 =       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						)						)







"""simple docstring"""






import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import PoolFormerImageProcessor





class       snake_case_       (    unittest.TestCase       ):



	"""simple docstring"""

	def __init__(      self  ,					__a  ,					__a=7  ,					__a=3  ,					__a=30  ,					__a=400  ,					__a=True  ,					__a=None  ,					__a=0.9  ,					__a=None  ,					__a=True  ,					__a=[0.5, 0.5, 0.5]  ,					__a=[0.5, 0.5, 0.5]  ,					):


					"""simple docstring"""

					A__							 =       size if size is not None else {'shortest_edge': 30}
					A__							 =       crop_size if crop_size is not None else {'height': 30, 'width': 30}
					A__							 =       parent
					A__							 =       batch_size
					A__							 =       num_channels
					A__							 =       min_resolution
					A__							 =       max_resolution
					A__							 =       do_resize_and_center_crop
					A__							 =       size
					A__							 =       crop_pct
					A__							 =       crop_size
					A__							 =       do_normalize
					A__							 =       image_mean
					A__							 =       image_std






	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					return {
					    "size": self.size,
					    "do_resize_and_center_crop": self.do_resize_and_center_crop,
					    "crop_pct": self.crop_pct,
					    "crop_size": self.crop_size,
					    "do_normalize": self.do_normalize,
					    "image_mean": self.image_mean,
					    "image_std": self.image_std,
					}





@require_torch
@require_vision
class       snake_case_       (    _lowerCamelCase    ,							unittest.TestCase       ):



	"""simple docstring"""

	SCREAMING_SNAKE_CASE_:							List[Any]   			=					PoolFormerImageProcessor if is_vision_available() else None


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       PoolFormerImageProcessingTester(self						)


	@property
	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					return self.image_processor_tester.prepare_image_processor_dict()


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.image_processing_class(**self.image_processor_dict						)
					self.assertTrue(hasattr(__a  ,					'do_resize_and_center_crop'						)						)
					self.assertTrue(hasattr(__a  ,					'size'						)						)
					self.assertTrue(hasattr(__a  ,					'crop_pct'						)						)
					self.assertTrue(hasattr(__a  ,					'do_normalize'						)						)
					self.assertTrue(hasattr(__a  ,					'image_mean'						)						)
					self.assertTrue(hasattr(__a  ,					'image_std'						)						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.image_processing_class.from_dict(self.image_processor_dict						)
					self.assertEqual(image_processor.size  ,					{'shortest_edge': 30}						)
					self.assertEqual(image_processor.crop_size  ,					{'height': 30, 'width': 30}						)

					A__							 =       self.image_processing_class.from_dict(self.image_processor_dict  ,					size=42  ,					crop_size=84						)
					self.assertEqual(image_processor.size  ,					{'shortest_edge': 42}						)
					self.assertEqual(image_processor.crop_size  ,					{'height': 84, 'width': 84}						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					pass


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.image_processing_class(**self.image_processor_dict						)
					# create random PIL images
					A__							 =       prepare_image_inputs(self.image_processor_tester  ,					equal_resolution=__a						)
					for image in image_inputs:
									self.assertIsInstance(__a  ,					Image.Image						)

					# Test not batched input
					A__							 =       image_processing(image_inputs[0]  ,					return_tensors='pt'						).pixel_values

					self.assertEqual(
					    encoded_images.shape  ,					(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['height'],
					        self.image_processor_tester.crop_size['width'],
					    )  ,					)

					# Test batched
					A__							 =       image_processing(__a  ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape  ,					(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['height'],
					        self.image_processor_tester.crop_size['width'],
					    )  ,					)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.image_processing_class(**self.image_processor_dict						)
					# create random numpy tensors
					A__							 =       prepare_image_inputs(self.image_processor_tester  ,					equal_resolution=__a  ,					numpify=__a						)
					for image in image_inputs:
									self.assertIsInstance(__a  ,					np.ndarray						)

					# Test not batched input
					A__							 =       image_processing(image_inputs[0]  ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape  ,					(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['height'],
					        self.image_processor_tester.crop_size['width'],
					    )  ,					)

					# Test batched
					A__							 =       image_processing(__a  ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape  ,					(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['height'],
					        self.image_processor_tester.crop_size['width'],
					    )  ,					)






	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.image_processing_class(**self.image_processor_dict						)
					# create random PyTorch tensors
					A__							 =       prepare_image_inputs(self.image_processor_tester  ,					equal_resolution=__a  ,					torchify=__a						)
					for image in image_inputs:
									self.assertIsInstance(__a  ,					torch.Tensor						)

					# Test not batched input
					A__							 =       image_processing(image_inputs[0]  ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape  ,					(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['height'],
					        self.image_processor_tester.crop_size['width'],
					    )  ,					)

					# Test batched
					A__							 =       image_processing(__a  ,					return_tensors='pt'						).pixel_values
					self.assertEqual(
					    encoded_images.shape  ,					(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['height'],
					        self.image_processor_tester.crop_size['width'],
					    )  ,					)


