


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

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





									"""simple docstring"""







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

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

							def       lowerCamelCase_ (							self:	Dict							)			->   str:





									"""simple docstring"""







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

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

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

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

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

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

									lowercase__            =      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:	Tuple    ,						UpperCamelCase_:	Optional[int]    ,						UpperCamelCase_:	Optional[Any]    ,						UpperCamelCase_:	List[Any]    ,						UpperCamelCase_:	Dict    ,						UpperCamelCase_:	List[Any]    ,						UpperCamelCase_:	List[str]    ,						UpperCamelCase_:	Tuple    ,						UpperCamelCase_:	Dict							)			->   str:





									"""simple docstring"""







									lowercase__            =      LayoutLMvaModel(config=UpperCamelCase_							)
									model.to(UpperCamelCase_							)
									model.eval()

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

									# image only
									lowercase__            =      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:	Optional[int]    ,						UpperCamelCase_:	Optional[Any]    ,						UpperCamelCase_:	Optional[Any]    ,						UpperCamelCase_:	Union[str, Any]    ,						UpperCamelCase_:	str    ,						UpperCamelCase_:	int    ,						UpperCamelCase_:	Optional[int]    ,						UpperCamelCase_:	Optional[int]    ,						UpperCamelCase_:	str							)			->   Tuple:





									"""simple docstring"""







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





									"""simple docstring"""







									lowercase__            =      self.num_labels
									lowercase__            =      LayoutLMvaForTokenClassification(config=UpperCamelCase_							)
									model.to(UpperCamelCase_							)
									model.eval()
									lowercase__            =      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:	Any    ,						UpperCamelCase_:	int    ,						UpperCamelCase_:	Optional[Any]    ,						UpperCamelCase_:	Union[str, Any]    ,						UpperCamelCase_:	Optional[Any]    ,						UpperCamelCase_:	Any    ,						UpperCamelCase_:	int    ,						UpperCamelCase_:	Tuple    ,						UpperCamelCase_:	Tuple							)			->   Optional[Any]:





									"""simple docstring"""







									lowercase__            =      LayoutLMvaForQuestionAnswering(config=UpperCamelCase_							)
									model.to(UpperCamelCase_							)
									model.eval()
									lowercase__            =      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:	int							)			->   List[Any]:





									"""simple docstring"""







									lowercase__            =      self.prepare_config_and_inputs()
									(
									    (
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		
									)            =      config_and_inputs
									lowercase__            =      {
									    '''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        _a							(			UpperCamelCase__     ,	UpperCamelCase__     ,	unittest.TestCase			):
							_lowercase	:			List[str]        = False
							_lowercase	:			List[Any]        = False
							_lowercase	:			int        = False

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

							def       lowerCamelCase_ (							self:	Union[str, Any]    ,						UpperCamelCase_:	Tuple    ,						UpperCamelCase_:	Dict    ,						UpperCamelCase_:	List[str]    ,						UpperCamelCase_:	List[Any]    ,						UpperCamelCase_:	Tuple							)			->   Optional[int]:





									"""simple docstring"""







									return True

							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   Optional[int]:





									"""simple docstring"""







									lowercase__            =      LayoutLMvaModelTester(self							)
									lowercase__            =      ConfigTester(self    ,						config_class=UpperCamelCase_    ,						hidden_size=37							)

							def       lowerCamelCase_ (							self:	Tuple    ,						UpperCamelCase_:	str    ,						UpperCamelCase_:	Any    ,						UpperCamelCase_:	Optional[int]=False							)			->   Tuple:





									"""simple docstring"""







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

									return inputs_dict

							def       lowerCamelCase_ (							self:	Optional[int]							)			->   Any:





									"""simple docstring"""







									self.config_tester.run_common_tests()

							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   Optional[Any]:





									"""simple docstring"""







									lowercase__            =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*UpperCamelCase_							)

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





									"""simple docstring"""







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

							def       lowerCamelCase_ (							self:	Any							)			->   List[str]:





									"""simple docstring"""







									lowercase__            =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase_							)

							def       lowerCamelCase_ (							self:	List[Any]							)			->   Dict:





									"""simple docstring"""







									lowercase__            =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*UpperCamelCase_							)

							def       lowerCamelCase_ (							self:	str							)			->   str:





									"""simple docstring"""







									lowercase__            =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*UpperCamelCase_							)


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





									"""simple docstring"""







									for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowercase__            =      LayoutLMvaModel.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)
def 			_a (      ):


		"""simple docstring"""




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


@require_torch
class        _a							(			unittest.TestCase			):

							@cached_property
							def       lowerCamelCase_ (							self:	Tuple							)			->   str:





									"""simple docstring"""







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


							@slow
							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   str:





									"""simple docstring"""







									lowercase__            =      LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''							).to(UpperCamelCase_							)

									lowercase__            =      self.default_image_processor
									lowercase__            =      prepare_img()
									lowercase__            =      image_processor(images=UpperCamelCase_    ,						return_tensors='''pt'''							).pixel_values.to(UpperCamelCase_							)

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

									# forward pass
									lowercase__            =      model(
									    input_ids=input_ids.to(UpperCamelCase_							)    ,						bbox=bbox.to(UpperCamelCase_							)    ,						pixel_values=pixel_values.to(UpperCamelCase_							)    ,						)

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

									lowercase__            =      torch.tensor(
									    [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]							).to(UpperCamelCase_							)

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






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


lowerCAmelCase       =							{
    'configuration_tapas': ['TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TapasConfig'],
    'tokenization_tapas': ['TapasTokenizer'],
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase       =							[
			    'TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'TapasForMaskedLM',
			    'TapasForQuestionAnswering',
			    'TapasForSequenceClassification',
			    'TapasModel',
			    'TapasPreTrainedModel',
			    'load_tf_weights_in_tapas',
			]
try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase       =							[
			    'TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'TFTapasForMaskedLM',
			    'TFTapasForQuestionAnswering',
			    'TFTapasForSequenceClassification',
			    'TFTapasModel',
			    'TFTapasPreTrainedModel',
			]


if TYPE_CHECKING:
			from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig
			from .tokenization_tapas import TapasTokenizer

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tapas import (
						    TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TapasForMaskedLM,
						    TapasForQuestionAnswering,
						    TapasForSequenceClassification,
						    TapasModel,
						    TapasPreTrainedModel,
						    load_tf_weights_in_tapas,
						)

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_tapas import (
						    TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFTapasForMaskedLM,
						    TFTapasForQuestionAnswering,
						    TFTapasForSequenceClassification,
						    TFTapasModel,
						    TFTapasPreTrainedModel,
						)


else:
			import sys

			lowerCAmelCase       =							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


