




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:    Optional[Any]				,    _lowerCAmelCase:    Tuple				,    _lowerCAmelCase:    str=2				,    _lowerCAmelCase:    Any=3				,    _lowerCAmelCase:    List[str]=4				,    _lowerCAmelCase:    Dict=2				,    _lowerCAmelCase:    List[str]=7				,    _lowerCAmelCase:    List[str]=True				,    _lowerCAmelCase:    int=True				,    _lowerCAmelCase:    Tuple=True				,    _lowerCAmelCase:    Dict=True				,    _lowerCAmelCase:    Optional[Any]=99				,    _lowerCAmelCase:    Tuple=36				,    _lowerCAmelCase:    List[Any]=3				,    _lowerCAmelCase:    List[str]=4				,    _lowerCAmelCase:    List[Any]=37				,    _lowerCAmelCase:    List[Any]="gelu"				,    _lowerCAmelCase:    List[str]=0.1				,    _lowerCAmelCase:    Optional[Any]=0.1				,    _lowerCAmelCase:    Optional[int]=5_12				,    _lowerCAmelCase:    List[Any]=16				,    _lowerCAmelCase:    Dict=2				,    _lowerCAmelCase:    Union[str, Any]=0.02				,    _lowerCAmelCase:    List[Any]=6				,    _lowerCAmelCase:    str=6				,    _lowerCAmelCase:    Optional[int]=3				,    _lowerCAmelCase:    Any=4				,    _lowerCAmelCase:    Optional[int]=None				,    _lowerCAmelCase:    Tuple=10_00				,    ):
         lowercase			:List[str]							=  parent
         lowercase			:Tuple							=  batch_size
         lowercase			:Union[str, Any]							=  num_channels
         lowercase			:Any							=  image_size
         lowercase			:Dict							=  patch_size
         lowercase			:Optional[int]							=  text_seq_length
         lowercase			:List[str]							=  is_training
         lowercase			:int							=  use_input_mask
         lowercase			:Tuple							=  use_token_type_ids
         lowercase			:Optional[Any]							=  use_labels
         lowercase			:Optional[int]							=  vocab_size
         lowercase			:Optional[int]							=  hidden_size
         lowercase			:List[Any]							=  num_hidden_layers
         lowercase			:List[Any]							=  num_attention_heads
         lowercase			:int							=  intermediate_size
         lowercase			:Optional[int]							=  hidden_act
         lowercase			:Optional[Any]							=  hidden_dropout_prob
         lowercase			:int							=  attention_probs_dropout_prob
         lowercase			:int							=  max_position_embeddings
         lowercase			:Union[str, Any]							=  type_vocab_size
         lowercase			:Dict							=  type_sequence_label_size
         lowercase			:Optional[int]							=  initializer_range
         lowercase			:Tuple							=  coordinate_size
         lowercase			:Optional[Any]							=  shape_size
         lowercase			:Dict							=  num_labels
         lowercase			:Dict							=  num_choices
         lowercase			:int							=  scope
         lowercase			:Dict							=  range_bbox

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






       def     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         lowercase			:Optional[Any]							=  ids_tensor([self.batch_size, self.text_seq_length]				,    self.vocab_size )

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

         lowercase			:Dict							=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

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

         lowercase			:Any							=  None
         lowercase			:Any							=  None
         if self.use_labels:
           lowercase			:List[Any]							=  ids_tensor([self.batch_size]				,    self.type_sequence_label_size )
           lowercase			:Union[str, Any]							=  ids_tensor([self.batch_size, self.text_seq_length]				,    self.num_labels )

         lowercase			:Dict							=  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     SCREAMING_SNAKE_CASE			(      self:    Union[str, Any]				,    _lowerCAmelCase:    Optional[int]				,    _lowerCAmelCase:    Union[str, Any]				,    _lowerCAmelCase:    Optional[int]				,    _lowerCAmelCase:    str				,    _lowerCAmelCase:    Dict				,    _lowerCAmelCase:    List[str]				,    _lowerCAmelCase:    List[Any]				,    _lowerCAmelCase:    Dict ):
         lowercase			:Dict							=  LayoutLMvaModel(config=_lowerCAmelCase )
         model.to(_lowerCAmelCase )
         model.eval()

         # text + image
         lowercase			:List[str]							=  model(_lowerCAmelCase				,    pixel_values=_lowerCAmelCase )
         lowercase			:Optional[int]							=  model(
             _lowerCAmelCase				,    bbox=_lowerCAmelCase				,    pixel_values=_lowerCAmelCase				,    attention_mask=_lowerCAmelCase				,    token_type_ids=_lowerCAmelCase )
         lowercase			:Union[str, Any]							=  model(_lowerCAmelCase				,    bbox=_lowerCAmelCase				,    pixel_values=_lowerCAmelCase				,    token_type_ids=_lowerCAmelCase )
         lowercase			:Dict							=  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
         lowercase			:Dict							=  model(_lowerCAmelCase )
         self.parent.assertEqual(
             result.last_hidden_state.shape				,    (self.batch_size, self.text_seq_length, self.hidden_size) )

         # image only
         lowercase			:List[str]							=  model(pixel_values=_lowerCAmelCase )
         self.parent.assertEqual(
             result.last_hidden_state.shape				,    (self.batch_size, self.image_seq_length, self.hidden_size) )






       def     SCREAMING_SNAKE_CASE			(      self:    List[str]				,    _lowerCAmelCase:    Optional[int]				,    _lowerCAmelCase:    List[Any]				,    _lowerCAmelCase:    Tuple				,    _lowerCAmelCase:    Tuple				,    _lowerCAmelCase:    Tuple				,    _lowerCAmelCase:    List[Any]				,    _lowerCAmelCase:    Optional[Any]				,    _lowerCAmelCase:    Dict ):
         lowercase			:Any							=  self.num_labels
         lowercase			:Optional[int]							=  LayoutLMvaForSequenceClassification(_lowerCAmelCase )
         model.to(_lowerCAmelCase )
         model.eval()
         lowercase			:str							=  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     SCREAMING_SNAKE_CASE			(      self:    Optional[Any]				,    _lowerCAmelCase:    Dict				,    _lowerCAmelCase:    Any				,    _lowerCAmelCase:    Dict				,    _lowerCAmelCase:    List[Any]				,    _lowerCAmelCase:    List[str]				,    _lowerCAmelCase:    List[str]				,    _lowerCAmelCase:    Tuple				,    _lowerCAmelCase:    Tuple ):
         lowercase			:List[Any]							=  self.num_labels
         lowercase			:Tuple							=  LayoutLMvaForTokenClassification(config=_lowerCAmelCase )
         model.to(_lowerCAmelCase )
         model.eval()
         lowercase			:Optional[int]							=  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     SCREAMING_SNAKE_CASE			(      self:    Optional[int]				,    _lowerCAmelCase:    List[Any]				,    _lowerCAmelCase:    Union[str, Any]				,    _lowerCAmelCase:    Any				,    _lowerCAmelCase:    Union[str, Any]				,    _lowerCAmelCase:    Tuple				,    _lowerCAmelCase:    Optional[int]				,    _lowerCAmelCase:    List[Any]				,    _lowerCAmelCase:    Dict ):
         lowercase			:List[str]							=  LayoutLMvaForQuestionAnswering(config=_lowerCAmelCase )
         model.to(_lowerCAmelCase )
         model.eval()
         lowercase			:List[Any]							=  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     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         lowercase			:Optional[int]							=  self.prepare_config_and_inputs()
         (
             (
             lowercase
         )		,			(
             lowercase
         )		,			(
             lowercase
         )		,			(
             lowercase
         )		,			(
             lowercase
         )		,			(
             lowercase
         )		,			(
             lowercase
         )		,			(
             lowercase
         )		,			
         )			:int							=  config_and_inputs
         lowercase			:Union[str, 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				(							lowerCAmelCase					,       lowerCAmelCase					,       unittest.TestCase):
       _a           =  False
       _a           =  False
       _a           =  False

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






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






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

         return inputs_dict






       def     SCREAMING_SNAKE_CASE			(      self:    Tuple ):
         self.config_tester.run_common_tests()






       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         lowercase			:Union[str, Any]							=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         lowercase			:Union[str, Any]							=  self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
           lowercase			:str							=  type
           self.model_tester.create_and_check_model(*_lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         lowercase			:Dict							=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    Any ):
         lowercase			:Union[str, Any]							=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    Any ):
         lowercase			:Any							=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase )






       @slow
       def     SCREAMING_SNAKE_CASE			(      self:    int ):
         for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase			:List[str]							=  LayoutLMvaModel.from_pretrained(_lowerCAmelCase )
           self.assertIsNotNone(_lowerCAmelCase )







