





"""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 							snake_case							:

      '''simple docstring'''



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


        '''simple docstring'''




        __UpperCAmelCase    :						Dict       =						parent
        __UpperCAmelCase    :						Union[str, Any]       =						batch_size
        __UpperCAmelCase    :						Union[str, Any]       =						num_channels
        __UpperCAmelCase    :						List[str]       =						image_size
        __UpperCAmelCase    :						Tuple       =						patch_size
        __UpperCAmelCase    :						Tuple       =						text_seq_length
        __UpperCAmelCase    :						Optional[int]       =						is_training
        __UpperCAmelCase    :						Union[str, Any]       =						use_input_mask
        __UpperCAmelCase    :						Any       =						use_token_type_ids
        __UpperCAmelCase    :						Optional[Any]       =						use_labels
        __UpperCAmelCase    :						int       =						vocab_size
        __UpperCAmelCase    :						Dict       =						hidden_size
        __UpperCAmelCase    :						str       =						num_hidden_layers
        __UpperCAmelCase    :						Union[str, Any]       =						num_attention_heads
        __UpperCAmelCase    :						List[str]       =						intermediate_size
        __UpperCAmelCase    :						Union[str, Any]       =						hidden_act
        __UpperCAmelCase    :						Union[str, Any]       =						hidden_dropout_prob
        __UpperCAmelCase    :						Optional[Any]       =						attention_probs_dropout_prob
        __UpperCAmelCase    :						Optional[Any]       =						max_position_embeddings
        __UpperCAmelCase    :						List[str]       =						type_vocab_size
        __UpperCAmelCase    :						Tuple       =						type_sequence_label_size
        __UpperCAmelCase    :						str       =						initializer_range
        __UpperCAmelCase    :						Dict       =						coordinate_size
        __UpperCAmelCase    :						Optional[Any]       =						shape_size
        __UpperCAmelCase    :						str       =						num_labels
        __UpperCAmelCase    :						str       =						num_choices
        __UpperCAmelCase    :						Optional[int]       =						scope
        __UpperCAmelCase    :						Optional[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    :						int       =						text_seq_length
        __UpperCAmelCase    :						List[Any]       =						(image_size // patch_size) ** 2 + 1
        __UpperCAmelCase    :						Any       =						self.text_seq_length + self.image_seq_length

      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




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

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

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

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

        __UpperCAmelCase    :						Any       =						None
        __UpperCAmelCase    :						str       =						None
        if self.use_labels:
          __UpperCAmelCase    :						List[str]       =						ids_tensor([self.batch_size]						,				self.type_sequence_label_size )
          __UpperCAmelCase    :						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       A_					(							self       :      int						,				__lowercase       :      int						,				__lowercase       :      Any						,				__lowercase       :      Optional[int]						,				__lowercase       :      Tuple						,				__lowercase       :      int						,				__lowercase       :      Optional[int]						,				__lowercase       :      Optional[Any]						,				__lowercase       :      Union[str, Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						str       =						LayoutLMvaModel(config=__lowercase )
        model.to(__lowercase )
        model.eval()

        # text + image
        __UpperCAmelCase    :						List[str]       =						model(__lowercase						,				pixel_values=__lowercase )
        __UpperCAmelCase    :						Tuple       =						model(
            __lowercase						,				bbox=__lowercase						,				pixel_values=__lowercase						,				attention_mask=__lowercase						,				token_type_ids=__lowercase )
        __UpperCAmelCase    :						str       =						model(__lowercase						,				bbox=__lowercase						,				pixel_values=__lowercase						,				token_type_ids=__lowercase )
        __UpperCAmelCase    :						List[Any]       =						model(__lowercase						,				bbox=__lowercase						,				pixel_values=__lowercase )

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

        # text only
        __UpperCAmelCase    :						str       =						model(__lowercase )
        self.parent.assertEqual(
            result.last_hidden_state.shape						,				(self.batch_size, self.text_seq_length, self.hidden_size) )

        # image only
        __UpperCAmelCase    :						Tuple       =						model(pixel_values=__lowercase )
        self.parent.assertEqual(
            result.last_hidden_state.shape						,				(self.batch_size, self.image_seq_length, self.hidden_size) )

      def       A_					(							self       :      Optional[Any]						,				__lowercase       :      List[str]						,				__lowercase       :      Optional[Any]						,				__lowercase       :      List[Any]						,				__lowercase       :      List[str]						,				__lowercase       :      List[Any]						,				__lowercase       :      int						,				__lowercase       :      Tuple						,				__lowercase       :      Dict ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						self.num_labels
        __UpperCAmelCase    :						Tuple       =						LayoutLMvaForSequenceClassification(__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						Any       =						model(
            __lowercase						,				bbox=__lowercase						,				pixel_values=__lowercase						,				attention_mask=__lowercase						,				token_type_ids=__lowercase						,				labels=__lowercase						,				)
        self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_labels) )

      def       A_					(							self       :      Dict						,				__lowercase       :      Tuple						,				__lowercase       :      List[str]						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      Optional[Any]						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      str						,				__lowercase       :      Optional[Any]						,				__lowercase       :      Union[str, Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						int       =						self.num_labels
        __UpperCAmelCase    :						List[Any]       =						LayoutLMvaForTokenClassification(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						str       =						model(
            __lowercase						,				bbox=__lowercase						,				pixel_values=__lowercase						,				attention_mask=__lowercase						,				token_type_ids=__lowercase						,				labels=__lowercase						,				)
        self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.text_seq_length, self.num_labels) )

      def       A_					(							self       :      Optional[Any]						,				__lowercase       :      Dict						,				__lowercase       :      List[str]						,				__lowercase       :      List[str]						,				__lowercase       :      str						,				__lowercase       :      Optional[int]						,				__lowercase       :      Any						,				__lowercase       :      int						,				__lowercase       :      Any ):


        '''simple docstring'''




        __UpperCAmelCase    :						Any       =						LayoutLMvaForQuestionAnswering(config=__lowercase )
        model.to(__lowercase )
        model.eval()
        __UpperCAmelCase    :						Optional[int]       =						model(
            __lowercase						,				bbox=__lowercase						,				pixel_values=__lowercase						,				attention_mask=__lowercase						,				token_type_ids=__lowercase						,				start_positions=__lowercase						,				end_positions=__lowercase						,				)
        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       :      Dict ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						self.prepare_config_and_inputs()
        (
            (
            __UpperCAmelCase
        )       ,     (
            __UpperCAmelCase
        )       ,     (
            __UpperCAmelCase
        )       ,     (
            __UpperCAmelCase
        )       ,     (
            __UpperCAmelCase
        )       ,     (
            __UpperCAmelCase
        )       ,     (
            __UpperCAmelCase
        )       ,     (
            __UpperCAmelCase
        )       ,     
        )    :						List[str]       =						config_and_inputs
        __UpperCAmelCase    :						Tuple       =						{
            '''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							(			__UpperCAmelCase      ,  __UpperCAmelCase      ,  unittest.TestCase ):

      '''simple docstring'''



      _A     :       List[str]         =       False
      _A     :       Tuple         =       False
      _A     :       Optional[Any]         =       False

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

      def       A_					(							self       :      int						,				__lowercase       :      Optional[Any]						,				__lowercase       :      str						,				__lowercase       :      Optional[int]						,				__lowercase       :      Optional[Any]						,				__lowercase       :      Tuple ):


        '''simple docstring'''




        return True

      def       A_					(							self       :      int ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						LayoutLMvaModelTester(self )
        __UpperCAmelCase    :						str       =						ConfigTester(self						,				config_class=__lowercase						,				hidden_size=37 )

      def       A_					(							self       :      Optional[int]						,				__lowercase       :      Dict						,				__lowercase       :      Union[str, Any]						,				__lowercase       :      Tuple=False ):


        '''simple docstring'''




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

        return inputs_dict

      def       A_					(							self       :      int ):


        '''simple docstring'''




        self.config_tester.run_common_tests()

      def       A_					(							self       :      str ):


        '''simple docstring'''




        __UpperCAmelCase    :						str       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowercase )

      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Tuple       =						self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
          __UpperCAmelCase    :						Dict       =						type
          self.model_tester.create_and_check_model(*__lowercase )

      def       A_					(							self       :      Dict ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*__lowercase )

      def       A_					(							self       :      Tuple ):


        '''simple docstring'''




        __UpperCAmelCase    :						Dict       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*__lowercase )

      def       A_					(							self       :      Any ):


        '''simple docstring'''




        __UpperCAmelCase    :						Union[str, Any]       =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*__lowercase )







      @slow
      def       A_					(							self       :      str ):


        '''simple docstring'''




        for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          __UpperCAmelCase    :						Any       =						LayoutLMvaModel.from_pretrained(__lowercase )
          self.assertIsNotNone(__lowercase )


def       lowerCamelCase_       (   )						->Tuple:





  """simple docstring"""


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







@require_torch
class 							snake_case							(			unittest.TestCase ):

      '''simple docstring'''



      @cached_property
      def       A_					(							self       :      Optional[Any] ):


        '''simple docstring'''




        return LayoutLMvaImageProcessor(apply_ocr=__lowercase ) if is_vision_available() else None







      @slow
      def       A_					(							self       :      Tuple ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''' ).to(__lowercase )

        __UpperCAmelCase    :						str       =						self.default_image_processor
        __UpperCAmelCase    :						Optional[Any]       =						prepare_img()
        __UpperCAmelCase    :						Optional[Any]       =						image_processor(images=__lowercase						,				return_tensors='''pt''' ).pixel_values.to(__lowercase )

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

        # forward pass
        __UpperCAmelCase    :						str       =						model(
            input_ids=input_ids.to(__lowercase )						,				bbox=bbox.to(__lowercase )						,				pixel_values=pixel_values.to(__lowercase )						,				)

        # verify the logits
        __UpperCAmelCase    :						Any       =						torch.Size((1, 199, 768) )
        self.assertEqual(outputs.last_hidden_state.shape						,				__lowercase )

        __UpperCAmelCase    :						Union[str, 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(__lowercase )

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






"""simple docstring"""





def       lowerCamelCase_       (   UpperCAmelCase_ = 10_00  )						->int:





  """simple docstring"""


  return sum(2 * a * ((a - 1) // 2) for a in range(3  ,			n + 1  )  )


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