




import unittest

from transformers import LayoutLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          LayoutLMForMaskedLM,
          LayoutLMForQuestionAnswering,
          LayoutLMForSequenceClassification,
          LayoutLMForTokenClassification,
          LayoutLMModel,
      )







class 				_a   :





     '''simple docstring'''



     def __init__(							self       ,__a       ,__a=13       ,__a=7       ,__a=True       ,__a=True       ,__a=True       ,__a=True       ,__a=99       ,__a=32       ,__a=5       ,__a=4       ,__a=37       ,__a="gelu"       ,__a=0.1       ,__a=0.1       ,__a=512       ,__a=16       ,__a=2       ,__a=0.02       ,__a=3       ,__a=4       ,__a=None       ,__a=1_000       ,)			->							List[str]:
          snake_case :			Tuple					   = parent
          snake_case :			List[str]					   = batch_size
          snake_case :			List[str]					   = seq_length
          snake_case :			str					   = is_training
          snake_case :			List[str]					   = use_input_mask
          snake_case :			Tuple					   = use_token_type_ids
          snake_case :			Tuple					   = use_labels
          snake_case :			Optional[int]					   = vocab_size
          snake_case :			List[Any]					   = hidden_size
          snake_case :			Union[str, Any]					   = num_hidden_layers
          snake_case :			Optional[int]					   = num_attention_heads
          snake_case :			Tuple					   = intermediate_size
          snake_case :			Any					   = hidden_act
          snake_case :			Optional[Any]					   = hidden_dropout_prob
          snake_case :			Tuple					   = attention_probs_dropout_prob
          snake_case :			str					   = max_position_embeddings
          snake_case :			List[Any]					   = type_vocab_size
          snake_case :			Union[str, Any]					   = type_sequence_label_size
          snake_case :			int					   = initializer_range
          snake_case :			str					   = num_labels
          snake_case :			str					   = num_choices
          snake_case :			List[Any]					   = scope
          snake_case :			List[Any]					   = range_bbox







     def  snake_case_			(							self       )			->							Tuple:
          snake_case :			List[str]					   = ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size       )

          snake_case :			Dict					   = ids_tensor([self.batch_size, self.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 :			Dict					   = bbox[i, j, 1]
                         snake_case :			str					   = t
                    if bbox[i, j, 2] < bbox[i, j, 0]:
                         snake_case :			str					   = bbox[i, j, 2]
                         snake_case :			Union[str, Any]					   = bbox[i, j, 0]
                         snake_case :			List[str]					   = t

          snake_case :			Tuple					   = None
          if self.use_input_mask:
               snake_case :			int					   = ids_tensor([self.batch_size, self.seq_length]       ,vocab_size=2       )

          snake_case :			Any					   = None
          if self.use_token_type_ids:
               snake_case :			Any					   = ids_tensor([self.batch_size, self.seq_length]       ,self.type_vocab_size       )

          snake_case :			Optional[Any]					   = None
          snake_case :			List[str]					   = None
          snake_case :			Union[str, Any]					   = None
          if self.use_labels:
               snake_case :			int					   = ids_tensor([self.batch_size]       ,self.type_sequence_label_size       )
               snake_case :			str					   = ids_tensor([self.batch_size, self.seq_length]       ,self.num_labels       )
               snake_case :			Optional[Any]					   = ids_tensor([self.batch_size]       ,self.num_choices       )

          snake_case :			Union[str, Any]					   = self.get_config()

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







     def  snake_case_			(							self       )			->							int:
          return LayoutLMConfig(
              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       ,)







     def  snake_case_			(							self       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       )			->							Tuple:
          snake_case :			Union[str, Any]					   = LayoutLMModel(config=__a       )
          model.to(__a       )
          model.eval()
          snake_case :			Tuple					   = model(__a       ,__a       ,attention_mask=__a       ,token_type_ids=__a       )
          snake_case :			Dict					   = model(__a       ,__a       ,token_type_ids=__a       )
          snake_case :			str					   = model(__a       ,__a       )
          self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.hidden_size)       )
          self.parent.assertEqual(result.pooler_output.shape       ,(self.batch_size, self.hidden_size)       )







     def  snake_case_			(							self       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       )			->							Tuple:
          snake_case :			Optional[int]					   = LayoutLMForMaskedLM(config=__a       )
          model.to(__a       )
          model.eval()
          snake_case :			str					   = model(__a       ,__a       ,attention_mask=__a       ,token_type_ids=__a       ,labels=__a       )
          self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length, self.vocab_size)       )







     def  snake_case_			(							self       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       )			->							Tuple:
          snake_case :			List[Any]					   = self.num_labels
          snake_case :			Optional[int]					   = LayoutLMForSequenceClassification(__a       )
          model.to(__a       )
          model.eval()
          snake_case :			str					   = model(
              __a       ,__a       ,attention_mask=__a       ,token_type_ids=__a       ,labels=__a       )
          self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_labels)       )







     def  snake_case_			(							self       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       )			->							Optional[Any]:
          snake_case :			int					   = self.num_labels
          snake_case :			Any					   = LayoutLMForTokenClassification(config=__a       )
          model.to(__a       )
          model.eval()
          snake_case :			Any					   = model(__a       ,__a       ,attention_mask=__a       ,token_type_ids=__a       ,labels=__a       )
          self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length, self.num_labels)       )







     def  snake_case_			(							self       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       ,__a       )			->							Tuple:
          snake_case :			Dict					   = LayoutLMForQuestionAnswering(config=__a       )
          model.to(__a       )
          model.eval()
          snake_case :			Tuple					   = model(
              __a       ,bbox=__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  snake_case_			(							self       )			->							Optional[Any]:
          snake_case :			int					   = self.prepare_config_and_inputs()
          (
              snake_case		
          ) :			List[str]					   = config_and_inputs
          snake_case :			int					   = {
              """input_ids""": input_ids,
              """bbox""": bbox,
              """token_type_ids""": token_type_ids,
              """attention_mask""": input_mask,
          }
          return config, inputs_dict







@require_torch
class 				_a   (a__,		a__,		unittest.TestCase  ):





     '''simple docstring'''



     lowerCAmelCase_     :    List[str]									=				(
         (
             LayoutLMModel,
             LayoutLMForMaskedLM,
             LayoutLMForSequenceClassification,
             LayoutLMForTokenClassification,
             LayoutLMForQuestionAnswering,
         )
         if is_torch_available()
         else None
     )
     lowerCAmelCase_     :    Union[str, Any]									=				(
         {
             """document-question-answering""": LayoutLMForQuestionAnswering,
             """feature-extraction""": LayoutLMModel,
             """fill-mask""": LayoutLMForMaskedLM,
             """text-classification""": LayoutLMForSequenceClassification,
             """token-classification""": LayoutLMForTokenClassification,
             """zero-shot""": LayoutLMForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     lowerCAmelCase_     :    int									=				True







     def  snake_case_			(							self       )			->							Union[str, Any]:
          snake_case :			Optional[int]					   = LayoutLMModelTester(self       )
          snake_case :			Dict					   = ConfigTester(self       ,config_class=__a       ,hidden_size=37       )







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







     def  snake_case_			(							self       )			->							List[str]:
          snake_case :			Dict					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__a       )







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







     def  snake_case_			(							self       )			->							Tuple:
          snake_case :			Any					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*__a       )







     def  snake_case_			(							self       )			->							Tuple:
          snake_case :			Optional[Any]					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*__a       )







     def  snake_case_			(							self       )			->							Optional[Any]:
          snake_case :			Optional[Any]					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*__a       )







     def  snake_case_			(							self       )			->							List[Any]:
          snake_case :			List[str]					   = self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*__a       )







     @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""       )
     def  snake_case_			(							self       )			->							int:
          pass



