





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







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

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







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       Optional[int]    =		ids_tensor([self.batch_size, self.text_seq_length]    ,						self.vocab_size						)

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

       lowerCAmelCase_					:       str    =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

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

       lowerCAmelCase_					:       List[Any]    =		None
       lowerCAmelCase_					:       Union[str, Any]    =		None
       if self.use_labels:
            lowerCAmelCase_					:       int    =		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_					:       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 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase						):
       lowerCAmelCase_					:       List[str]    =		TFLayoutLMvaModel(config=__UpperCamelCase						)

       # text + image
       lowerCAmelCase_					:       str    =		model(__UpperCamelCase    ,						pixel_values=__UpperCamelCase    ,						training=__UpperCamelCase						)
       lowerCAmelCase_					:       List[str]    =		model(
           __UpperCamelCase    ,						bbox=__UpperCamelCase    ,						pixel_values=__UpperCamelCase    ,						attention_mask=__UpperCamelCase    ,						token_type_ids=__UpperCamelCase    ,						training=__UpperCamelCase    ,						)
       lowerCAmelCase_					:       List[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
       lowerCAmelCase_					:       List[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
       lowerCAmelCase_					:       int    =		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 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase						):
       lowerCAmelCase_					:       List[str]    =		self.num_labels
       lowerCAmelCase_					:       Tuple    =		TFLayoutLMvaForSequenceClassification(config=__UpperCamelCase						)
       lowerCAmelCase_					:       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.num_labels)						)







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase						):
       lowerCAmelCase_					:       List[Any]    =		self.num_labels
       lowerCAmelCase_					:       Tuple    =		TFLayoutLMvaForTokenClassification(config=__UpperCamelCase						)
       lowerCAmelCase_					:       Optional[int]    =		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 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase						):
       lowerCAmelCase_					:       Union[str, Any]    =		2
       lowerCAmelCase_					:       str    =		TFLayoutLMvaForQuestionAnswering(config=__UpperCamelCase						)
       lowerCAmelCase_					:       List[Any]    =		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 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       Optional[Any]    =		self.prepare_config_and_inputs()
       ((lowerCAmelCase_)   ,  (lowerCAmelCase_)   ,  (lowerCAmelCase_)   ,  (lowerCAmelCase_)   ,  (lowerCAmelCase_)   ,  (lowerCAmelCase_)   ,  (lowerCAmelCase_)   ,  (lowerCAmelCase_))					:       str    =		config_and_inputs
       lowerCAmelCase_					:       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      lowercase__    (							__A     ,					__A     ,					unittest.TestCase  ):
  __UpperCamelCase	     =  (
      (
          TFLayoutLMvaModel,
          TFLayoutLMvaForQuestionAnswering,
          TFLayoutLMvaForSequenceClassification,
          TFLayoutLMvaForTokenClassification,
      )
      if is_tf_available()
      else ()
  )
  __UpperCamelCase	     =  (
      {"""document-question-answering""": TFLayoutLMvaForQuestionAnswering, """feature-extraction""": TFLayoutLMvaModel}
      if is_tf_available()
      else {}
  )

  __UpperCamelCase	     =  False
  __UpperCamelCase	     =  False
  __UpperCamelCase	     =  False







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase    ,						_lowercase						):
       return True







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase    ,						_lowercase=False						):
       lowerCAmelCase_					:       Tuple    =		copy.deepcopy(__UpperCamelCase						)

       if model_class in get_values(__UpperCamelCase						):
            lowerCAmelCase_					:       Tuple    =		{
                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						):
                 lowerCAmelCase_					:       Optional[Any]    =		tf.ones(self.model_tester.batch_size    ,						dtype=tf.intaa						)
            elif model_class in get_values(__UpperCamelCase						):
                 lowerCAmelCase_					:       Union[str, Any]    =		tf.zeros(self.model_tester.batch_size    ,						dtype=tf.intaa						)
                 lowerCAmelCase_					:       Optional[int]    =		tf.zeros(self.model_tester.batch_size    ,						dtype=tf.intaa						)
            elif model_class in get_values(__UpperCamelCase						):
                 lowerCAmelCase_					:       Union[str, Any]    =		tf.zeros(self.model_tester.batch_size    ,						dtype=tf.intaa						)
            elif model_class in get_values(__UpperCamelCase						):
                 lowerCAmelCase_					:       Union[str, Any]    =		tf.zeros(
                     (self.model_tester.batch_size, self.model_tester.text_seq_length)    ,						dtype=tf.intaa						)

       return inputs_dict







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       str    =		TFLayoutLMvaModelTester(self						)
       lowerCAmelCase_					:       List[str]    =		ConfigTester(self    ,						config_class=__UpperCamelCase    ,						hidden_size=37						)







  def 	UpperCAmelCase__		(	self						):
       self.config_tester.run_common_tests()







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_   ,  lowerCAmelCase_					:       Dict    =		self.model_tester.prepare_config_and_inputs_for_common()
       for model_class in self.all_model_classes:
            lowerCAmelCase_					:       Any    =		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
                 lowerCAmelCase_					:       Any    =		self._prepare_for_class(inputs_dict.copy()    ,						__UpperCamelCase    ,						return_labels=__UpperCamelCase						)
                 lowerCAmelCase_					:       Optional[int]    =		prepared_for_class[
                     sorted(prepared_for_class.keys() - inputs_dict.keys()    ,						reverse=__UpperCamelCase						)[0]
                 ]
                 lowerCAmelCase_					:       Dict    =		added_label.shape.as_list()[:1]

                 # Test that model correctly compute the loss with kwargs
                 lowerCAmelCase_					:       Union[str, Any]    =		self._prepare_for_class(inputs_dict.copy()    ,						__UpperCamelCase    ,						return_labels=__UpperCamelCase						)
                 lowerCAmelCase_					:       Optional[int]    =		prepared_for_class.pop("""input_ids"""						)

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

                 # Get keys that were added with the _prepare_for_class function
                 lowerCAmelCase_					:       Dict    =		prepared_for_class.keys() - inputs_dict.keys()
                 lowerCAmelCase_					:       Optional[int]    =		inspect.signature(model.call						).parameters
                 lowerCAmelCase_					:       Optional[Any]    =		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_					:       Union[str, Any]    =		signature_names.index(__UpperCamelCase						)
                      lowerCAmelCase_					:       Optional[Any]    =		label_key
                 lowerCAmelCase_					:       List[Any]    =		sorted(tuple_index_mapping.items()						)
                 # Initialize a list with their default values, update the values and convert to a tuple
                 lowerCAmelCase_					:       Dict    =		[]

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

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

                 lowerCAmelCase_					:       Any    =		tuple(__UpperCamelCase						)

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

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







  def 	UpperCAmelCase__		(	self						):
       (
           (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  						
       )					:       List[str]    =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase						)







  def 	UpperCAmelCase__		(	self						):
       (
           (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  						
       )					:       Union[str, Any]    =		self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
            lowerCAmelCase_					:       List[str]    =		type
            self.model_tester.create_and_check_model(__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase						)







  def 	UpperCAmelCase__		(	self						):
       (
           (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  						
       )					:       str    =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(
           __UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase						)







  def 	UpperCAmelCase__		(	self						):
       (
           (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  						
       )					:       Dict    =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(
           __UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase    ,						__UpperCamelCase						)







  def 	UpperCAmelCase__		(	self						):
       (
           (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  (
           lowerCAmelCase_						
       )   ,  						
       )					:       Optional[int]    =		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 	UpperCAmelCase__		(	self						):
       for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            lowerCAmelCase_					:       List[str]    =		TFLayoutLMvaModel.from_pretrained(__UpperCamelCase						)
            self.assertIsNotNone(__UpperCamelCase						)






def 							_lowerCAmelCase (  )	->			List[str]:
     lowerCAmelCase_					:       Any    =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
     return image






@require_tf
class      lowercase__    (							unittest.TestCase  ):







  @cached_property
  def 	UpperCAmelCase__		(	self						):
       return LayoutLMvaImageProcessor(apply_ocr=__UpperCamelCase						) if is_vision_available() else None







  @slow
  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       List[str]    =		TFLayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""						)

       lowerCAmelCase_					:       Tuple    =		self.default_image_processor
       lowerCAmelCase_					:       str    =		prepare_img()
       lowerCAmelCase_					:       Optional[int]    =		image_processor(images=__UpperCamelCase    ,						return_tensors="""tf"""						).pixel_values

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

       # forward pass
       lowerCAmelCase_					:       int    =		model(input_ids=__UpperCamelCase    ,						bbox=__UpperCamelCase    ,						pixel_values=__UpperCamelCase    ,						training=__UpperCamelCase						)

       # verify the logits
       lowerCAmelCase_					:       str    =		(1, 199, 768)
       self.assertEqual(outputs.last_hidden_state.shape    ,						__UpperCamelCase						)

       lowerCAmelCase_					:       Union[str, 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]    ,						__UpperCamelCase    ,						atol=1e-4						)						)







import torch
from transformers import PreTrainedModel, XLMRobertaConfig, XLMRobertaModel





class      lowercase__    (							__A  ):
  __UpperCamelCase	     =  """M-CLIP"""







  def __init__(	self    ,						_lowercase=1_024    ,						_lowercase=768    ,						**_lowercase						):
       lowerCAmelCase_					:       Tuple    =		transformerDimSize
       lowerCAmelCase_					:       Optional[Any]    =		imageDimSize
       super().__init__(**_lowercase						)





class      lowercase__    (							__A  ):
  __UpperCamelCase	     =  MCLIPConfig







  def __init__(	self    ,						_lowercase    ,						*_lowercase    ,						**_lowercase						):
       super().__init__(_lowercase    ,						*_lowercase    ,						**_lowercase						)
       lowerCAmelCase_					:       str    =		XLMRobertaModel(_lowercase						)
       lowerCAmelCase_					:       str    =		torch.nn.Linear(
           in_features=config.transformerDimensions    ,						out_features=config.numDims						)







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase						):
       lowerCAmelCase_					:       Optional[int]    =		self.transformer(input_ids=_lowercase    ,						attention_mask=_lowercase						)[0]
       lowerCAmelCase_					:       Any    =		(embs * attention_mask.unsqueeze(2						)).sum(dim=1						) / attention_mask.sum(dim=1						)[:, None]
       return self.LinearTransformation(_lowercase						), embs
