import argparse
import glob
import logging
import os
import time
from argparse import Namespace

import numpy as np
import torch
from lightning_base import BaseTransformer, add_generic_args, generic_train
from torch.utils.data import DataLoader, TensorDataset

from transformers import glue_compute_metrics as compute_metrics
from transformers import glue_convert_examples_to_features as convert_examples_to_features
from transformers import glue_output_modes, glue_tasks_num_labels
from transformers import glue_processors as processors


__lowerCamelCase      :			Union[str, Any]							       =		logging.getLogger(__name__)







class __snake_case    (						lowerCamelCase_		):
   lowerCAmelCase_														=							"sequence-classification"




   def __init__(   self      :    Optional[Any]	,     _lowercase      :    int				):



     """simple docstring"""






     if type(_lowercase				) == dict:
       SCREAMING_SNAKE_CASE__      							=				Namespace(**_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				glue_output_modes[hparams.task]
     SCREAMING_SNAKE_CASE__      							=				glue_tasks_num_labels[hparams.task]

     super().__init__(_lowercase	,     _lowercase	,     self.mode				)




   def      __a						(   self      :    Any	,     **_lowercase      :    Optional[int]				):



     """simple docstring"""






     return self.model(**_lowercase				)




   def      __a						(   self      :    List[str]	,     _lowercase      :    Optional[int]	,     _lowercase      :    int				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				{"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]}

     if self.config.model_type not in ["distilbert", "bart"]:
       SCREAMING_SNAKE_CASE__      							=				batch[2] if self.config.model_type in ["""bert""", """xlnet""", """albert"""] else None

     SCREAMING_SNAKE_CASE__      							=				self(**_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				outputs[0]

     SCREAMING_SNAKE_CASE__      							=				self.trainer.lr_schedulers[0]["""scheduler"""]
     SCREAMING_SNAKE_CASE__      							=				{"""loss""": loss, """rate""": lr_scheduler.get_last_lr()[-1]}
     return {"loss": loss, "log": tensorboard_logs}




   def      __a						(   self      :    int				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				self.hparams
     SCREAMING_SNAKE_CASE__      							=				processors[args.task]()
     SCREAMING_SNAKE_CASE__      							=				processor.get_labels()

     for mode in ["train", "dev"]:
       SCREAMING_SNAKE_CASE__      							=				self._feature_file(_lowercase				)
       if os.path.exists(_lowercase				) and not args.overwrite_cache:
         logger.info("""Loading features from cached file %s"""	,     _lowercase				)
       else:
         logger.info("""Creating features from dataset file at %s"""	,     args.data_dir				)
         SCREAMING_SNAKE_CASE__      							=				(
             processor.get_dev_examples(args.data_dir				)
             if mode == """dev"""
             else processor.get_train_examples(args.data_dir				)
         )
         SCREAMING_SNAKE_CASE__      							=				convert_examples_to_features(
             _lowercase	,     self.tokenizer	,     max_length=args.max_seq_length	,     label_list=self.labels	,     output_mode=args.glue_output_mode	,     )
         logger.info("""Saving features into cached file %s"""	,     _lowercase				)
         torch.save(_lowercase	,     _lowercase				)




   def      __a						(   self      :    List[Any]	,     _lowercase      :    str	,     _lowercase      :    int	,     _lowercase      :    bool = False				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				"""dev""" if mode == """test""" else mode

     SCREAMING_SNAKE_CASE__      							=				self._feature_file(_lowercase				)
     logger.info("""Loading features from cached file %s"""	,     _lowercase				)
     SCREAMING_SNAKE_CASE__      							=				torch.load(_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				torch.tensor([f.input_ids for f in features]	,     dtype=torch.long				)
     SCREAMING_SNAKE_CASE__      							=				torch.tensor([f.attention_mask for f in features]	,     dtype=torch.long				)
     SCREAMING_SNAKE_CASE__      							=				torch.tensor([f.token_type_ids for f in features]	,     dtype=torch.long				)
     if self.hparams.glue_output_mode == "classification":
       SCREAMING_SNAKE_CASE__      							=				torch.tensor([f.label for f in features]	,     dtype=torch.long				)
     elif self.hparams.glue_output_mode == "regression":
       SCREAMING_SNAKE_CASE__      							=				torch.tensor([f.label for f in features]	,     dtype=torch.float				)

     return DataLoader(
         TensorDataset(_lowercase	,     _lowercase	,     _lowercase	,     _lowercase				)	,     batch_size=_lowercase	,     shuffle=_lowercase	,     )




   def      __a						(   self      :    Any	,     _lowercase      :    Union[str, Any]	,     _lowercase      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				{"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]}

     if self.config.model_type not in ["distilbert", "bart"]:
       SCREAMING_SNAKE_CASE__      							=				batch[2] if self.config.model_type in ["""bert""", """xlnet""", """albert"""] else None

     SCREAMING_SNAKE_CASE__      							=				self(**_lowercase				)
     SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__      							=				outputs[:2]
     SCREAMING_SNAKE_CASE__      							=				logits.detach().cpu().numpy()
     SCREAMING_SNAKE_CASE__      							=				inputs["""labels"""].detach().cpu().numpy()

     return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids}




   def      __a						(   self      :    List[str]	,     _lowercase      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				torch.stack([x["""val_loss"""] for x in outputs]				).mean().detach().cpu().item()
     SCREAMING_SNAKE_CASE__      							=				np.concatenate([x["""pred"""] for x in outputs]	,     axis=0				)

     if self.hparams.glue_output_mode == "classification":
       SCREAMING_SNAKE_CASE__      							=				np.argmax(_lowercase	,     axis=1				)
     elif self.hparams.glue_output_mode == "regression":
       SCREAMING_SNAKE_CASE__      							=				np.squeeze(_lowercase				)

     SCREAMING_SNAKE_CASE__      							=				np.concatenate([x["""target"""] for x in outputs]	,     axis=0				)
     SCREAMING_SNAKE_CASE__      							=				[[] for _ in range(out_label_ids.shape[0]				)]
     SCREAMING_SNAKE_CASE__      							=				[[] for _ in range(out_label_ids.shape[0]				)]

     SCREAMING_SNAKE_CASE__      							=				{**{"""val_loss""": val_loss_mean}, **compute_metrics(self.hparams.task	,     _lowercase	,     _lowercase				)}

     SCREAMING_SNAKE_CASE__      							=				dict(results.items()				)
     SCREAMING_SNAKE_CASE__      							=				results
     return ret, preds_list, out_label_list




   def      __a						(   self      :    Union[str, Any]	,     _lowercase      :    list				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__      							=				self._eval_end(_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				ret["""log"""]
     return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}




   def      __a						(   self      :    Tuple	,     _lowercase      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__      							=				self._eval_end(_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				ret["""log"""]
     # `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss`
     return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs}




   @staticmethod
   def      __a						(   _lowercase      :    str	,     _lowercase      :    Optional[Any]				):



     """simple docstring"""






     BaseTransformer.add_model_specific_args(_lowercase	,     _lowercase				)
     parser.add_argument(
         """--max_seq_length"""	,     default=1_28	,     type=_lowercase	,     help=(
             """The maximum total input sequence length after tokenization. Sequences longer """
             """than this will be truncated, sequences shorter will be padded."""
         )	,     )

     parser.add_argument(
         """--task"""	,     default=""""""	,     type=_lowercase	,     required=_lowercase	,     help="""The GLUE task to run"""	,     )
     parser.add_argument(
         """--gpus"""	,     default=0	,     type=_lowercase	,     help="""The number of GPUs allocated for this, it is by default 0 meaning none"""	,     )

     parser.add_argument(
         """--overwrite_cache"""	,     action="""store_true"""	,     help="""Overwrite the cached training and evaluation sets"""				)

     return parser





def      __SCREAMING_SNAKE_CASE  ( )							-> int:







  """simple docstring"""





  SCREAMING_SNAKE_CASE__      							=				argparse.ArgumentParser()
  add_generic_args(__UpperCamelCase			,					os.getcwd()     )
  SCREAMING_SNAKE_CASE__      							=				GLUETransformer.add_model_specific_args(__UpperCamelCase			,					os.getcwd()     )
  SCREAMING_SNAKE_CASE__      							=				parser.parse_args()

  # If output_dir not provided, a folder will be generated in pwd
  if args.output_dir is None:
    SCREAMING_SNAKE_CASE__      							=				os.path.join(
        """./results"""			,					f"""{args.task}_{time.strftime('%Y%m%d_%H%M%S'     )}"""			,					)
    os.makedirs(args.output_dir     )

  SCREAMING_SNAKE_CASE__      							=				GLUETransformer(__UpperCamelCase     )
  SCREAMING_SNAKE_CASE__      							=				generic_train(__UpperCamelCase			,					__UpperCamelCase     )

  # Optionally, predict on dev set and write to output_dir
  if args.do_predict:
    SCREAMING_SNAKE_CASE__      							=				sorted(glob.glob(os.path.join(args.output_dir			,					"""checkpoint-epoch=*.ckpt"""     )			,					recursive=__UpperCamelCase     )     )
    SCREAMING_SNAKE_CASE__      							=				model.load_from_checkpoint(checkpoints[-1]     )
    return trainer.test(__UpperCamelCase     )


if __name__ == "__main__":
   main()

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




   def __init__(   self      :    Any	,     _lowercase      :    Tuple	,     _lowercase      :    str=2	,     _lowercase      :    List[Any]=3	,     _lowercase      :    Optional[Any]=4	,     _lowercase      :    Optional[Any]=2	,     _lowercase      :    str=7	,     _lowercase      :    Dict=True	,     _lowercase      :    List[str]=True	,     _lowercase      :    Union[str, Any]=True	,     _lowercase      :    Optional[int]=True	,     _lowercase      :    Dict=99	,     _lowercase      :    Dict=36	,     _lowercase      :    Tuple=2	,     _lowercase      :    Optional[int]=4	,     _lowercase      :    int=37	,     _lowercase      :    Tuple="gelu"	,     _lowercase      :    Optional[Any]=0.1	,     _lowercase      :    Tuple=0.1	,     _lowercase      :    str=5_12	,     _lowercase      :    Dict=16	,     _lowercase      :    int=2	,     _lowercase      :    int=0.02	,     _lowercase      :    Any=6	,     _lowercase      :    List[Any]=6	,     _lowercase      :    List[Any]=3	,     _lowercase      :    List[Any]=4	,     _lowercase      :    int=None	,     _lowercase      :    Optional[int]=10_00	,     ):



     """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      :    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(_lowercase				)

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



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				TFLayoutLMvaModel(config=_lowercase				)

     # text + image
     SCREAMING_SNAKE_CASE__      							=				model(_lowercase	,     pixel_values=_lowercase	,     training=_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				model(
         _lowercase	,     bbox=_lowercase	,     pixel_values=_lowercase	,     attention_mask=_lowercase	,     token_type_ids=_lowercase	,     training=_lowercase	,     )
     SCREAMING_SNAKE_CASE__      							=				model(_lowercase	,     bbox=_lowercase	,     pixel_values=_lowercase	,     training=_lowercase				)

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

     # text only
     SCREAMING_SNAKE_CASE__      							=				model(_lowercase	,     training=_lowercase				)
     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=_lowercase				)
     self.parent.assertEqual(
         result.last_hidden_state.shape	,     (self.batch_size, self.image_seq_length, self.hidden_size)				)




   def      __a						(   self      :    int	,     _lowercase      :    int	,     _lowercase      :    Optional[int]	,     _lowercase      :    Optional[Any]	,     _lowercase      :    Optional[int]	,     _lowercase      :    Tuple	,     _lowercase      :    List[Any]	,     _lowercase      :    Tuple				):



     """simple docstring"""






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




   def      __a						(   self      :    Any	,     _lowercase      :    Dict	,     _lowercase      :    Tuple	,     _lowercase      :    int	,     _lowercase      :    Optional[int]	,     _lowercase      :    Optional[int]	,     _lowercase      :    Union[str, Any]	,     _lowercase      :    Tuple				):



     """simple docstring"""






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




   def      __a						(   self      :    str	,     _lowercase      :    int	,     _lowercase      :    List[str]	,     _lowercase      :    str	,     _lowercase      :    str	,     _lowercase      :    List[str]	,     _lowercase      :    List[str]	,     _lowercase      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				2
     SCREAMING_SNAKE_CASE__      							=				TFLayoutLMvaForQuestionAnswering(config=_lowercase				)
     SCREAMING_SNAKE_CASE__      							=				model(
         _lowercase	,     bbox=_lowercase	,     pixel_values=_lowercase	,     attention_mask=_lowercase	,     token_type_ids=_lowercase	,     start_positions=_lowercase	,     end_positions=_lowercase	,     training=_lowercase	,     )
     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]				):



     """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 __snake_case    (						lowerCamelCase_       ,		lowerCamelCase_       ,		unittest.TestCase		):
   lowerCAmelCase_														=							(
       (
           TFLayoutLMvaModel,
           TFLayoutLMvaForQuestionAnswering,
           TFLayoutLMvaForSequenceClassification,
           TFLayoutLMvaForTokenClassification,
       )
       if is_tf_available()
       else ()
   )
   lowerCAmelCase_														=							(
       {"document-question-answering": TFLayoutLMvaForQuestionAnswering, "feature-extraction": TFLayoutLMvaModel}
       if is_tf_available()
       else {}
   )

   lowerCAmelCase_														=							False
   lowerCAmelCase_														=							False
   lowerCAmelCase_														=							False




   def      __a						(   self      :    Union[str, Any]	,     _lowercase      :    Optional[int]	,     _lowercase      :    List[str]	,     _lowercase      :    Optional[Any]	,     _lowercase      :    Optional[int]	,     _lowercase      :    List[Any]				):



     """simple docstring"""






     return True




   def      __a						(   self      :    List[str]	,     _lowercase      :    List[Any]	,     _lowercase      :    str	,     _lowercase      :    str=False				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				copy.deepcopy(_lowercase				)

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

     if return_labels:
       if model_class in get_values(_lowercase				):
         SCREAMING_SNAKE_CASE__      							=				tf.ones(self.model_tester.batch_size	,     dtype=tf.intaa				)
       elif model_class in get_values(_lowercase				):
         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(_lowercase				):
         SCREAMING_SNAKE_CASE__      							=				tf.zeros(self.model_tester.batch_size	,     dtype=tf.intaa				)
       elif model_class in get_values(_lowercase				):
         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      :    Tuple				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				TFLayoutLMvaModelTester(self				)
     SCREAMING_SNAKE_CASE__      							=				ConfigTester(self	,     config_class=_lowercase	,     hidden_size=37				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     self.config_tester.run_common_tests()




   def      __a						(   self      :    Dict				):



     """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(_lowercase				)
       if getattr(_lowercase	,     """hf_compute_loss"""	,     _lowercase				):
         # 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()	,     _lowercase	,     return_labels=_lowercase				)
         SCREAMING_SNAKE_CASE__      							=				prepared_for_class[
             sorted(prepared_for_class.keys() - inputs_dict.keys()	,     reverse=_lowercase				)[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()	,     _lowercase	,     return_labels=_lowercase				)
         SCREAMING_SNAKE_CASE__      							=				prepared_for_class.pop("""input_ids"""				)

         SCREAMING_SNAKE_CASE__      							=				model(_lowercase	,     **_lowercase				)[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()	,     _lowercase	,     return_labels=_lowercase				)
         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__      							=				-1_00
             SCREAMING_SNAKE_CASE__      							=				tf.convert_to_tensor(_lowercase				)
             SCREAMING_SNAKE_CASE__      							=				model(_lowercase	,     **_lowercase				)[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()	,     _lowercase	,     return_labels=_lowercase				)
         SCREAMING_SNAKE_CASE__      							=				model(_lowercase				)[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()	,     _lowercase	,     return_labels=_lowercase				)

         # 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(_lowercase				)
           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(_lowercase				)

         # 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      :    Optional[int]				):



     """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(_lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase				)




   def      __a						(   self      :    int				):



     """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(_lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase				)




   def      __a						(   self      :    int				):



     """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(
         _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase				)




   def      __a						(   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__
     )							,					
     )      							=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(
         _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase				)




   def      __a						(   self      :    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_for_question_answering(
         _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase	,     _lowercase				)




   @slow
   def      __a						(   self      :    Tuple				):



     """simple docstring"""






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





def      __SCREAMING_SNAKE_CASE  ( )							-> List[Any]:







  """simple docstring"""





  SCREAMING_SNAKE_CASE__      							=				Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
  return image







@require_tf
class __snake_case    (						unittest.TestCase		):




   @cached_property
   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






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




   @slow
   def      __a						(   self      :    Tuple				):



     """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=_lowercase	,     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=_lowercase	,     bbox=_lowercase	,     pixel_values=_lowercase	,     training=_lowercase				)

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

     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]	,     _lowercase	,     atol=1E-4				)				)
