





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     :					List[Any],			_UpperCAmelCase     :					List[str],			_UpperCAmelCase     :					Any=2,			_UpperCAmelCase     :					List[str]=3,			_UpperCAmelCase     :					Union[str, Any]=4,			_UpperCAmelCase     :					Union[str, Any]=2,			_UpperCAmelCase     :					int=7,			_UpperCAmelCase     :					Tuple=True,			_UpperCAmelCase     :					int=True,			_UpperCAmelCase     :					List[str]=True,			_UpperCAmelCase     :					List[str]=True,			_UpperCAmelCase     :					Tuple=9_9,			_UpperCAmelCase     :					Any=3_6,			_UpperCAmelCase     :					List[str]=2,			_UpperCAmelCase     :					int=4,			_UpperCAmelCase     :					str=3_7,			_UpperCAmelCase     :					List[str]="gelu",			_UpperCAmelCase     :					Optional[int]=0.1,			_UpperCAmelCase     :					str=0.1,			_UpperCAmelCase     :					Optional[int]=5_1_2,			_UpperCAmelCase     :					Optional[Any]=1_6,			_UpperCAmelCase     :					int=2,			_UpperCAmelCase     :					Tuple=0.02,			_UpperCAmelCase     :					Optional[int]=6,			_UpperCAmelCase     :					List[Any]=6,			_UpperCAmelCase     :					Any=3,			_UpperCAmelCase     :					List[str]=4,			_UpperCAmelCase     :					Tuple=None,			_UpperCAmelCase     :					str=1_0_0_0,			)				->			str:





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

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






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





              """simple docstring"""
              SCREAMING_SNAKE_CASE__       :      List[str]     =						ids_tensor([self.batch_size, self.text_seq_length],			self.vocab_size						)

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

              SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

              SCREAMING_SNAKE_CASE__       :      List[Any]     =						None
              if self.use_input_mask:
                     SCREAMING_SNAKE_CASE__       :      Dict     =						random_attention_mask([self.batch_size, self.text_seq_length]						)

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

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

              SCREAMING_SNAKE_CASE__       :      List[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  A_     ( self     :					Tuple,			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					Optional[Any],			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					List[Any],			_UpperCAmelCase     :					Union[str, Any],			_UpperCAmelCase     :					Optional[Any]						)				->			Union[str, Any]:





              """simple docstring"""
              SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						TFLayoutLMvaModel(config=_UpperCAmelCase						)

              # text + image
              SCREAMING_SNAKE_CASE__       :      str     =						model(_UpperCAmelCase,			pixel_values=_UpperCAmelCase,			training=_UpperCAmelCase						)
              SCREAMING_SNAKE_CASE__       :      List[Any]     =						model(
                  _UpperCAmelCase,			bbox=_UpperCAmelCase,			pixel_values=_UpperCAmelCase,			attention_mask=_UpperCAmelCase,			token_type_ids=_UpperCAmelCase,			training=_UpperCAmelCase,			)
              SCREAMING_SNAKE_CASE__       :      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
              SCREAMING_SNAKE_CASE__       :      Optional[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
              SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						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  A_     ( self     :					List[Any],			_UpperCAmelCase     :					List[Any],			_UpperCAmelCase     :					int,			_UpperCAmelCase     :					Union[str, Any],			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					List[str],			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					List[Any]						)				->			Union[str, Any]:





              """simple docstring"""
              SCREAMING_SNAKE_CASE__       :      Tuple     =						self.num_labels
              SCREAMING_SNAKE_CASE__       :      List[str]     =						TFLayoutLMvaForSequenceClassification(config=_UpperCAmelCase						)
              SCREAMING_SNAKE_CASE__       :      Union[str, 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  A_     ( self     :					List[Any],			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					int,			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					List[str],			_UpperCAmelCase     :					List[str],			_UpperCAmelCase     :					Dict						)				->			Dict:





              """simple docstring"""
              SCREAMING_SNAKE_CASE__       :      int     =						self.num_labels
              SCREAMING_SNAKE_CASE__       :      Dict     =						TFLayoutLMvaForTokenClassification(config=_UpperCAmelCase						)
              SCREAMING_SNAKE_CASE__       :      Tuple     =						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  A_     ( self     :					Dict,			_UpperCAmelCase     :					Optional[int],			_UpperCAmelCase     :					Optional[Any],			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					Union[str, Any],			_UpperCAmelCase     :					int,			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					Optional[Any]						)				->			str:





              """simple docstring"""
              SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						2
              SCREAMING_SNAKE_CASE__       :      str     =						TFLayoutLMvaForQuestionAnswering(config=_UpperCAmelCase						)
              SCREAMING_SNAKE_CASE__       :      int     =						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  A_     ( self     :					List[Any]						)				->			Any:





              """simple docstring"""
              SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						self.prepare_config_and_inputs()
              (SCREAMING_SNAKE_CASE__)       :      Optional[Any]     =						config_and_inputs
              SCREAMING_SNAKE_CASE__       :      List[str]     =						{
                  "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       (__lowerCamelCase							,     __lowerCamelCase							,     unittest.TestCase      ):







       """simple docstring"""




       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  A_     ( self     :					Optional[Any],			_UpperCAmelCase     :					Tuple,			_UpperCAmelCase     :					Any,			_UpperCAmelCase     :					Dict,			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					Optional[int]						)				->			List[str]:





              """simple docstring"""
              return True






       def  A_     ( self     :					Optional[int],			_UpperCAmelCase     :					Optional[int],			_UpperCAmelCase     :					str,			_UpperCAmelCase     :					Dict=False						)				->			dict:





              """simple docstring"""
              SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						copy.deepcopy(_UpperCAmelCase						)

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

              return inputs_dict






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





              """simple docstring"""
              SCREAMING_SNAKE_CASE__       :      List[Any]     =						TFLayoutLMvaModelTester(self						)
              SCREAMING_SNAKE_CASE__       :      List[Any]     =						ConfigTester(self,			config_class=_UpperCAmelCase,			hidden_size=3_7						)






       def  A_     ( self     :					Optional[int]						)				->			List[str]:





              """simple docstring"""
              self.config_tester.run_common_tests()






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





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

                            # Test that model correctly compute the loss with kwargs
                            SCREAMING_SNAKE_CASE__       :      List[Any]     =						self._prepare_for_class(inputs_dict.copy(),			_UpperCAmelCase,			return_labels=_UpperCAmelCase						)
                            SCREAMING_SNAKE_CASE__       :      int     =						prepared_for_class.pop("input_ids"						)

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

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

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

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

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

                            SCREAMING_SNAKE_CASE__       :      List[Any]     =						tuple(_UpperCAmelCase						)

                            # Send to model
                            SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						model(tuple_input[:-1]						)[0]

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






       def  A_     ( self     :					Dict						)				->			int:





              """simple docstring"""
              (
                  SCREAMING_SNAKE_CASE__
              )       :      Union[str, Any]     =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase						)






       def  A_     ( self     :					List[Any]						)				->			int:





              """simple docstring"""
              (
                  SCREAMING_SNAKE_CASE__
              )       :      Tuple     =						self.model_tester.prepare_config_and_inputs()
              for type in ["absolute", "relative_key", "relative_key_query"]:
                     SCREAMING_SNAKE_CASE__       :      Optional[int]     =						type
                     self.model_tester.create_and_check_model(_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase,			_UpperCAmelCase						)






       def  A_     ( self     :					Dict						)				->			Optional[Any]:





              """simple docstring"""
              (
                  SCREAMING_SNAKE_CASE__
              )       :      List[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  A_     ( self     :					Dict						)				->			str:





              """simple docstring"""
              (
                  SCREAMING_SNAKE_CASE__
              )       :      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  A_     ( self     :					Any						)				->			int:





              """simple docstring"""
              (
                  SCREAMING_SNAKE_CASE__
              )       :      str     =						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  A_     ( self     :					Optional[int]						)				->			int:





              """simple docstring"""
              for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     SCREAMING_SNAKE_CASE__       :      List[Any]     =						TFLayoutLMvaModel.from_pretrained(_UpperCAmelCase						)
                     self.assertIsNotNone(_UpperCAmelCase						)




def _a (  )	->    List[str]:

       '''simple docstring'''






       SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"       )
       return image







@require_tf
class 						lowerCamelCase       (unittest.TestCase      ):







       """simple docstring"""




       @cached_property
       def  A_     ( self     :					Tuple						)				->			List[Any]:





              """simple docstring"""
              return LayoutLMvaImageProcessor(apply_ocr=_UpperCAmelCase						) if is_vision_available() else None






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





              """simple docstring"""
              SCREAMING_SNAKE_CASE__       :      List[Any]     =						TFLayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"						)

              SCREAMING_SNAKE_CASE__       :      Optional[int]     =						self.default_image_processor
              SCREAMING_SNAKE_CASE__       :      Dict     =						prepare_img()
              SCREAMING_SNAKE_CASE__       :      List[str]     =						image_processor(images=_UpperCAmelCase,			return_tensors="tf"						).pixel_values

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

              # forward pass
              SCREAMING_SNAKE_CASE__       :      Dict     =						model(input_ids=_UpperCAmelCase,			bbox=_UpperCAmelCase,			pixel_values=_UpperCAmelCase,			training=_UpperCAmelCase						)

              # verify the logits
              SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						(1, 1_9_9, 7_6_8)
              self.assertEqual(outputs.last_hidden_state.shape,			_UpperCAmelCase						)

              SCREAMING_SNAKE_CASE__       :      Optional[int]     =						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 requests
from bsa import BeautifulSoup




def _a (  SCREAMING_SNAKE_CASE__      :  str			,				SCREAMING_SNAKE_CASE__      :  dict       )	->    str:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      Any     =						BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE__			,				params=SCREAMING_SNAKE_CASE__       ).content			,				"html.parser"       )
							SCREAMING_SNAKE_CASE__       :      str     =						soup.find("div"			,				attrs={"class": "gs_ri"}       )
							SCREAMING_SNAKE_CASE__       :      int     =						div.find("div"			,				attrs={"class": "gs_fl"}       ).find_all("a"       )
							return anchors[2].get_text()


if __name__ == "__main__":
		_lowerCamelCase :    Any  							= {
		    '''title''': (
		        '''Precisely geometry controlled microsupercapacitors for ultrahigh areal '''
		        '''capacitance, volumetric capacitance, and energy density'''
		    ),
		    '''journal''': '''Chem. Mater.''',
		    '''volume''': 3_0,
		    '''pages''': '''3979-3990''',
		    '''year''': 2_0_1_8,
		    '''hl''': '''en''',
		}
		print(get_citation('''https://scholar.google.com/scholar_lookup''', params=params))






