





"""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 					_SCREAMING_SNAKE_CASE:




       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__=99		,SCREAMING_SNAKE_CASE__=36		,SCREAMING_SNAKE_CASE__=2		,SCREAMING_SNAKE_CASE__=4		,SCREAMING_SNAKE_CASE__=37		,SCREAMING_SNAKE_CASE__="gelu"		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=0.1		,SCREAMING_SNAKE_CASE__=5_12		,SCREAMING_SNAKE_CASE__=16		,SCREAMING_SNAKE_CASE__=2		,SCREAMING_SNAKE_CASE__=0.0_2		,SCREAMING_SNAKE_CASE__=6		,SCREAMING_SNAKE_CASE__=6		,SCREAMING_SNAKE_CASE__=3		,SCREAMING_SNAKE_CASE__=4		,SCREAMING_SNAKE_CASE__=None		,SCREAMING_SNAKE_CASE__=10_00		,)			->      List[Any]:





             """simple docstring"""






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




       def     _UpperCamelCase     (						self							)			->      Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  ids_tensor([self.batch_size, self.text_seq_length]		,self.vocab_size							)

             __SCREAMING_SNAKE_CASE   :List[str]						=  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   :List[str]						=  bbox[i, j, 3]
                               __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  bbox[i, j, 1]
                               __SCREAMING_SNAKE_CASE   :str						=  tmp_coordinate
                         if bbox[i, j, 2] < bbox[i, j, 0]:
                               __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  bbox[i, j, 2]
                               __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  bbox[i, j, 0]
                               __SCREAMING_SNAKE_CASE   :int						=  tmp_coordinate
             __SCREAMING_SNAKE_CASE   :Any						=  tf.constant(SCREAMING_SNAKE_CASE__							)

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

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

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

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

             __SCREAMING_SNAKE_CASE   :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     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  TFLayoutLMvaModel(config=SCREAMING_SNAKE_CASE__							)

             # text + image
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  model(SCREAMING_SNAKE_CASE__		,pixel_values=SCREAMING_SNAKE_CASE__		,training=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :Optional[int]						=  model(
                 SCREAMING_SNAKE_CASE__		,bbox=SCREAMING_SNAKE_CASE__		,pixel_values=SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,training=SCREAMING_SNAKE_CASE__		,)
             __SCREAMING_SNAKE_CASE   :Optional[Any]						=  model(SCREAMING_SNAKE_CASE__		,bbox=SCREAMING_SNAKE_CASE__		,pixel_values=SCREAMING_SNAKE_CASE__		,training=SCREAMING_SNAKE_CASE__							)

             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(SCREAMING_SNAKE_CASE__		,training=SCREAMING_SNAKE_CASE__							)
             self.parent.assertEqual(
                 result.last_hidden_state.shape		,(self.batch_size, self.text_seq_length, self.hidden_size)							)

             # image only
             __SCREAMING_SNAKE_CASE   :List[str]						=  model({'''pixel_values''': pixel_values}		,training=SCREAMING_SNAKE_CASE__							)
             self.parent.assertEqual(
                 result.last_hidden_state.shape		,(self.batch_size, self.image_seq_length, self.hidden_size)							)




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :int						=  self.num_labels
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  TFLayoutLMvaForSequenceClassification(config=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE   :str						=  model(
                 SCREAMING_SNAKE_CASE__		,bbox=SCREAMING_SNAKE_CASE__		,pixel_values=SCREAMING_SNAKE_CASE__		,attention_mask=SCREAMING_SNAKE_CASE__		,token_type_ids=SCREAMING_SNAKE_CASE__		,labels=SCREAMING_SNAKE_CASE__		,training=SCREAMING_SNAKE_CASE__		,)
             self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.num_labels)							)




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      List[str]:





             """simple docstring"""






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




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)			->      int:





             """simple docstring"""






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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Dict						=  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))   :List[str]						=  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 					_SCREAMING_SNAKE_CASE(   A    ,	A    ,	unittest.TestCase  ):
       SCREAMING_SNAKE_CASE_   :      List[Any]							=  (
           (
               TFLayoutLMvaModel,
               TFLayoutLMvaForQuestionAnswering,
               TFLayoutLMvaForSequenceClassification,
               TFLayoutLMvaForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       SCREAMING_SNAKE_CASE_   :      Optional[int]							=  (
           {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
           if is_tf_available()
           else {}
       )

       SCREAMING_SNAKE_CASE_   :      List[Any]							=  False
       SCREAMING_SNAKE_CASE_   :      Any							=  False
       SCREAMING_SNAKE_CASE_   :      Optional[Any]							=  False




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





             """simple docstring"""






             return True




       def     _UpperCamelCase     (						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=False							)			->      dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Tuple						=  copy.deepcopy(SCREAMING_SNAKE_CASE__							)

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

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

             return inputs_dict




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  TFLayoutLMvaModelTester(self							)
             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  ConfigTester(self		,config_class=SCREAMING_SNAKE_CASE__		,hidden_size=37							)




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





             """simple docstring"""






             self.config_tester.run_common_tests()




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE				,__SCREAMING_SNAKE_CASE   :Any						=  self.model_tester.prepare_config_and_inputs_for_common()
             for model_class in self.all_model_classes:
                   __SCREAMING_SNAKE_CASE   :List[Any]						=  model_class(SCREAMING_SNAKE_CASE__							)
                   if getattr(SCREAMING_SNAKE_CASE__		,'''hf_compute_loss'''		,SCREAMING_SNAKE_CASE__							):
                         # 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()		,SCREAMING_SNAKE_CASE__		,return_labels=SCREAMING_SNAKE_CASE__							)
                         __SCREAMING_SNAKE_CASE   :Optional[int]						=  prepared_for_class[
                             sorted(prepared_for_class.keys() - inputs_dict.keys()		,reverse=SCREAMING_SNAKE_CASE__							)[0]
                         ]
                         __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  added_label.shape.as_list()[:1]

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

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

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

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

                         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   :str						=  tuple(SCREAMING_SNAKE_CASE__							)

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

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




       def     _UpperCamelCase     (						self							)			->      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
             )				,
             )   :str						=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self							)			->      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
             )				,
             )   :Optional[Any]						=  self.model_tester.prepare_config_and_inputs()
             for type in ["absolute", "relative_key", "relative_key_query"]:
                   __SCREAMING_SNAKE_CASE   :List[str]						=  type
                   self.model_tester.create_and_check_model(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self							)			->      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
             )				,
             )   :Optional[int]						=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(
                 SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self							)			->      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
             )				,
             )   :Dict						=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(
                 SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       def     _UpperCamelCase     (						self							)			->      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
             )				,
             )   :str						=  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(
                 SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)




       @slow
       def     _UpperCamelCase     (						self							)			->      int:





             """simple docstring"""






             for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   __SCREAMING_SNAKE_CASE   :List[Any]						=  TFLayoutLMvaModel.from_pretrained(SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)




def      __lowerCamelCase   (							)			->      str:
      __SCREAMING_SNAKE_CASE   :Any						=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
      return image







@require_tf
class 					_SCREAMING_SNAKE_CASE(   unittest.TestCase  ):




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





             """simple docstring"""






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




       @slow
       def     _UpperCamelCase     (						self							)			->      List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''							)

             __SCREAMING_SNAKE_CASE   :List[str]						=  self.default_image_processor
             __SCREAMING_SNAKE_CASE   :Dict						=  prepare_img()
             __SCREAMING_SNAKE_CASE   :List[str]						=  image_processor(images=SCREAMING_SNAKE_CASE__		,return_tensors='''tf'''							).pixel_values

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

             # forward pass
             __SCREAMING_SNAKE_CASE   :Any						=  model(input_ids=SCREAMING_SNAKE_CASE__		,bbox=SCREAMING_SNAKE_CASE__		,pixel_values=SCREAMING_SNAKE_CASE__		,training=SCREAMING_SNAKE_CASE__							)

             # verify the logits
             __SCREAMING_SNAKE_CASE   :int						=  (1, 1_99, 7_68)
             self.assertEqual(outputs.last_hidden_state.shape		,SCREAMING_SNAKE_CASE__							)

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

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






"""simple docstring"""
import argparse
import os

import torch

from transformers import FlavaImageCodebook, FlavaImageCodebookConfig



def      __lowerCamelCase   (							a_     :		str ,   a_     :		Dict ,   a_     :		Any ,   a_     :		str							)			->      str:
      __SCREAMING_SNAKE_CASE   :int						=  s.rsplit(a_ ,   a_							)
      return new.join(a_							)



def      __lowerCamelCase   (							a_     :		List[str]							)			->      Dict:
      # encoder.embeddings are double copied in original FLAVA
      return sum(param.float().sum() if '''encoder.embeddings''' not in key else 0 for key, param in state_dict.items()							)



def      __lowerCamelCase   (							a_     :		Optional[int]							)			->      Any:
      __SCREAMING_SNAKE_CASE   :Optional[int]						=  {}

      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  ['''group_1''', '''group_2''', '''group_3''', '''group_4''']
      for key, value in state_dict.items():
            for group_key in group_keys:
                  if group_key in key:
                        __SCREAMING_SNAKE_CASE   :Optional[Any]						=  key.replace(f'''{group_key}.''' ,   f'''{group_key}.group.'''							)

            if "res_path" in key:
                  __SCREAMING_SNAKE_CASE   :str						=  key.replace('''res_path.''' ,   '''res_path.path.'''							)

            if key.endswith('''.w'''							):
                  __SCREAMING_SNAKE_CASE   :List[Any]						=  rreplace(a_ ,   '''.w''' ,   '''.weight''' ,   1							)
            if key.endswith('''.b'''							):
                  __SCREAMING_SNAKE_CASE   :List[Any]						=  rreplace(a_ ,   '''.b''' ,   '''.bias''' ,   1							)

            __SCREAMING_SNAKE_CASE   :Optional[Any]						=  value.float()

      return upgrade



@torch.no_grad()
def      __lowerCamelCase   (							a_     :		List[Any] ,   a_     :		Optional[int] ,   a_     :		Optional[int]=None ,   a_     :		Dict=True							)			->      Union[str, Any]:
      from dall_e import Encoder

      __SCREAMING_SNAKE_CASE   :int						=  Encoder()
      if os.path.exists(a_							):
            __SCREAMING_SNAKE_CASE   :Dict						=  torch.load(a_							)
      else:
            __SCREAMING_SNAKE_CASE   :List[str]						=  torch.hub.load_state_dict_from_url(a_							)

      if isinstance(a_ ,   a_							):
            __SCREAMING_SNAKE_CASE   :List[str]						=  ckpt.state_dict()
      encoder.load_state_dict(a_							)

      if config_path is not None:
            __SCREAMING_SNAKE_CASE   :Any						=  FlavaImageCodebookConfig.from_pretrained(a_							)
      else:
            __SCREAMING_SNAKE_CASE   :Optional[int]						=  FlavaImageCodebookConfig()

      __SCREAMING_SNAKE_CASE   :Tuple						=  FlavaImageCodebook(a_							).eval()
      __SCREAMING_SNAKE_CASE   :List[str]						=  encoder.state_dict()

      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  upgrade_state_dict(a_							)
      hf_model.load_state_dict(a_							)
      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  hf_model.state_dict()
      __SCREAMING_SNAKE_CASE   :Union[str, Any]						=  count_parameters(a_							)
      __SCREAMING_SNAKE_CASE   :Any						=  count_parameters(a_							)

      assert torch.allclose(a_ ,   a_ ,   atol=1e-3							)

      if save_checkpoint:
            hf_model.save_pretrained(a_							)
      else:
            return hf_state_dict


if __name__ == "__main__":
  lowerCamelCase_								=		argparse.ArgumentParser()
  parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
  parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to flava checkpoint")
  parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert")
  lowerCamelCase_								=		parser.parse_args()

  convert_dalle_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)