

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







   '''simple docstring'''




   def __init__(       self					:			int     , __lowerCamelCase					:			List[str]     , __lowerCamelCase					:			Dict=2     , __lowerCamelCase					:			List[Any]=3     , __lowerCamelCase					:			Optional[Any]=4     , __lowerCamelCase					:			Union[str, Any]=2     , __lowerCamelCase					:			Any=7     , __lowerCamelCase					:			Optional[int]=True     , __lowerCamelCase					:			List[str]=True     , __lowerCamelCase					:			Optional[int]=True     , __lowerCamelCase					:			Optional[Any]=True     , __lowerCamelCase					:			Union[str, Any]=99     , __lowerCamelCase					:			Dict=36     , __lowerCamelCase					:			Optional[int]=3     , __lowerCamelCase					:			str=4     , __lowerCamelCase					:			Union[str, Any]=37     , __lowerCamelCase					:			List[str]="gelu"     , __lowerCamelCase					:			Union[str, Any]=0.1     , __lowerCamelCase					:			List[Any]=0.1     , __lowerCamelCase					:			List[str]=512     , __lowerCamelCase					:			Any=16     , __lowerCamelCase					:			List[str]=2     , __lowerCamelCase					:			Any=0.02     , __lowerCamelCase					:			List[str]=6     , __lowerCamelCase					:			Dict=6     , __lowerCamelCase					:			int=3     , __lowerCamelCase					:			Union[str, Any]=4     , __lowerCamelCase					:			Any=None     , __lowerCamelCase					:			Any=1000     , ):
       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      _snake_case		(       self					:			Dict			):
       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      _snake_case		(       self					:			List[str]     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			List[str]     , __lowerCamelCase					:			Any     , __lowerCamelCase					:			List[str]     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			Optional[int]     , __lowerCamelCase					:			Optional[Any]			):
       SCREAMING_SNAKE_CASE										=   LayoutLMvaModel(config=__lowerCamelCase			)
       model.to(__lowerCamelCase			)
       model.eval()

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





   def      _snake_case		(       self					:			List[str]     , __lowerCamelCase					:			int     , __lowerCamelCase					:			List[Any]     , __lowerCamelCase					:			int     , __lowerCamelCase					:			List[str]     , __lowerCamelCase					:			Optional[int]     , __lowerCamelCase					:			Optional[int]     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			Optional[Any]			):
       SCREAMING_SNAKE_CASE										=   self.num_labels
       SCREAMING_SNAKE_CASE										=   LayoutLMvaForSequenceClassification(__lowerCamelCase			)
       model.to(__lowerCamelCase			)
       model.eval()
       SCREAMING_SNAKE_CASE										=   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      _snake_case		(       self					:			Union[str, Any]     , __lowerCamelCase					:			List[str]     , __lowerCamelCase					:			int     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			List[Any]     , __lowerCamelCase					:			List[Any]     , __lowerCamelCase					:			int     , __lowerCamelCase					:			List[str]     , __lowerCamelCase					:			str			):
       SCREAMING_SNAKE_CASE										=   self.num_labels
       SCREAMING_SNAKE_CASE										=   LayoutLMvaForTokenClassification(config=__lowerCamelCase			)
       model.to(__lowerCamelCase			)
       model.eval()
       SCREAMING_SNAKE_CASE										=   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      _snake_case		(       self					:			Optional[int]     , __lowerCamelCase					:			List[Any]     , __lowerCamelCase					:			Tuple     , __lowerCamelCase					:			Union[str, Any]     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			Union[str, Any]     , __lowerCamelCase					:			Any			):
       SCREAMING_SNAKE_CASE										=   LayoutLMvaForQuestionAnswering(config=__lowerCamelCase			)
       model.to(__lowerCamelCase			)
       model.eval()
       SCREAMING_SNAKE_CASE										=   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      _snake_case		(       self					:			List[str]			):
       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 			_SCREAMING_SNAKE_CASE (   __snake_case					,       __snake_case					,       unittest.TestCase		):







   '''simple docstring'''




   lowerCamelCase__				=      False
   lowerCamelCase__				=      False
   lowerCamelCase__				=      False

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





   def      _snake_case		(       self					:			Any     , __lowerCamelCase					:			str     , __lowerCamelCase					:			int     , __lowerCamelCase					:			Tuple     , __lowerCamelCase					:			List[Any]     , __lowerCamelCase					:			Union[str, Any]			):
       # `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      _snake_case		(       self					:			str			):
       SCREAMING_SNAKE_CASE										=   LayoutLMvaModelTester(self			)
       SCREAMING_SNAKE_CASE										=   ConfigTester(self     , config_class=__lowerCamelCase     , hidden_size=37			)





   def      _snake_case		(       self					:			List[Any]     , __lowerCamelCase					:			Optional[int]     , __lowerCamelCase					:			str     , __lowerCamelCase					:			List[Any]=False			):
       SCREAMING_SNAKE_CASE										=   copy.deepcopy(__lowerCamelCase			)
       if model_class in get_values(__lowerCamelCase			):
           SCREAMING_SNAKE_CASE										=   {
               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			):
               SCREAMING_SNAKE_CASE										=   torch.ones(self.model_tester.batch_size     , dtype=torch.long     , device=__lowerCamelCase			)
           elif model_class in get_values(__lowerCamelCase			):
               SCREAMING_SNAKE_CASE										=   torch.zeros(
                   self.model_tester.batch_size     , dtype=torch.long     , device=__lowerCamelCase			)
               SCREAMING_SNAKE_CASE										=   torch.zeros(
                   self.model_tester.batch_size     , dtype=torch.long     , device=__lowerCamelCase			)
           elif model_class in [
               *get_values(__lowerCamelCase			),
           ]:
               SCREAMING_SNAKE_CASE										=   torch.zeros(
                   self.model_tester.batch_size     , dtype=torch.long     , device=__lowerCamelCase			)
           elif model_class in [
               *get_values(__lowerCamelCase			),
           ]:
               SCREAMING_SNAKE_CASE										=   torch.zeros(
                   (self.model_tester.batch_size, self.model_tester.text_seq_length)     , dtype=torch.long     , device=__lowerCamelCase     , )

       return inputs_dict





   def      _snake_case		(       self					:			Any			):
       self.config_tester.run_common_tests()





   def      _snake_case		(       self					:			Dict			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowerCamelCase			)





   def      _snake_case		(       self					:			Optional[int]			):
       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(*__lowerCamelCase			)





   def      _snake_case		(       self					:			List[Any]			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase			)





   def      _snake_case		(       self					:			Any			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase			)





   def      _snake_case		(       self					:			List[Any]			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase			)





   @slow
   def      _snake_case		(       self					:			Optional[Any]			):
       for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           SCREAMING_SNAKE_CASE										=   LayoutLMvaModel.from_pretrained(__lowerCamelCase			)
           self.assertIsNotNone(__lowerCamelCase			)





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





