


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


       """simple docstring"""




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


             '''simple docstring'''



             lowerCamelCase__		:				Any    =  parent
             lowerCamelCase__		:				Optional[Any]    =  batch_size
             lowerCamelCase__		:				Union[str, Any]    =  num_channels
             lowerCamelCase__		:				Optional[int]    =  image_size
             lowerCamelCase__		:				int    =  patch_size
             lowerCamelCase__		:				List[Any]    =  is_training
             lowerCamelCase__		:				Optional[Any]    =  use_input_mask
             lowerCamelCase__		:				str    =  use_token_type_ids
             lowerCamelCase__		:				str    =  use_labels
             lowerCamelCase__		:				Union[str, Any]    =  vocab_size
             lowerCamelCase__		:				Optional[Any]    =  hidden_size
             lowerCamelCase__		:				Dict    =  num_hidden_layers
             lowerCamelCase__		:				List[Any]    =  num_attention_heads
             lowerCamelCase__		:				Optional[int]    =  intermediate_size
             lowerCamelCase__		:				Optional[Any]    =  hidden_act
             lowerCamelCase__		:				Dict    =  hidden_dropout_prob
             lowerCamelCase__		:				List[str]    =  attention_probs_dropout_prob
             lowerCamelCase__		:				Optional[Any]    =  max_position_embeddings
             lowerCamelCase__		:				Tuple    =  type_vocab_size
             lowerCamelCase__		:				Optional[Any]    =  type_sequence_label_size
             lowerCamelCase__		:				Union[str, Any]    =  initializer_range
             lowerCamelCase__		:				List[str]    =  coordinate_size
             lowerCamelCase__		:				Optional[Any]    =  shape_size
             lowerCamelCase__		:				Union[str, Any]    =  num_labels
             lowerCamelCase__		:				Union[str, Any]    =  num_choices
             lowerCamelCase__		:				List[str]    =  scope
             lowerCamelCase__		:				List[str]    =  range_bbox

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





       def 						lowerCAmelCase     (   self							:			List[str]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Optional[int]    =  ids_tensor([self.batch_size, self.text_seq_length]					,      self.vocab_size       )

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

             lowerCamelCase__		:				int    =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

             lowerCamelCase__		:				Optional[int]    =  None
             if self.use_input_mask:
                   lowerCamelCase__		:				List[str]    =  random_attention_mask([self.batch_size, self.text_seq_length]       )

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

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

             lowerCamelCase__		:				Union[str, 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							:			Tuple					,      __lowerCamelCase							:			Optional[Any]					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			List[str]					,      __lowerCamelCase							:			Tuple					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			List[Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Any    =  TFLayoutLMvaModel(config=__lowerCamelCase       )

             # text + image
             lowerCamelCase__		:				Tuple    =  model(__lowerCamelCase					,      pixel_values=__lowerCamelCase					,      training=__lowerCamelCase       )
             lowerCamelCase__		:				List[Any]    =  model(
                 __lowerCamelCase					,      bbox=__lowerCamelCase					,      pixel_values=__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      training=__lowerCamelCase					,      )
             lowerCamelCase__		:				Union[str, Any]    =  model(__lowerCamelCase					,      bbox=__lowerCamelCase					,      pixel_values=__lowerCamelCase					,      training=__lowerCamelCase       )

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

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

             # image only
             lowerCamelCase__		:				List[Any]    =  model({"pixel_values": pixel_values}					,      training=__lowerCamelCase       )
             self.parent.assertEqual(
                 result.last_hidden_state.shape					,      (self.batch_size, self.image_seq_length, self.hidden_size)       )





       def 						lowerCAmelCase     (   self							:			List[Any]					,      __lowerCamelCase							:			List[str]					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			Tuple					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			Union[str, Any]					,      __lowerCamelCase							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				Any    =  self.num_labels
             lowerCamelCase__		:				Optional[int]    =  TFLayoutLMvaForSequenceClassification(config=__lowerCamelCase       )
             lowerCamelCase__		:				Tuple    =  model(
                 __lowerCamelCase					,      bbox=__lowerCamelCase					,      pixel_values=__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase					,      training=__lowerCamelCase					,      )
             self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)       )





       def 						lowerCAmelCase     (   self							:			Optional[int]					,      __lowerCamelCase							:			Optional[Any]					,      __lowerCamelCase							:			Dict					,      __lowerCamelCase							:			List[str]					,      __lowerCamelCase							:			Optional[Any]					,      __lowerCamelCase							:			Union[str, Any]					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			Optional[Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Any    =  self.num_labels
             lowerCamelCase__		:				Optional[Any]    =  TFLayoutLMvaForTokenClassification(config=__lowerCamelCase       )
             lowerCamelCase__		:				List[str]    =  model(
                 __lowerCamelCase					,      bbox=__lowerCamelCase					,      pixel_values=__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase					,      training=__lowerCamelCase					,      )
             self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.text_seq_length, self.num_labels)       )





       def 						lowerCAmelCase     (   self							:			Tuple					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			Any					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			List[Any]					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			Union[str, Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				int    =  2
             lowerCamelCase__		:				Tuple    =  TFLayoutLMvaForQuestionAnswering(config=__lowerCamelCase       )
             lowerCamelCase__		:				List[Any]    =  model(
                 __lowerCamelCase					,      bbox=__lowerCamelCase					,      pixel_values=__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      start_positions=__lowerCamelCase					,      end_positions=__lowerCamelCase					,      training=__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 						lowerCAmelCase     (   self							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				Optional[Any]    =  self.prepare_config_and_inputs()
             ((lowerCamelCase__)    ,   (lowerCamelCase__)    ,   (lowerCamelCase__)    ,   (lowerCamelCase__)    ,   (lowerCamelCase__)    ,   (lowerCamelCase__)    ,   (lowerCamelCase__)    ,   (lowerCamelCase__))		:				Dict    =  config_and_inputs
             lowerCamelCase__		:				Tuple    =  {
                 "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 			_lowercase (							lowercase__							,  lowercase__							,  unittest.TestCase):


       """simple docstring"""




       A__											=			(
           (
               TFLayoutLMvaModel,
               TFLayoutLMvaForQuestionAnswering,
               TFLayoutLMvaForSequenceClassification,
               TFLayoutLMvaForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       A__											=			(
           {"document-question-answering": TFLayoutLMvaForQuestionAnswering, "feature-extraction": TFLayoutLMvaModel}
           if is_tf_available()
           else {}
       )

       A__											=			False
       A__											=			False
       A__											=			False





       def 						lowerCAmelCase     (   self							:			str					,      __lowerCamelCase							:			List[str]					,      __lowerCamelCase							:			List[str]					,      __lowerCamelCase							:			Union[str, Any]					,      __lowerCamelCase							:			Optional[Any]					,      __lowerCamelCase							:			Tuple       ):


             '''simple docstring'''



             return True





       def 						lowerCAmelCase     (   self							:			int					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			List[Any]					,      __lowerCamelCase							:			List[Any]=False       ):


             '''simple docstring'''



             lowerCamelCase__		:				str    =  copy.deepcopy(__lowerCamelCase       )

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

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

             return inputs_dict





       def 						lowerCAmelCase     (   self							:			Optional[int]       ):


             '''simple docstring'''



             lowerCamelCase__		:				int    =  TFLayoutLMvaModelTester(self       )
             lowerCamelCase__		:				List[Any]    =  ConfigTester(self					,      config_class=__lowerCamelCase					,      hidden_size=37       )





       def 						lowerCAmelCase     (   self							:			str       ):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       def 						lowerCAmelCase     (   self							:			str       ):


             '''simple docstring'''



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

                         # Test that model correctly compute the loss with kwargs
                         lowerCamelCase__		:				int    =  self._prepare_for_class(inputs_dict.copy()					,      __lowerCamelCase					,      return_labels=__lowerCamelCase       )
                         lowerCamelCase__		:				str    =  prepared_for_class.pop("input_ids"       )

                         lowerCamelCase__		:				Any    =  model(__lowerCamelCase					,      **__lowerCamelCase       )[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
                         lowerCamelCase__		:				str    =  self._prepare_for_class(inputs_dict.copy()					,      __lowerCamelCase					,      return_labels=__lowerCamelCase       )
                         lowerCamelCase__		:				Optional[int]    =  prepared_for_class.pop("input_ids"       )
                         if "labels" in prepared_for_class:
                               lowerCamelCase__		:				Tuple    =  prepared_for_class["labels"].numpy()
                               if len(labels.shape       ) > 1 and labels.shape[1] != 1:
                                     lowerCamelCase__		:				Union[str, Any]    =  -100
                                     lowerCamelCase__		:				Dict    =  tf.convert_to_tensor(__lowerCamelCase       )
                                     lowerCamelCase__		:				int    =  model(__lowerCamelCase					,      **__lowerCamelCase       )[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
                         lowerCamelCase__		:				str    =  self._prepare_for_class(inputs_dict.copy()					,      __lowerCamelCase					,      return_labels=__lowerCamelCase       )
                         lowerCamelCase__		:				Union[str, Any]    =  model(__lowerCamelCase       )[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
                         lowerCamelCase__		:				str    =  self._prepare_for_class(inputs_dict.copy()					,      __lowerCamelCase					,      return_labels=__lowerCamelCase       )

                         # Get keys that were added with the _prepare_for_class function
                         lowerCamelCase__		:				Union[str, Any]    =  prepared_for_class.keys() - inputs_dict.keys()
                         lowerCamelCase__		:				Optional[Any]    =  inspect.signature(model.call       ).parameters
                         lowerCamelCase__		:				Tuple    =  list(signature.keys()       )

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

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

                         for index, value in sorted_tuple_index_mapping:
                               lowerCamelCase__		:				List[Any]    =  prepared_for_class[value]

                         lowerCamelCase__		:				Union[str, Any]    =  tuple(__lowerCamelCase       )

                         # Send to model
                         lowerCamelCase__		:				Optional[int]    =  model(tuple_input[:-1]       )[0]

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





       def 						lowerCAmelCase     (   self							:			Optional[int]       ):


             '''simple docstring'''



             (
                 (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   
             )		:				Any    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Union[str, Any]       ):


             '''simple docstring'''



             (
                 (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   
             )		:				int    =  self.model_tester.prepare_config_and_inputs()
             for type in ["absolute", "relative_key", "relative_key_query"]:
                   lowerCamelCase__		:				Tuple    =  type
                   self.model_tester.create_and_check_model(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Dict       ):


             '''simple docstring'''



             (
                 (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   
             )		:				Any    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(
                 __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			List[Any]       ):


             '''simple docstring'''



             (
                 (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   
             )		:				Tuple    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(
                 __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Optional[int]       ):


             '''simple docstring'''



             (
                 (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   (
                 lowerCamelCase__
             )    ,   
             )		:				Optional[Any]    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(
                 __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )





       @slow
       def 						lowerCAmelCase     (   self							:			int       ):


             '''simple docstring'''



             for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   lowerCamelCase__		:				Tuple    =  TFLayoutLMvaModel.from_pretrained(__lowerCamelCase       )
                   self.assertIsNotNone(__lowerCamelCase       )



def       lowercase_		(		):

      """simple docstring"""




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







@require_tf
class 			_lowercase (							unittest.TestCase):


       """simple docstring"""




       @cached_property
       def 						lowerCAmelCase     (   self							:			int       ):


             '''simple docstring'''



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





       @slow
       def 						lowerCAmelCase     (   self							:			str       ):


             '''simple docstring'''



             lowerCamelCase__		:				Optional[int]    =  TFLayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"       )

             lowerCamelCase__		:				Optional[int]    =  self.default_image_processor
             lowerCamelCase__		:				str    =  prepare_img()
             lowerCamelCase__		:				List[Any]    =  image_processor(images=__lowerCamelCase					,      return_tensors="tf"       ).pixel_values

             lowerCamelCase__		:				Optional[Any]    =  tf.constant([[1, 2]]       )
             lowerCamelCase__		:				Tuple    =  tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]       )					,      axis=0       )

             # forward pass
             lowerCamelCase__		:				List[str]    =  model(input_ids=__lowerCamelCase					,      bbox=__lowerCamelCase					,      pixel_values=__lowerCamelCase					,      training=__lowerCamelCase       )

             # verify the logits
             lowerCamelCase__		:				Union[str, Any]    =  (1, 199, 768)
             self.assertEqual(outputs.last_hidden_state.shape					,      __lowerCamelCase       )

             lowerCamelCase__		:				Any    =  tf.constant(
                 [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]       )

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






import warnings

from ...utils import logging
from .image_processing_beit import BeitImageProcessor


A      :    List[str]		=					logging.get_logger(__name__)







class 			_lowercase (							lowercase__):


       """simple docstring"""




       def __init__(   self							:			Optional[int]					,      *__lowerCamelCase							:			List[str]					,      **__lowerCamelCase							:			str       ):


             '''simple docstring'''



             warnings.warn(
                 "The class BeitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"
                 " use BeitImageProcessor instead."					,      __lowerCamelCase					,      )
             super().__init__(*__lowerCamelCase					,      **__lowerCamelCase       )


