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





     """simple docstring"""







     def __init__(				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE=2    ,    __SCREAMING_SNAKE_CASE=3    ,    __SCREAMING_SNAKE_CASE=4    ,    __SCREAMING_SNAKE_CASE=2    ,    __SCREAMING_SNAKE_CASE=7    ,    __SCREAMING_SNAKE_CASE=True    ,    __SCREAMING_SNAKE_CASE=True    ,    __SCREAMING_SNAKE_CASE=True    ,    __SCREAMING_SNAKE_CASE=True    ,    __SCREAMING_SNAKE_CASE=9_9    ,    __SCREAMING_SNAKE_CASE=3_6    ,    __SCREAMING_SNAKE_CASE=2    ,    __SCREAMING_SNAKE_CASE=4    ,    __SCREAMING_SNAKE_CASE=3_7    ,    __SCREAMING_SNAKE_CASE="gelu"    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE=5_1_2    ,    __SCREAMING_SNAKE_CASE=1_6    ,    __SCREAMING_SNAKE_CASE=2    ,    __SCREAMING_SNAKE_CASE=0.02    ,    __SCREAMING_SNAKE_CASE=6    ,    __SCREAMING_SNAKE_CASE=6    ,    __SCREAMING_SNAKE_CASE=3    ,    __SCREAMING_SNAKE_CASE=4    ,    __SCREAMING_SNAKE_CASE=None    ,    __SCREAMING_SNAKE_CASE=1_0_0_0    ,    )	-> Optional[int]:




           """simple docstring"""







           UpperCamelCase__     : int											=					parent
           UpperCamelCase__     : int											=					batch_size
           UpperCamelCase__     : Optional[Any]											=					num_channels
           UpperCamelCase__     : int											=					image_size
           UpperCamelCase__     : Any											=					patch_size
           UpperCamelCase__     : Any											=					is_training
           UpperCamelCase__     : int											=					use_input_mask
           UpperCamelCase__     : Union[str, Any]											=					use_token_type_ids
           UpperCamelCase__     : Optional[Any]											=					use_labels
           UpperCamelCase__     : str											=					vocab_size
           UpperCamelCase__     : Any											=					hidden_size
           UpperCamelCase__     : str											=					num_hidden_layers
           UpperCamelCase__     : Union[str, Any]											=					num_attention_heads
           UpperCamelCase__     : Tuple											=					intermediate_size
           UpperCamelCase__     : Optional[Any]											=					hidden_act
           UpperCamelCase__     : Union[str, Any]											=					hidden_dropout_prob
           UpperCamelCase__     : Tuple											=					attention_probs_dropout_prob
           UpperCamelCase__     : Optional[Any]											=					max_position_embeddings
           UpperCamelCase__     : List[str]											=					type_vocab_size
           UpperCamelCase__     : Tuple											=					type_sequence_label_size
           UpperCamelCase__     : Optional[Any]											=					initializer_range
           UpperCamelCase__     : Tuple											=					coordinate_size
           UpperCamelCase__     : Tuple											=					shape_size
           UpperCamelCase__     : List[str]											=					num_labels
           UpperCamelCase__     : Optional[Any]											=					num_choices
           UpperCamelCase__     : Optional[int]											=					scope
           UpperCamelCase__     : 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)
           UpperCamelCase__     : Optional[Any]											=					text_seq_length
           UpperCamelCase__     : int											=					(image_size // patch_size) ** 2 + 1
           UpperCamelCase__     : Any											=					self.text_seq_length + self.image_seq_length






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Dict:




           """simple docstring"""







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

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

           UpperCamelCase__     : Tuple											=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

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

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

           UpperCamelCase__     : 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    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)	-> Union[str, Any]:




           """simple docstring"""







           UpperCamelCase__     : int											=					TFLayoutLMvaModel(config=__UpperCAmelCase	)

           # text + image
           UpperCamelCase__     : Dict											=					model(__UpperCAmelCase    ,    pixel_values=__UpperCAmelCase    ,    training=__UpperCAmelCase	)
           UpperCamelCase__     : List[Any]											=					model(
               __UpperCAmelCase    ,    bbox=__UpperCAmelCase    ,    pixel_values=__UpperCAmelCase    ,    attention_mask=__UpperCAmelCase    ,    token_type_ids=__UpperCAmelCase    ,    training=__UpperCAmelCase    ,    )
           UpperCamelCase__     : Any											=					model(__UpperCAmelCase    ,    bbox=__UpperCAmelCase    ,    pixel_values=__UpperCAmelCase    ,    training=__UpperCAmelCase	)

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

           # text only
           UpperCamelCase__     : Union[str, Any]											=					model(__UpperCAmelCase    ,    training=__UpperCAmelCase	)
           self.parent.assertEqual(
               result.last_hidden_state.shape    ,    (self.batch_size, self.text_seq_length, self.hidden_size)	)

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






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)	-> Union[str, Any]:




           """simple docstring"""







           UpperCamelCase__     : str											=					self.num_labels
           UpperCamelCase__     : Any											=					TFLayoutLMvaForSequenceClassification(config=__UpperCAmelCase	)
           UpperCamelCase__     : str											=					model(
               __UpperCAmelCase    ,    bbox=__UpperCAmelCase    ,    pixel_values=__UpperCAmelCase    ,    attention_mask=__UpperCAmelCase    ,    token_type_ids=__UpperCAmelCase    ,    labels=__UpperCAmelCase    ,    training=__UpperCAmelCase    ,    )
           self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.num_labels)	)






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)	-> Tuple:




           """simple docstring"""







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






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)	-> Optional[Any]:




           """simple docstring"""







           UpperCamelCase__     : Tuple											=					2
           UpperCamelCase__     : List[Any]											=					TFLayoutLMvaForQuestionAnswering(config=__UpperCAmelCase	)
           UpperCamelCase__     : str											=					model(
               __UpperCAmelCase    ,    bbox=__UpperCAmelCase    ,    pixel_values=__UpperCAmelCase    ,    attention_mask=__UpperCAmelCase    ,    token_type_ids=__UpperCAmelCase    ,    start_positions=__UpperCAmelCase    ,    end_positions=__UpperCAmelCase    ,    training=__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  __SCREAMING_SNAKE_CASE      (				self	)	-> Optional[Any]:




           """simple docstring"""







           UpperCamelCase__     : Tuple											=					self.prepare_config_and_inputs()
           (UpperCamelCase__)     : int											=					config_and_inputs
           UpperCamelCase__     : 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_tf
class 					_lowerCamelCase			(   _SCREAMING_SNAKE_CASE						,				_SCREAMING_SNAKE_CASE						,				unittest.TestCase			):





     """simple docstring"""







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

     SCREAMING_SNAKE_CASE_		 =	False
     SCREAMING_SNAKE_CASE_		 =	False
     SCREAMING_SNAKE_CASE_		 =	False






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)	-> Union[str, Any]:




           """simple docstring"""







           return True






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE=False	)	-> dict:




           """simple docstring"""







           UpperCamelCase__     : Dict											=					copy.deepcopy(__UpperCAmelCase	)

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

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

           return inputs_dict






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Optional[int]:




           """simple docstring"""







           UpperCamelCase__     : str											=					TFLayoutLMvaModelTester(self	)
           UpperCamelCase__     : Optional[int]											=					ConfigTester(self    ,    config_class=__UpperCAmelCase    ,    hidden_size=3_7	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Any:




           """simple docstring"""







           self.config_tester.run_common_tests()






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Union[str, Any]:




           """simple docstring"""







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

                       # Test that model correctly compute the loss with kwargs
                       UpperCamelCase__     : Optional[Any]											=					self._prepare_for_class(inputs_dict.copy()    ,    __UpperCAmelCase    ,    return_labels=__UpperCAmelCase	)
                       UpperCamelCase__     : Optional[int]											=					prepared_for_class.pop('''input_ids'''	)

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

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

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

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

                       for index, value in sorted_tuple_index_mapping:
                             UpperCamelCase__     : int											=					prepared_for_class[value]

                       UpperCamelCase__     : Union[str, Any]											=					tuple(__UpperCAmelCase	)

                       # Send to model
                       UpperCamelCase__     : Any											=					model(tuple_input[:-1]	)[0]

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






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Optional[int]:




           """simple docstring"""







           (
               UpperCamelCase__		
           )     : Union[str, Any]											=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(__UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[Any]:




           """simple docstring"""







           (
               UpperCamelCase__		
           )     : List[Any]											=					self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                 UpperCamelCase__     : Dict											=					type
                 self.model_tester.create_and_check_model(__UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Any:




           """simple docstring"""







           (
               UpperCamelCase__		
           )     : Any											=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(
               __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Any:




           """simple docstring"""







           (
               UpperCamelCase__		
           )     : Union[str, Any]											=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(
               __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Optional[int]:




           """simple docstring"""







           (
               UpperCamelCase__		
           )     : List[Any]											=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(
               __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase    ,    __UpperCAmelCase	)






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




           """simple docstring"""







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


def 				SCREAMING_SNAKE_CASE_   (      ):
      UpperCamelCase__     : Dict											=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
      return image




@require_tf
class 					_lowerCamelCase			(   unittest.TestCase			):





     """simple docstring"""







     @cached_property
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







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






     @slow
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







           UpperCamelCase__     : Dict											=					TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''	)

           UpperCamelCase__     : List[str]											=					self.default_image_processor
           UpperCamelCase__     : str											=					prepare_img()
           UpperCamelCase__     : List[str]											=					image_processor(images=__UpperCAmelCase    ,    return_tensors='''tf'''	).pixel_values

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

           # forward pass
           UpperCamelCase__     : Tuple											=					model(input_ids=__UpperCAmelCase    ,    bbox=__UpperCAmelCase    ,    pixel_values=__UpperCAmelCase    ,    training=__UpperCAmelCase	)

           # verify the logits
           UpperCamelCase__     : List[str]											=					(1, 1_9_9, 7_6_8)
           self.assertEqual(outputs.last_hidden_state.shape    ,    __UpperCAmelCase	)

           UpperCamelCase__     : str											=					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]    ,    __UpperCAmelCase    ,    atol=1e-4	)	)





import importlib
import json
import os
from collections import OrderedDict
from typing import Dict, Optional, Union

# Build the list of all image processors
from ...configuration_utils import PretrainedConfig
from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code
from ...image_processing_utils import ImageProcessingMixin
from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging
from .auto_factory import _LazyAutoMapping
from .configuration_auto import (
    CONFIG_MAPPING_NAMES,
    AutoConfig,
    model_type_to_module_name,
    replace_list_option_in_docstrings,
)


lowercase			:				Dict   =     logging.get_logger(__name__)

lowercase			:				Dict   =     OrderedDict(
    [
        ("align", "EfficientNetImageProcessor"),
        ("beit", "BeitImageProcessor"),
        ("bit", "BitImageProcessor"),
        ("blip", "BlipImageProcessor"),
        ("blip-2", "BlipImageProcessor"),
        ("bridgetower", "BridgeTowerImageProcessor"),
        ("chinese_clip", "ChineseCLIPImageProcessor"),
        ("clip", "CLIPImageProcessor"),
        ("clipseg", "ViTImageProcessor"),
        ("conditional_detr", "ConditionalDetrImageProcessor"),
        ("convnext", "ConvNextImageProcessor"),
        ("convnextv2", "ConvNextImageProcessor"),
        ("cvt", "ConvNextImageProcessor"),
        ("data2vec-vision", "BeitImageProcessor"),
        ("deformable_detr", "DeformableDetrImageProcessor"),
        ("deit", "DeiTImageProcessor"),
        ("deta", "DetaImageProcessor"),
        ("detr", "DetrImageProcessor"),
        ("dinat", "ViTImageProcessor"),
        ("donut-swin", "DonutImageProcessor"),
        ("dpt", "DPTImageProcessor"),
        ("efficientformer", "EfficientFormerImageProcessor"),
        ("efficientnet", "EfficientNetImageProcessor"),
        ("flava", "FlavaImageProcessor"),
        ("focalnet", "BitImageProcessor"),
        ("git", "CLIPImageProcessor"),
        ("glpn", "GLPNImageProcessor"),
        ("groupvit", "CLIPImageProcessor"),
        ("imagegpt", "ImageGPTImageProcessor"),
        ("instructblip", "BlipImageProcessor"),
        ("layoutlmv2", "LayoutLMv2ImageProcessor"),
        ("layoutlmv3", "LayoutLMv3ImageProcessor"),
        ("levit", "LevitImageProcessor"),
        ("mask2former", "Mask2FormerImageProcessor"),
        ("maskformer", "MaskFormerImageProcessor"),
        ("mgp-str", "ViTImageProcessor"),
        ("mobilenet_v1", "MobileNetV1ImageProcessor"),
        ("mobilenet_v2", "MobileNetV2ImageProcessor"),
        ("mobilevit", "MobileViTImageProcessor"),
        ("mobilevit", "MobileViTImageProcessor"),
        ("mobilevitv2", "MobileViTImageProcessor"),
        ("nat", "ViTImageProcessor"),
        ("oneformer", "OneFormerImageProcessor"),
        ("owlvit", "OwlViTImageProcessor"),
        ("perceiver", "PerceiverImageProcessor"),
        ("pix2struct", "Pix2StructImageProcessor"),
        ("poolformer", "PoolFormerImageProcessor"),
        ("regnet", "ConvNextImageProcessor"),
        ("resnet", "ConvNextImageProcessor"),
        ("sam", "SamImageProcessor"),
        ("segformer", "SegformerImageProcessor"),
        ("swiftformer", "ViTImageProcessor"),
        ("swin", "ViTImageProcessor"),
        ("swin2sr", "Swin2SRImageProcessor"),
        ("swinv2", "ViTImageProcessor"),
        ("table-transformer", "DetrImageProcessor"),
        ("timesformer", "VideoMAEImageProcessor"),
        ("tvlt", "TvltImageProcessor"),
        ("upernet", "SegformerImageProcessor"),
        ("van", "ConvNextImageProcessor"),
        ("videomae", "VideoMAEImageProcessor"),
        ("vilt", "ViltImageProcessor"),
        ("vit", "ViTImageProcessor"),
        ("vit_hybrid", "ViTHybridImageProcessor"),
        ("vit_mae", "ViTImageProcessor"),
        ("vit_msn", "ViTImageProcessor"),
        ("xclip", "CLIPImageProcessor"),
        ("yolos", "YolosImageProcessor"),
    ]
)

lowercase			:				Tuple   =     _LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES)