def 					lowerCamelCase__							(			):
     # Here we prepare a batch of 2 sequences to test a LayoutLM forward pass on:
     # fmt: off
     snake_case :			str					   = torch.tensor([[101,1_019,1_014,1_016,1_037,12_849,4_747,1_004,14_246,2_278,5_439,4_524,5_002,2_930,2_193,2_930,4_341,3_208,1_005,1_055,2_171,2_848,11_300,3_531,102],[101,4_070,4_034,7_020,1_024,3_058,1_015,1_013,2_861,1_013,6_070,19_274,2_772,6_205,27_814,16_147,16_147,4_343,2_047,10_283,10_969,14_389,1_012,2_338,102]]    ,      device=__lowercase							)  # noqa: E231
     snake_case :			int					   = torch.tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],]    ,      device=__lowercase							)  # noqa: E231
     snake_case :			Dict					   = torch.tensor([[[0,0,0,0],[423,237,440,251],[427,272,441,287],[419,115,437,129],[961,885,992,912],[256,38,330,58],[256,38,330,58],[336,42,353,57],[360,39,401,56],[360,39,401,56],[411,39,471,59],[479,41,528,59],[533,39,630,60],[67,113,134,131],[141,115,209,132],[68,149,133,166],[141,149,187,164],[195,148,287,165],[195,148,287,165],[195,148,287,165],[295,148,349,165],[441,149,492,166],[497,149,546,164],[64,201,125,218],[1_000,1_000,1_000,1_000]],[[0,0,0,0],[662,150,754,166],[665,199,742,211],[519,213,554,228],[519,213,554,228],[134,433,187,454],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[130,467,204,480],[314,469,376,482],[504,684,582,706],[941,825,973,900],[941,825,973,900],[941,825,973,900],[941,825,973,900],[610,749,652,765],[130,659,168,672],[176,657,237,672],[238,657,312,672],[443,653,628,672],[443,653,628,672],[716,301,825,317],[1_000,1_000,1_000,1_000]]]    ,      device=__lowercase							)  # noqa: E231
     snake_case :			List[str]					   = torch.tensor([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]    ,      device=__lowercase							)  # noqa: E231
     # these are sequence labels (i.e. at the token level)
     snake_case :			str					   = torch.tensor([[-100,10,10,10,9,1,-100,7,7,-100,7,7,4,2,5,2,8,8,-100,-100,5,0,3,2,-100],[-100,12,12,12,-100,12,10,-100,-100,-100,-100,10,12,9,-100,-100,-100,10,10,10,9,12,-100,10,-100]]    ,      device=__lowercase							)  # noqa: E231
     # fmt: on

     return input_ids, attention_mask, bbox, token_type_ids, labels







