






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






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

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






						def      __snake_case      (		self  :Union[str, Any]				)      ->List[str]:
										lowercase    :      Tuple       = ids_tensor([self.batch_size, self.text_seq_length]			, self.vocab_size				)

										lowercase    :      Optional[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]:
																						lowercase    :      Dict       = bbox[i, j, 3]
																						lowercase    :      List[str]       = bbox[i, j, 1]
																						lowercase    :      Optional[Any]       = t
																		if bbox[i, j, 2] < bbox[i, j, 0]:
																						lowercase    :      str       = bbox[i, j, 2]
																						lowercase    :      str       = bbox[i, j, 0]
																						lowercase    :      str       = t

										lowercase    :      Optional[int]       = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

										lowercase    :      Dict       = None
										if self.use_token_type_ids:
														lowercase    :      List[str]       = ids_tensor([self.batch_size, self.text_seq_length]			, self.type_vocab_size				)

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

										lowercase    :      Tuple       = 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      __snake_case      (		self  :Optional[Any]			, __magic_name__  :int			, __magic_name__  :Tuple			, __magic_name__  :Tuple			, __magic_name__  :Optional[int]			, __magic_name__  :List[Any]			, __magic_name__  :List[Any]			, __magic_name__  :int			, __magic_name__  :Optional[int]				)      ->Optional[int]:
										lowercase    :      Any       = LayoutLMvaModel(config=__magic_name__				)
										model.to(__magic_name__				)
										model.eval()

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

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

										# text only
										lowercase    :      List[str]       = model(__magic_name__				)
										self.parent.assertEqual(
										    result.last_hidden_state.shape			, (self.batch_size, self.text_seq_length, self.hidden_size)				)

										# image only
										lowercase    :      int       = model(pixel_values=__magic_name__				)
										self.parent.assertEqual(
										    result.last_hidden_state.shape			, (self.batch_size, self.image_seq_length, self.hidden_size)				)






						def      __snake_case      (		self  :Dict			, __magic_name__  :List[Any]			, __magic_name__  :Any			, __magic_name__  :Any			, __magic_name__  :Optional[int]			, __magic_name__  :Dict			, __magic_name__  :Tuple			, __magic_name__  :Dict			, __magic_name__  :List[Any]				)      ->int:
										lowercase    :      Optional[Any]       = self.num_labels
										lowercase    :      Union[str, Any]       = LayoutLMvaForSequenceClassification(__magic_name__				)
										model.to(__magic_name__				)
										model.eval()
										lowercase    :      str       = model(
										    __magic_name__			, bbox=__magic_name__			, pixel_values=__magic_name__			, attention_mask=__magic_name__			, token_type_ids=__magic_name__			, labels=__magic_name__			, )
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.num_labels)				)






						def      __snake_case      (		self  :List[Any]			, __magic_name__  :Union[str, Any]			, __magic_name__  :List[str]			, __magic_name__  :Any			, __magic_name__  :Any			, __magic_name__  :List[Any]			, __magic_name__  :List[str]			, __magic_name__  :Any			, __magic_name__  :List[Any]				)      ->Any:
										lowercase    :      Dict       = self.num_labels
										lowercase    :      Optional[int]       = LayoutLMvaForTokenClassification(config=__magic_name__				)
										model.to(__magic_name__				)
										model.eval()
										lowercase    :      str       = model(
										    __magic_name__			, bbox=__magic_name__			, pixel_values=__magic_name__			, attention_mask=__magic_name__			, token_type_ids=__magic_name__			, labels=__magic_name__			, )
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.text_seq_length, self.num_labels)				)






						def      __snake_case      (		self  :int			, __magic_name__  :Tuple			, __magic_name__  :List[Any]			, __magic_name__  :str			, __magic_name__  :List[str]			, __magic_name__  :Tuple			, __magic_name__  :Tuple			, __magic_name__  :List[Any]			, __magic_name__  :List[Any]				)      ->str:
										lowercase    :      Optional[int]       = LayoutLMvaForQuestionAnswering(config=__magic_name__				)
										model.to(__magic_name__				)
										model.eval()
										lowercase    :      List[Any]       = model(
										    __magic_name__			, bbox=__magic_name__			, pixel_values=__magic_name__			, attention_mask=__magic_name__			, token_type_ids=__magic_name__			, start_positions=__magic_name__			, end_positions=__magic_name__			, )
										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      __snake_case      (		self  :Dict				)      ->int:
										lowercase    :      Optional[int]       = self.prepare_config_and_inputs()
										(
										    (
										    lowercase
										)      ,      (
										    lowercase
										)      ,      (
										    lowercase
										)      ,      (
										    lowercase
										)      ,      (
										    lowercase
										)      ,      (
										    lowercase
										)      ,      (
										    lowercase
										)      ,      (
										    lowercase
										)      ,      
										)    :      List[Any]       = config_and_inputs
										lowercase    :      Optional[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		(__snake_case						,						__snake_case						,						unittest.TestCase     ):
						_SCREAMING_SNAKE_CASE  : int												=		False
						_SCREAMING_SNAKE_CASE  : int												=		False
						_SCREAMING_SNAKE_CASE  : Optional[Any]												=		False

						_SCREAMING_SNAKE_CASE  : Optional[Any]												=		(
						    (
						        LayoutLMvaModel,
						        LayoutLMvaForSequenceClassification,
						        LayoutLMvaForTokenClassification,
						        LayoutLMvaForQuestionAnswering,
						    )
						    if is_torch_available()
						    else ()
						)
						_SCREAMING_SNAKE_CASE  : Optional[int]												=		(
						    {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel}
						    if is_torch_available()
						    else {}
						)






						def      __snake_case      (		self  :Dict			, __magic_name__  :List[str]			, __magic_name__  :Optional[int]			, __magic_name__  :Union[str, Any]			, __magic_name__  :Union[str, Any]			, __magic_name__  :Union[str, Any]				)      ->List[str]:
										# `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      __snake_case      (		self  :Dict				)      ->Optional[int]:
										lowercase    :      Tuple       = LayoutLMvaModelTester(self				)
										lowercase    :      Dict       = ConfigTester(self			, config_class=__magic_name__			, hidden_size=37				)






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

										return inputs_dict






						def      __snake_case      (		self  :Optional[int]				)      ->Dict:
										self.config_tester.run_common_tests()






						def      __snake_case      (		self  :List[Any]				)      ->Optional[Any]:
										lowercase    :      str       = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__magic_name__				)






						def      __snake_case      (		self  :Dict				)      ->str:
										lowercase    :      Tuple       = self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
														lowercase    :      Optional[Any]       = type
														self.model_tester.create_and_check_model(*__magic_name__				)






						def      __snake_case      (		self  :List[Any]				)      ->Dict:
										lowercase    :      List[Any]       = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*__magic_name__				)






						def      __snake_case      (		self  :str				)      ->Tuple:
										lowercase    :      List[str]       = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*__magic_name__				)






						def      __snake_case      (		self  :Optional[Any]				)      ->Optional[int]:
										lowercase    :      List[str]       = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*__magic_name__				)






						@slow
						def      __snake_case      (		self  :Optional[Any]				)      ->Tuple:
										for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowercase    :      Any       = LayoutLMvaModel.from_pretrained(__magic_name__				)
														self.assertIsNotNone(__magic_name__				)