def        snake_case__		(    lowerCamelCase_		):
							for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items():
														if class_name in extractors:
																					A      :					Optional[Any]      		=					model_type_to_module_name(lowerCamelCase_		)

																					A      :					Optional[int]      		=					importlib.import_module(F'.{module_name}'     ,				'''transformers.models'''		)
																					try:
																												return getattr(lowerCamelCase_     ,				lowerCamelCase_		)
																					except AttributeError:
																												continue

							for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items():
														if getattr(lowerCamelCase_     ,				'''__name__'''     ,				lowerCamelCase_		) == class_name:
																					return extractor

    # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main
    # init and we return the proper dummy to get an appropriate error message.
							A      :					Any      		=					importlib.import_module('''transformers'''		)
							if hasattr(lowerCamelCase_     ,				lowerCamelCase_		):
														return getattr(lowerCamelCase_     ,				lowerCamelCase_		)

							return None






def        snake_case__		(    lowerCamelCase_     ,				lowerCamelCase_ = None     ,				lowerCamelCase_ = False     ,				lowerCamelCase_ = False     ,				lowerCamelCase_ = None     ,				lowerCamelCase_ = None     ,				lowerCamelCase_ = None     ,				lowerCamelCase_ = False     ,				**lowerCamelCase_     ,				):
							A      :					Optional[Any]      		=					get_file_from_repo(
							    lowerCamelCase_     ,				lowerCamelCase_     ,				cache_dir=lowerCamelCase_     ,				force_download=lowerCamelCase_     ,				resume_download=lowerCamelCase_     ,				proxies=lowerCamelCase_     ,				use_auth_token=lowerCamelCase_     ,				revision=lowerCamelCase_     ,				local_files_only=lowerCamelCase_     ,				)
							if resolved_config_file is None:
														logger.info(
														    '''Could not locate the image processor configuration file, will try to use the model config instead.'''		)
														return {}

							with open(lowerCamelCase_     ,				encoding='''utf-8'''		) as reader:
														return json.load(lowerCamelCase_		)







