

'''simple docstring'''
from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

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

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


if is_tf_available():
     import tensorflow as tf

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

if is_vision_available():
     from PIL import Image

     from transformers import LayoutLMvaImageProcessor







class 							A__    :





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =parent
       _SCREAMING_SNAKE_CASE							    =batch_size
       _SCREAMING_SNAKE_CASE							    =num_channels
       _SCREAMING_SNAKE_CASE							    =image_size
       _SCREAMING_SNAKE_CASE							    =patch_size
       _SCREAMING_SNAKE_CASE							    =is_training
       _SCREAMING_SNAKE_CASE							    =use_input_mask
       _SCREAMING_SNAKE_CASE							    =use_token_type_ids
       _SCREAMING_SNAKE_CASE							    =use_labels
       _SCREAMING_SNAKE_CASE							    =vocab_size
       _SCREAMING_SNAKE_CASE							    =hidden_size
       _SCREAMING_SNAKE_CASE							    =num_hidden_layers
       _SCREAMING_SNAKE_CASE							    =num_attention_heads
       _SCREAMING_SNAKE_CASE							    =intermediate_size
       _SCREAMING_SNAKE_CASE							    =hidden_act
       _SCREAMING_SNAKE_CASE							    =hidden_dropout_prob
       _SCREAMING_SNAKE_CASE							    =attention_probs_dropout_prob
       _SCREAMING_SNAKE_CASE							    =max_position_embeddings
       _SCREAMING_SNAKE_CASE							    =type_vocab_size
       _SCREAMING_SNAKE_CASE							    =type_sequence_label_size
       _SCREAMING_SNAKE_CASE							    =initializer_range
       _SCREAMING_SNAKE_CASE							    =coordinate_size
       _SCREAMING_SNAKE_CASE							    =shape_size
       _SCREAMING_SNAKE_CASE							    =num_labels
       _SCREAMING_SNAKE_CASE							    =num_choices
       _SCREAMING_SNAKE_CASE							    =scope
       _SCREAMING_SNAKE_CASE							    =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							    =text_seq_length
       _SCREAMING_SNAKE_CASE							    =(image_size // patch_size) ** 2 + 1
       _SCREAMING_SNAKE_CASE							    =self.text_seq_length + self.image_seq_length





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =ids_tensor([self.batch_size, self.text_seq_length]							,					self.vocab_size )

       _SCREAMING_SNAKE_CASE							    =ids_tensor([self.batch_size, self.text_seq_length, 4]							,					self.range_bbox )
       _SCREAMING_SNAKE_CASE							    =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							    =bbox[i, j, 3]
                      _SCREAMING_SNAKE_CASE							    =bbox[i, j, 1]
                      _SCREAMING_SNAKE_CASE							    =tmp_coordinate
                 if bbox[i, j, 2] < bbox[i, j, 0]:
                      _SCREAMING_SNAKE_CASE							    =bbox[i, j, 2]
                      _SCREAMING_SNAKE_CASE							    =bbox[i, j, 0]
                      _SCREAMING_SNAKE_CASE							    =tmp_coordinate
       _SCREAMING_SNAKE_CASE							    =tf.constant(_a )

       _SCREAMING_SNAKE_CASE							    =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

       _SCREAMING_SNAKE_CASE							    =None
       if self.use_input_mask:
            _SCREAMING_SNAKE_CASE							    =random_attention_mask([self.batch_size, self.text_seq_length] )

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

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

       _SCREAMING_SNAKE_CASE							    =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      :	Optional[int]							,					_a      :	List[str]							,					_a      :	str							,					_a      :	Any							,					_a      :	Any							,					_a      :	Tuple							,					_a      :	Optional[Any] )	->    Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =TFLayoutLMvaModel(config=_a )

       # text + image
       _SCREAMING_SNAKE_CASE							    =model(_a							,					pixel_values=_a							,					training=_a )
       _SCREAMING_SNAKE_CASE							    =model(
           _a							,					bbox=_a							,					pixel_values=_a							,					attention_mask=_a							,					token_type_ids=_a							,					training=_a							,					)
       _SCREAMING_SNAKE_CASE							    =model(_a							,					bbox=_a							,					pixel_values=_a							,					training=_a )

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

       # text only
       _SCREAMING_SNAKE_CASE							    =model(_a							,					training=_a )
       self.parent.assertEqual(
           result.last_hidden_state.shape							,					(self.batch_size, self.text_seq_length, self.hidden_size) )

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





  def 						A  (		self      :	List[Any]							,					_a      :	Optional[int]							,					_a      :	Optional[int]							,					_a      :	Dict							,					_a      :	List[str]							,					_a      :	Any							,					_a      :	Tuple							,					_a      :	List[str] )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.num_labels
       _SCREAMING_SNAKE_CASE							    =TFLayoutLMvaForSequenceClassification(config=_a )
       _SCREAMING_SNAKE_CASE							    =model(
           _a							,					bbox=_a							,					pixel_values=_a							,					attention_mask=_a							,					token_type_ids=_a							,					labels=_a							,					training=_a							,					)
       self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.num_labels) )





  def 						A  (		self      :	Tuple							,					_a      :	Dict							,					_a      :	List[str]							,					_a      :	List[str]							,					_a      :	Optional[Any]							,					_a      :	Dict							,					_a      :	str							,					_a      :	str )	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.num_labels
       _SCREAMING_SNAKE_CASE							    =TFLayoutLMvaForTokenClassification(config=_a )
       _SCREAMING_SNAKE_CASE							    =model(
           _a							,					bbox=_a							,					pixel_values=_a							,					attention_mask=_a							,					token_type_ids=_a							,					labels=_a							,					training=_a							,					)
       self.parent.assertEqual(result.logits.shape							,					(self.batch_size, self.text_seq_length, self.num_labels) )





  def 						A  (		self      :	Union[str, Any]							,					_a      :	int							,					_a      :	Dict							,					_a      :	Optional[int]							,					_a      :	Optional[int]							,					_a      :	Union[str, Any]							,					_a      :	Any							,					_a      :	str )	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =2
       _SCREAMING_SNAKE_CASE							    =TFLayoutLMvaForQuestionAnswering(config=_a )
       _SCREAMING_SNAKE_CASE							    =model(
           _a							,					bbox=_a							,					pixel_values=_a							,					attention_mask=_a							,					token_type_ids=_a							,					start_positions=_a							,					end_positions=_a							,					training=_a							,					)
       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[str] )	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.prepare_config_and_inputs()
       ((_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE)    ,						(_SCREAMING_SNAKE_CASE))							    =config_and_inputs
       _SCREAMING_SNAKE_CASE							    ={
           'input_ids': input_ids,
           'bbox': bbox,
           'pixel_values': pixel_values,
           'token_type_ids': token_type_ids,
           'attention_mask': input_mask,
       }
       return config, inputs_dict







