






"""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					: List[Any]	,	lowerCAmelCase_					: Optional[int]	,	lowerCAmelCase_					: List[str]=2	,	lowerCAmelCase_					: int=3	,	lowerCAmelCase_					: List[Any]=4	,	lowerCAmelCase_					: Any=2	,	lowerCAmelCase_					: str=7	,	lowerCAmelCase_					: Optional[int]=True	,	lowerCAmelCase_					: Optional[int]=True	,	lowerCAmelCase_					: str=True	,	lowerCAmelCase_					: Union[str, Any]=True	,	lowerCAmelCase_					: Optional[int]=99	,	lowerCAmelCase_					: List[Any]=36	,	lowerCAmelCase_					: Optional[Any]=3	,	lowerCAmelCase_					: Union[str, Any]=4	,	lowerCAmelCase_					: Any=37	,	lowerCAmelCase_					: Dict="gelu"	,	lowerCAmelCase_					: Optional[int]=0.1	,	lowerCAmelCase_					: Optional[Any]=0.1	,	lowerCAmelCase_					: Any=512	,	lowerCAmelCase_					: int=16	,	lowerCAmelCase_					: List[Any]=2	,	lowerCAmelCase_					: List[Any]=0.0_2	,	lowerCAmelCase_					: List[Any]=6	,	lowerCAmelCase_					: Optional[Any]=6	,	lowerCAmelCase_					: Union[str, Any]=3	,	lowerCAmelCase_					: List[str]=4	,	lowerCAmelCase_					: Dict=None	,	lowerCAmelCase_					: Optional[int]=1_000	,	)						->			List[Any]:
				UpperCAmelCase_     : Union[str, Any]			     =					parent
				UpperCAmelCase_     : Dict			     =					batch_size
				UpperCAmelCase_     : Optional[int]			     =					num_channels
				UpperCAmelCase_     : Optional[int]			     =					image_size
				UpperCAmelCase_     : Optional[int]			     =					patch_size
				UpperCAmelCase_     : Optional[Any]			     =					text_seq_length
				UpperCAmelCase_     : Union[str, Any]			     =					is_training
				UpperCAmelCase_     : Union[str, Any]			     =					use_input_mask
				UpperCAmelCase_     : str			     =					use_token_type_ids
				UpperCAmelCase_     : List[Any]			     =					use_labels
				UpperCAmelCase_     : Optional[int]			     =					vocab_size
				UpperCAmelCase_     : Optional[int]			     =					hidden_size
				UpperCAmelCase_     : Optional[int]			     =					num_hidden_layers
				UpperCAmelCase_     : Optional[Any]			     =					num_attention_heads
				UpperCAmelCase_     : Dict			     =					intermediate_size
				UpperCAmelCase_     : List[Any]			     =					hidden_act
				UpperCAmelCase_     : Union[str, Any]			     =					hidden_dropout_prob
				UpperCAmelCase_     : List[str]			     =					attention_probs_dropout_prob
				UpperCAmelCase_     : Dict			     =					max_position_embeddings
				UpperCAmelCase_     : Dict			     =					type_vocab_size
				UpperCAmelCase_     : Any			     =					type_sequence_label_size
				UpperCAmelCase_     : Any			     =					initializer_range
				UpperCAmelCase_     : List[Any]			     =					coordinate_size
				UpperCAmelCase_     : List[Any]			     =					shape_size
				UpperCAmelCase_     : Union[str, Any]			     =					num_labels
				UpperCAmelCase_     : Any			     =					num_choices
				UpperCAmelCase_     : Tuple			     =					scope
				UpperCAmelCase_     : Any			     =					range_bbox

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





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[int]						)						->			Dict:
				UpperCAmelCase_     : Union[str, Any]			     =					ids_tensor([self.batch_size, self.text_seq_length]	,	self.vocab_size						)

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

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

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

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

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

				UpperCAmelCase_     : str			     =					LayoutLMvaConfig(
				    vocab_size=self.vocab_size	,	hidden_size=self.hidden_size	,	num_hidden_layers=self.num_hidden_layers	,	num_attention_heads=self.num_attention_heads	,	intermediate_size=self.intermediate_size	,	hidden_act=self.hidden_act	,	hidden_dropout_prob=self.hidden_dropout_prob	,	attention_probs_dropout_prob=self.attention_probs_dropout_prob	,	max_position_embeddings=self.max_position_embeddings	,	type_vocab_size=self.type_vocab_size	,	initializer_range=self.initializer_range	,	coordinate_size=self.coordinate_size	,	shape_size=self.shape_size	,	input_size=self.image_size	,	patch_size=self.patch_size	,	)

				return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels





	def      _SCREAMING_SNAKE_CASE      (      self					: str	,	lowerCAmelCase_					: Union[str, Any]	,	lowerCAmelCase_					: Dict	,	lowerCAmelCase_					: Tuple	,	lowerCAmelCase_					: str	,	lowerCAmelCase_					: Tuple	,	lowerCAmelCase_					: str	,	lowerCAmelCase_					: Dict	,	lowerCAmelCase_					: Union[str, Any]						)						->			str:
				UpperCAmelCase_     : List[str]			     =					LayoutLMvaModel(config=lowerCAmelCase_						)
				model.to(lowerCAmelCase_						)
				model.eval()

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

				# image only
				UpperCAmelCase_     : List[Any]			     =					model(pixel_values=lowerCAmelCase_						)
				self.parent.assertEqual(
				    result.last_hidden_state.shape	,	(self.batch_size, self.image_seq_length, self.hidden_size)						)





	def      _SCREAMING_SNAKE_CASE      (      self					: int	,	lowerCAmelCase_					: List[str]	,	lowerCAmelCase_					: List[Any]	,	lowerCAmelCase_					: List[str]	,	lowerCAmelCase_					: Dict	,	lowerCAmelCase_					: Optional[Any]	,	lowerCAmelCase_					: List[Any]	,	lowerCAmelCase_					: Optional[int]	,	lowerCAmelCase_					: Dict						)						->			Union[str, Any]:
				UpperCAmelCase_     : Dict			     =					self.num_labels
				UpperCAmelCase_     : int			     =					LayoutLMvaForSequenceClassification(lowerCAmelCase_						)
				model.to(lowerCAmelCase_						)
				model.eval()
				UpperCAmelCase_     : List[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      _SCREAMING_SNAKE_CASE      (      self					: List[Any]	,	lowerCAmelCase_					: Optional[int]	,	lowerCAmelCase_					: Any	,	lowerCAmelCase_					: str	,	lowerCAmelCase_					: Optional[Any]	,	lowerCAmelCase_					: str	,	lowerCAmelCase_					: Dict	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: Optional[int]						)						->			List[str]:
				UpperCAmelCase_     : Dict			     =					self.num_labels
				UpperCAmelCase_     : int			     =					LayoutLMvaForTokenClassification(config=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.text_seq_length, self.num_labels)						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[Any]	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: Union[str, Any]	,	lowerCAmelCase_					: str	,	lowerCAmelCase_					: List[str]	,	lowerCAmelCase_					: Optional[Any]	,	lowerCAmelCase_					: int	,	lowerCAmelCase_					: Tuple	,	lowerCAmelCase_					: Any						)						->			Union[str, Any]:
				UpperCAmelCase_     : str			     =					LayoutLMvaForQuestionAnswering(config=lowerCAmelCase_						)
				model.to(lowerCAmelCase_						)
				model.eval()
				UpperCAmelCase_     : int			     =					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      _SCREAMING_SNAKE_CASE      (      self					: Optional[Any]						)						->			List[Any]:
				UpperCAmelCase_     : List[str]			     =					self.prepare_config_and_inputs()
				(
				    (
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	(
				    UpperCAmelCase_
				)     ,	
				)     : Union[str, Any]			     =					config_and_inputs
				UpperCAmelCase_     : List[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 			UpperCamelCase_       (__A				,						__A				,						unittest.TestCase     ):
	__magic_name__	      =				False
	__magic_name__	      =				False
	__magic_name__	      =				False

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





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





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

				return inputs_dict





	def      _SCREAMING_SNAKE_CASE      (      self					: List[str]						)						->			str:
				self.config_tester.run_common_tests()





	def      _SCREAMING_SNAKE_CASE      (      self					: List[Any]						)						->			Optional[int]:
				UpperCAmelCase_     : Union[str, Any]			     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			Tuple:
				UpperCAmelCase_     : int			     =					self.model_tester.prepare_config_and_inputs()
				for type in ["absolute", "relative_key", "relative_key_query"]:
							UpperCAmelCase_     : List[str]			     =					type
							self.model_tester.create_and_check_model(*lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: List[Any]						)						->			Optional[Any]:
				UpperCAmelCase_     : Optional[Any]			     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[Any]						)						->			Tuple:
				UpperCAmelCase_     : Union[str, Any]			     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: int						)						->			List[str]:
				UpperCAmelCase_     : List[Any]			     =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase_						)





	@slow
	def      _SCREAMING_SNAKE_CASE      (      self					: Dict						)						->			Dict:
				for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase_     : int			     =					LayoutLMvaModel.from_pretrained(lowerCAmelCase_						)
							self.assertIsNotNone(lowerCAmelCase_						)





def 				snake_case      (   ):
			UpperCAmelCase_     : int			     =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
			return image







@require_torch
class 			UpperCamelCase_       (unittest.TestCase     ):





	@cached_property
	def      _SCREAMING_SNAKE_CASE      (      self					: List[str]						)						->			str:
				return LayoutLMvaImageProcessor(apply_ocr=lowerCAmelCase_						) if is_vision_available() else None





	@slow
	def      _SCREAMING_SNAKE_CASE      (      self					: int						)						->			List[Any]:
				UpperCAmelCase_     : Any			     =					LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"						).to(lowerCAmelCase_						)

				UpperCAmelCase_     : Union[str, Any]			     =					self.default_image_processor
				UpperCAmelCase_     : int			     =					prepare_img()
				UpperCAmelCase_     : Dict			     =					image_processor(images=lowerCAmelCase_	,	return_tensors="pt"						).pixel_values.to(lowerCAmelCase_						)

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

				# forward pass
				UpperCAmelCase_     : Optional[int]			     =					model(
				    input_ids=input_ids.to(lowerCAmelCase_						)	,	bbox=bbox.to(lowerCAmelCase_						)	,	pixel_values=pixel_values.to(lowerCAmelCase_						)	,	)

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

				UpperCAmelCase_     : Optional[Any]			     =					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"""
import os

def 				snake_case      (   ):
			with open(os.path.dirname(A__						) + "/grid.txt"						) as f:
						UpperCAmelCase_     : Any			     =					[]  # noqa: E741
						for _ in range(20						):
									l.append([int(A__						) for x in f.readline().split()]						)

						UpperCAmelCase_     : Any			     =					0

						# right
						for i in range(20						):
									for j in range(17						):
												UpperCAmelCase_     : Union[str, Any]			     =					l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3]
												if temp > maximum:
															UpperCAmelCase_     : Any			     =					temp

        # down
						for i in range(17						):
									for j in range(20						):
												UpperCAmelCase_     : List[Any]			     =					l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j]
												if temp > maximum:
															UpperCAmelCase_     : Tuple			     =					temp

        # diagonal 1
						for i in range(17						):
									for j in range(17						):
												UpperCAmelCase_     : str			     =					l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3]
												if temp > maximum:
															UpperCAmelCase_     : List[str]			     =					temp

        # diagonal 2
						for i in range(17						):
									for j in range(3	,20						):
												UpperCAmelCase_     : List[Any]			     =					l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3]
												if temp > maximum:
															UpperCAmelCase_     : List[str]			     =					temp
						return maximum


if __name__ == "__main__":
			print(solution())