def 					UpperCAmelCase__     (   ):
  lowercase			:Optional[int]							=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
  return image







@require_torch
class       __lowerCAmelCase				(							unittest.TestCase):






       @cached_property
       def     SCREAMING_SNAKE_CASE			(      self:    Tuple ):
         return LayoutLMvaImageProcessor(apply_ocr=_lowerCAmelCase ) if is_vision_available() else None






       @slow
       def     SCREAMING_SNAKE_CASE			(      self:    Optional[int] ):
         lowercase			:int							=  LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base" ).to(_lowerCAmelCase )

         lowercase			:Optional[Any]							=  self.default_image_processor
         lowercase			:Optional[int]							=  prepare_img()
         lowercase			:Tuple							=  image_processor(images=_lowerCAmelCase				,    return_tensors="pt" ).pixel_values.to(_lowerCAmelCase )

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

         # forward pass
         lowercase			:List[str]							=  model(
             input_ids=input_ids.to(_lowerCAmelCase )				,    bbox=bbox.to(_lowerCAmelCase )				,    pixel_values=pixel_values.to(_lowerCAmelCase )				,    )

         # verify the logits
         lowercase			:List[str]							=  torch.Size((1, 1_99, 7_68) )
         self.assertEqual(outputs.last_hidden_state.shape				,    _lowerCAmelCase )

         lowercase			:Optional[Any]							=  torch.tensor(
             [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]] ).to(_lowerCAmelCase )

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