@require_torch
class 				_a   (unittest.TestCase  ):





     '''simple docstring'''



     @slow
     def  snake_case_			(							self       )			->							List[Any]:
          snake_case :			Optional[Any]					   = LayoutLMModel.from_pretrained("""microsoft/layoutlm-base-uncased"""       ).to(__a       )

          snake_case :			Union[str, Any]					   = prepare_layoutlm_batch_inputs()

          # forward pass
          snake_case :			Optional[Any]					   = model(input_ids=__a       ,bbox=__a       ,attention_mask=__a       ,token_type_ids=__a       )

          # test the sequence output on [0, :3, :3]
          snake_case :			Any					   = torch.tensor(
              [[0.1_785, -0.1_947, -0.0_425], [-0.3_254, -0.2_807, 0.2_553], [-0.5_391, -0.3_322, 0.3_364]]       ,device=__a       ,)

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

          # test the pooled output on [1, :3]
          snake_case :			List[Any]					   = torch.tensor([-0.6_580, -0.0_214, 0.8_552]       ,device=__a       )

          self.assertTrue(torch.allclose(outputs.pooler_output[1, :3]       ,__a       ,atol=1E-3       )       )







     @slow
     def  snake_case_			(							self       )			->							List[Any]:
          # initialize model with randomly initialized sequence classification head
          snake_case :			Optional[Any]					   = LayoutLMForSequenceClassification.from_pretrained("""microsoft/layoutlm-base-uncased"""       ,num_labels=2       ).to(
              __a       )

          snake_case :			Optional[int]					   = prepare_layoutlm_batch_inputs()

          # forward pass
          snake_case :			str					   = model(
              input_ids=__a       ,bbox=__a       ,attention_mask=__a       ,token_type_ids=__a       ,labels=torch.tensor([1, 1]       ,device=__a       )       ,)

          # test whether we get a loss as a scalar
          snake_case :			Optional[Any]					   = outputs.loss
          snake_case :			Union[str, Any]					   = torch.Size([]       )
          self.assertEqual(loss.shape       ,__a       )

          # test the shape of the logits
          snake_case :			Tuple					   = outputs.logits
          snake_case :			Optional[int]					   = torch.Size((2, 2)       )
          self.assertEqual(logits.shape       ,__a       )







     @slow
     def  snake_case_			(							self       )			->							List[str]:
          # initialize model with randomly initialized token classification head
          snake_case :			Union[str, Any]					   = LayoutLMForTokenClassification.from_pretrained("""microsoft/layoutlm-base-uncased"""       ,num_labels=13       ).to(
              __a       )

          snake_case :			Union[str, Any]					   = prepare_layoutlm_batch_inputs()

          # forward pass
          snake_case :			int					   = model(
              input_ids=__a       ,bbox=__a       ,attention_mask=__a       ,token_type_ids=__a       ,labels=__a       )

          # test the loss calculation to be around 2.65
          # expected_loss = torch.tensor(2.65, device=torch_device)

          # The loss is currently somewhat random and can vary between 0.1-0.3 atol.
          # self.assertTrue(torch.allclose(outputs.loss, expected_loss, atol=0.1))

          # test the shape of the logits
          snake_case :			int					   = outputs.logits
          snake_case :			Any					   = torch.Size((2, 25, 13)       )
          self.assertEqual(logits.shape       ,__a       )







     @slow
     def  snake_case_			(							self       )			->							Optional[int]:
          # initialize model with randomly initialized token classification head
          snake_case :			str					   = LayoutLMForQuestionAnswering.from_pretrained("""microsoft/layoutlm-base-uncased"""       ).to(__a       )

          snake_case :			Union[str, Any]					   = prepare_layoutlm_batch_inputs()

          # forward pass
          snake_case :			Optional[int]					   = model(input_ids=__a       ,bbox=__a       ,attention_mask=__a       ,token_type_ids=__a       )

          # test the shape of the logits
          snake_case :			List[Any]					   = torch.Size((2, 25)       )
          self.assertEqual(outputs.start_logits.shape       ,__a       )
          self.assertEqual(outputs.end_logits.shape       ,__a       )









'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowercase			:      Any			=					{"""configuration_visual_bert""": ["""VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """VisualBertConfig"""]}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase			:      Optional[int]			=					[
          """VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """VisualBertForMultipleChoice""",
          """VisualBertForPreTraining""",
          """VisualBertForQuestionAnswering""",
          """VisualBertForRegionToPhraseAlignment""",
          """VisualBertForVisualReasoning""",
          """VisualBertLayer""",
          """VisualBertModel""",
          """VisualBertPreTrainedModel""",
      ]


if TYPE_CHECKING:
      from .configuration_visual_bert import VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, VisualBertConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_visual_bert import (
                VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                VisualBertForMultipleChoice,
                VisualBertForPreTraining,
                VisualBertForQuestionAnswering,
                VisualBertForRegionToPhraseAlignment,
                VisualBertForVisualReasoning,
                VisualBertLayer,
                VisualBertModel,
                VisualBertPreTrainedModel,
            )


else:
      import sys

      lowercase			:      List[Any]			=					_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



