

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




			"""simple docstring"""







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

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



			def        _SCREAMING_SNAKE_CASE			( self  :     str):
									SCREAMING_SNAKE_CASE_: List[Any]  		=       ids_tensor([self.batch_size, self.text_seq_length]   ,						self.vocab_size)

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

									SCREAMING_SNAKE_CASE_: str  		=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

									SCREAMING_SNAKE_CASE_: int  		=       None
									if self.use_input_mask:
															SCREAMING_SNAKE_CASE_: Tuple  		=       random_attention_mask([self.batch_size, self.text_seq_length])

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

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

									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       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   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     List[Any]):
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       TFLayoutLMvaModel(config=lowerCAmelCase__)

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

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

									# text only
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       model(lowerCAmelCase__   ,						training=lowerCAmelCase__)
									self.parent.assertEqual(
									    result.last_hidden_state.shape   ,						(self.batch_size, self.text_seq_length, self.hidden_size))

									# image only
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       model({"pixel_values": pixel_values}   ,						training=lowerCAmelCase__)
									self.parent.assertEqual(
									    result.last_hidden_state.shape   ,						(self.batch_size, self.image_seq_length, self.hidden_size))



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[int]   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Union[str, Any]   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     int):
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       self.num_labels
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       TFLayoutLMvaForSequenceClassification(config=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: Tuple  		=       model(
									    lowerCAmelCase__   ,						bbox=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						attention_mask=lowerCAmelCase__   ,						token_type_ids=lowerCAmelCase__   ,						labels=lowerCAmelCase__   ,						training=lowerCAmelCase__   ,						)
									self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.num_labels))



			def        _SCREAMING_SNAKE_CASE			( self  :     List[Any]   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Optional[Any]   ,						lowerCAmelCase__  :     Union[str, Any]   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     Any):
									SCREAMING_SNAKE_CASE_: str  		=       self.num_labels
									SCREAMING_SNAKE_CASE_: str  		=       TFLayoutLMvaForTokenClassification(config=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: int  		=       model(
									    lowerCAmelCase__   ,						bbox=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						attention_mask=lowerCAmelCase__   ,						token_type_ids=lowerCAmelCase__   ,						labels=lowerCAmelCase__   ,						training=lowerCAmelCase__   ,						)
									self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.text_seq_length, self.num_labels))



			def        _SCREAMING_SNAKE_CASE			( self  :     List[str]   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     Optional[Any]   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Optional[Any]):
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       2
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       TFLayoutLMvaForQuestionAnswering(config=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       model(
									    lowerCAmelCase__   ,						bbox=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						attention_mask=lowerCAmelCase__   ,						token_type_ids=lowerCAmelCase__   ,						start_positions=lowerCAmelCase__   ,						end_positions=lowerCAmelCase__   ,						training=lowerCAmelCase__   ,						)
									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):
									SCREAMING_SNAKE_CASE_: List[str]  		=       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_)): Union[str, Any]  		=       config_and_inputs
									SCREAMING_SNAKE_CASE_: int  		=       {
									    "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   __lowercase				( UpperCAmelCase_		,       UpperCAmelCase_		,       unittest.TestCase	):




			"""simple docstring"""







			_UpperCAmelCase   :							str										=     (
			    (
			        TFLayoutLMvaModel,
			        TFLayoutLMvaForQuestionAnswering,
			        TFLayoutLMvaForSequenceClassification,
			        TFLayoutLMvaForTokenClassification,
			    )
			    if is_tf_available()
			    else ()
			)
			_UpperCAmelCase   :							Dict										=     (
			    {'''document-question-answering''': TFLayoutLMvaForQuestionAnswering, '''feature-extraction''': TFLayoutLMvaModel}
			    if is_tf_available()
			    else {}
			)

			_UpperCAmelCase   :							List[str]										=     False
			_UpperCAmelCase   :							List[str]										=     False
			_UpperCAmelCase   :							Optional[Any]										=     False



			def        _SCREAMING_SNAKE_CASE			( self  :     int   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     Optional[Any]   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     Union[str, Any]   ,						lowerCAmelCase__  :     Tuple):
									return True



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Tuple=False):
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       copy.deepcopy(lowerCAmelCase__)

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

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

									return inputs_dict



			def        _SCREAMING_SNAKE_CASE			( self  :     List[str]):
									SCREAMING_SNAKE_CASE_: Tuple  		=       TFLayoutLMvaModelTester(self)
									SCREAMING_SNAKE_CASE_: int  		=       ConfigTester(self   ,						config_class=lowerCAmelCase__   ,						hidden_size=37)



			def        _SCREAMING_SNAKE_CASE			( self  :     Union[str, Any]):
									self.config_tester.run_common_tests()



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Tuple  		=       self.model_tester.prepare_config_and_inputs_for_common()
									for model_class in self.all_model_classes:
															SCREAMING_SNAKE_CASE_: Tuple  		=       model_class(lowerCAmelCase__)
															if getattr(lowerCAmelCase__   ,						"hf_compute_loss"   ,						lowerCAmelCase__):
																					# The number of elements in the loss should be the same as the number of elements in the label
																					SCREAMING_SNAKE_CASE_: Optional[int]  		=       self._prepare_for_class(inputs_dict.copy()   ,						lowerCAmelCase__   ,						return_labels=lowerCAmelCase__)
																					SCREAMING_SNAKE_CASE_: Any  		=       prepared_for_class[
																					    sorted(prepared_for_class.keys() - inputs_dict.keys()   ,						reverse=lowerCAmelCase__)[0]
																					]
																					SCREAMING_SNAKE_CASE_: List[Any]  		=       added_label.shape.as_list()[:1]

																					# Test that model correctly compute the loss with kwargs
																					SCREAMING_SNAKE_CASE_: List[Any]  		=       self._prepare_for_class(inputs_dict.copy()   ,						lowerCAmelCase__   ,						return_labels=lowerCAmelCase__)
																					SCREAMING_SNAKE_CASE_: Optional[Any]  		=       prepared_for_class.pop("input_ids")

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

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

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

																					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_: Tuple  		=       prepared_for_class[value]

																					SCREAMING_SNAKE_CASE_: int  		=       tuple(lowerCAmelCase__)

																					# Send to model
																					SCREAMING_SNAKE_CASE_: List[str]  		=       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  :     Tuple):
									(
									    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    
									): Optional[Any]  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]):
									(
									    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    
									): int  		=       self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
															SCREAMING_SNAKE_CASE_: Tuple  		=       type
															self.model_tester.create_and_check_model(lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple):
									(
									    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    
									): Tuple  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(
									    lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]):
									(
									    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    
									): Dict  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(
									    lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     List[str]):
									(
									    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    
									): Optional[int]  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(
									    lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__   ,						lowerCAmelCase__)




			@slow
			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]):
									for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															SCREAMING_SNAKE_CASE_: List[str]  		=       TFLayoutLMvaModel.from_pretrained(lowerCAmelCase__)
															self.assertIsNotNone(lowerCAmelCase__)







