

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







			def __init__(			self							: List[Any]      ,      a							: List[str]      ,      a							: Union[str, Any]=2      ,      a							: Tuple=3      ,      a							: int=4      ,      a							: str=2      ,      a							: Union[str, Any]=7      ,      a							: List[Any]=True      ,      a							: List[Any]=True      ,      a							: Dict=True      ,      a							: Any=True      ,      a							: List[Any]=99      ,      a							: Union[str, Any]=36      ,      a							: List[Any]=3      ,      a							: Dict=4      ,      a							: Optional[int]=37      ,      a							: Dict="gelu"      ,      a							: Tuple=0.1      ,      a							: int=0.1      ,      a							: Optional[Any]=512      ,      a							: List[Any]=16      ,      a							: Optional[int]=2      ,      a							: List[str]=0.02      ,      a							: Any=6      ,      a							: int=6      ,      a							: Dict=3      ,      a							: int=4      ,      a							: Dict=None      ,      a							: Optional[Any]=1000      ,      )							->       List[str]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       parent
				SCREAMING_SNAKE_CASE   :      Any				     =       batch_size
				SCREAMING_SNAKE_CASE   :      int				     =       num_channels
				SCREAMING_SNAKE_CASE   :      int				     =       image_size
				SCREAMING_SNAKE_CASE   :      str				     =       patch_size
				SCREAMING_SNAKE_CASE   :      int				     =       text_seq_length
				SCREAMING_SNAKE_CASE   :      int				     =       is_training
				SCREAMING_SNAKE_CASE   :      str				     =       use_input_mask
				SCREAMING_SNAKE_CASE   :      Tuple				     =       use_token_type_ids
				SCREAMING_SNAKE_CASE   :      str				     =       use_labels
				SCREAMING_SNAKE_CASE   :      Dict				     =       vocab_size
				SCREAMING_SNAKE_CASE   :      List[str]				     =       hidden_size
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       num_hidden_layers
				SCREAMING_SNAKE_CASE   :      Dict				     =       num_attention_heads
				SCREAMING_SNAKE_CASE   :      Any				     =       intermediate_size
				SCREAMING_SNAKE_CASE   :      List[str]				     =       hidden_act
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       hidden_dropout_prob
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       attention_probs_dropout_prob
				SCREAMING_SNAKE_CASE   :      Dict				     =       max_position_embeddings
				SCREAMING_SNAKE_CASE   :      Any				     =       type_vocab_size
				SCREAMING_SNAKE_CASE   :      str				     =       type_sequence_label_size
				SCREAMING_SNAKE_CASE   :      List[str]				     =       initializer_range
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       coordinate_size
				SCREAMING_SNAKE_CASE   :      List[str]				     =       shape_size
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       num_labels
				SCREAMING_SNAKE_CASE   :      int				     =       num_choices
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       scope
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       range_bbox

				# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
				SCREAMING_SNAKE_CASE   :      str				     =       text_seq_length
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       (image_size // patch_size) ** 2 + 1
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       self.text_seq_length + self.image_seq_length
			def 							__UpperCamelCase (			self							: List[Any]	)							->       int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[str]				     =       ids_tensor([self.batch_size, self.text_seq_length]      ,      self.vocab_size	)

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

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       None
				if self.use_input_mask:
					SCREAMING_SNAKE_CASE   :      str				     =       random_attention_mask([self.batch_size, self.text_seq_length]	)

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

				SCREAMING_SNAKE_CASE   :      Tuple				     =       None
				SCREAMING_SNAKE_CASE   :      int				     =       None
				if self.use_labels:
					SCREAMING_SNAKE_CASE   :      Any				     =       ids_tensor([self.batch_size]      ,      self.type_sequence_label_size	)
					SCREAMING_SNAKE_CASE   :      str				     =       ids_tensor([self.batch_size, self.text_seq_length]      ,      self.num_labels	)

				SCREAMING_SNAKE_CASE   :      str				     =       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							: List[str]      ,      a							: str      ,      a							: Union[str, Any]      ,      a							: Any      ,      a							: str      ,      a							: Union[str, Any]      ,      a							: Dict      ,      a							: List[Any]      ,      a							: Union[str, Any]	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Tuple				     =       LayoutLMvaModel(config=a	)
				model.to(a	)
				model.eval()

				# text + image
				SCREAMING_SNAKE_CASE   :      Any				     =       model(a      ,      pixel_values=a	)
				SCREAMING_SNAKE_CASE   :      int				     =       model(
				    a      ,      bbox=a      ,      pixel_values=a      ,      attention_mask=a      ,      token_type_ids=a	)
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       model(a      ,      bbox=a      ,      pixel_values=a      ,      token_type_ids=a	)
				SCREAMING_SNAKE_CASE   :      Any				     =       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
				SCREAMING_SNAKE_CASE   :      int				     =       model(a	)
				self.parent.assertEqual(
				    result.last_hidden_state.shape      ,      (self.batch_size, self.text_seq_length, self.hidden_size)	)

				# image only
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       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							: Dict      ,      a							: int      ,      a							: Union[str, Any]      ,      a							: Tuple      ,      a							: Any      ,      a							: Optional[Any]      ,      a							: List[Any]      ,      a							: Tuple      ,      a							: Optional[Any]	)							->       str:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.num_labels
				SCREAMING_SNAKE_CASE   :      Any				     =       LayoutLMvaForSequenceClassification(a	)
				model.to(a	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       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							: Optional[int]      ,      a							: Optional[Any]      ,      a							: Tuple      ,      a							: List[str]      ,      a							: Any      ,      a							: Union[str, Any]      ,      a							: int      ,      a							: List[Any]      ,      a							: List[Any]	)							->       int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       self.num_labels
				SCREAMING_SNAKE_CASE   :      Any				     =       LayoutLMvaForTokenClassification(config=a	)
				model.to(a	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      str				     =       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							: Tuple      ,      a							: List[str]      ,      a							: str      ,      a							: Dict      ,      a							: str      ,      a							: Union[str, Any]      ,      a							: str      ,      a							: List[str]      ,      a							: Tuple	)							->       str:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       LayoutLMvaForQuestionAnswering(config=a	)
				model.to(a	)
				model.eval()
				SCREAMING_SNAKE_CASE   :      Dict				     =       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							: Optional[int]	)							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Any				     =       self.prepare_config_and_inputs()
				(
				    (
				    SCREAMING_SNAKE_CASE
				)     ,(
				    SCREAMING_SNAKE_CASE
				)     ,(
				    SCREAMING_SNAKE_CASE
				)     ,(
				    SCREAMING_SNAKE_CASE
				)     ,(
				    SCREAMING_SNAKE_CASE
				)     ,(
				    SCREAMING_SNAKE_CASE
				)     ,(
				    SCREAMING_SNAKE_CASE
				)     ,(
				    SCREAMING_SNAKE_CASE
				)     ,
				)   :      Union[str, Any]				     =       config_and_inputs
				SCREAMING_SNAKE_CASE   :      Any				     =       {
				    "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 _UpperCamelCase							(							__A							,	__A							,	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 							__UpperCamelCase (			self							: Any      ,      a							: Optional[int]      ,      a							: Dict      ,      a							: Tuple      ,      a							: List[Any]      ,      a							: Optional[Any]	)							->       Optional[int]:

				"""simple docstring"""

				return True
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      int				     =       LayoutLMvaModelTester(self	)
				SCREAMING_SNAKE_CASE   :      List[str]				     =       ConfigTester(self      ,      config_class=a      ,      hidden_size=37	)
			def 							__UpperCamelCase (			self							: Union[str, Any]      ,      a							: Tuple      ,      a							: Tuple      ,      a							: Union[str, Any]=False	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       copy.deepcopy(a	)
				if model_class in get_values(a	):
					SCREAMING_SNAKE_CASE   :      Dict				     =       {
					    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	):
						SCREAMING_SNAKE_CASE   :      List[Any]				     =       torch.ones(self.model_tester.batch_size      ,      dtype=torch.long      ,      device=a	)
					elif model_class in get_values(a	):
						SCREAMING_SNAKE_CASE   :      Optional[int]				     =       torch.zeros(
						    self.model_tester.batch_size      ,      dtype=torch.long      ,      device=a	)
						SCREAMING_SNAKE_CASE   :      str				     =       torch.zeros(
						    self.model_tester.batch_size      ,      dtype=torch.long      ,      device=a	)
					elif model_class in [
					    *get_values(a	),
					]:
						SCREAMING_SNAKE_CASE   :      int				     =       torch.zeros(
						    self.model_tester.batch_size      ,      dtype=torch.long      ,      device=a	)
					elif model_class in [
					    *get_values(a	),
					]:
						SCREAMING_SNAKE_CASE   :      Tuple				     =       torch.zeros(
						    (self.model_tester.batch_size, self.model_tester.text_seq_length)      ,      dtype=torch.long      ,      device=a      ,      )

				return inputs_dict
			def 							__UpperCamelCase (			self							: List[Any]	)							->       Any:

				"""simple docstring"""

				self.config_tester.run_common_tests()
			def 							__UpperCamelCase (			self							: str	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Any				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*a	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Any				     =       self.model_tester.prepare_config_and_inputs()
				for type in ["absolute", "relative_key", "relative_key_query"]:
					SCREAMING_SNAKE_CASE   :      Optional[int]				     =       type
					self.model_tester.create_and_check_model(*a	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Tuple				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*a	)
			def 							__UpperCamelCase (			self							: Union[str, Any]	)							->       Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*a	)
			def 							__UpperCamelCase (			self							: Any	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      str				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*a	)






			@slow
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Union[str, Any]:

				"""simple docstring"""

				for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       LayoutLMvaModel.from_pretrained(a	)
					self.assertIsNotNone(a	)

def  lowerCamelCase__ (					):
	SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
	return image






@require_torch
class _UpperCamelCase							(							unittest.TestCase   ):
			'''simple docstring'''







			@cached_property
			def 							__UpperCamelCase (			self							: Optional[Any]	)							->       Union[str, Any]:

				"""simple docstring"""

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






			@slow
			def 							__UpperCamelCase (			self							: List[Any]	)							->       List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"	).to(a	)

				SCREAMING_SNAKE_CASE   :      List[str]				     =       self.default_image_processor
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       prepare_img()
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       image_processor(images=a      ,      return_tensors="pt"	).pixel_values.to(a	)

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

				# forward pass
				SCREAMING_SNAKE_CASE   :      Tuple				     =       model(
				    input_ids=input_ids.to(a	)      ,      bbox=bbox.to(a	)      ,      pixel_values=pixel_values.to(a	)      ,      )

				# verify the logits
				SCREAMING_SNAKE_CASE   :      str				     =       torch.Size((1, 199, 768)	)
				self.assertEqual(outputs.last_hidden_state.shape      ,      a	)

				SCREAMING_SNAKE_CASE   :      int				     =       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	)	)


