



'''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 							lowercase__     :





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







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

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





	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       ids_tensor([self.batch_size, self.text_seq_length]			,self.vocab_size			)

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

							_UpperCamelCase :		str			    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

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

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

							_UpperCamelCase :		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      UpperCamelCase_  (			self :    Dict			,lowerCamelCase__ :    List[Any]			,lowerCamelCase__ :    Optional[Any]			,lowerCamelCase__ :    str			,lowerCamelCase__ :    Tuple			,lowerCamelCase__ :    int			,lowerCamelCase__ :    Optional[int]			,lowerCamelCase__ :    Any			,lowerCamelCase__ :    Optional[int]			):







							'''simple docstring'''
							_UpperCamelCase :		Tuple			    =       LayoutLMvaModel(config=lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()

							# text + image
							_UpperCamelCase :		Dict			    =       model(lowerCamelCase__			,pixel_values=lowerCamelCase__			)
							_UpperCamelCase :		Optional[int]			    =       model(
							    lowerCamelCase__			,bbox=lowerCamelCase__			,pixel_values=lowerCamelCase__			,attention_mask=lowerCamelCase__			,token_type_ids=lowerCamelCase__			)
							_UpperCamelCase :		Union[str, Any]			    =       model(lowerCamelCase__			,bbox=lowerCamelCase__			,pixel_values=lowerCamelCase__			,token_type_ids=lowerCamelCase__			)
							_UpperCamelCase :		List[str]			    =       model(lowerCamelCase__			,bbox=lowerCamelCase__			,pixel_values=lowerCamelCase__			)

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

							# text only
							_UpperCamelCase :		int			    =       model(lowerCamelCase__			)
							self.parent.assertEqual(
							    result.last_hidden_state.shape			,(self.batch_size, self.text_seq_length, self.hidden_size)			)

							# image only
							_UpperCamelCase :		Optional[int]			    =       model(pixel_values=lowerCamelCase__			)
							self.parent.assertEqual(
							    result.last_hidden_state.shape			,(self.batch_size, self.image_seq_length, self.hidden_size)			)





	def      UpperCamelCase_  (			self :    List[str]			,lowerCamelCase__ :    Any			,lowerCamelCase__ :    Any			,lowerCamelCase__ :    List[str]			,lowerCamelCase__ :    Any			,lowerCamelCase__ :    Any			,lowerCamelCase__ :    Any			,lowerCamelCase__ :    str			,lowerCamelCase__ :    Optional[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Dict			    =       self.num_labels
							_UpperCamelCase :		List[Any]			    =       LayoutLMvaForSequenceClassification(lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()
							_UpperCamelCase :		str			    =       model(
							    lowerCamelCase__			,bbox=lowerCamelCase__			,pixel_values=lowerCamelCase__			,attention_mask=lowerCamelCase__			,token_type_ids=lowerCamelCase__			,labels=lowerCamelCase__			,)
							self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.num_labels)			)





	def      UpperCamelCase_  (			self :    Union[str, Any]			,lowerCamelCase__ :    Optional[int]			,lowerCamelCase__ :    List[str]			,lowerCamelCase__ :    Tuple			,lowerCamelCase__ :    str			,lowerCamelCase__ :    List[Any]			,lowerCamelCase__ :    Tuple			,lowerCamelCase__ :    List[str]			,lowerCamelCase__ :    Union[str, Any]			):







							'''simple docstring'''
							_UpperCamelCase :		Any			    =       self.num_labels
							_UpperCamelCase :		Dict			    =       LayoutLMvaForTokenClassification(config=lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()
							_UpperCamelCase :		Optional[int]			    =       model(
							    lowerCamelCase__			,bbox=lowerCamelCase__			,pixel_values=lowerCamelCase__			,attention_mask=lowerCamelCase__			,token_type_ids=lowerCamelCase__			,labels=lowerCamelCase__			,)
							self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.text_seq_length, self.num_labels)			)





	def      UpperCamelCase_  (			self :    int			,lowerCamelCase__ :    int			,lowerCamelCase__ :    str			,lowerCamelCase__ :    Tuple			,lowerCamelCase__ :    Optional[int]			,lowerCamelCase__ :    int			,lowerCamelCase__ :    List[str]			,lowerCamelCase__ :    Optional[Any]			,lowerCamelCase__ :    Dict			):







							'''simple docstring'''
							_UpperCamelCase :		int			    =       LayoutLMvaForQuestionAnswering(config=lowerCamelCase__			)
							model.to(lowerCamelCase__			)
							model.eval()
							_UpperCamelCase :		str			    =       model(
							    lowerCamelCase__			,bbox=lowerCamelCase__			,pixel_values=lowerCamelCase__			,attention_mask=lowerCamelCase__			,token_type_ids=lowerCamelCase__			,start_positions=lowerCamelCase__			,end_positions=lowerCamelCase__			,)
							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 :    List[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		List[str]			    =       self.prepare_config_and_inputs()
							(
							    (
							    _UpperCamelCase
							)    ,      (
							    _UpperCamelCase
							)    ,      (
							    _UpperCamelCase
							)    ,      (
							    _UpperCamelCase
							)    ,      (
							    _UpperCamelCase
							)    ,      (
							    _UpperCamelCase
							)    ,      (
							    _UpperCamelCase
							)    ,      (
							    _UpperCamelCase
							)    ,      
							) :		Any			    =       config_and_inputs
							_UpperCamelCase :		str			    =       {
							    '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 							lowercase__     (							lowercase						, lowercase						, unittest.TestCase       ):
	lowercase__      =      False
	lowercase__      =      False
	lowercase__      =      False

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





	def      UpperCamelCase_  (			self :    Tuple			,lowerCamelCase__ :    Optional[int]			,lowerCamelCase__ :    int			,lowerCamelCase__ :    Any			,lowerCamelCase__ :    Optional[int]			,lowerCamelCase__ :    str			):







							'''simple docstring'''
							# `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      UpperCamelCase_  (			self :    Optional[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		List[str]			    =       LayoutLMvaModelTester(self			)
							_UpperCamelCase :		Optional[int]			    =       ConfigTester(self			,config_class=lowerCamelCase__			,hidden_size=37			)





	def      UpperCamelCase_  (			self :    Optional[Any]			,lowerCamelCase__ :    Dict			,lowerCamelCase__ :    List[str]			,lowerCamelCase__ :    Union[str, Any]=False			):







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

							return inputs_dict





	def      UpperCamelCase_  (			self :    Dict			):







							'''simple docstring'''
							self.config_tester.run_common_tests()





	def      UpperCamelCase_  (			self :    Any			):







							'''simple docstring'''
							_UpperCamelCase :		Any			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    Tuple			):







							'''simple docstring'''
							_UpperCamelCase :		Dict			    =       self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
													_UpperCamelCase :		str			    =       type
													self.model_tester.create_and_check_model(*lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    Optional[int]			):







							'''simple docstring'''
							_UpperCamelCase :		List[Any]			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							_UpperCamelCase :		int			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__			)





	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__			)





	@slow
	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_UpperCamelCase :		int			    =       LayoutLMvaModel.from_pretrained(lowerCamelCase__			)
													self.assertIsNotNone(lowerCamelCase__			)






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






@require_torch
class 							lowercase__     (							unittest.TestCase       ):





	@cached_property
	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase__			) if is_vision_available() else None





	@slow
	def      UpperCamelCase_  (			self :    int			):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'			).to(lowerCamelCase__			)

							_UpperCamelCase :		Any			    =       self.default_image_processor
							_UpperCamelCase :		Tuple			    =       prepare_img()
							_UpperCamelCase :		Optional[Any]			    =       image_processor(images=lowerCamelCase__			,return_tensors='pt'			).pixel_values.to(lowerCamelCase__			)

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

							# forward pass
							_UpperCamelCase :		List[Any]			    =       model(
							    input_ids=input_ids.to(lowerCamelCase__			)			,bbox=bbox.to(lowerCamelCase__			)			,pixel_values=pixel_values.to(lowerCamelCase__			)			,)

							# verify the logits
							_UpperCamelCase :		int			    =       torch.Size((1, 199, 768)			)
							self.assertEqual(outputs.last_hidden_state.shape			,lowerCamelCase__			)

							_UpperCamelCase :		int			    =       torch.tensor(
							    [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]			).to(lowerCamelCase__			)

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







'''simple docstring'''
from __future__ import annotations

import numpy as np






def A__   (					UpperCAmelCase_							):
						_UpperCamelCase    ,      _UpperCamelCase :		Optional[int]			    =       np.shape(UpperCAmelCase_							)
						if rows != columns:
												_UpperCamelCase :		Union[str, Any]			    =       (
												    '\'table\' has to be of square shaped array but got a '
												    f'{rows}x{columns} array:\n{table}'
												)
												raise ValueError(UpperCAmelCase_							)

						_UpperCamelCase :		Optional[Any]			    =       np.zeros((rows, columns)							)
						_UpperCamelCase :		Tuple			    =       np.zeros((rows, columns)							)
						for i in range(UpperCAmelCase_							):
												for j in range(UpperCAmelCase_							):
																		_UpperCamelCase :		Optional[Any]			    =       sum(lower[i][k] * upper[k][j] for k in range(UpperCAmelCase_							)							)
																		if upper[j][j] == 0:
																								raise ArithmeticError('No LU decomposition exists'							)
																		_UpperCamelCase :		Optional[Any]			    =       (table[i][j] - total) / upper[j][j]
												_UpperCamelCase :		int			    =       1
												for j in range(UpperCAmelCase_  ,  UpperCAmelCase_							):
																		_UpperCamelCase :		Optional[int]			    =       sum(lower[i][k] * upper[k][j] for k in range(UpperCAmelCase_							)							)
																		_UpperCamelCase :		Tuple			    =       table[i][j] - total
						return lower, upper


if __name__ == "__main__":
					import doctest

					doctest.testmod()


