






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






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

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






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Union[str, Any]:
						SCREAMING_SNAKE_CASE						= ids_tensor([self.batch_size, self.text_seq_length]		,					self.vocab_size)

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

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

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

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

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

						SCREAMING_SNAKE_CASE						= 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		,					a		,					a		,					a		,					a		,					a		,					a		,					a		,					a)      ->       Any:
						SCREAMING_SNAKE_CASE						= LayoutLMvaModel(config=a)
						model.to(a)
						model.eval()

						# text + image
						SCREAMING_SNAKE_CASE						= model(a		,					pixel_values=a)
						SCREAMING_SNAKE_CASE						= model(
						    a		,					bbox=a		,					pixel_values=a		,					attention_mask=a		,					token_type_ids=a)
						SCREAMING_SNAKE_CASE						= model(a		,					bbox=a		,					pixel_values=a		,					token_type_ids=a)
						SCREAMING_SNAKE_CASE						= model(a		,					bbox=a		,					pixel_values=a)

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

						# text only
						SCREAMING_SNAKE_CASE						= model(a)
						self.parent.assertEqual(
						    result.last_hidden_state.shape		,					(self.batch_size, self.text_seq_length, self.hidden_size))

						# image only
						SCREAMING_SNAKE_CASE						= model(pixel_values=a)
						self.parent.assertEqual(
						    result.last_hidden_state.shape		,					(self.batch_size, self.image_seq_length, self.hidden_size))






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a		,					a		,					a		,					a		,					a		,					a)      ->       Optional[int]:
						SCREAMING_SNAKE_CASE						= self.num_labels
						SCREAMING_SNAKE_CASE						= LayoutLMvaForSequenceClassification(a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(
						    a		,					bbox=a		,					pixel_values=a		,					attention_mask=a		,					token_type_ids=a		,					labels=a		,					)
						self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.num_labels))






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a		,					a		,					a		,					a		,					a		,					a)      ->       int:
						SCREAMING_SNAKE_CASE						= self.num_labels
						SCREAMING_SNAKE_CASE						= LayoutLMvaForTokenClassification(config=a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(
						    a		,					bbox=a		,					pixel_values=a		,					attention_mask=a		,					token_type_ids=a		,					labels=a		,					)
						self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.text_seq_length, self.num_labels))






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a		,					a		,					a		,					a		,					a		,					a)      ->       Tuple:
						SCREAMING_SNAKE_CASE						= LayoutLMvaForQuestionAnswering(config=a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(
						    a		,					bbox=a		,					pixel_values=a		,					attention_mask=a		,					token_type_ids=a		,					start_positions=a		,					end_positions=a		,					)
						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)      ->       Any:
						SCREAMING_SNAKE_CASE						= self.prepare_config_and_inputs()
						(
						    (
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						
						)						= config_and_inputs
						SCREAMING_SNAKE_CASE						= {
						    '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 		_snake_case  (  A__   ,    A__   ,    unittest.TestCase					):
			_lowercase		:    Optional[Any]			     =							False
			_lowercase		:    Tuple			     =							False
			_lowercase		:    Dict			     =							False

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






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a		,					a		,					a)      ->       Dict:
						# `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:
						SCREAMING_SNAKE_CASE						= LayoutLMvaModelTester(self)
						SCREAMING_SNAKE_CASE						= ConfigTester(self		,					config_class=a		,					hidden_size=37)






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

						return inputs_dict






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Tuple:
						self.config_tester.run_common_tests()






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Any:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[Any]:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
									SCREAMING_SNAKE_CASE						= type
									self.model_tester.create_and_check_model(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       int:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       List[Any]:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Any:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*a)







			@slow
			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Any:
						for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE						= LayoutLMvaModel.from_pretrained(a)
									self.assertIsNotNone(a)






def 				lowerCamelCase__				():
			SCREAMING_SNAKE_CASE						= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
			return image







@require_torch
class 		_snake_case  (  unittest.TestCase					):






			@cached_property
			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Dict:
						return LayoutLMvaImageProcessor(apply_ocr=a) if is_vision_available() else None







			@slow
			def    SCREAMING_SNAKE_CASE__    (       self)      ->       int:
						SCREAMING_SNAKE_CASE						= LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base').to(a)

						SCREAMING_SNAKE_CASE						= self.default_image_processor
						SCREAMING_SNAKE_CASE						= prepare_img()
						SCREAMING_SNAKE_CASE						= image_processor(images=a		,					return_tensors='pt').pixel_values.to(a)

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

						# forward pass
						SCREAMING_SNAKE_CASE						= model(
						    input_ids=input_ids.to(a)		,					bbox=bbox.to(a)		,					pixel_values=pixel_values.to(a)		,					)

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

						SCREAMING_SNAKE_CASE						= 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(a)

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












from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
				from ..image_utils import load_image


if is_torch_available():
				import torch

				from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING

a_		:						str             =    logging.get_logger(__name__)


a_		:						List[Any]             =    Dict[str, Any]
a_		:						Optional[int]             =    List[Prediction]

@add_end_docstrings(A__					)
class 		_snake_case  (  A__					):






			def __init__(       self		,					*a		,					**a)      ->       int:
						super().__init__(*a		,					**a)

						if self.framework == "tf":
									raise ValueError(f'''The {self.__class__} is only available in PyTorch.''')

						requires_backends(self		,					'vision')
						self.check_model_type(
						    dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items()))






			def    SCREAMING_SNAKE_CASE__    (       self		,					**a)      ->       int:
						SCREAMING_SNAKE_CASE						= {}
						if "threshold" in kwargs:
									SCREAMING_SNAKE_CASE						= kwargs['threshold']
						return {}, {}, postprocess_kwargs






			def __call__(       self		,					*a		,					**a)      ->       Union[Predictions, List[Prediction]]:
						return super().__call__(*a		,					**a)






			def    SCREAMING_SNAKE_CASE__    (       self		,					a)      ->       List[str]:
						SCREAMING_SNAKE_CASE						= load_image(a)
						SCREAMING_SNAKE_CASE						= torch.IntTensor([[image.height, image.width]])
						SCREAMING_SNAKE_CASE						= self.image_processor(images=[image]		,					return_tensors='pt')
						if self.tokenizer is not None:
									SCREAMING_SNAKE_CASE						= self.tokenizer(text=inputs['words']		,					boxes=inputs['boxes']		,					return_tensors='pt')
						SCREAMING_SNAKE_CASE						= target_size
						return inputs






			def    SCREAMING_SNAKE_CASE__    (       self		,					a)      ->       int:
						SCREAMING_SNAKE_CASE						= model_inputs.pop('target_size')
						SCREAMING_SNAKE_CASE						= self.model(**a)
						SCREAMING_SNAKE_CASE						= outputs.__class__({'target_size': target_size, **outputs})
						if self.tokenizer is not None:
									SCREAMING_SNAKE_CASE						= model_inputs['bbox']
						return model_outputs






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a=0.9)      ->       Dict:
						SCREAMING_SNAKE_CASE						= model_outputs['target_size']
						if self.tokenizer is not None:
									# This is a LayoutLMForTokenClassification variant.
									# The OCR got the boxes and the model classified the words.
									SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						= target_size[0].tolist()

									def unnormalize(a):
												return self._get_bounding_box(
												    torch.Tensor(
												        [
												            (width * bbox[0] / 1000),
												            (height * bbox[1] / 1000),
												            (width * bbox[2] / 1000),
												            (height * bbox[3] / 1000),
												        ]))

									SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						= model_outputs['logits'].squeeze(0).softmax(dim=-1).max(dim=-1)
									SCREAMING_SNAKE_CASE						= [self.model.config.idalabel[prediction] for prediction in classes.tolist()]
									SCREAMING_SNAKE_CASE						= [unnormalize(a) for bbox in model_outputs['bbox'].squeeze(0)]
									SCREAMING_SNAKE_CASE						= ['score', 'label', 'box']
									SCREAMING_SNAKE_CASE						= [dict(zip(a		,					a)) for vals in zip(scores.tolist()		,					a		,					a) if vals[0] > threshold]
						else:
									# This is a regular ForObjectDetectionModel
									SCREAMING_SNAKE_CASE						= self.image_processor.post_process_object_detection(a		,					a		,					a)
									SCREAMING_SNAKE_CASE						= raw_annotations[0]
									SCREAMING_SNAKE_CASE						= raw_annotation['scores']
									SCREAMING_SNAKE_CASE						= raw_annotation['labels']
									SCREAMING_SNAKE_CASE						= raw_annotation['boxes']

									SCREAMING_SNAKE_CASE						= scores.tolist()
									SCREAMING_SNAKE_CASE						= [self.model.config.idalabel[label.item()] for label in labels]
									SCREAMING_SNAKE_CASE						= [self._get_bounding_box(a) for box in boxes]

									# {"scores": [...], ...} --> [{"score":x, ...}, ...]
									SCREAMING_SNAKE_CASE						= ['score', 'label', 'box']
									SCREAMING_SNAKE_CASE						= [
									    dict(zip(a		,					a))
									    for vals in zip(raw_annotation['scores']		,					raw_annotation['labels']		,					raw_annotation['boxes'])
									]

						return annotation







			def    SCREAMING_SNAKE_CASE__    (       self		,					a)      ->       Dict[str, int]:
						if self.framework != "pt":
									raise ValueError('The ObjectDetectionPipeline is only available in PyTorch.')
						SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						= box.int().tolist()
						SCREAMING_SNAKE_CASE						= {
						    'xmin': xmin,
						    'ymin': ymin,
						    'xmax': xmax,
						    'ymax': ymax,
						}
						return bbox