@require_torch
class 			_SCREAMING_SNAKE_CASE (   unittest.TestCase		):







   '''simple docstring'''




   @cached_property
   def      _snake_case		(       self					:			List[Any]			):
       return LayoutLMvaImageProcessor(apply_ocr=__lowerCamelCase			) if is_vision_available() else None





   @slow
   def      _snake_case		(       self					:			Optional[Any]			):
       SCREAMING_SNAKE_CASE										=   LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"			).to(__lowerCamelCase			)

       SCREAMING_SNAKE_CASE										=   self.default_image_processor
       SCREAMING_SNAKE_CASE										=   prepare_img()
       SCREAMING_SNAKE_CASE										=   image_processor(images=__lowerCamelCase     , return_tensors="pt"			).pixel_values.to(__lowerCamelCase			)

       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(__lowerCamelCase			)     , bbox=bbox.to(__lowerCamelCase			)     , pixel_values=pixel_values.to(__lowerCamelCase			)     , )

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

       SCREAMING_SNAKE_CASE										=   torch.tensor(
           [[-0.0_529, 0.3_618, 0.1_632], [-0.1_587, -0.1_667, -0.0_400], [-0.1_557, -0.1_671, -0.0_505]]			).to(__lowerCamelCase			)

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


from collections.abc import Callable

import numpy as np





def 	__a     (      A__			:  Callable			,  A__			:  float			,  A__			:  float			,  A__			:  float			,  A__			:  float       ):
    SCREAMING_SNAKE_CASE										=   int(np.ceil((x_end - xa) / step_size       )       )
    SCREAMING_SNAKE_CASE										=   np.zeros((n + 1,)       )
    SCREAMING_SNAKE_CASE										=   ya
    SCREAMING_SNAKE_CASE										=   xa

    for k in range(A__       ):
        SCREAMING_SNAKE_CASE										=   y[k] + step_size * ode_func(A__			,  y[k]       )
        SCREAMING_SNAKE_CASE										=   y[k] + (
            (step_size / 2) * (ode_func(A__			,  y[k]       ) + ode_func(x + step_size			,  A__       ))
        )
        x += step_size

    return y


if __name__ == "__main__":
    import doctest

    doctest.testmod()