


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






    '''simple docstring'''





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





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





    def 					_lowerCamelCase (	self				:   Tuple	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[int]						=   ids_tensor([self.batch_size, self.text_seq_length]       ,   self.vocab_size	)

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

       UpperCAmelCase_							:      str						=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

       UpperCAmelCase_							:      Optional[int]						=   None
       if self.use_token_type_ids:
          UpperCAmelCase_							:      List[Any]						=   ids_tensor([self.batch_size, self.text_seq_length]       ,   self.type_vocab_size	)

       UpperCAmelCase_							:      Tuple						=   None
       UpperCAmelCase_							:      Any						=   None
       if self.use_labels:
          UpperCAmelCase_							:      List[Any]						=   ids_tensor([self.batch_size]       ,   self.type_sequence_label_size	)
          UpperCAmelCase_							:      Any						=   ids_tensor([self.batch_size, self.text_seq_length]       ,   self.num_labels	)

       UpperCAmelCase_							:      Optional[Any]						=   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 					_lowerCamelCase (	self				:   Any       ,   __snake_case				:   Dict       ,   __snake_case				:   Tuple       ,   __snake_case				:   Dict       ,   __snake_case				:   str       ,   __snake_case				:   int       ,   __snake_case				:   Dict       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[Any]						=   LayoutLMvaModel(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()

       # text + image
       UpperCAmelCase_							:      Dict						=   model(__snake_case       ,   pixel_values=__snake_case	)
       UpperCAmelCase_							:      str						=   model(
           __snake_case       ,   bbox=__snake_case       ,   pixel_values=__snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case	)
       UpperCAmelCase_							:      str						=   model(__snake_case       ,   bbox=__snake_case       ,   pixel_values=__snake_case       ,   token_type_ids=__snake_case	)
       UpperCAmelCase_							:      Tuple						=   model(__snake_case       ,   bbox=__snake_case       ,   pixel_values=__snake_case	)

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

       # text only
       UpperCAmelCase_							:      Optional[Any]						=   model(__snake_case	)
       self.parent.assertEqual(
           result.last_hidden_state.shape       ,   (self.batch_size, self.text_seq_length, self.hidden_size)	)

       # image only
       UpperCAmelCase_							:      Dict						=   model(pixel_values=__snake_case	)
       self.parent.assertEqual(
           result.last_hidden_state.shape       ,   (self.batch_size, self.image_seq_length, self.hidden_size)	)





    def 					_lowerCamelCase (	self				:   Union[str, Any]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   Any       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Tuple       ,   __snake_case				:   str       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   self.num_labels
       UpperCAmelCase_							:      Any						=   LayoutLMvaForSequenceClassification(__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      Union[str, Any]						=   model(
           __snake_case       ,   bbox=__snake_case       ,   pixel_values=__snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   labels=__snake_case       ,   )
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_labels)	)





    def 					_lowerCamelCase (	self				:   List[Any]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   List[str]       ,   __snake_case				:   Any       ,   __snake_case				:   Dict       ,   __snake_case				:   List[Any]       ,   __snake_case				:   str       ,   __snake_case				:   Tuple	):





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   self.num_labels
       UpperCAmelCase_							:      Any						=   LayoutLMvaForTokenClassification(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      str						=   model(
           __snake_case       ,   bbox=__snake_case       ,   pixel_values=__snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   labels=__snake_case       ,   )
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.text_seq_length, self.num_labels)	)





    def 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   Tuple       ,   __snake_case				:   str       ,   __snake_case				:   str       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   List[Any]       ,   __snake_case				:   Optional[int]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Dict						=   LayoutLMvaForQuestionAnswering(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      str						=   model(
           __snake_case       ,   bbox=__snake_case       ,   pixel_values=__snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   start_positions=__snake_case       ,   end_positions=__snake_case       ,   )
       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 					_lowerCamelCase (	self				:   Optional[int]	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[str]						=   self.prepare_config_and_inputs()
       (
           (
           UpperCAmelCase_       
       )		,      (
           UpperCAmelCase_       
       )		,      (
           UpperCAmelCase_       
       )		,      (
           UpperCAmelCase_       
       )		,      (
           UpperCAmelCase_       
       )		,      (
           UpperCAmelCase_       
       )		,      (
           UpperCAmelCase_       
       )		,      (
           UpperCAmelCase_       
       )		,             
       )							:      Union[str, Any]						=   config_and_inputs
       UpperCAmelCase_							:      Any						=   {
           '''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						):






    '''simple docstring'''





    A_  :     Dict			= False
    A_  :     List[Any]			= False
    A_  :     Optional[Any]			= False

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





    def 					_lowerCamelCase (	self				:   List[str]       ,   __snake_case				:   int       ,   __snake_case				:   List[str]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   int       ,   __snake_case				:   Any	):





       '''simple docstring'''
       # `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 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[Any]						=   LayoutLMvaModelTester(self	)
       UpperCAmelCase_							:      Optional[int]						=   ConfigTester(self       ,   config_class=__snake_case       ,   hidden_size=37	)





    def 					_lowerCamelCase (	self				:   List[Any]       ,   __snake_case				:   str       ,   __snake_case				:   List[Any]       ,   __snake_case				:   Tuple=False	):





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

       return inputs_dict





    def 					_lowerCamelCase (	self				:   Optional[Any]	):





       '''simple docstring'''
       self.config_tester.run_common_tests()





    def 					_lowerCamelCase (	self				:   Any	):





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__snake_case	)





    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      str						=   self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
          UpperCAmelCase_							:      Any						=   type
          self.model_tester.create_and_check_model(*__snake_case	)





    def 					_lowerCamelCase (	self				:   Any	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[str]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*__snake_case	)





    def 					_lowerCamelCase (	self				:   List[str]	):





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*__snake_case	)





    def 					_lowerCamelCase (	self				:   Optional[Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*__snake_case	)





    @slow
    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          UpperCAmelCase_							:      int						=   LayoutLMvaModel.from_pretrained(__snake_case	)
          self.assertIsNotNone(__snake_case	)





def      snake_case_						( ):
   UpperCAmelCase_							:      Optional[int]						=   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
   return image





@require_torch
class  lowerCAmelCase__(    unittest.TestCase						):






    '''simple docstring'''





    @cached_property
    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       return LayoutLMvaImageProcessor(apply_ocr=__snake_case	) if is_vision_available() else None





    @slow
    def 					_lowerCamelCase (	self				:   Tuple	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''	).to(__snake_case	)

       UpperCAmelCase_							:      Optional[int]						=   self.default_image_processor
       UpperCAmelCase_							:      Tuple						=   prepare_img()
       UpperCAmelCase_							:      Optional[int]						=   image_processor(images=__snake_case       ,   return_tensors='''pt'''	).pixel_values.to(__snake_case	)

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

       # forward pass
       UpperCAmelCase_							:      Dict						=   model(
           input_ids=input_ids.to(__snake_case	)       ,   bbox=bbox.to(__snake_case	)       ,   pixel_values=pixel_values.to(__snake_case	)       ,   )

       # verify the logits
       UpperCAmelCase_							:      str						=   torch.Size((1, 199, 768)	)
       self.assertEqual(outputs.last_hidden_state.shape       ,   __snake_case	)

       UpperCAmelCase_							:      int						=   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(__snake_case	)

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



from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCamelCase				:  int      					=							logging.get_logger(__name__)

__UpperCamelCase				:  Union[str, Any]      					=							{
    'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json',
}





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     str			= 'timesformer'





    def __init__(	self				:   int       ,   __snake_case				:   Any=224       ,   __snake_case				:   str=16       ,   __snake_case				:   Any=3       ,   __snake_case				:   List[Any]=8       ,   __snake_case				:   Dict=768       ,   __snake_case				:   Dict=12       ,   __snake_case				:   Tuple=12       ,   __snake_case				:   Dict=3_072       ,   __snake_case				:   str="gelu"       ,   __snake_case				:   Union[str, Any]=0.0       ,   __snake_case				:   List[Any]=0.0       ,   __snake_case				:   Tuple=0.02       ,   __snake_case				:   Optional[Any]=1E-6       ,   __snake_case				:   List[Any]=True       ,   __snake_case				:   List[str]="divided_space_time"       ,   __snake_case				:   Optional[int]=0       ,   **__snake_case				:   Dict       ,   ):





       '''simple docstring'''
       super().__init__(**__snake_case	)

       UpperCAmelCase_							:      Optional[int]						=   image_size
       UpperCAmelCase_							:      Optional[Any]						=   patch_size
       UpperCAmelCase_							:      Any						=   num_channels
       UpperCAmelCase_							:      int						=   num_frames

       UpperCAmelCase_							:      List[Any]						=   hidden_size
       UpperCAmelCase_							:      Any						=   num_hidden_layers
       UpperCAmelCase_							:      Dict						=   num_attention_heads
       UpperCAmelCase_							:      List[Any]						=   intermediate_size
       UpperCAmelCase_							:      str						=   hidden_act
       UpperCAmelCase_							:      List[str]						=   hidden_dropout_prob
       UpperCAmelCase_							:      List[str]						=   attention_probs_dropout_prob
       UpperCAmelCase_							:      Optional[int]						=   initializer_range
       UpperCAmelCase_							:      List[str]						=   layer_norm_eps
       UpperCAmelCase_							:      str						=   qkv_bias

       UpperCAmelCase_							:      Dict						=   attention_type
       UpperCAmelCase_							:      str						=   drop_path_rate