class 					__lowercase  :





							"""simple docstring"""

							def __init__(					self						)		->       int:
														raise EnvironmentError(
														    '''AutoImageProcessor is designed to be instantiated '''
														    '''using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method.'''						)




							@classmethod
							@replace_list_option_in_docstrings(__UpperCAmelCase						)
							def       snake_case			(					cls    ,						__UpperCAmelCase    ,						**__UpperCAmelCase						)		->       Tuple:
														A      :					Tuple      		=					kwargs.pop('''config'''    ,						__UpperCAmelCase						)
														A      :					Union[str, Any]      		=					kwargs.pop('''trust_remote_code'''    ,						__UpperCAmelCase						)
														A      :					str      		=					True

														A		,		A      :					str      		=					ImageProcessingMixin.get_image_processor_dict(__UpperCAmelCase    ,						**__UpperCAmelCase						)
														A      :					List[Any]      		=					config_dict.get('''image_processor_type'''    ,						__UpperCAmelCase						)
														A      :					List[Any]      		=					None
														if "AutoImageProcessor" in config_dict.get('''auto_map'''    ,						{}						):
																					A      :					List[str]      		=					config_dict['''auto_map''']['''AutoImageProcessor''']

														# If we still don't have the image processor class, check if we're loading from a previous feature extractor config
														# and if so, infer the image processor class from there.
														if image_processor_class is None and image_processor_auto_map is None:
																					A      :					List[str]      		=					config_dict.pop('''feature_extractor_type'''    ,						__UpperCAmelCase						)
																					if feature_extractor_class is not None:
																												logger.warning(
																												    '''Could not find image processor class in the image processor config or the model config. Loading'''
																												    ''' based on pattern matching with the model\'s feature extractor configuration.'''						)
																												A      :					Tuple      		=					feature_extractor_class.replace('''FeatureExtractor'''    ,						'''ImageProcessor'''						)
																					if "AutoFeatureExtractor" in config_dict.get('''auto_map'''    ,						{}						):
																												A      :					Any      		=					config_dict['''auto_map''']['''AutoFeatureExtractor''']
																												A      :					int      		=					feature_extractor_auto_map.replace('''FeatureExtractor'''    ,						'''ImageProcessor'''						)
																												logger.warning(
																												    '''Could not find image processor auto map in the image processor config or the model config.'''
																												    ''' Loading based on pattern matching with the model\'s feature extractor configuration.'''						)

        # If we don't find the image processor class in the image processor config, let's try the model config.
														if image_processor_class is None and image_processor_auto_map is None:
																					if not isinstance(__UpperCAmelCase    ,						__UpperCAmelCase						):
																												A      :					List[str]      		=					AutoConfig.from_pretrained(__UpperCAmelCase    ,						**__UpperCAmelCase						)
																					# It could be in `config.image_processor_type``
																					A      :					Any      		=					getattr(__UpperCAmelCase    ,						'''image_processor_type'''    ,						__UpperCAmelCase						)
																					if hasattr(__UpperCAmelCase    ,						'''auto_map'''						) and "AutoImageProcessor" in config.auto_map:
																												A      :					List[Any]      		=					config.auto_map['''AutoImageProcessor''']

														if image_processor_class is not None:
																					A      :					str      		=					image_processor_class_from_name(__UpperCAmelCase						)

														A      :					int      		=					image_processor_auto_map is not None
														A      :					List[str]      		=					image_processor_class is not None or type(__UpperCAmelCase						) in IMAGE_PROCESSOR_MAPPING
														A      :					Optional[Any]      		=					resolve_trust_remote_code(
														    __UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						)

														if has_remote_code and trust_remote_code:
																					A      :					Tuple      		=					get_class_from_dynamic_module(
																					    __UpperCAmelCase    ,						__UpperCAmelCase    ,						**__UpperCAmelCase						)
																					A      :					Optional[Any]      		=					kwargs.pop('''code_revision'''    ,						__UpperCAmelCase						)
																					if os.path.isdir(__UpperCAmelCase						):
																												image_processor_class.register_for_auto_class()
																					return image_processor_class.from_dict(__UpperCAmelCase    ,						**__UpperCAmelCase						)
														elif image_processor_class is not None:
																					return image_processor_class.from_dict(__UpperCAmelCase    ,						**__UpperCAmelCase						)
														# Last try: we use the IMAGE_PROCESSOR_MAPPING.
														elif type(__UpperCAmelCase						) in IMAGE_PROCESSOR_MAPPING:
																					A      :					Tuple      		=					IMAGE_PROCESSOR_MAPPING[type(__UpperCAmelCase						)]
																					return image_processor_class.from_dict(__UpperCAmelCase    ,						**__UpperCAmelCase						)

														raise ValueError(
														    f'Unrecognized image processor in {pretrained_model_name_or_path}. Should have a '
														    f'`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following '
														    f'`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys()						)}'						)






							@staticmethod
							def       snake_case			(					__UpperCAmelCase    ,						__UpperCAmelCase						)		->       Any:
														IMAGE_PROCESSOR_MAPPING.register(__UpperCAmelCase    ,						__UpperCAmelCase						)