@require_tf
class 							A__    (       A__     ,   A__     ,   unittest.TestCase  ):
  A__		 =				(
      (
          TFLayoutLMvaModel,
          TFLayoutLMvaForQuestionAnswering,
          TFLayoutLMvaForSequenceClassification,
          TFLayoutLMvaForTokenClassification,
      )
      if is_tf_available()
      else ()
  )
  A__		 =				(
      {'document-question-answering': TFLayoutLMvaForQuestionAnswering, 'feature-extraction': TFLayoutLMvaModel}
      if is_tf_available()
      else {}
  )

  A__		 =				False
  A__		 =				False
  A__		 =				False





  def 						A  (		self      :	Optional[Any]							,					_a      :	List[str]							,					_a      :	int							,					_a      :	Any							,					_a      :	Optional[Any]							,					_a      :	str )	->    Union[str, Any]:





       '''simple docstring'''






       return True





  def 						A  (		self      :	Dict							,					_a      :	Dict							,					_a      :	Union[str, Any]							,					_a      :	Optional[int]=False )	->    dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =copy.deepcopy(_a )

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

       if return_labels:
            if model_class in get_values(_a ):
                 _SCREAMING_SNAKE_CASE							    =tf.ones(self.model_tester.batch_size							,					dtype=tf.intaa )
            elif model_class in get_values(_a ):
                 _SCREAMING_SNAKE_CASE							    =tf.zeros(self.model_tester.batch_size							,					dtype=tf.intaa )
                 _SCREAMING_SNAKE_CASE							    =tf.zeros(self.model_tester.batch_size							,					dtype=tf.intaa )
            elif model_class in get_values(_a ):
                 _SCREAMING_SNAKE_CASE							    =tf.zeros(self.model_tester.batch_size							,					dtype=tf.intaa )
            elif model_class in get_values(_a ):
                 _SCREAMING_SNAKE_CASE							    =tf.zeros(
                     (self.model_tester.batch_size, self.model_tester.text_seq_length)							,					dtype=tf.intaa )

       return inputs_dict





  def 						A  (		self      :	int )	->    Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =TFLayoutLMvaModelTester(self )
       _SCREAMING_SNAKE_CASE							    =ConfigTester(self							,					config_class=_a							,					hidden_size=37 )





  def 						A  (		self      :	Any )	->    int:





       '''simple docstring'''






       self.config_tester.run_common_tests()





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





       '''simple docstring'''






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

                 # Test that model correctly compute the loss with kwargs
                 _SCREAMING_SNAKE_CASE							    =self._prepare_for_class(inputs_dict.copy()							,					_a							,					return_labels=_a )
                 _SCREAMING_SNAKE_CASE							    =prepared_for_class.pop('input_ids' )

                 _SCREAMING_SNAKE_CASE							    =model(_a							,					**_a )[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							    =self._prepare_for_class(inputs_dict.copy()							,					_a							,					return_labels=_a )
                 _SCREAMING_SNAKE_CASE							    =prepared_for_class.pop('input_ids' )
                 if "labels" in prepared_for_class:
                      _SCREAMING_SNAKE_CASE							    =prepared_for_class['labels'].numpy()
                      if len(labels.shape ) > 1 and labels.shape[1] != 1:
                           _SCREAMING_SNAKE_CASE							    =-100
                           _SCREAMING_SNAKE_CASE							    =tf.convert_to_tensor(_a )
                           _SCREAMING_SNAKE_CASE							    =model(_a							,					**_a )[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							    =self._prepare_for_class(inputs_dict.copy()							,					_a							,					return_labels=_a )
                 _SCREAMING_SNAKE_CASE							    =model(_a )[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							    =self._prepare_for_class(inputs_dict.copy()							,					_a							,					return_labels=_a )

                 # Get keys that were added with the _prepare_for_class function
                 _SCREAMING_SNAKE_CASE							    =prepared_for_class.keys() - inputs_dict.keys()
                 _SCREAMING_SNAKE_CASE							    =inspect.signature(model.call ).parameters
                 _SCREAMING_SNAKE_CASE							    =list(signature.keys() )

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

                 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							    =prepared_for_class[value]

                 _SCREAMING_SNAKE_CASE							    =tuple(_a )

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

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





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





       '''simple docstring'''






       (
           (
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						
       )							    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(_a							,					_a							,					_a							,					_a							,					_a							,					_a )





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





       '''simple docstring'''






       (
           (
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						
       )							    =self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
            _SCREAMING_SNAKE_CASE							    =type
            self.model_tester.create_and_check_model(_a							,					_a							,					_a							,					_a							,					_a							,					_a )





  def 						A  (		self      :	str )	->    Dict:





       '''simple docstring'''






       (
           (
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						
       )							    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(
           _a							,					_a							,					_a							,					_a							,					_a							,					_a							,					_a )





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





       '''simple docstring'''






       (
           (
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						
       )							    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(
           _a							,					_a							,					_a							,					_a							,					_a							,					_a							,					_a )





  def 						A  (		self      :	str )	->    Tuple:





       '''simple docstring'''






       (
           (
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						(
           _SCREAMING_SNAKE_CASE
       )    ,						
       )							    =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(
           _a							,					_a							,					_a							,					_a							,					_a							,					_a							,					_a )





  @slow
  def 						A  (		self      :	Any )	->    str:





       '''simple docstring'''






       for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            _SCREAMING_SNAKE_CASE							    =TFLayoutLMvaModel.from_pretrained(_a )
            self.assertIsNotNone(_a )


def        _lowerCAmelCase					(						)      ->			List[Any]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
     return image







@require_tf
class 							A__    (       unittest.TestCase  ):





  @cached_property
  def 						A  (		self      :	Dict )	->    List[Any]:





       '''simple docstring'''






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





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =TFLayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base' )

       _SCREAMING_SNAKE_CASE							    =self.default_image_processor
       _SCREAMING_SNAKE_CASE							    =prepare_img()
       _SCREAMING_SNAKE_CASE							    =image_processor(images=_a							,					return_tensors='tf' ).pixel_values

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

       # forward pass
       _SCREAMING_SNAKE_CASE							    =model(input_ids=_a							,					bbox=_a							,					pixel_values=_a							,					training=_a )

       # verify the logits
       _SCREAMING_SNAKE_CASE							    =(1, 199, 768)
       self.assertEqual(outputs.last_hidden_state.shape							,					_a )

       _SCREAMING_SNAKE_CASE							    =tf.constant(
           [[-0.05_29, 0.36_18, 0.16_32], [-0.15_87, -0.16_67, -0.04_00], [-0.15_57, -0.16_71, -0.05_05]] )

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



'''simple docstring'''
import numpy as np
from PIL import Image


def        _lowerCAmelCase					(						_UpperCamelCase						:     np.ndarray							, _UpperCamelCase						:     int							, _UpperCamelCase						:     int				)      ->			np.ndarray:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =np.array(_UpperCamelCase				)
     if arr.shape[0] != arr.shape[1]:
          raise ValueError('The input array is not a square matrix'				)
     _SCREAMING_SNAKE_CASE							    =0
     _SCREAMING_SNAKE_CASE							    =0
     _SCREAMING_SNAKE_CASE							    =0
     _SCREAMING_SNAKE_CASE							    =0

     # compute the shape of the output matrix
     _SCREAMING_SNAKE_CASE							    =(arr.shape[0] - size) // stride + 1
     # initialize the output matrix with zeros of shape maxpool_shape
     _SCREAMING_SNAKE_CASE							    =np.zeros((maxpool_shape, maxpool_shape)				)

     while i < arr.shape[0]:
          if i + size > arr.shape[0]:
               # if the end of the matrix is reached, break
               break
          while j < arr.shape[1]:
               # if the end of the matrix is reached, break
               if j + size > arr.shape[1]:
                    break
               # compute the maximum of the pooling matrix
               _SCREAMING_SNAKE_CASE							    =np.max(arr[i : i + size, j : j + size]				)
               # shift the pooling matrix by stride of column pixels
               j += stride
               mat_j += 1

          # shift the pooling matrix by stride of row pixels
          i += stride
          mat_i += 1

          # reset the column index to 0
          _SCREAMING_SNAKE_CASE							    =0
          _SCREAMING_SNAKE_CASE							    =0

     return updated_arr


def        _lowerCAmelCase					(						_UpperCamelCase						:     np.ndarray							, _UpperCamelCase						:     int							, _UpperCamelCase						:     int				)      ->			np.ndarray:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							    =np.array(_UpperCamelCase				)
     if arr.shape[0] != arr.shape[1]:
          raise ValueError('The input array is not a square matrix'				)
     _SCREAMING_SNAKE_CASE							    =0
     _SCREAMING_SNAKE_CASE							    =0
     _SCREAMING_SNAKE_CASE							    =0
     _SCREAMING_SNAKE_CASE							    =0

     # compute the shape of the output matrix
     _SCREAMING_SNAKE_CASE							    =(arr.shape[0] - size) // stride + 1
     # initialize the output matrix with zeros of shape avgpool_shape
     _SCREAMING_SNAKE_CASE							    =np.zeros((avgpool_shape, avgpool_shape)				)

     while i < arr.shape[0]:
          # if the end of the matrix is reached, break
          if i + size > arr.shape[0]:
               break
          while j < arr.shape[1]:
               # if the end of the matrix is reached, break
               if j + size > arr.shape[1]:
                    break
               # compute the average of the pooling matrix
               _SCREAMING_SNAKE_CASE							    =int(np.average(arr[i : i + size, j : j + size]				)				)
               # shift the pooling matrix by stride of column pixels
               j += stride
               mat_j += 1

          # shift the pooling matrix by stride of row pixels
          i += stride
          mat_i += 1
          # reset the column index to 0
          _SCREAMING_SNAKE_CASE							    =0
          _SCREAMING_SNAKE_CASE							    =0

     return updated_arr


# Main Function
if __name__ == "__main__":
     from doctest import testmod

     testmod(name="avgpooling", verbose=True)

     # Loading the image
     lowerCamelCase :      Optional[Any]					 =     Image.open("path_to_image")

     # Converting the image to numpy array and maxpooling, displaying the result
     # Ensure that the image is a square matrix

     Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

     # Converting the image to numpy array and averagepooling, displaying the result
     # Ensure that the image is a square matrix

     Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()
