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



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

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



     def 			a__  (					self       ):
            _A=	ids_tensor([self.batch_size, self.text_seq_length]				,      self.vocab_size       )

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

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

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

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

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

            _A=	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 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	LayoutLMvaModel(config=_UpperCAmelCase       )
            model.to(_UpperCAmelCase       )
            model.eval()

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

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



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	self.num_labels
            _A=	LayoutLMvaForSequenceClassification(_UpperCAmelCase       )
            model.to(_UpperCAmelCase       )
            model.eval()
            _A=	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 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	self.num_labels
            _A=	LayoutLMvaForTokenClassification(config=_UpperCAmelCase       )
            model.to(_UpperCAmelCase       )
            model.eval()
            _A=	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 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            _A=	LayoutLMvaForQuestionAnswering(config=_UpperCAmelCase       )
            model.to(_UpperCAmelCase       )
            model.eval()
            _A=	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 			a__  (					self       ):
            _A=	self.prepare_config_and_inputs()
            (
                _A						
            )=	config_and_inputs
            _A=	{
                '''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      lowerCAmelCase						(					_UpperCamelCase			,		_UpperCamelCase			,		unittest.TestCase			):
     _SCREAMING_SNAKE_CASE     :     Any		=False
     _SCREAMING_SNAKE_CASE     :     List[str]		=False
     _SCREAMING_SNAKE_CASE     :     List[Any]		=False

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



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__       ):
            # `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 			a__  (					self       ):
            _A=	LayoutLMvaModelTester(self       )
            _A=	ConfigTester(self				,      config_class=_UpperCAmelCase				,      hidden_size=37       )



     def 			a__  (					self				,      lowerCAmelCase__				,      lowerCAmelCase__				,      lowerCAmelCase__=False       ):
            _A=	copy.deepcopy(_UpperCAmelCase       )
            if model_class in get_values(_UpperCAmelCase       ):
                   _A=	{
                       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       ):
                          _A=	torch.ones(self.model_tester.batch_size				,      dtype=torch.long				,      device=_UpperCAmelCase       )
                   elif model_class in get_values(_UpperCAmelCase       ):
                          _A=	torch.zeros(
                              self.model_tester.batch_size				,      dtype=torch.long				,      device=_UpperCAmelCase       )
                          _A=	torch.zeros(
                              self.model_tester.batch_size				,      dtype=torch.long				,      device=_UpperCAmelCase       )
                   elif model_class in [
                       *get_values(_UpperCAmelCase       ),
                   ]:
                          _A=	torch.zeros(
                              self.model_tester.batch_size				,      dtype=torch.long				,      device=_UpperCAmelCase       )
                   elif model_class in [
                       *get_values(_UpperCAmelCase       ),
                   ]:
                          _A=	torch.zeros(
                              (self.model_tester.batch_size, self.model_tester.text_seq_length)				,      dtype=torch.long				,      device=_UpperCAmelCase				,      )

            return inputs_dict



     def 			a__  (					self       ):
            self.config_tester.run_common_tests()



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*_UpperCAmelCase       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            for type in ["absolute", "relative_key", "relative_key_query"]:
                   _A=	type
                   self.model_tester.create_and_check_model(*_UpperCAmelCase       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase       )



     def 			a__  (					self       ):
            _A=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase       )





     @slow
     def 			a__  (					self       ):
            for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _A=	LayoutLMvaModel.from_pretrained(_UpperCAmelCase       )
                   self.assertIsNotNone(_UpperCAmelCase       )


def 							UpperCamelCase				(    )		->						Dict:




       '''simple docstring'''






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





@require_torch
class      lowerCAmelCase						(					unittest.TestCase			):



     @cached_property
     def 			a__  (					self       ):
            return LayoutLMvaImageProcessor(apply_ocr=_UpperCAmelCase       ) if is_vision_available() else None





     @slow
     def 			a__  (					self       ):
            _A=	LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'       ).to(_UpperCAmelCase       )

            _A=	self.default_image_processor
            _A=	prepare_img()
            _A=	image_processor(images=_UpperCAmelCase				,      return_tensors='pt'       ).pixel_values.to(_UpperCAmelCase       )

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

            # forward pass
            _A=	model(
                input_ids=input_ids.to(_UpperCAmelCase       )				,      bbox=bbox.to(_UpperCAmelCase       )				,      pixel_values=pixel_values.to(_UpperCAmelCase       )				,      )

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

            _A=	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 ..utils import DummyObject, requires_backends





class      lowerCAmelCase						(					metaclass=_a			):
     _SCREAMING_SNAKE_CASE     :     Optional[int]		=["""transformers""", """torch""", """note_seq"""]



     def __init__(					self				,      *lowerCAmelCase__				,      **lowerCAmelCase__       ):
            requires_backends(self				,      ['transformers', 'torch', 'note_seq']       )



     @classmethod
     def 			a__  (					cls				,      *lowerCAmelCase__				,      **lowerCAmelCase__       ):
            requires_backends(cls				,      ['transformers', 'torch', 'note_seq']       )





     @classmethod
     def 			a__  (					cls				,      *lowerCAmelCase__				,      **lowerCAmelCase__       ):
            requires_backends(cls				,      ['transformers', 'torch', 'note_seq']       )