



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






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







								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 SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					List[str]:
								'''simple docstring'''







								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 SCREAMING_SNAKE_CASE_			(						self      :     int				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     List[Any]				,		UpperCamelCase__      :     Tuple				,		UpperCamelCase__      :     int				,		UpperCamelCase__      :     List[str]				,		UpperCamelCase__      :     Union[str, Any]      )				->					Optional[int]:
								'''simple docstring'''







								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 SCREAMING_SNAKE_CASE_			(						self      :     List[str]				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     str				,		UpperCamelCase__      :     List[str]				,		UpperCamelCase__      :     Union[str, Any]				,		UpperCamelCase__      :     str				,		UpperCamelCase__      :     int				,		UpperCamelCase__      :     Union[str, Any]      )				->					Union[str, Any]:
								'''simple docstring'''







								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 SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]				,		UpperCamelCase__      :     int				,		UpperCamelCase__      :     int				,		UpperCamelCase__      :     Tuple				,		UpperCamelCase__      :     Union[str, Any]				,		UpperCamelCase__      :     Dict				,		UpperCamelCase__      :     List[str]				,		UpperCamelCase__      :     int      )				->					List[str]:
								'''simple docstring'''







								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 SCREAMING_SNAKE_CASE_			(						self      :     str				,		UpperCamelCase__      :     Any				,		UpperCamelCase__      :     Any				,		UpperCamelCase__      :     Union[str, Any]				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     List[str]				,		UpperCamelCase__      :     str				,		UpperCamelCase__      :     Any      )				->					Any:
								'''simple docstring'''







								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 SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Optional[Any]:
								'''simple docstring'''







								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  __magic_name__   (							A__,  A__,  unittest.TestCase						):
		lowercase      :       List[str]          =(
		    (
		        TFLayoutLMvaModel,
		        TFLayoutLMvaForQuestionAnswering,
		        TFLayoutLMvaForSequenceClassification,
		        TFLayoutLMvaForTokenClassification,
		    )
		    if is_tf_available()
		    else ()
		)
		lowercase      :       Union[str, Any]          =(
		    {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
		    if is_tf_available()
		    else {}
		)

		lowercase      :       str          =False
		lowercase      :       Optional[Any]          =False
		lowercase      :       Any          =False






		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]				,		UpperCamelCase__      :     Union[str, Any]				,		UpperCamelCase__      :     Union[str, Any]				,		UpperCamelCase__      :     Any				,		UpperCamelCase__      :     int				,		UpperCamelCase__      :     str      )				->					int:
								'''simple docstring'''







								return True






		def SCREAMING_SNAKE_CASE_			(						self      :     Any				,		UpperCamelCase__      :     List[str]				,		UpperCamelCase__      :     str				,		UpperCamelCase__      :     Dict=False      )				->					dict:
								'''simple docstring'''







								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 SCREAMING_SNAKE_CASE_			(						self      :     Any      )				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =     TFLayoutLMvaModelTester(self      )
								UpperCAmelCase         =     ConfigTester(self				,		config_class=UpperCamelCase__				,		hidden_size=37      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Any      )				->					List[Any]:
								'''simple docstring'''







								self.config_tester.run_common_tests()






		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					List[str]:
								'''simple docstring'''







								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_00
																																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 SCREAMING_SNAKE_CASE_			(						self      :     Any      )				->					Tuple:
								'''simple docstring'''







								(
								    (
								    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 SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					int:
								'''simple docstring'''







								(
								    (
								    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 SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					Union[str, Any]:
								'''simple docstring'''







								(
								    (
								    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 SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					List[str]:
								'''simple docstring'''







								(
								    (
								    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 SCREAMING_SNAKE_CASE_			(						self      :     List[Any]      )				->					Tuple:
								'''simple docstring'''







								(
								    (
								    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 SCREAMING_SNAKE_CASE_			(						self      :     Any      )				->					int:
								'''simple docstring'''







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

def    lowerCamelCase_() ->       List[Any]:
						UpperCAmelCase         =     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
						return image





@require_tf
class  __magic_name__   (							unittest.TestCase						):






		@cached_property
		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[int]      )				->					str:
								'''simple docstring'''







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







		@slow
		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]      )				->					Union[str, Any]:
								'''simple docstring'''







								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_99, 7_68)
								self.assertEqual(outputs.last_hidden_state.shape				,		UpperCamelCase__      )

								UpperCAmelCase         =     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]				,		UpperCamelCase__				,		atol=1e-4      )      )








import unittest

import torch
from torch import nn

from diffusers.models.activations import get_activation





class  __magic_name__   (							unittest.TestCase						):






		def SCREAMING_SNAKE_CASE_			(						self      :     int      )				->					str:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("swish"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.SiLU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					List[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("silu"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.SiLU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					int:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("mish"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.Mish      )

								self.assertEqual(act(torch.tensor(-2_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )







		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("gelu"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.GELU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )



