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


  '''simple docstring'''







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







    """simple docstring"""






    __magic_name__				     =       parent
    __magic_name__				     =       batch_size
    __magic_name__				     =       num_channels
    __magic_name__				     =       image_size
    __magic_name__				     =       patch_size
    __magic_name__				     =       text_seq_length
    __magic_name__				     =       is_training
    __magic_name__				     =       use_input_mask
    __magic_name__				     =       use_token_type_ids
    __magic_name__				     =       use_labels
    __magic_name__				     =       vocab_size
    __magic_name__				     =       hidden_size
    __magic_name__				     =       num_hidden_layers
    __magic_name__				     =       num_attention_heads
    __magic_name__				     =       intermediate_size
    __magic_name__				     =       hidden_act
    __magic_name__				     =       hidden_dropout_prob
    __magic_name__				     =       attention_probs_dropout_prob
    __magic_name__				     =       max_position_embeddings
    __magic_name__				     =       type_vocab_size
    __magic_name__				     =       type_sequence_label_size
    __magic_name__				     =       initializer_range
    __magic_name__				     =       coordinate_size
    __magic_name__				     =       shape_size
    __magic_name__				     =       num_labels
    __magic_name__				     =       num_choices
    __magic_name__				     =       scope
    __magic_name__				     =       range_bbox

    # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
    __magic_name__				     =       text_seq_length
    __magic_name__				     =       (image_size // patch_size) ** 2 + 1
    __magic_name__				     =       self.text_seq_length + self.image_seq_length
  def 							_lowercase							( self			: int    )			->	Union[str, Any]:







    """simple docstring"""






    __magic_name__				     =       ids_tensor([self.batch_size, self.text_seq_length]			,							self.vocab_size    )

    __magic_name__				     =       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]:
          __magic_name__				     =       bbox[i, j, 3]
          __magic_name__				     =       bbox[i, j, 1]
          __magic_name__				     =       t
        if bbox[i, j, 2] < bbox[i, j, 0]:
          __magic_name__				     =       bbox[i, j, 2]
          __magic_name__				     =       bbox[i, j, 0]
          __magic_name__				     =       t

    __magic_name__				     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

    __magic_name__				     =       None
    if self.use_input_mask:
      __magic_name__				     =       random_attention_mask([self.batch_size, self.text_seq_length]    )

    __magic_name__				     =       None
    if self.use_token_type_ids:
      __magic_name__				     =       ids_tensor([self.batch_size, self.text_seq_length]			,							self.type_vocab_size    )

    __magic_name__				     =       None
    __magic_name__				     =       None
    if self.use_labels:
      __magic_name__				     =       ids_tensor([self.batch_size]			,							self.type_sequence_label_size    )
      __magic_name__				     =       ids_tensor([self.batch_size, self.text_seq_length]			,							self.num_labels    )

    __magic_name__				     =       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 							_lowercase							( self			: List[Any]			,							UpperCamelCase__			: Optional[Any]			,							UpperCamelCase__			: Union[str, Any]			,							UpperCamelCase__			: Dict			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: Optional[Any]			,							UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: int			,							UpperCamelCase__			: Dict    )			->	Optional[int]:







    """simple docstring"""






    __magic_name__				     =       LayoutLMvaModel(config=UpperCamelCase__    )
    model.to(UpperCamelCase__    )
    model.eval()

    # text + image
    __magic_name__				     =       model(UpperCamelCase__			,							pixel_values=UpperCamelCase__    )
    __magic_name__				     =       model(
        UpperCamelCase__			,							bbox=UpperCamelCase__			,							pixel_values=UpperCamelCase__			,							attention_mask=UpperCamelCase__			,							token_type_ids=UpperCamelCase__    )
    __magic_name__				     =       model(UpperCamelCase__			,							bbox=UpperCamelCase__			,							pixel_values=UpperCamelCase__			,							token_type_ids=UpperCamelCase__    )
    __magic_name__				     =       model(UpperCamelCase__			,							bbox=UpperCamelCase__			,							pixel_values=UpperCamelCase__    )

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

    # text only
    __magic_name__				     =       model(UpperCamelCase__    )
    self.parent.assertEqual(
        result.last_hidden_state.shape			,							(self.batch_size, self.text_seq_length, self.hidden_size)    )

    # image only
    __magic_name__				     =       model(pixel_values=UpperCamelCase__    )
    self.parent.assertEqual(
        result.last_hidden_state.shape			,							(self.batch_size, self.image_seq_length, self.hidden_size)    )
  def 							_lowercase							( self			: Union[str, Any]			,							UpperCamelCase__			: Dict			,							UpperCamelCase__			: Optional[Any]			,							UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: str			,							UpperCamelCase__			: Any			,							UpperCamelCase__			: Tuple			,							UpperCamelCase__			: List[str]			,							UpperCamelCase__			: Dict    )			->	Tuple:







    """simple docstring"""






    __magic_name__				     =       self.num_labels
    __magic_name__				     =       LayoutLMvaForSequenceClassification(UpperCamelCase__    )
    model.to(UpperCamelCase__    )
    model.eval()
    __magic_name__				     =       model(
        UpperCamelCase__			,							bbox=UpperCamelCase__			,							pixel_values=UpperCamelCase__			,							attention_mask=UpperCamelCase__			,							token_type_ids=UpperCamelCase__			,							labels=UpperCamelCase__			,							)
    self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.num_labels)    )
  def 							_lowercase							( self			: List[str]			,							UpperCamelCase__			: Union[str, Any]			,							UpperCamelCase__			: Any			,							UpperCamelCase__			: Optional[int]			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: List[str]			,							UpperCamelCase__			: str			,							UpperCamelCase__			: List[str]			,							UpperCamelCase__			: Tuple    )			->	List[Any]:







    """simple docstring"""






    __magic_name__				     =       self.num_labels
    __magic_name__				     =       LayoutLMvaForTokenClassification(config=UpperCamelCase__    )
    model.to(UpperCamelCase__    )
    model.eval()
    __magic_name__				     =       model(
        UpperCamelCase__			,							bbox=UpperCamelCase__			,							pixel_values=UpperCamelCase__			,							attention_mask=UpperCamelCase__			,							token_type_ids=UpperCamelCase__			,							labels=UpperCamelCase__			,							)
    self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.text_seq_length, self.num_labels)    )
  def 							_lowercase							( self			: Any			,							UpperCamelCase__			: Union[str, Any]			,							UpperCamelCase__			: int			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: int			,							UpperCamelCase__			: Tuple			,							UpperCamelCase__			: int			,							UpperCamelCase__			: str			,							UpperCamelCase__			: Optional[int]    )			->	List[Any]:







    """simple docstring"""






    __magic_name__				     =       LayoutLMvaForQuestionAnswering(config=UpperCamelCase__    )
    model.to(UpperCamelCase__    )
    model.eval()
    __magic_name__				     =       model(
        UpperCamelCase__			,							bbox=UpperCamelCase__			,							pixel_values=UpperCamelCase__			,							attention_mask=UpperCamelCase__			,							token_type_ids=UpperCamelCase__			,							start_positions=UpperCamelCase__			,							end_positions=UpperCamelCase__			,							)
    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 							_lowercase							( self			: int    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       self.prepare_config_and_inputs()
    (
        (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    (
        __magic_name__
    )     ,    
    )				     =       config_and_inputs
    __magic_name__				     =       {
        """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        UpperCAmelCase_			( _A  ,       _A  ,       unittest.TestCase     ):


  '''simple docstring'''







  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 							_lowercase							( self			: Any			,							UpperCamelCase__			: List[str]			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: Any			,							UpperCamelCase__			: List[Any]			,							UpperCamelCase__			: Optional[int]    )			->	List[str]:







    """simple docstring"""






    return True
  def 							_lowercase							( self			: Optional[Any]    )			->	Optional[Any]:







    """simple docstring"""






    __magic_name__				     =       LayoutLMvaModelTester(self    )
    __magic_name__				     =       ConfigTester(self			,							config_class=UpperCamelCase__			,							hidden_size=37    )
  def 							_lowercase							( self			: Tuple			,							UpperCamelCase__			: List[str]			,							UpperCamelCase__			: Any			,							UpperCamelCase__			: Union[str, Any]=False    )			->	Optional[Any]:







    """simple docstring"""






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

    return inputs_dict
  def 							_lowercase							( self			: Dict    )			->	Union[str, Any]:







    """simple docstring"""






    self.config_tester.run_common_tests()
  def 							_lowercase							( self			: Any    )			->	Optional[int]:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCamelCase__    )
  def 							_lowercase							( self			: Optional[Any]    )			->	List[str]:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
      __magic_name__				     =       type
      self.model_tester.create_and_check_model(*UpperCamelCase__    )
  def 							_lowercase							( self			: Optional[Any]    )			->	Optional[Any]:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__    )
  def 							_lowercase							( self			: str    )			->	Tuple:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__    )
  def 							_lowercase							( self			: Tuple    )			->	Any:







    """simple docstring"""






    __magic_name__				     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__    )





  @slow
  def 							_lowercase							( self			: int    )			->	Optional[Any]:







    """simple docstring"""






    for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      __magic_name__				     =       LayoutLMvaModel.from_pretrained(UpperCamelCase__    )
      self.assertIsNotNone(UpperCamelCase__    )







def a__    (       ):






  '''simple docstring'''





  __magic_name__				     =       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
  return image





@require_torch
class        UpperCAmelCase_			( unittest.TestCase     ):


  '''simple docstring'''







  @cached_property
  def 							_lowercase							( self			: Dict    )			->	int:







    """simple docstring"""






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





  @slow
  def 							_lowercase							( self			: str    )			->	Optional[Any]:







    """simple docstring"""






    __magic_name__				     =       LayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""    ).to(UpperCamelCase__    )

    __magic_name__				     =       self.default_image_processor
    __magic_name__				     =       prepare_img()
    __magic_name__				     =       image_processor(images=UpperCamelCase__			,							return_tensors="""pt"""    ).pixel_values.to(UpperCamelCase__    )

    __magic_name__				     =       torch.tensor([[1, 2]]    )
    __magic_name__				     =       torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]    ).unsqueeze(0    )

    # forward pass
    __magic_name__				     =       model(
        input_ids=input_ids.to(UpperCamelCase__    )			,							bbox=bbox.to(UpperCamelCase__    )			,							pixel_values=pixel_values.to(UpperCamelCase__    )			,							)

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

    __magic_name__				     =       torch.tensor(
        [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]    ).to(UpperCamelCase__    )

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



from typing import TYPE_CHECKING

from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowerCAmelCase			:     Any           =   {
    'configuration_mctct': ['MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MCTCTConfig'],
    'feature_extraction_mctct': ['MCTCTFeatureExtractor'],
    'processing_mctct': ['MCTCTProcessor'],
}


try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCAmelCase			:     int           =   [
        'MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST',
        'MCTCTForCTC',
        'MCTCTModel',
        'MCTCTPreTrainedModel',
    ]


if TYPE_CHECKING:
    from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig
    from .feature_extraction_mctct import MCTCTFeatureExtractor
    from .processing_mctct import MCTCTProcessor

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel

else:
    import sys

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

