





from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

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

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


if is_tf_available():
    import tensorflow as tf

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

if is_vision_available():
    from PIL import Image

    from transformers import LayoutLMvaImageProcessor

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

     # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
     lowerCamelCase__    :      List[Any]					 =		text_seq_length
     lowerCamelCase__    :      Tuple					 =		(image_size // patch_size) ** 2 + 1
     lowerCamelCase__    :      Optional[Any]					 =		self.text_seq_length + self.image_seq_length
 def      A_	(      self  :      Union[str, Any]	)			->	Any:
     lowerCamelCase__    :      Union[str, Any]					 =		ids_tensor([self.batch_size, self.text_seq_length]			,     self.vocab_size	)

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

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

     lowerCamelCase__    :      Optional[Any]					 =		None
     if self.use_input_mask:
         lowerCamelCase__    :      int					 =		random_attention_mask([self.batch_size, self.text_seq_length]	)

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

     lowerCamelCase__    :      Dict					 =		None
     lowerCamelCase__    :      Any					 =		None
     if self.use_labels:
         lowerCamelCase__    :      Tuple					 =		ids_tensor([self.batch_size]			,     self.type_sequence_label_size	)
         lowerCamelCase__    :      Dict					 =		ids_tensor([self.batch_size, self.text_seq_length]			,     self.num_labels	)

     lowerCamelCase__    :      int					 =		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]			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      List[Any]			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      Any	)			->	str:
     lowerCamelCase__    :      List[Any]					 =		TFLayoutLMvaModel(config=UpperCAmelCase	)

     # text + image
     lowerCamelCase__    :      str					 =		model(UpperCAmelCase			,     pixel_values=UpperCAmelCase			,     training=UpperCAmelCase	)
     lowerCamelCase__    :      List[Any]					 =		model(
         UpperCAmelCase			,     bbox=UpperCAmelCase			,     pixel_values=UpperCAmelCase			,     attention_mask=UpperCAmelCase			,     token_type_ids=UpperCAmelCase			,     training=UpperCAmelCase			,     )
     lowerCamelCase__    :      Any					 =		model(UpperCAmelCase			,     bbox=UpperCAmelCase			,     pixel_values=UpperCAmelCase			,     training=UpperCAmelCase	)

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

     # text only
     lowerCamelCase__    :      Optional[int]					 =		model(UpperCAmelCase			,     training=UpperCAmelCase	)
     self.parent.assertEqual(
         result.last_hidden_state.shape			,     (self.batch_size, self.text_seq_length, self.hidden_size)	)

     # image only
     lowerCamelCase__    :      int					 =		model({'pixel_values': pixel_values}			,     training=UpperCAmelCase	)
     self.parent.assertEqual(
         result.last_hidden_state.shape			,     (self.batch_size, self.image_seq_length, self.hidden_size)	)
 def      A_	(      self  :      Tuple			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      Dict			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      str			,     UpperCAmelCase  :      List[Any]			,     UpperCAmelCase  :      int			,     UpperCAmelCase  :      List[Any]	)			->	Optional[int]:
     lowerCamelCase__    :      Optional[int]					 =		self.num_labels
     lowerCamelCase__    :      Tuple					 =		TFLayoutLMvaForSequenceClassification(config=UpperCAmelCase	)
     lowerCamelCase__    :      Union[str, Any]					 =		model(
         UpperCAmelCase			,     bbox=UpperCAmelCase			,     pixel_values=UpperCAmelCase			,     attention_mask=UpperCAmelCase			,     token_type_ids=UpperCAmelCase			,     labels=UpperCAmelCase			,     training=UpperCAmelCase			,     )
     self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_labels)	)
 def      A_	(      self  :      Tuple			,     UpperCAmelCase  :      str			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      str			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      Any			,     UpperCAmelCase  :      List[str]	)			->	str:
     lowerCamelCase__    :      List[Any]					 =		self.num_labels
     lowerCamelCase__    :      Any					 =		TFLayoutLMvaForTokenClassification(config=UpperCAmelCase	)
     lowerCamelCase__    :      Optional[int]					 =		model(
         UpperCAmelCase			,     bbox=UpperCAmelCase			,     pixel_values=UpperCAmelCase			,     attention_mask=UpperCAmelCase			,     token_type_ids=UpperCAmelCase			,     labels=UpperCAmelCase			,     training=UpperCAmelCase			,     )
     self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.text_seq_length, self.num_labels)	)
 def      A_	(      self  :      Union[str, Any]			,     UpperCAmelCase  :      int			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      str			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      Optional[int]	)			->	Optional[Any]:
     lowerCamelCase__    :      List[Any]					 =		2
     lowerCamelCase__    :      List[str]					 =		TFLayoutLMvaForQuestionAnswering(config=UpperCAmelCase	)
     lowerCamelCase__    :      Any					 =		model(
         UpperCAmelCase			,     bbox=UpperCAmelCase			,     pixel_values=UpperCAmelCase			,     attention_mask=UpperCAmelCase			,     token_type_ids=UpperCAmelCase			,     start_positions=UpperCAmelCase			,     end_positions=UpperCAmelCase			,     training=UpperCAmelCase			,     )
     self.parent.assertEqual(result.start_logits.shape			,     (self.batch_size, self.seq_length)	)
     self.parent.assertEqual(result.end_logits.shape			,     (self.batch_size, self.seq_length)	)







 def      A_	(      self  :      str	)			->	Optional[Any]:
     lowerCamelCase__    :      Tuple					 =		self.prepare_config_and_inputs()
     ((lowerCamelCase__) ,							(lowerCamelCase__) ,							(lowerCamelCase__) ,							(lowerCamelCase__) ,							(lowerCamelCase__) ,							(lowerCamelCase__) ,							(lowerCamelCase__) ,							(lowerCamelCase__))    :      Union[str, Any]					 =		config_and_inputs
     lowerCamelCase__    :      Dict					 =		{
         'input_ids': input_ids,
         'bbox': bbox,
         'pixel_values': pixel_values,
         'token_type_ids': token_type_ids,
         'attention_mask': input_mask,
     }
     return config, inputs_dict


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

 UpperCAmelCase__													=		False
 UpperCAmelCase__													=		False
 UpperCAmelCase__													=		False
 def      A_	(      self  :      Tuple			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      Optional[int]			,     UpperCAmelCase  :      int			,     UpperCAmelCase  :      Union[str, Any]	)			->	str:
     return True
 def      A_	(      self  :      List[Any]			,     UpperCAmelCase  :      str			,     UpperCAmelCase  :      Union[str, Any]			,     UpperCAmelCase  :      Optional[int]=False	)			->	dict:
     lowerCamelCase__    :      str					 =		copy.deepcopy(UpperCAmelCase	)

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

     if return_labels:
         if model_class in get_values(UpperCAmelCase	):
             lowerCamelCase__    :      List[Any]					 =		tf.ones(self.model_tester.batch_size			,     dtype=tf.intaa	)
         elif model_class in get_values(UpperCAmelCase	):
             lowerCamelCase__    :      int					 =		tf.zeros(self.model_tester.batch_size			,     dtype=tf.intaa	)
             lowerCamelCase__    :      str					 =		tf.zeros(self.model_tester.batch_size			,     dtype=tf.intaa	)
         elif model_class in get_values(UpperCAmelCase	):
             lowerCamelCase__    :      Tuple					 =		tf.zeros(self.model_tester.batch_size			,     dtype=tf.intaa	)
         elif model_class in get_values(UpperCAmelCase	):
             lowerCamelCase__    :      Optional[Any]					 =		tf.zeros(
                 (self.model_tester.batch_size, self.model_tester.text_seq_length)			,     dtype=tf.intaa	)

     return inputs_dict
 def      A_	(      self  :      Dict	)			->	Union[str, Any]:
     lowerCamelCase__    :      str					 =		TFLayoutLMvaModelTester(self	)
     lowerCamelCase__    :      int					 =		ConfigTester(self			,     config_class=UpperCAmelCase			,     hidden_size=37	)
 def      A_	(      self  :      Union[str, Any]	)			->	Tuple:
     self.config_tester.run_common_tests()
 def      A_	(      self  :      Optional[int]	)			->	Tuple:
     lowerCamelCase__ ,							lowerCamelCase__    :      List[str]					 =		self.model_tester.prepare_config_and_inputs_for_common()
     for model_class in self.all_model_classes:
         lowerCamelCase__    :      Optional[Any]					 =		model_class(UpperCAmelCase	)
         if getattr(UpperCAmelCase			,     'hf_compute_loss'			,     UpperCAmelCase	):
             # The number of elements in the loss should be the same as the number of elements in the label
             lowerCamelCase__    :      Optional[Any]					 =		self._prepare_for_class(inputs_dict.copy()			,     UpperCAmelCase			,     return_labels=UpperCAmelCase	)
             lowerCamelCase__    :      Any					 =		prepared_for_class[
                 sorted(prepared_for_class.keys() - inputs_dict.keys()			,     reverse=UpperCAmelCase	)[0]
             ]
             lowerCamelCase__    :      List[str]					 =		added_label.shape.as_list()[:1]

             # Test that model correctly compute the loss with kwargs
             lowerCamelCase__    :      str					 =		self._prepare_for_class(inputs_dict.copy()			,     UpperCAmelCase			,     return_labels=UpperCAmelCase	)
             lowerCamelCase__    :      Tuple					 =		prepared_for_class.pop('input_ids'	)

             lowerCamelCase__    :      List[Any]					 =		model(UpperCAmelCase			,     **UpperCAmelCase	)[0]
             self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]	)

             # Test that model correctly compute the loss when we mask some positions
             lowerCamelCase__    :      Any					 =		self._prepare_for_class(inputs_dict.copy()			,     UpperCAmelCase			,     return_labels=UpperCAmelCase	)
             lowerCamelCase__    :      Tuple					 =		prepared_for_class.pop('input_ids'	)
             if "labels" in prepared_for_class:
                 lowerCamelCase__    :      Tuple					 =		prepared_for_class['labels'].numpy()
                 if len(labels.shape	) > 1 and labels.shape[1] != 1:
                     lowerCamelCase__    :      Union[str, Any]					 =		-100
                     lowerCamelCase__    :      List[str]					 =		tf.convert_to_tensor(UpperCAmelCase	)
                     lowerCamelCase__    :      Tuple					 =		model(UpperCAmelCase			,     **UpperCAmelCase	)[0]
                     self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]	)
                     self.assertTrue(not np.any(np.isnan(loss.numpy()	)	)	)

                # Test that model correctly compute the loss with a dict
             lowerCamelCase__    :      Any					 =		self._prepare_for_class(inputs_dict.copy()			,     UpperCAmelCase			,     return_labels=UpperCAmelCase	)
             lowerCamelCase__    :      Any					 =		model(UpperCAmelCase	)[0]
             self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]	)

             # Test that model correctly compute the loss with a tuple
             lowerCamelCase__    :      Dict					 =		self._prepare_for_class(inputs_dict.copy()			,     UpperCAmelCase			,     return_labels=UpperCAmelCase	)

             # Get keys that were added with the _prepare_for_class function
             lowerCamelCase__    :      Dict					 =		prepared_for_class.keys() - inputs_dict.keys()
             lowerCamelCase__    :      Tuple					 =		inspect.signature(model.call	).parameters
             lowerCamelCase__    :      str					 =		list(signature.keys()	)

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

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

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

             lowerCamelCase__    :      Union[str, Any]					 =		tuple(UpperCAmelCase	)

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

             self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]	)
 def      A_	(      self  :      List[str]	)			->	List[Any]:
     (
         (
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							
     )    :      Tuple					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase	)
 def      A_	(      self  :      List[Any]	)			->	str:
     (
         (
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							
     )    :      int					 =		self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
         lowerCamelCase__    :      Tuple					 =		type
         self.model_tester.create_and_check_model(UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase	)
 def      A_	(      self  :      Union[str, Any]	)			->	Any:
     (
         (
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							
     )    :      Optional[Any]					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(
         UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase	)
 def      A_	(      self  :      str	)			->	List[str]:
     (
         (
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							
     )    :      Tuple					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(
         UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase	)
 def      A_	(      self  :      List[Any]	)			->	str:
     (
         (
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							(
         lowerCamelCase__
     ) ,							
     )    :      Any					 =		self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(
         UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase	)







 @slow
 def      A_	(      self  :      List[Any]	)			->	List[Any]:
     for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowerCamelCase__    :      List[Any]					 =		TFLayoutLMvaModel.from_pretrained(UpperCAmelCase	)
         self.assertIsNotNone(UpperCAmelCase	)


def        SCREAMING_SNAKE_CASE      (       )			->     List[Any]:
    lowerCamelCase__    :      Union[str, Any]					 =		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
    return image


@require_tf
class    lowerCAmelCase							(					unittest.TestCase			):
 @cached_property
 def      A_	(      self  :      int	)			->	Tuple:
     return LayoutLMvaImageProcessor(apply_ocr=UpperCAmelCase	) if is_vision_available() else None







 @slow
 def      A_	(      self  :      Tuple	)			->	Optional[Any]:
     lowerCamelCase__    :      List[Any]					 =		TFLayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'	)

     lowerCamelCase__    :      List[str]					 =		self.default_image_processor
     lowerCamelCase__    :      Any					 =		prepare_img()
     lowerCamelCase__    :      Any					 =		image_processor(images=UpperCAmelCase			,     return_tensors='tf'	).pixel_values

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

     # forward pass
     lowerCamelCase__    :      Optional[int]					 =		model(input_ids=UpperCAmelCase			,     bbox=UpperCAmelCase			,     pixel_values=UpperCAmelCase			,     training=UpperCAmelCase	)

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

     lowerCamelCase__    :      List[str]					 =		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]			,     UpperCAmelCase			,     atol=1e-4	)	)












from __future__ import annotations

import math


def 	UpperCamelCase__(       UpperCamelCase__			:       int  )->bool:

       if 1 < number < 4:
              # 2 and 3 are primes
              return True
       elif number < 2 or number % 2 == 0 or number % 3 == 0:
              # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
              return False

       # All primes number are in format of 6k +/- 1
       for i in range(5 ,     int(math.sqrt(UpperCamelCase__  ) + 1  ) ,     6  ):
              if number % i == 0 or number % (i + 2) == 0:
                     return False
       return True


def 	UpperCamelCase__(       UpperCamelCase__			:       int  )->list[int]:
       A__											=						str(UpperCamelCase__  )
       A__											=						[n]
       for i in range(1 ,     len(UpperCamelCase__  )  ):
              list_nums.append(int(str_num[i:]  )  )
              list_nums.append(int(str_num[:-i]  )  )
       return list_nums


def 	UpperCamelCase__(       UpperCamelCase__			:       int  )->bool:
       if len(str(UpperCamelCase__  )  ) > 3:
              if not is_prime(int(str(UpperCamelCase__  )[-3:]  )  ) or not is_prime(int(str(UpperCamelCase__  )[:3]  )  ):
                     return False
       return True


def 	UpperCamelCase__(       UpperCamelCase__			:       int = 11  )->list[int]:
       A__											=						[]
       A__											=						13
       while len(UpperCamelCase__  ) != count:
              if validate(UpperCamelCase__  ):
                     A__											=						list_truncated_nums(UpperCamelCase__  )
                     if all(is_prime(UpperCamelCase__  ) for i in list_nums  ):
                            list_truncated_primes.append(UpperCamelCase__  )
              num += 2
       return list_truncated_primes


def 	UpperCamelCase__(       )->int:
       return sum(compute_truncated_primes(11  )  )


if __name__ == "__main__":
       print(F"{sum(compute_truncated_primes(11)) = }")