def      UpperCamelCase       ( )     ->       Any:
				lowercase    :      str       = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
				return image


@require_torch
class      UpperCamelCase		(unittest.TestCase     ):






						@cached_property
						def      __snake_case      (		self  :Optional[int]				)      ->str:
										return LayoutLMvaImageProcessor(apply_ocr=__magic_name__				) if is_vision_available() else None






						@slow
						def      __snake_case      (		self  :Dict				)      ->Optional[Any]:
										lowercase    :      List[Any]       = LayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""				).to(__magic_name__				)

										lowercase    :      str       = self.default_image_processor
										lowercase    :      List[str]       = prepare_img()
										lowercase    :      Optional[int]       = image_processor(images=__magic_name__			, return_tensors="""pt"""				).pixel_values.to(__magic_name__				)

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

										# forward pass
										lowercase    :      Any       = model(
										    input_ids=input_ids.to(__magic_name__				)			, bbox=bbox.to(__magic_name__				)			, pixel_values=pixel_values.to(__magic_name__				)			, )

										# verify the logits
										lowercase    :      List[Any]       = torch.Size((1, 199, 768)				)
										self.assertEqual(outputs.last_hidden_state.shape			, __magic_name__				)

										lowercase    :      Optional[int]       = torch.tensor(
										    [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]				).to(__magic_name__				)

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








