


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						:




							"""simple docstring"""


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

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

							def        __snake_case   (				self			:    Any							)      ->       List[Any]:
														__snake_case						:     int									=  ids_tensor([self.batch_size, self.text_seq_length]       ,      self.vocab_size							)

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

														__snake_case						:     Tuple									=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

														__snake_case						:     Tuple									=  None
														if self.use_input_mask:
																					__snake_case						:     int									=  random_attention_mask([self.batch_size, self.text_seq_length]							)

														__snake_case						:     int									=  None
														if self.use_token_type_ids:
																					__snake_case						:     Optional[Any]									=  ids_tensor([self.batch_size, self.text_seq_length]       ,      self.type_vocab_size							)

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

														__snake_case						:     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			:    Any       ,      lowerCamelCase			:    Tuple       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    Tuple       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    Dict       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    List[Any]       ,      lowerCamelCase			:    Any							)      ->       Dict:
														__snake_case						:     List[Any]									=  LayoutLMvaModel(config=__UpperCAmelCase							)
														model.to(__UpperCAmelCase							)
														model.eval()

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

														# image only
														__snake_case						:     Union[str, Any]									=  model(pixel_values=__UpperCAmelCase							)
														self.parent.assertEqual(
														    result.last_hidden_state.shape       ,      (self.batch_size, self.image_seq_length, self.hidden_size)							)

							def        __snake_case   (				self			:    List[Any]       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    Optional[int]       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    Any       ,      lowerCamelCase			:    List[Any]       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    Tuple							)      ->       Tuple:
														__snake_case						:     Dict									=  self.num_labels
														__snake_case						:     Optional[Any]									=  LayoutLMvaForSequenceClassification(__UpperCAmelCase							)
														model.to(__UpperCAmelCase							)
														model.eval()
														__snake_case						:     Optional[Any]									=  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        __snake_case   (				self			:    List[str]       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    Any       ,      lowerCamelCase			:    Any       ,      lowerCamelCase			:    Tuple       ,      lowerCamelCase			:    Optional[Any]       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    Any							)      ->       str:
														__snake_case						:     Any									=  self.num_labels
														__snake_case						:     Any									=  LayoutLMvaForTokenClassification(config=__UpperCAmelCase							)
														model.to(__UpperCAmelCase							)
														model.eval()
														__snake_case						:     Union[str, Any]									=  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        __snake_case   (				self			:    Optional[Any]       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    Optional[int]       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    Any       ,      lowerCamelCase			:    Any       ,      lowerCamelCase			:    int							)      ->       List[str]:
														__snake_case						:     Optional[Any]									=  LayoutLMvaForQuestionAnswering(config=__UpperCAmelCase							)
														model.to(__UpperCAmelCase							)
														model.eval()
														__snake_case						:     str									=  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        __snake_case   (				self			:    Any							)      ->       Tuple:
														__snake_case						:     Tuple									=  self.prepare_config_and_inputs()
														(
														    __snake_case		
														)						:     List[str]									=  config_and_inputs
														__snake_case						:     Tuple									=  {
														    '''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						(_SCREAMING_SNAKE_CASE	,							_SCREAMING_SNAKE_CASE	,							unittest.TestCase ):




							"""simple docstring"""


							__UpperCAmelCase       :				Tuple	    =    False
							__UpperCAmelCase       :				str	    =    False
							__UpperCAmelCase       :				List[Any]	    =    False

							__UpperCAmelCase       :				List[str]	    =    (
							    (
							        LayoutLMvaModel,
							        LayoutLMvaForSequenceClassification,
							        LayoutLMvaForTokenClassification,
							        LayoutLMvaForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							__UpperCAmelCase       :				List[str]	    =    (
							    {'''document-question-answering''': LayoutLMvaForQuestionAnswering, '''feature-extraction''': LayoutLMvaModel}
							    if is_torch_available()
							    else {}
							)

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

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

														return inputs_dict

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

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

							def        __snake_case   (				self			:    List[Any]							)      ->       str:
														__snake_case						:     str									=  self.model_tester.prepare_config_and_inputs()
														for type in ["absolute", "relative_key", "relative_key_query"]:
																					__snake_case						:     List[Any]									=  type
																					self.model_tester.create_and_check_model(*__UpperCAmelCase							)

							def        __snake_case   (				self			:    int							)      ->       str:
														__snake_case						:     Union[str, Any]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase							)

							def        __snake_case   (				self			:    Optional[int]							)      ->       Any:
														__snake_case						:     Any									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase							)

							def        __snake_case   (				self			:    Union[str, Any]							)      ->       Tuple:
														__snake_case						:     List[Any]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase							)






							@slow
							def        __snake_case   (				self			:    int							)      ->       Dict:
														for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					__snake_case						:     Optional[int]									=  LayoutLMvaModel.from_pretrained(__UpperCAmelCase							)
																					self.assertIsNotNone(__UpperCAmelCase							)







def 							lowerCAmelCase_	(   ):
							__snake_case						:     str									=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
							return image

@require_torch
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							@cached_property
							def        __snake_case   (				self			:    Dict							)      ->       int:
														return LayoutLMvaImageProcessor(apply_ocr=__UpperCAmelCase							) if is_vision_available() else None






							@slow
							def        __snake_case   (				self			:    Any							)      ->       Optional[int]:
														__snake_case						:     Union[str, Any]									=  LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"							).to(__UpperCAmelCase							)

														__snake_case						:     Tuple									=  self.default_image_processor
														__snake_case						:     Tuple									=  prepare_img()
														__snake_case						:     Optional[Any]									=  image_processor(images=__UpperCAmelCase       ,      return_tensors="pt"							).pixel_values.to(__UpperCAmelCase							)

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

														# forward pass
														__snake_case						:     Optional[Any]									=  model(
														    input_ids=input_ids.to(__UpperCAmelCase							)       ,      bbox=bbox.to(__UpperCAmelCase							)       ,      pixel_values=pixel_values.to(__UpperCAmelCase							)       ,      )

														# verify the logits
														__snake_case						:     Optional[Any]									=  torch.Size((1, 199, 768)							)
														self.assertEqual(outputs.last_hidden_state.shape       ,      __UpperCAmelCase							)

														__snake_case						:     Optional[int]									=  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(__UpperCAmelCase							)

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




import math

import tensorflow as tf
from packaging import version






def        snake_case__		(    lowerCamelCase_		):
							A      :					Optional[int]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					Union[str, Any]      		=					0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0		)     ,				x.dtype		)		))

							return x * cdf






def        snake_case__		(    lowerCamelCase_		):
							A      :					List[str]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					Any      		=					tf.cast(math.pi     ,				x.dtype		)
							A      :					int      		=					tf.cast(0.044_715     ,				x.dtype		)
							A      :					Tuple      		=					0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi		) * (x + coeff * tf.pow(lowerCamelCase_     ,				3		))		))

							return x * cdf






def        snake_case__		(    lowerCamelCase_		):
							A      :					Union[str, Any]      		=					tf.convert_to_tensor(lowerCamelCase_		)

							return x * tf.tanh(tf.math.softplus(lowerCamelCase_		)		)






def        snake_case__		(    lowerCamelCase_		):
							A      :					Optional[int]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					List[Any]      		=					tf.cast(0.044_715     ,				x.dtype		)
							A      :					str      		=					tf.cast(0.7_978_845_608     ,				x.dtype		)

							return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)		))






def        snake_case__		(    lowerCamelCase_		):
							A      :					List[str]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					Any      		=					tf.cast(1.702     ,				x.dtype		)
							return x * tf.math.sigmoid(coeff * x		)






def        snake_case__		(    lowerCamelCase_		):
							return tf.clip_by_value(_gelu(lowerCamelCase_		)     ,				-10     ,				10		)






def        snake_case__		(    lowerCamelCase_     ,				lowerCamelCase_=-1		):
							A		,		A      :					Dict      		=					tf.split(lowerCamelCase_     ,				2     ,				axis=lowerCamelCase_		)
							return a * tf.math.sigmoid(lowerCamelCase_		)


if version.parse(tf.version.VERSION) >= version.parse("2.4"):






		def        snake_case__		(    lowerCamelCase_		):
									return tf.keras.activations.gelu(lowerCamelCase_     ,				approximate=lowerCamelCase_		)

		lowercase			:				Tuple   =     tf.keras.activations.gelu

		lowercase			:				Union[str, Any]   =     approximate_gelu_wrap
else:
		lowercase			:				str   =     _gelu
		lowercase			:				Dict   =     _gelu_new


lowercase			:				Union[str, Any]   =     {
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}






def        snake_case__		(    lowerCamelCase_		):
							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(F'function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()		)}'		)






