



from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

from transformers import is_tf_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
         TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
         TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
         TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
         TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
         LayoutLMvaConfig,
         TFLayoutLMvaForQuestionAnswering,
         TFLayoutLMvaForSequenceClassification,
         TFLayoutLMvaForTokenClassification,
         TFLayoutLMvaModel,
     )

if is_vision_available():
     from PIL import Image

     from transformers import LayoutLMvaImageProcessor

class 							a__:







    def __init__(				self			:  Any						,      __snake_case			:  Union[str, Any]						,      __snake_case			:  List[str]=2						,      __snake_case			:  List[Any]=3						,      __snake_case			:  List[Any]=4						,      __snake_case			:  Optional[Any]=2						,      __snake_case			:  Optional[int]=7						,      __snake_case			:  List[Any]=True						,      __snake_case			:  Optional[int]=True						,      __snake_case			:  Optional[Any]=True						,      __snake_case			:  int=True						,      __snake_case			:  List[Any]=99						,      __snake_case			:  int=36						,      __snake_case			:  str=2						,      __snake_case			:  Optional[int]=4						,      __snake_case			:  Dict=37						,      __snake_case			:  Tuple="gelu"						,      __snake_case			:  List[str]=0.1						,      __snake_case			:  str=0.1						,      __snake_case			:  Dict=5_12						,      __snake_case			:  List[str]=16						,      __snake_case			:  Dict=2						,      __snake_case			:  Union[str, Any]=0.02						,      __snake_case			:  str=6						,      __snake_case			:  int=6						,      __snake_case			:  str=3						,      __snake_case			:  Optional[int]=4						,      __snake_case			:  Union[str, Any]=None						,      __snake_case			:  List[Any]=10_00						,      ):
      a						: Optional[Any]					       =					parent
      a						: List[str]					       =					batch_size
      a						: List[Any]					       =					num_channels
      a						: int					       =					image_size
      a						: Tuple					       =					patch_size
      a						: Any					       =					is_training
      a						: Tuple					       =					use_input_mask
      a						: Dict					       =					use_token_type_ids
      a						: Union[str, Any]					       =					use_labels
      a						: Tuple					       =					vocab_size
      a						: str					       =					hidden_size
      a						: Dict					       =					num_hidden_layers
      a						: Optional[Any]					       =					num_attention_heads
      a						: Optional[int]					       =					intermediate_size
      a						: str					       =					hidden_act
      a						: Tuple					       =					hidden_dropout_prob
      a						: Optional[int]					       =					attention_probs_dropout_prob
      a						: Dict					       =					max_position_embeddings
      a						: Tuple					       =					type_vocab_size
      a						: Tuple					       =					type_sequence_label_size
      a						: Dict					       =					initializer_range
      a						: Optional[int]					       =					coordinate_size
      a						: Tuple					       =					shape_size
      a						: Optional[int]					       =					num_labels
      a						: Tuple					       =					num_choices
      a						: Optional[Any]					       =					scope
      a						: str					       =					range_bbox

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







    def       lowercase_	(				self			:  Dict     ):
      a						: List[Any]					       =					ids_tensor([self.batch_size, self.text_seq_length]						,      self.vocab_size     )

      a						: Any					       =					ids_tensor([self.batch_size, self.text_seq_length, 4]						,      self.range_bbox     )
      a						: Dict					       =					bbox.numpy()
      # 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]:
            a						: int					       =					bbox[i, j, 3]
            a						: List[Any]					       =					bbox[i, j, 1]
            a						: int					       =					tmp_coordinate
          if bbox[i, j, 2] < bbox[i, j, 0]:
            a						: Any					       =					bbox[i, j, 2]
            a						: Tuple					       =					bbox[i, j, 0]
            a						: List[str]					       =					tmp_coordinate
      a						: List[str]					       =					tf.constant(__snake_case     )

      a						: List[Any]					       =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

      a						: Tuple					       =					None
      if self.use_input_mask:
        a						: str					       =					random_attention_mask([self.batch_size, self.text_seq_length]     )

      a						: Tuple					       =					None
      if self.use_token_type_ids:
        a						: Optional[int]					       =					ids_tensor([self.batch_size, self.text_seq_length]						,      self.type_vocab_size     )

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

      a						: 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       lowercase_	(				self			:  List[Any]						,      __snake_case			:  Union[str, Any]						,      __snake_case			:  Optional[int]						,      __snake_case			:  Any						,      __snake_case			:  List[Any]						,      __snake_case			:  Any						,      __snake_case			:  Any     ):
      a						: List[str]					       =					TFLayoutLMvaModel(config=__snake_case     )

      # text + image
      a						: Optional[int]					       =					model(__snake_case						,      pixel_values=__snake_case						,      training=__snake_case     )
      a						: Tuple					       =					model(
          __snake_case						,      bbox=__snake_case						,      pixel_values=__snake_case						,      attention_mask=__snake_case						,      token_type_ids=__snake_case						,      training=__snake_case						,      )
      a						: Optional[int]					       =					model(__snake_case						,      bbox=__snake_case						,      pixel_values=__snake_case						,      training=__snake_case     )

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

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

      # image only
      a						: Tuple					       =					model({'pixel_values': pixel_values}						,      training=__snake_case     )
      self.parent.assertEqual(
          result.last_hidden_state.shape						,      (self.batch_size, self.image_seq_length, self.hidden_size)     )







    def       lowercase_	(				self			:  int						,      __snake_case			:  Optional[int]						,      __snake_case			:  str						,      __snake_case			:  Optional[Any]						,      __snake_case			:  Tuple						,      __snake_case			:  Dict						,      __snake_case			:  List[str]						,      __snake_case			:  Union[str, Any]     ):
      a						: int					       =					self.num_labels
      a						: int					       =					TFLayoutLMvaForSequenceClassification(config=__snake_case     )
      a						: List[Any]					       =					model(
          __snake_case						,      bbox=__snake_case						,      pixel_values=__snake_case						,      attention_mask=__snake_case						,      token_type_ids=__snake_case						,      labels=__snake_case						,      training=__snake_case						,      )
      self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.num_labels)     )







    def       lowercase_	(				self			:  Any						,      __snake_case			:  Optional[Any]						,      __snake_case			:  List[str]						,      __snake_case			:  Tuple						,      __snake_case			:  Union[str, Any]						,      __snake_case			:  Dict						,      __snake_case			:  Tuple						,      __snake_case			:  List[Any]     ):
      a						: Union[str, Any]					       =					self.num_labels
      a						: Any					       =					TFLayoutLMvaForTokenClassification(config=__snake_case     )
      a						: List[str]					       =					model(
          __snake_case						,      bbox=__snake_case						,      pixel_values=__snake_case						,      attention_mask=__snake_case						,      token_type_ids=__snake_case						,      labels=__snake_case						,      training=__snake_case						,      )
      self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.text_seq_length, self.num_labels)     )







    def       lowercase_	(				self			:  Union[str, Any]						,      __snake_case			:  Union[str, Any]						,      __snake_case			:  Union[str, Any]						,      __snake_case			:  str						,      __snake_case			:  Dict						,      __snake_case			:  int						,      __snake_case			:  Any						,      __snake_case			:  int     ):
      a						: List[str]					       =					2
      a						: List[Any]					       =					TFLayoutLMvaForQuestionAnswering(config=__snake_case     )
      a						: List[Any]					       =					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						,      training=__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       lowercase_	(				self			:  Optional[int]     ):
      a						: Optional[int]					       =					self.prepare_config_and_inputs()
      ((a)       ,     (a)       ,     (a)       ,     (a)       ,     (a)       ,     (a)       ,     (a)       ,     (a))						: Optional[int]					       =					config_and_inputs
      a						: int					       =					{
          '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_tf
class 							a__( snake_case_		,			snake_case_		,			unittest.TestCase     ):
    lowercase__			=   (
        (
            TFLayoutLMvaModel,
            TFLayoutLMvaForQuestionAnswering,
            TFLayoutLMvaForSequenceClassification,
            TFLayoutLMvaForTokenClassification,
        )
        if is_tf_available()
        else ()
    )
    lowercase__			=   (
        {"""document-question-answering""": TFLayoutLMvaForQuestionAnswering, """feature-extraction""": TFLayoutLMvaModel}
        if is_tf_available()
        else {}
    )

    lowercase__			=   False
    lowercase__			=   False
    lowercase__			=   False







    def       lowercase_	(				self			:  Dict						,      __snake_case			:  Optional[int]						,      __snake_case			:  Optional[Any]						,      __snake_case			:  Dict						,      __snake_case			:  Tuple						,      __snake_case			:  Dict     ):
      return True







    def       lowercase_	(				self			:  List[Any]						,      __snake_case			:  Union[str, Any]						,      __snake_case			:  Dict						,      __snake_case			:  Union[str, Any]=False     ):
      a						: List[Any]					       =					copy.deepcopy(__snake_case     )

      if model_class in get_values(__snake_case     ):
        a						: List[Any]					       =					{
            k: tf.tile(tf.expand_dims(__snake_case						,      1     )						,      (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)     )
            if isinstance(__snake_case						,      tf.Tensor     ) and v.ndim > 0
            else v
            for k, v in inputs_dict.items()
        }

      if return_labels:
        if model_class in get_values(__snake_case     ):
          a						: Any					       =					tf.ones(self.model_tester.batch_size						,      dtype=tf.intaa     )
        elif model_class in get_values(__snake_case     ):
          a						: int					       =					tf.zeros(self.model_tester.batch_size						,      dtype=tf.intaa     )
          a						: str					       =					tf.zeros(self.model_tester.batch_size						,      dtype=tf.intaa     )
        elif model_class in get_values(__snake_case     ):
          a						: List[Any]					       =					tf.zeros(self.model_tester.batch_size						,      dtype=tf.intaa     )
        elif model_class in get_values(__snake_case     ):
          a						: Optional[int]					       =					tf.zeros(
              (self.model_tester.batch_size, self.model_tester.text_seq_length)						,      dtype=tf.intaa     )

      return inputs_dict







    def       lowercase_	(				self			:  int     ):
      a						: Union[str, Any]					       =					TFLayoutLMvaModelTester(self     )
      a						: str					       =					ConfigTester(self						,      config_class=__snake_case						,      hidden_size=37     )







    def       lowercase_	(				self			:  List[Any]     ):
      self.config_tester.run_common_tests()







    def       lowercase_	(				self			:  Dict     ):
      a       ,     a						: str					       =					self.model_tester.prepare_config_and_inputs_for_common()
      for model_class in self.all_model_classes:
        a						: Optional[int]					       =					model_class(__snake_case     )
        if getattr(__snake_case						,      'hf_compute_loss'						,      __snake_case     ):
          # The number of elements in the loss should be the same as the number of elements in the label
          a						: int					       =					self._prepare_for_class(inputs_dict.copy()						,      __snake_case						,      return_labels=__snake_case     )
          a						: int					       =					prepared_for_class[
              sorted(prepared_for_class.keys() - inputs_dict.keys()						,      reverse=__snake_case     )[0]
          ]
          a						: Optional[int]					       =					added_label.shape.as_list()[:1]

          # Test that model correctly compute the loss with kwargs
          a						: Tuple					       =					self._prepare_for_class(inputs_dict.copy()						,      __snake_case						,      return_labels=__snake_case     )
          a						: List[Any]					       =					prepared_for_class.pop('input_ids'     )

          a						: Tuple					       =					model(__snake_case						,      **__snake_case     )[0]
          self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]     )

          # Test that model correctly compute the loss when we mask some positions
          a						: Dict					       =					self._prepare_for_class(inputs_dict.copy()						,      __snake_case						,      return_labels=__snake_case     )
          a						: List[Any]					       =					prepared_for_class.pop('input_ids'     )
          if "labels" in prepared_for_class:
            a						: Any					       =					prepared_for_class['labels'].numpy()
            if len(labels.shape     ) > 1 and labels.shape[1] != 1:
              a						: Tuple					       =					-1_00
              a						: str					       =					tf.convert_to_tensor(__snake_case     )
              a						: List[Any]					       =					model(__snake_case						,      **__snake_case     )[0]
              self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]     )
              self.assertTrue(not np.any(np.isnan(loss.numpy()     )     )     )

                # Test that model correctly compute the loss with a dict
          a						: Optional[Any]					       =					self._prepare_for_class(inputs_dict.copy()						,      __snake_case						,      return_labels=__snake_case     )
          a						: Optional[Any]					       =					model(__snake_case     )[0]
          self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]     )

          # Test that model correctly compute the loss with a tuple
          a						: int					       =					self._prepare_for_class(inputs_dict.copy()						,      __snake_case						,      return_labels=__snake_case     )

          # Get keys that were added with the _prepare_for_class function
          a						: List[Any]					       =					prepared_for_class.keys() - inputs_dict.keys()
          a						: Any					       =					inspect.signature(model.call     ).parameters
          a						: int					       =					list(signature.keys()     )

          # Create a dictionary holding the location of the tensors in the tuple
          a						: int					       =					{0: 'input_ids'}
          for label_key in label_keys:
            a						: Optional[Any]					       =					signature_names.index(__snake_case     )
            a						: Union[str, Any]					       =					label_key
          a						: int					       =					sorted(tuple_index_mapping.items()     )
          # Initialize a list with their default values, update the values and convert to a tuple
          a						: List[str]					       =					[]

          for name in signature_names:
            if name != "kwargs":
              list_input.append(signature[name].default     )

          for index, value in sorted_tuple_index_mapping:
            a						: Optional[Any]					       =					prepared_for_class[value]

          a						: Optional[int]					       =					tuple(__snake_case     )

          # Send to model
          a						: str					       =					model(tuple_input[:-1]     )[0]

          self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]     )







    def       lowercase_	(				self			:  List[Any]     ):
      (
          (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,        
      )						: int					       =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(__snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case     )







    def       lowercase_	(				self			:  Tuple     ):
      (
          (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,        
      )						: Union[str, Any]					       =					self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        a						: Optional[Any]					       =					type
        self.model_tester.create_and_check_model(__snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case     )







    def       lowercase_	(				self			:  Optional[Any]     ):
      (
          (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,        
      )						: List[Any]					       =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(
          __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case     )







    def       lowercase_	(				self			:  Optional[Any]     ):
      (
          (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,        
      )						: List[Any]					       =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(
          __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case     )







    def       lowercase_	(				self			:  Any     ):
      (
          (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,     (
          a   
      )       ,        
      )						: Optional[int]					       =					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(
          __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case						,      __snake_case     )







    @slow
    def       lowercase_	(				self			:  Optional[int]     ):
      for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a						: Union[str, Any]					       =					TFLayoutLMvaModel.from_pretrained(__snake_case     )
        self.assertIsNotNone(__snake_case     )
def 				lowerCamelCase__	(						):
  a						: Optional[Any]					       =					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'    )
  return image

@require_tf
class 							a__( unittest.TestCase     ):







    @cached_property
    def       lowercase_	(				self			:  List[str]     ):
      return LayoutLMvaImageProcessor(apply_ocr=__snake_case     ) if is_vision_available() else None







    @slow
    def       lowercase_	(				self			:  List[str]     ):
      a						: Optional[Any]					       =					TFLayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'     )

      a						: Union[str, Any]					       =					self.default_image_processor
      a						: int					       =					prepare_img()
      a						: List[str]					       =					image_processor(images=__snake_case						,      return_tensors='tf'     ).pixel_values

      a						: str					       =					tf.constant([[1, 2]]     )
      a						: str					       =					tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]     )						,      axis=0     )

      # forward pass
      a						: Optional[int]					       =					model(input_ids=__snake_case						,      bbox=__snake_case						,      pixel_values=__snake_case						,      training=__snake_case     )

      # verify the logits
      a						: List[Any]					       =					(1, 1_99, 7_68)
      self.assertEqual(outputs.last_hidden_state.shape						,      __snake_case     )

      a						: List[Any]					       =					tf.constant(
          [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]     )

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




'''simple docstring'''






import copy

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


lowerCAmelCase: Any							=						logging.get_logger(__name__)

class 							a__( lowerCamelCase__     ):
    lowercase__			=   """encoder-decoder"""
    lowercase__			=   True







    def __init__(				self			:  Dict						,      **__snake_case			:  Union[str, Any]     ):
      super().__init__(**__snake_case     )
      assert (
          "encoder" in kwargs and "decoder" in kwargs
      ), "Config has to be initialized with encoder and decoder config"
      a						: List[str]					       =					kwargs.pop('encoder'     )
      a						: Optional[Any]					       =					encoder_config.pop('model_type'     )
      a						: Tuple					       =					kwargs.pop('decoder'     )
      a						: Optional[int]					       =					decoder_config.pop('model_type'     )

      from ..auto.configuration_auto import AutoConfig

      a						: Any					       =					AutoConfig.for_model(__snake_case						,      **__snake_case     )
      a						: Optional[int]					       =					AutoConfig.for_model(__snake_case						,      **__snake_case     )
      a						: Tuple					       =					True







    @classmethod
    def       lowercase_	(				cls			:  int						,      __snake_case			:  PretrainedConfig						,      __snake_case			:  PretrainedConfig						,      **__snake_case			:  Union[str, Any]     ):
      logger.info('Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'     )
      a						: List[Any]					       =					True
      a						: Tuple					       =					True

      return cls(encoder=encoder_config.to_dict()						,      decoder=decoder_config.to_dict()						,      **__snake_case     )







    def       lowercase_	(				self			:  List[Any]     ):
      a						: int					       =					copy.deepcopy(self.__dict__     )
      a						: List[str]					       =					self.encoder.to_dict()
      a						: Optional[int]					       =					self.decoder.to_dict()
      a						: Optional[Any]					       =					self.__class__.model_type
      return output