"""simple docstring"""







from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_torch_available():
				import torch

if is_torch_tpu_available(check_device=False):
				import torch_xla.core.xla_model as xm


_lowerCAmelCase            =	logging.get_logger(__name__)


@dataclass
class      UpperCamelCase		(__snake_case     ):
						_SCREAMING_SNAKE_CASE  : str												=		[
						    """no_inference""",
						    """no_cuda""",
						    """no_tpu""",
						    """no_speed""",
						    """no_memory""",
						    """no_env_print""",
						    """no_multi_process""",
						]






						def __init__(		self  :Union[str, Any]			, **__magic_name__  :int				)      ->Any:
										for deprecated_arg in self.deprecated_args:
														if deprecated_arg in kwargs:
																		lowercase    :      int       = deprecated_arg[3:]
																		setattr(self			, __magic_name__			, not kwargs.pop(__magic_name__				)				)
																		logger.warning(
																		    f"""{deprecated_arg} is depreciated. Please use --no_{positive_arg} or"""
																		    f""" {positive_arg}={kwargs[positive_arg]}"""				)

										lowercase    :      List[str]       = kwargs.pop("""torchscript"""			, self.torchscript				)
										lowercase    :      Any       = kwargs.pop("""torch_xla_tpu_print_metrics"""			, self.torch_xla_tpu_print_metrics				)
										lowercase    :      Union[str, Any]       = kwargs.pop("""fp16_opt_level"""			, self.fpaa_opt_level				)
										super().__init__(**__magic_name__				)

						_SCREAMING_SNAKE_CASE  : bool												=		field(default=__snake_case						,						metadata={"""help""": """Trace the models using torchscript"""}     )
						_SCREAMING_SNAKE_CASE  : bool												=		field(default=__snake_case						,						metadata={"""help""": """Print Xla/PyTorch tpu metrics"""}     )
						_SCREAMING_SNAKE_CASE  : str												=		field(
						    default="""O1"""						,						metadata={
						        """help""": (
						            """For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']. """
						            """See details at https://nvidia.github.io/apex/amp.html"""
						        )
						    }						,						)






						@cached_property
						def      __snake_case      (		self  :Optional[int]				)      ->Tuple["torch.device", int]:
										requires_backends(self			, ["""torch"""]				)
										logger.info("""PyTorch: setting up devices"""				)
										if not self.cuda:
														lowercase    :      Optional[Any]       = torch.device("""cpu"""				)
														lowercase    :      int       = 0
										elif is_torch_tpu_available():
														lowercase    :      Any       = xm.xla_device()
														lowercase    :      List[str]       = 0
										else:
														lowercase    :      int       = torch.device("""cuda""" if torch.cuda.is_available() else """cpu"""				)
														lowercase    :      Any       = torch.cuda.device_count()
										return device, n_gpu






						@property
						def      __snake_case      (		self  :Optional[int]				)      ->Union[str, Any]:
										return is_torch_tpu_available() and self.tpu






						@property
						def      __snake_case      (		self  :Optional[int]				)      ->int:
										requires_backends(self			, ["""torch"""]				)
										# TODO(PVP): currently only single GPU is supported
										return torch.cuda.current_device()






						@property
						def      __snake_case      (		self  :Optional[int]				)      ->"torch.device":
										requires_backends(self			, ["""torch"""]				)
										return self._setup_devices[0]






						@property
						def      __snake_case      (		self  :Optional[Any]				)      ->str:
										requires_backends(self			, ["""torch"""]				)
										return self._setup_devices[1]






						@property
						def      __snake_case      (		self  :int				)      ->List[str]:
										return self.n_gpu > 0