class _UpperCamelCase							:
			'''simple docstring'''







			def __init__(			self							: Optional[Any]      ,      a							: int	)							->       None:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Tuple				     =       size
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       [0] * size
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       [0] * size
			@staticmethod
			def 							__UpperCamelCase (			a							: int	)							->       int:

				"""simple docstring"""

				return index | (index + 1)
			@staticmethod
			def 							__UpperCamelCase (			a							: int	)							->       int:

				"""simple docstring"""

				return (index & (index + 1)) - 1
			def 							__UpperCamelCase (			self							: Any      ,      a							: int      ,      a							: int	)							->       None:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       value
				while index < self.size:
					SCREAMING_SNAKE_CASE   :      Dict				     =       self.get_prev(a	) + 1
					if current_left_border == index:
						SCREAMING_SNAKE_CASE   :      Optional[int]				     =       value
					else:
						SCREAMING_SNAKE_CASE   :      Tuple				     =       max(a      ,      a      ,      a	)
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.get_next(a	)






			def 							__UpperCamelCase (			self							: Optional[int]      ,      a							: int      ,      a							: int	)							->       int:

				"""simple docstring"""

				right -= 1  # Because of right is exclusive
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       0
				while left <= right:
					SCREAMING_SNAKE_CASE   :      List[Any]				     =       self.get_prev(a	)
					if left <= current_left:
						SCREAMING_SNAKE_CASE   :      List[Any]				     =       max(a      ,      self.tree[right]	)
						SCREAMING_SNAKE_CASE   :      str				     =       current_left
					else:
						SCREAMING_SNAKE_CASE   :      List[str]				     =       max(a      ,      self.arr[right]	)
						right -= 1
				return result


if __name__ == "__main__":
			import doctest

			doctest.testmod()