import itertools
from dataclasses import dataclass
from typing import Optional

import pandas as pd
import pyarrow as pa

import datasets
from datasets.table import table_cast




@dataclass
class       __lowerCAmelCase				(							datasets.BuilderConfig):
       _a           =  None







class       __lowerCAmelCase				(							datasets.ArrowBasedBuilder):
       _a           =  PandasConfig






       def     SCREAMING_SNAKE_CASE			(      self:    Any ):
         return datasets.DatasetInfo(features=self.config.features )






       def     SCREAMING_SNAKE_CASE			(      self:    str				,    _lowerCAmelCase:    List[str] ):
         if not self.config.data_files:
           raise ValueError(F"At least one data file must be specified, but got data_files={self.config.data_files}" )
         lowercase			:int							=  dl_manager.download_and_extract(self.config.data_files )
         if isinstance(_lowerCAmelCase				,    (str, list, tuple) ):
           lowercase			:Union[str, Any]							=  data_files
           if isinstance(_lowerCAmelCase				,    _lowerCAmelCase ):
             lowercase			:str							=  [files]
           # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
           lowercase			:Optional[int]							=  [dl_manager.iter_files(_lowerCAmelCase ) for file in files]
           return [datasets.SplitGenerator(name=datasets.Split.TRAIN				,    gen_kwargs={"files": files} )]
         lowercase			:List[Any]							=  []
         for split_name, files in data_files.items():
           if isinstance(_lowerCAmelCase				,    _lowerCAmelCase ):
             lowercase			:Optional[Any]							=  [files]
           # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
           lowercase			:Optional[Any]							=  [dl_manager.iter_files(_lowerCAmelCase ) for file in files]
           splits.append(datasets.SplitGenerator(name=_lowerCAmelCase				,    gen_kwargs={"files": files} ) )
         return splits






       def     SCREAMING_SNAKE_CASE			(      self:    int				,    _lowerCAmelCase:    pa.Table ):
         if self.config.features is not None:
           # more expensive cast to support nested features with keys in a different order
           # allows str <-> int/float or str to Audio for example
           lowercase			:Any							=  table_cast(_lowerCAmelCase				,    self.config.features.arrow_schema )
         return pa_table






       def     SCREAMING_SNAKE_CASE			(      self:    Optional[int]				,    _lowerCAmelCase:    Union[str, Any] ):
         for i, file in enumerate(itertools.chain.from_iterable(_lowerCAmelCase ) ):
           with open(_lowerCAmelCase				,    "rb" ) as f:
             lowercase			:int							=  pa.Table.from_pandas(pd.read_pickle(_lowerCAmelCase ) )
             yield i, self._cast_table(_lowerCAmelCase )