def 			A_      (    ):
						SCREAMING_SNAKE_CASE_: Optional[int]  		=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
						return image



@require_tf
class   __lowercase				( unittest.TestCase	):




			"""simple docstring"""







			@cached_property
			def        _SCREAMING_SNAKE_CASE			( self  :     List[str]):
									return LayoutLMvaImageProcessor(apply_ocr=lowerCAmelCase__) if is_vision_available() else None




			@slow
			def        _SCREAMING_SNAKE_CASE			( self  :     List[Any]):
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       TFLayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base")

									SCREAMING_SNAKE_CASE_: List[str]  		=       self.default_image_processor
									SCREAMING_SNAKE_CASE_: Dict  		=       prepare_img()
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       image_processor(images=lowerCAmelCase__   ,						return_tensors="tf").pixel_values

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

									# forward pass
									SCREAMING_SNAKE_CASE_: Any  		=       model(input_ids=lowerCAmelCase__   ,						bbox=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						training=lowerCAmelCase__)

									# verify the logits
									SCREAMING_SNAKE_CASE_: Dict  		=       (1, 199, 768)
									self.assertEqual(outputs.last_hidden_state.shape   ,						lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: Dict  		=       tf.constant(
									    [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]])

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









import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
						import torch



@is_pipeline_test
class   __lowercase				( unittest.TestCase	):




			"""simple docstring"""







			_UpperCAmelCase   :							List[Any]										=     MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
			_UpperCAmelCase   :							str										=     TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING



			def        _SCREAMING_SNAKE_CASE			( self  :     Any   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Dict):
									SCREAMING_SNAKE_CASE_: Any  		=       TextaTextGenerationPipeline(model=lowerCAmelCase__   ,						tokenizer=lowerCAmelCase__)
									return generator, ["Something to write", "Something else"]



			def        _SCREAMING_SNAKE_CASE			( self  :     str   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Any):
									SCREAMING_SNAKE_CASE_: List[Any]  		=       generator("Something there")
									self.assertEqual(lowerCAmelCase__   ,						[{"generated_text": ANY(lowerCAmelCase__)}])
									# These are encoder decoder, they don't just append to incoming string
									self.assertFalse(outputs[0]["generated_text"].startswith("Something there"))

									SCREAMING_SNAKE_CASE_: List[Any]  		=       generator(["This is great !", "Something else"]   ,						num_return_sequences=2   ,						do_sample=lowerCAmelCase__)
									self.assertEqual(
									    lowerCAmelCase__   ,						[
									        [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}],
									        [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}],
									    ]   ,						)

									SCREAMING_SNAKE_CASE_: Dict  		=       generator(
									    ["This is great !", "Something else"]   ,						num_return_sequences=2   ,						batch_size=2   ,						do_sample=lowerCAmelCase__)
									self.assertEqual(
									    lowerCAmelCase__   ,						[
									        [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}],
									        [{"generated_text": ANY(lowerCAmelCase__)}, {"generated_text": ANY(lowerCAmelCase__)}],
									    ]   ,						)

									with self.assertRaises(lowerCAmelCase__):
															generator(4)



			@require_torch
			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple):
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       pipeline("text2text-generation"   ,						model="patrickvonplaten/t5-tiny-random"   ,						framework="pt")
									# do_sample=False necessary for reproducibility
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       generator("Something there"   ,						do_sample=lowerCAmelCase__)
									self.assertEqual(lowerCAmelCase__   ,						[{"generated_text": ""}])

									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       3
									SCREAMING_SNAKE_CASE_: Any  		=       generator(
									    "Something there"   ,						num_return_sequences=lowerCAmelCase__   ,						num_beams=lowerCAmelCase__   ,						)
									SCREAMING_SNAKE_CASE_: Any  		=       [
									    {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide Beide"},
									    {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide"},
									    {"generated_text": ""},
									]
									self.assertEqual(lowerCAmelCase__   ,						lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: int  		=       generator("This is a test"   ,						do_sample=lowerCAmelCase__   ,						num_return_sequences=2   ,						return_tensors=lowerCAmelCase__)
									self.assertEqual(
									    lowerCAmelCase__   ,						[
									        {"generated_token_ids": ANY(torch.Tensor)},
									        {"generated_token_ids": ANY(torch.Tensor)},
									    ]   ,						)
									SCREAMING_SNAKE_CASE_: str  		=       generator.model.config.eos_token_id
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       "<pad>"
									SCREAMING_SNAKE_CASE_: Tuple  		=       generator(
									    ["This is a test", "This is a second test"]   ,						do_sample=lowerCAmelCase__   ,						num_return_sequences=2   ,						batch_size=2   ,						return_tensors=lowerCAmelCase__   ,						)
									self.assertEqual(
									    lowerCAmelCase__   ,						[
									        [
									            {"generated_token_ids": ANY(torch.Tensor)},
									            {"generated_token_ids": ANY(torch.Tensor)},
									        ],
									        [
									            {"generated_token_ids": ANY(torch.Tensor)},
									            {"generated_token_ids": ANY(torch.Tensor)},
									        ],
									    ]   ,						)




			@require_tf
			def        _SCREAMING_SNAKE_CASE			( self  :     Union[str, Any]):
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       pipeline("text2text-generation"   ,						model="patrickvonplaten/t5-tiny-random"   ,						framework="tf")
									# do_sample=False necessary for reproducibility
									SCREAMING_SNAKE_CASE_: List[Any]  		=       generator("Something there"   ,						do_sample=lowerCAmelCase__)
									self.assertEqual(lowerCAmelCase__   ,						[{"generated_text": ""}])






