



"""simple docstring"""


from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

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

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


if is_tf_available():
					import tensorflow as tf

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

if is_vision_available():
					from PIL import Image

					from transformers import LayoutLMvaImageProcessor
class 			_a :
			"""simple docstring"""



			def __init__(		self     :    Union[str, Any]				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    int=2				,   __UpperCamelCase     :    int=3				,   __UpperCamelCase     :    str=4				,   __UpperCamelCase     :    Union[str, Any]=2				,   __UpperCamelCase     :    Dict=7				,   __UpperCamelCase     :    int=True				,   __UpperCamelCase     :    List[str]=True				,   __UpperCamelCase     :    Any=True				,   __UpperCamelCase     :    Any=True				,   __UpperCamelCase     :    str=9_9				,   __UpperCamelCase     :    str=3_6				,   __UpperCamelCase     :    Any=2				,   __UpperCamelCase     :    Optional[Any]=4				,   __UpperCamelCase     :    str=3_7				,   __UpperCamelCase     :    Optional[int]="gelu"				,   __UpperCamelCase     :    List[str]=0.1				,   __UpperCamelCase     :    Any=0.1				,   __UpperCamelCase     :    Union[str, Any]=5_1_2				,   __UpperCamelCase     :    str=1_6				,   __UpperCamelCase     :    str=2				,   __UpperCamelCase     :    List[Any]=0.0_2				,   __UpperCamelCase     :    Dict=6				,   __UpperCamelCase     :    List[str]=6				,   __UpperCamelCase     :    Optional[Any]=3				,   __UpperCamelCase     :    List[str]=4				,   __UpperCamelCase     :    Optional[int]=None				,   __UpperCamelCase     :    Tuple=1_0_0_0				,   )->Tuple:
							_UpperCAmelCase					=    parent
							_UpperCAmelCase					=    batch_size
							_UpperCAmelCase					=    num_channels
							_UpperCAmelCase					=    image_size
							_UpperCAmelCase					=    patch_size
							_UpperCAmelCase					=    is_training
							_UpperCAmelCase					=    use_input_mask
							_UpperCAmelCase					=    use_token_type_ids
							_UpperCAmelCase					=    use_labels
							_UpperCAmelCase					=    vocab_size
							_UpperCAmelCase					=    hidden_size
							_UpperCAmelCase					=    num_hidden_layers
							_UpperCAmelCase					=    num_attention_heads
							_UpperCAmelCase					=    intermediate_size
							_UpperCAmelCase					=    hidden_act
							_UpperCAmelCase					=    hidden_dropout_prob
							_UpperCAmelCase					=    attention_probs_dropout_prob
							_UpperCAmelCase					=    max_position_embeddings
							_UpperCAmelCase					=    type_vocab_size
							_UpperCAmelCase					=    type_sequence_label_size
							_UpperCAmelCase					=    initializer_range
							_UpperCAmelCase					=    coordinate_size
							_UpperCAmelCase					=    shape_size
							_UpperCAmelCase					=    num_labels
							_UpperCAmelCase					=    num_choices
							_UpperCAmelCase					=    scope
							_UpperCAmelCase					=    range_bbox

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



			def  lowercase__						(		self     :    str   )->Tuple:
							_UpperCAmelCase					=    ids_tensor([self.batch_size, self.text_seq_length]				,   self.vocab_size   )

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

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

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

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

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

							_UpperCAmelCase					=    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  lowercase__						(		self     :    Union[str, Any]				,   __UpperCamelCase     :    Union[str, Any]				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    int   )->Optional[int]:
							_UpperCAmelCase					=    TFLayoutLMvaModel(config=__UpperCamelCase   )

							# text + image
							_UpperCAmelCase					=    model(__UpperCamelCase				,   pixel_values=__UpperCamelCase				,   training=__UpperCamelCase   )
							_UpperCAmelCase					=    model(
							    __UpperCamelCase				,   bbox=__UpperCamelCase				,   pixel_values=__UpperCamelCase				,   attention_mask=__UpperCamelCase				,   token_type_ids=__UpperCamelCase				,   training=__UpperCamelCase				,   )
							_UpperCAmelCase					=    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
							_UpperCAmelCase					=    model(__UpperCamelCase				,   training=__UpperCamelCase   )
							self.parent.assertEqual(
							    result.last_hidden_state.shape				,   (self.batch_size, self.text_seq_length, self.hidden_size)   )

							# image only
							_UpperCAmelCase					=    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  lowercase__						(		self     :    Union[str, Any]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    Optional[Any]				,   __UpperCamelCase     :    Union[str, Any]				,   __UpperCamelCase     :    Any   )->Optional[int]:
							_UpperCAmelCase					=    self.num_labels
							_UpperCAmelCase					=    TFLayoutLMvaForSequenceClassification(config=__UpperCamelCase   )
							_UpperCAmelCase					=    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  lowercase__						(		self     :    int				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    Optional[int]   )->str:
							_UpperCAmelCase					=    self.num_labels
							_UpperCAmelCase					=    TFLayoutLMvaForTokenClassification(config=__UpperCamelCase   )
							_UpperCAmelCase					=    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  lowercase__						(		self     :    Tuple				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Union[str, Any]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    Tuple   )->str:
							_UpperCAmelCase					=    2
							_UpperCAmelCase					=    TFLayoutLMvaForQuestionAnswering(config=__UpperCamelCase   )
							_UpperCAmelCase					=    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  lowercase__						(		self     :    Any   )->Union[str, Any]:
							_UpperCAmelCase					=    self.prepare_config_and_inputs()
							((_UpperCAmelCase)		,						(_UpperCAmelCase)		,						(_UpperCAmelCase)		,						(_UpperCAmelCase)		,						(_UpperCAmelCase)		,						(_UpperCAmelCase)		,						(_UpperCAmelCase)		,						(_UpperCAmelCase))					=    config_and_inputs
							_UpperCAmelCase					=    {
							    '''input_ids''': input_ids,
							    '''bbox''': bbox,
							    '''pixel_values''': pixel_values,
							    '''token_type_ids''': token_type_ids,
							    '''attention_mask''': input_mask,
							}
							return config, inputs_dict



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

			UpperCamelCase__			      =	False
			UpperCamelCase__			      =	False
			UpperCamelCase__			      =	False



			def  lowercase__						(		self     :    Any				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    Union[str, Any]				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    List[str]   )->List[Any]:
							return True



			def  lowercase__						(		self     :    Dict				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    List[Any]=False   )->dict:
							_UpperCAmelCase					=    copy.deepcopy(__UpperCamelCase   )

							if model_class in get_values(__UpperCamelCase   ):
											_UpperCAmelCase					=    {
											    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   ):
															_UpperCAmelCase					=    tf.ones(self.model_tester.batch_size				,   dtype=tf.intaa   )
											elif model_class in get_values(__UpperCamelCase   ):
															_UpperCAmelCase					=    tf.zeros(self.model_tester.batch_size				,   dtype=tf.intaa   )
															_UpperCAmelCase					=    tf.zeros(self.model_tester.batch_size				,   dtype=tf.intaa   )
											elif model_class in get_values(__UpperCamelCase   ):
															_UpperCAmelCase					=    tf.zeros(self.model_tester.batch_size				,   dtype=tf.intaa   )
											elif model_class in get_values(__UpperCamelCase   ):
															_UpperCAmelCase					=    tf.zeros(
															    (self.model_tester.batch_size, self.model_tester.text_seq_length)				,   dtype=tf.intaa   )

							return inputs_dict



			def  lowercase__						(		self     :    str   )->List[str]:
							_UpperCAmelCase					=    TFLayoutLMvaModelTester(self   )
							_UpperCAmelCase					=    ConfigTester(self				,   config_class=__UpperCamelCase				,   hidden_size=3_7   )



			def  lowercase__						(		self     :    List[Any]   )->Optional[int]:
							self.config_tester.run_common_tests()



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

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

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

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

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

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

															for index, value in sorted_tuple_index_mapping:
																			_UpperCAmelCase					=    prepared_for_class[value]

															_UpperCAmelCase					=    tuple(__UpperCamelCase   )

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

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



			def  lowercase__						(		self     :    int   )->Optional[int]:
							(
							    (
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						
							)					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(__UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase   )



			def  lowercase__						(		self     :    Any   )->Union[str, Any]:
							(
							    (
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						
							)					=    self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
											_UpperCAmelCase					=    type
											self.model_tester.create_and_check_model(__UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase   )



			def  lowercase__						(		self     :    Union[str, Any]   )->int:
							(
							    (
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						
							)					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(
							    __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase   )



			def  lowercase__						(		self     :    Union[str, Any]   )->Union[str, Any]:
							(
							    (
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						
							)					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(
							    __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase				,   __UpperCamelCase   )



			def  lowercase__						(		self     :    int   )->List[Any]:
							(
							    (
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						
							)					=    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  lowercase__						(		self     :    List[Any]   )->Union[str, Any]:
							for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											_UpperCAmelCase					=    TFLayoutLMvaModel.from_pretrained(__UpperCamelCase   )
											self.assertIsNotNone(__UpperCamelCase   )







def  lowercase	(      ):






				'''simple docstring'''



				_UpperCAmelCase					=    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
				return image
@require_tf
class 			_a (					unittest.TestCase):
			"""simple docstring"""



			@cached_property
			def  lowercase__						(		self     :    Union[str, Any]   )->List[Any]:
							return LayoutLMvaImageProcessor(apply_ocr=__UpperCamelCase   ) if is_vision_available() else None



			@slow
			def  lowercase__						(		self     :    Optional[Any]   )->str:
							_UpperCAmelCase					=    TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''   )

							_UpperCAmelCase					=    self.default_image_processor
							_UpperCAmelCase					=    prepare_img()
							_UpperCAmelCase					=    image_processor(images=__UpperCamelCase				,   return_tensors='''tf'''   ).pixel_values

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

							# forward pass
							_UpperCAmelCase					=    model(input_ids=__UpperCamelCase				,   bbox=__UpperCamelCase				,   pixel_values=__UpperCamelCase				,   training=__UpperCamelCase   )

							# verify the logits
							_UpperCAmelCase					=    (1, 1_9_9, 7_6_8)
							self.assertEqual(outputs.last_hidden_state.shape				,   __UpperCamelCase   )

							_UpperCAmelCase					=    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   )   )








"""simple docstring"""


from typing import List, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A   :							Any										=   logging.get_logger(__name__)

__A   :							Dict										=   {
    "huggingface/informer-tourism-monthly": (
        "https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json"
    ),
    # See all Informer models at https://huggingface.co/models?filter=informer
}
class 			_a (					lowerCAmelCase):
			"""simple docstring"""
			UpperCamelCase__			      =	"""informer"""
			UpperCamelCase__			      =	{
			    """hidden_size""": """d_model""",
			    """num_attention_heads""": """encoder_attention_heads""",
			    """num_hidden_layers""": """encoder_layers""",
			}



			def __init__(		self     :    List[str]				,   __UpperCamelCase     :    Optional[int] = None				,   __UpperCamelCase     :    Optional[int] = None				,   __UpperCamelCase     :    str = "student_t"				,   __UpperCamelCase     :    str = "nll"				,   __UpperCamelCase     :    int = 1				,   __UpperCamelCase     :    List[int] = None				,   __UpperCamelCase     :    Optional[Union[str, bool]] = "mean"				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    Optional[List[int]] = None				,   __UpperCamelCase     :    Optional[List[int]] = None				,   __UpperCamelCase     :    int = 6_4				,   __UpperCamelCase     :    int = 3_2				,   __UpperCamelCase     :    int = 3_2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    bool = True				,   __UpperCamelCase     :    str = "gelu"				,   __UpperCamelCase     :    float = 0.0_5				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    int = 1_0_0				,   __UpperCamelCase     :    float = 0.0_2				,   __UpperCamelCase     :    Optional[Any]=True				,   __UpperCamelCase     :    str = "prob"				,   __UpperCamelCase     :    int = 5				,   __UpperCamelCase     :    bool = True				,   **__UpperCamelCase     :    Union[str, Any]				,   )->Dict:
							# time series specific configuration
							_UpperCAmelCase					=    prediction_length
							_UpperCAmelCase					=    context_length or prediction_length
							_UpperCAmelCase					=    distribution_output
							_UpperCAmelCase					=    loss
							_UpperCAmelCase					=    input_size
							_UpperCAmelCase					=    num_time_features
							_UpperCAmelCase					=    lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
							_UpperCAmelCase					=    scaling
							_UpperCAmelCase					=    num_dynamic_real_features
							_UpperCAmelCase					=    num_static_real_features
							_UpperCAmelCase					=    num_static_categorical_features

							# set cardinality
							if cardinality and num_static_categorical_features > 0:
											if len(__UpperCamelCase   ) != num_static_categorical_features:
															raise ValueError(
															    '''The cardinality should be a list of the same length as `num_static_categorical_features`'''   )
											_UpperCAmelCase					=    cardinality
							else:
											_UpperCAmelCase					=    [0]

							# set embedding_dimension
							if embedding_dimension and num_static_categorical_features > 0:
											if len(__UpperCamelCase   ) != num_static_categorical_features:
															raise ValueError(
															    '''The embedding dimension should be a list of the same length as `num_static_categorical_features`'''   )
											_UpperCAmelCase					=    embedding_dimension
							else:
											_UpperCAmelCase					=    [min(5_0				,   (cat + 1) // 2   ) for cat in self.cardinality]

							_UpperCAmelCase					=    num_parallel_samples

							# Transformer architecture configuration
							_UpperCAmelCase					=    input_size * len(self.lags_sequence   ) + self._number_of_features
							_UpperCAmelCase					=    d_model
							_UpperCAmelCase					=    encoder_attention_heads
							_UpperCAmelCase					=    decoder_attention_heads
							_UpperCAmelCase					=    encoder_ffn_dim
							_UpperCAmelCase					=    decoder_ffn_dim
							_UpperCAmelCase					=    encoder_layers
							_UpperCAmelCase					=    decoder_layers

							_UpperCAmelCase					=    dropout
							_UpperCAmelCase					=    attention_dropout
							_UpperCAmelCase					=    activation_dropout
							_UpperCAmelCase					=    encoder_layerdrop
							_UpperCAmelCase					=    decoder_layerdrop

							_UpperCAmelCase					=    activation_function
							_UpperCAmelCase					=    init_std

							_UpperCAmelCase					=    use_cache

							# Informer
							_UpperCAmelCase					=    attention_type
							_UpperCAmelCase					=    sampling_factor
							_UpperCAmelCase					=    distil

							super().__init__(is_encoder_decoder=__UpperCamelCase				,   **__UpperCamelCase   )



			@property
			def  lowercase__						(		self     :    Optional[int]   )->int:
							return (
							    sum(self.embedding_dimension   )
							    + self.num_dynamic_real_features
							    + self.num_time_features
							    + self.num_static_real_features
							    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
							)



