

'''simple docstring'''


import inspect
import tempfile
import unittest

from huggingface_hub import hf_hub_download

from transformers import is_torch_available
from transformers.testing_utils import is_flaky, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


lowerCAmelCase_   :   int       =					1E-4

if is_torch_available():
		import torch

		from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel
		from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder






@require_torch
class       UpperCamelCase__  :







	def __init__(				self			:   List[Any] , lowerCamelCase			:   Dict , lowerCamelCase			:   List[str]=1_6 , lowerCamelCase			:   Optional[Any]=1_3 , lowerCamelCase			:   Dict=7 , lowerCamelCase			:   int=1_4 , lowerCamelCase			:   Tuple=1_0 , lowerCamelCase			:   str=1_9 , lowerCamelCase			:   Dict=5 , lowerCamelCase			:   Union[str, Any]=4 , lowerCamelCase			:   Union[str, Any]=True , lowerCamelCase			:   List[str]=1_6 , lowerCamelCase			:   List[str]=2 , lowerCamelCase			:   Optional[int]=4 , lowerCamelCase			:   Dict=4 , lowerCamelCase			:   Any="gelu" , lowerCamelCase			:   int=0.1 , lowerCamelCase			:   Optional[int]=0.1 , lowerCamelCase			:   Any=[1, 2, 3, 4, 5] , lowerCamelCase			:   int=2_5 , lowerCamelCase			:   List[str]=5 , ):




				'''simple docstring'''
				a__     = d_model
				a__     = parent
				a__     = batch_size
				a__     = prediction_length
				a__     = context_length
				a__     = cardinality
				a__     = num_time_features
				a__     = lags_sequence
				a__     = embedding_dimension
				a__     = is_training
				a__     = hidden_size
				a__     = num_hidden_layers
				a__     = num_attention_heads
				a__     = intermediate_size
				a__     = hidden_act
				a__     = hidden_dropout_prob
				a__     = attention_probs_dropout_prob

				a__     = context_length
				a__     = prediction_length + label_length
				a__     = label_length

				a__     = moving_average
				a__     = autocorrelation_factor







	def 						__a   (				self			:   List[str]		):




				'''simple docstring'''
				return AutoformerConfig(
				    d_model=self.d_model , encoder_layers=self.num_hidden_layers , decoder_layers=self.num_hidden_layers , encoder_attention_heads=self.num_attention_heads , decoder_attention_heads=self.num_attention_heads , encoder_ffn_dim=self.intermediate_size , decoder_ffn_dim=self.intermediate_size , dropout=self.hidden_dropout_prob , attention_dropout=self.attention_probs_dropout_prob , prediction_length=self.prediction_length , context_length=self.context_length , label_length=self.label_length , lags_sequence=self.lags_sequence , num_time_features=self.num_time_features , num_static_categorical_features=1 , cardinality=[self.cardinality] , embedding_dimension=[self.embedding_dimension] , moving_average=self.moving_average , )







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




				'''simple docstring'''
				a__     = config.context_length + max(config.lags_sequence		)

				a__     = ids_tensor([self.batch_size, 1] , config.cardinality[0]		)
				a__     = floats_tensor([self.batch_size, _past_length, config.num_time_features]		)
				a__     = floats_tensor([self.batch_size, _past_length]		)
				a__     = floats_tensor([self.batch_size, _past_length]		) > 0.5

				# decoder inputs
				a__     = floats_tensor([self.batch_size, config.prediction_length, config.num_time_features]		)
				a__     = floats_tensor([self.batch_size, config.prediction_length]		)

				a__     = {
				    'past_values': past_values,
				    'static_categorical_features': static_categorical_features,
				    'past_time_features': past_time_features,
				    'past_observed_mask': past_observed_mask,
				    'future_time_features': future_time_features,
				    'future_values': future_values,
				}
				return inputs_dict







	def 						__a   (				self			:   str		):




				'''simple docstring'''
				a__     = self.get_config()
				a__     = self.prepare_autoformer_inputs_dict(_A		)
				return config, inputs_dict







	def 						__a   (				self			:   Optional[Any]		):




				'''simple docstring'''
				a__     = self.prepare_config_and_inputs()
				return config, inputs_dict







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




				'''simple docstring'''
				a__     = AutoformerModel(config=_A		).to(_A		).eval()
				a__     = model(**_A		)

				a__     = outputs.encoder_last_hidden_state
				a__     = outputs.last_hidden_state

				with tempfile.TemporaryDirectory() as tmpdirname:
							a__     = model.get_encoder()
							encoder.save_pretrained(_A		)
							a__     = AutoformerEncoder.from_pretrained(_A		).to(_A		)

				a__     = model.create_network_inputs(**_A		)
				a__     = model.decomposition_layer(transformer_inputs[:, : config.context_length, ...]		)

				a__     = torch.cat(
				    (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...]) , dim=-1 , )
				a__     = encoder(inputs_embeds=_A		)[0]
				self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3		)

				a__     = (
				    torch.mean(transformer_inputs[:, : config.context_length, ...] , dim=1		)
				    .unsqueeze(1		)
				    .repeat(1 , config.prediction_length , 1		)
				)
				a__     = torch.zeros(
				    [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]] , device=enc_input.device , )

				a__     = torch.cat(
				    (
				        torch.cat((seasonal_input[:, -config.label_length :, ...], zeros) , dim=1		),
				        feature[:, config.context_length - config.label_length :, ...],
				    ) , dim=-1 , )
				a__     = torch.cat(
				    (
				        torch.cat((trend_input[:, -config.label_length :, ...], mean) , dim=1		),
				        feature[:, config.context_length - config.label_length :, ...],
				    ) , dim=-1 , )

				with tempfile.TemporaryDirectory() as tmpdirname:
							a__     = model.get_decoder()
							decoder.save_pretrained(_A		)
							a__     = AutoformerDecoder.from_pretrained(_A		).to(_A		)

				a__     = decoder(
				    trend=_A , inputs_embeds=_A , encoder_hidden_states=_A , )[0]

				self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3		)






@require_torch
class       UpperCamelCase__  (		a__	,a__	,unittest.TestCase     ):
	lowerCAmelCase__					:			List[str]								=						(AutoformerModel, AutoformerForPrediction) if is_torch_available() else ()
	lowerCAmelCase__					:			Any								=						(AutoformerForPrediction,) if is_torch_available() else ()
	lowerCAmelCase__					:			List[str]								=						{"feature-extraction": AutoformerModel} if is_torch_available() else {}
	lowerCAmelCase__					:			str								=						False
	lowerCAmelCase__					:			List[str]								=						False
	lowerCAmelCase__					:			Union[str, Any]								=						False
	lowerCAmelCase__					:			List[str]								=						False
	lowerCAmelCase__					:			Any								=						False
	lowerCAmelCase__					:			str								=						False







	def 						__a   (				self			:   Any		):




				'''simple docstring'''
				a__     = AutoformerModelTester(self		)
				a__     = ConfigTester(self , config_class=_A , has_text_modality=_A		)







	def 						__a   (				self			:   str		):




				'''simple docstring'''
				self.config_tester.run_common_tests()







	def 						__a   (				self			:   Any		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				for model_class in self.all_model_classes:
							a__     = model_class(_A		)

							with tempfile.TemporaryDirectory() as tmpdirname:
										model.save_pretrained(_A		)
										a__     = model_class.from_pretrained(_A , output_loading_info=_A		)
							self.assertEqual(info["missing_keys"] , []		)







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




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs_for_common()
				self.model_tester.check_encoder_decoder_model_standalone(*_A		)







	@unittest.skip(reason="Model has no tokens embeddings"		)
	def 						__a   (				self			:   Optional[Any]		):




				'''simple docstring'''
				pass







	def 						__a   (				self			:   List[str]		):




				'''simple docstring'''
				a__     = inspect.signature(getattr(_A , "forward"		)		)
				# The main input is the name of the argument after `self`
				a__     = list(model_signature.parameters.keys()		)[1]
				self.assertEqual(AutoformerModel.main_input_name , _A		)







	def 						__a   (				self			:   Optional[int]		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							a__     = model_class(_A		)
							a__     = inspect.signature(model.forward		)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							a__     = [*signature.parameters.keys()]

							a__     = [
							    'past_values',
							    'past_time_features',
							    'past_observed_mask',
							    'static_categorical_features',
							    'static_real_features',
							    'future_values',
							    'future_time_features',
							]

							if model.__class__.__name__ in ["AutoformerForPrediction"]:
										expected_arg_names.append("future_observed_mask"		)

							expected_arg_names.extend(
							    [
							        "decoder_attention_mask",
							        "head_mask",
							        "decoder_head_mask",
							        "cross_attn_head_mask",
							        "encoder_outputs",
							        "past_key_values",
							        "output_hidden_states",
							        "output_attentions",
							        "use_cache",
							        "return_dict",
							    ]		)

							self.assertListEqual(arg_names[: len(_A		)] , _A		)







	def 						__a   (				self			:   List[str]		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs_for_common()
				a__     = True

				a__     = getattr(self.model_tester , "seq_length" , _A		)
				a__     = getattr(self.model_tester , "decoder_seq_length" , _A		)
				a__     = getattr(self.model_tester , "encoder_seq_length" , _A		)
				a__     = getattr(self.model_tester , "d_model" , _A		)
				a__     = getattr(self.model_tester , "num_attention_heads" , _A		)
				a__     = d_model // num_attention_heads

				for model_class in self.all_model_classes:
							a__     = True
							a__     = False
							a__     = True
							a__     = model_class(_A		)
							model.to(_A		)
							model.eval()
							with torch.no_grad():
										a__     = model(**self._prepare_for_class(_A , _A		)		)
							a__     = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
							self.assertEqual(len(_A		) , self.model_tester.num_hidden_layers		)

							# check that output_attentions also work using config
							del inputs_dict["output_attentions"]
							a__     = True
							a__     = model_class(_A		)
							model.to(_A		)
							model.eval()
							with torch.no_grad():
										a__     = model(**self._prepare_for_class(_A , _A		)		)
							a__     = outputs.encoder_attentions
							self.assertEqual(len(_A		) , self.model_tester.num_hidden_layers		)

							self.assertListEqual(
							    list(attentions[0].shape[-3:]		) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , )
							a__     = len(_A		)

							a__     = 7

							if "last_hidden_state" in outputs:
										correct_outlen += 1

							if "trend" in outputs:
										correct_outlen += 1

							if "past_key_values" in outputs:
										correct_outlen += 1  # past_key_values have been returned

							if "loss" in outputs:
										correct_outlen += 1

							if "params" in outputs:
										correct_outlen += 1

							self.assertEqual(_A , _A		)

							# decoder attentions
							a__     = outputs.decoder_attentions
							self.assertIsInstance(_A , (list, tuple)		)
							self.assertEqual(len(_A		) , self.model_tester.num_hidden_layers		)
							self.assertListEqual(
							    list(decoder_attentions[0].shape[-3:]		) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , )

							# cross attentions
							a__     = outputs.cross_attentions
							self.assertIsInstance(_A , (list, tuple)		)
							self.assertEqual(len(_A		) , self.model_tester.num_hidden_layers		)
							self.assertListEqual(
							    list(cross_attentions[0].shape[-3:]		) , [self.model_tester.num_attention_heads, decoder_seq_length, dim] , )

				# Check attention is always last and order is fine
				a__     = True
				a__     = True
				a__     = model_class(_A		)
				model.to(_A		)
				model.eval()
				with torch.no_grad():
							a__     = model(**self._prepare_for_class(_A , _A		)		)

				self.assertEqual(out_len + 2 , len(_A		)		)

				a__     = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

				self.assertEqual(len(_A		) , self.model_tester.num_hidden_layers		)
				self.assertListEqual(
				    list(self_attentions[0].shape[-3:]		) , [self.model_tester.num_attention_heads, encoder_seq_length, dim] , )







	@is_flaky()
	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				super().test_retain_grad_hidden_states_attentions()







def    _lowerCamelCase  (__lowerCamelCase  :     Any="train-batch.pt"     )   ->    int:
			a__     = hf_hub_download(repo_id="hf-internal-testing/tourism-monthly-batch"					,  filename=_lowerCamelCase					,  repo_type="dataset"     )
			a__     = torch.load(_lowerCamelCase					,  map_location=_lowerCamelCase     )
			return batch






@require_torch
@slow
class       UpperCamelCase__  (		unittest.TestCase     ):







	def 						__a   (				self			:   Tuple		):




				'''simple docstring'''
				a__     = AutoformerModel.from_pretrained("huggingface/autoformer-tourism-monthly"		).to(_A		)
				a__     = prepare_batch()

				with torch.no_grad():
							a__     = model(
							    past_values=batch["past_values"] , past_time_features=batch["past_time_features"] , past_observed_mask=batch["past_observed_mask"] , static_categorical_features=batch["static_categorical_features"] , future_values=batch["future_values"] , future_time_features=batch["future_time_features"] , )[0]

				a__     = torch.Size(
				    (6_4, model.config.prediction_length + model.config.label_length, model.config.feature_size)		)
				self.assertEqual(output.shape , _A		)

				a__     = torch.tensor(
				    [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]] , device=_A		)
				self.assertTrue(torch.allclose(output[0, :3, :3] , _A , atol=_A		)		)







	def 						__a   (				self			:   Optional[Any]		):




				'''simple docstring'''
				a__     = AutoformerForPrediction.from_pretrained("huggingface/autoformer-tourism-monthly"		).to(_A		)
				a__     = prepare_batch("val-batch.pt"		)
				with torch.no_grad():
							a__     = model(
							    past_values=batch["past_values"] , past_time_features=batch["past_time_features"] , past_observed_mask=batch["past_observed_mask"] , static_categorical_features=batch["static_categorical_features"] , ).encoder_last_hidden_state
				a__     = torch.Size((6_4, model.config.context_length, model.config.d_model)		)
				self.assertEqual(output.shape , _A		)

				a__     = torch.tensor(
				    [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]] , device=_A		)
				self.assertTrue(torch.allclose(output[0, :3, :3] , _A , atol=_A		)		)







	def 						__a   (				self			:   int		):




				'''simple docstring'''
				a__     = AutoformerForPrediction.from_pretrained("huggingface/autoformer-tourism-monthly"		).to(_A		)
				a__     = prepare_batch("val-batch.pt"		)
				with torch.no_grad():
							a__     = model.generate(
							    static_categorical_features=batch["static_categorical_features"] , past_time_features=batch["past_time_features"] , past_values=batch["past_values"] , future_time_features=batch["future_time_features"] , past_observed_mask=batch["past_observed_mask"] , )
				a__     = torch.Size((6_4, model.config.num_parallel_samples, model.config.prediction_length)		)
				self.assertEqual(outputs.sequences.shape , _A		)

				a__     = torch.tensor([3_1_3_0.6_7_6_3, 4_0_5_6.5_2_9_3, 7_0_5_3.0_7_8_6] , device=_A		)
				a__     = outputs.sequences.mean(dim=1		)
				self.assertTrue(torch.allclose(mean_prediction[0, -3:] , _A , rtol=1e-1		)		)










'''simple docstring'''


import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
		    MODEL_FOR_QUESTION_ANSWERING_MAPPING,
		    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
		    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
		    LayoutLMvaConfig,
		    LayoutLMvaForQuestionAnswering,
		    LayoutLMvaForSequenceClassification,
		    LayoutLMvaForTokenClassification,
		    LayoutLMvaModel,
		)
		from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
		from PIL import Image

		from transformers import LayoutLMvaImageProcessor






class       UpperCamelCase__  :







	def __init__(				self			:   List[Any] , lowerCamelCase			:   Optional[Any] , lowerCamelCase			:   Any=2 , lowerCamelCase			:   Optional[Any]=3 , lowerCamelCase			:   Optional[Any]=4 , lowerCamelCase			:   Optional[Any]=2 , lowerCamelCase			:   Tuple=7 , lowerCamelCase			:   Dict=True , lowerCamelCase			:   Optional[Any]=True , lowerCamelCase			:   int=True , lowerCamelCase			:   Any=True , lowerCamelCase			:   int=9_9 , lowerCamelCase			:   Tuple=3_6 , lowerCamelCase			:   List[Any]=3 , lowerCamelCase			:   Optional[Any]=4 , lowerCamelCase			:   Any=3_7 , lowerCamelCase			:   List[Any]="gelu" , lowerCamelCase			:   Optional[Any]=0.1 , lowerCamelCase			:   Optional[int]=0.1 , lowerCamelCase			:   Union[str, Any]=5_1_2 , lowerCamelCase			:   Dict=1_6 , lowerCamelCase			:   int=2 , lowerCamelCase			:   Union[str, Any]=0.02 , lowerCamelCase			:   Tuple=6 , lowerCamelCase			:   Tuple=6 , lowerCamelCase			:   int=3 , lowerCamelCase			:   str=4 , lowerCamelCase			:   int=None , lowerCamelCase			:   Optional[int]=1_0_0_0 , ):




				'''simple docstring'''
				a__     = parent
				a__     = batch_size
				a__     = num_channels
				a__     = image_size
				a__     = patch_size
				a__     = text_seq_length
				a__     = is_training
				a__     = use_input_mask
				a__     = use_token_type_ids
				a__     = use_labels
				a__     = vocab_size
				a__     = hidden_size
				a__     = num_hidden_layers
				a__     = num_attention_heads
				a__     = intermediate_size
				a__     = hidden_act
				a__     = hidden_dropout_prob
				a__     = attention_probs_dropout_prob
				a__     = max_position_embeddings
				a__     = type_vocab_size
				a__     = type_sequence_label_size
				a__     = initializer_range
				a__     = coordinate_size
				a__     = shape_size
				a__     = num_labels
				a__     = num_choices
				a__     = scope
				a__     = range_bbox

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







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




				'''simple docstring'''
				a__     = ids_tensor([self.batch_size, self.text_seq_length] , self.vocab_size		)

				a__     = ids_tensor([self.batch_size, self.text_seq_length, 4] , self.range_bbox		)
				# 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]:
													a__     = bbox[i, j, 3]
													a__     = bbox[i, j, 1]
													a__     = t
										if bbox[i, j, 2] < bbox[i, j, 0]:
													a__     = bbox[i, j, 2]
													a__     = bbox[i, j, 0]
													a__     = t

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

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

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

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

				a__     = LayoutLMvaConfig(
				    vocab_size=self.vocab_size , hidden_size=self.hidden_size , num_hidden_layers=self.num_hidden_layers , num_attention_heads=self.num_attention_heads , intermediate_size=self.intermediate_size , hidden_act=self.hidden_act , hidden_dropout_prob=self.hidden_dropout_prob , attention_probs_dropout_prob=self.attention_probs_dropout_prob , max_position_embeddings=self.max_position_embeddings , type_vocab_size=self.type_vocab_size , initializer_range=self.initializer_range , coordinate_size=self.coordinate_size , shape_size=self.shape_size , input_size=self.image_size , patch_size=self.patch_size , )

				return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels







	def 						__a   (				self			:   Tuple , lowerCamelCase			:   int , lowerCamelCase			:   Dict , lowerCamelCase			:   int , lowerCamelCase			:   str , lowerCamelCase			:   int , lowerCamelCase			:   Tuple , lowerCamelCase			:   Optional[int] , lowerCamelCase			:   Dict		):




				'''simple docstring'''
				a__     = LayoutLMvaModel(config=lowerCamelCase		)
				model.to(lowerCamelCase		)
				model.eval()

				# text + image
				a__     = model(lowerCamelCase , pixel_values=lowerCamelCase		)
				a__     = model(
				    lowerCamelCase , bbox=lowerCamelCase , pixel_values=lowerCamelCase , attention_mask=lowerCamelCase , token_type_ids=lowerCamelCase		)
				a__     = model(lowerCamelCase , bbox=lowerCamelCase , pixel_values=lowerCamelCase , token_type_ids=lowerCamelCase		)
				a__     = model(lowerCamelCase , bbox=lowerCamelCase , pixel_values=lowerCamelCase		)

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

				# text only
				a__     = model(lowerCamelCase		)
				self.parent.assertEqual(
				    result.last_hidden_state.shape , (self.batch_size, self.text_seq_length, self.hidden_size)		)

				# image only
				a__     = model(pixel_values=lowerCamelCase		)
				self.parent.assertEqual(
				    result.last_hidden_state.shape , (self.batch_size, self.image_seq_length, self.hidden_size)		)







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




				'''simple docstring'''
				a__     = self.num_labels
				a__     = LayoutLMvaForSequenceClassification(lowerCamelCase		)
				model.to(lowerCamelCase		)
				model.eval()
				a__     = model(
				    lowerCamelCase , bbox=lowerCamelCase , pixel_values=lowerCamelCase , attention_mask=lowerCamelCase , token_type_ids=lowerCamelCase , labels=lowerCamelCase , )
				self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)		)







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




				'''simple docstring'''
				a__     = self.num_labels
				a__     = LayoutLMvaForTokenClassification(config=lowerCamelCase		)
				model.to(lowerCamelCase		)
				model.eval()
				a__     = model(
				    lowerCamelCase , bbox=lowerCamelCase , pixel_values=lowerCamelCase , attention_mask=lowerCamelCase , token_type_ids=lowerCamelCase , labels=lowerCamelCase , )
				self.parent.assertEqual(result.logits.shape , (self.batch_size, self.text_seq_length, self.num_labels)		)







	def 						__a   (				self			:   Dict , lowerCamelCase			:   Tuple , lowerCamelCase			:   int , lowerCamelCase			:   Any , lowerCamelCase			:   Union[str, Any] , lowerCamelCase			:   List[str] , lowerCamelCase			:   Union[str, Any] , lowerCamelCase			:   Tuple , lowerCamelCase			:   Any		):




				'''simple docstring'''
				a__     = LayoutLMvaForQuestionAnswering(config=lowerCamelCase		)
				model.to(lowerCamelCase		)
				model.eval()
				a__     = model(
				    lowerCamelCase , bbox=lowerCamelCase , pixel_values=lowerCamelCase , attention_mask=lowerCamelCase , token_type_ids=lowerCamelCase , start_positions=lowerCamelCase , end_positions=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 						__a   (				self			:   Optional[int]		):




				'''simple docstring'''
				a__     = self.prepare_config_and_inputs()
				(
				    (
				    a__
				)   ,				(
				    a__
				)   ,				(
				    a__
				)   ,				(
				    a__
				)   ,				(
				    a__
				)   ,				(
				    a__
				)   ,				(
				    a__
				)   ,				(
				    a__
				)   ,				
				)     = config_and_inputs
				a__     = {
				    "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_torch
class       UpperCamelCase__  (		__lowerCAmelCase	,__lowerCAmelCase	,unittest.TestCase     ):
	lowerCAmelCase__					:			str								=						False
	lowerCAmelCase__					:			Optional[Any]								=						False
	lowerCAmelCase__					:			Any								=						False

	lowerCAmelCase__					:			List[str]								=						(
	    (
	        LayoutLMvaModel,
	        LayoutLMvaForSequenceClassification,
	        LayoutLMvaForTokenClassification,
	        LayoutLMvaForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	lowerCAmelCase__					:			List[Any]								=						(
	    {"document-question-answering": LayoutLMvaForQuestionAnswering, "feature-extraction": LayoutLMvaModel}
	    if is_torch_available()
	    else {}
	)







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




				'''simple docstring'''
				# `DocumentQuestionAnsweringPipeline` is expected to work with this model, but it combines the text and visual
				# embedding along the sequence dimension (dim 1), which causes an error during post-processing as `p_mask` has
				# the sequence dimension of the text embedding only.
				# (see the line `embedding_output = torch.cat([embedding_output, visual_embeddings], dim=1)`)
				return True







	def 						__a   (				self			:   int		):




				'''simple docstring'''
				a__     = LayoutLMvaModelTester(self		)
				a__     = ConfigTester(self , config_class=lowerCamelCase , hidden_size=3_7		)







	def 						__a   (				self			:   List[str] , lowerCamelCase			:   Optional[int] , lowerCamelCase			:   Any , lowerCamelCase			:   Tuple=False		):




				'''simple docstring'''
				a__     = copy.deepcopy(lowerCamelCase		)
				if model_class in get_values(lowerCamelCase		):
							a__     = {
							    k: v.unsqueeze(1		).expand(-1 , self.model_tester.num_choices , -1		).contiguous()
							    if isinstance(lowerCamelCase , torch.Tensor		) and v.ndim > 1
							    else v
							    for k, v in inputs_dict.items()
							}
				if return_labels:
							if model_class in get_values(lowerCamelCase		):
										a__     = torch.ones(self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase		)
							elif model_class in get_values(lowerCamelCase		):
										a__     = torch.zeros(
										    self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase		)
										a__     = torch.zeros(
										    self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase		)
							elif model_class in [
							    *get_values(lowerCamelCase		),
							]:
										a__     = torch.zeros(
										    self.model_tester.batch_size , dtype=torch.long , device=lowerCamelCase		)
							elif model_class in [
							    *get_values(lowerCamelCase		),
							]:
										a__     = torch.zeros(
										    (self.model_tester.batch_size, self.model_tester.text_seq_length) , dtype=torch.long , device=lowerCamelCase , )

				return inputs_dict







	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				self.config_tester.run_common_tests()







	def 						__a   (				self			:   Optional[Any]		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowerCamelCase		)







	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				for type in ["absolute", "relative_key", "relative_key_query"]:
							a__     = type
							self.model_tester.create_and_check_model(*lowerCamelCase		)







	def 						__a   (				self			:   Tuple		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase		)







	def 						__a   (				self			:   Optional[int]		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*lowerCamelCase		)







	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*lowerCamelCase		)







	@slow
	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							a__     = LayoutLMvaModel.from_pretrained(lowerCamelCase		)
							self.assertIsNotNone(lowerCamelCase		)







def    _lowerCamelCase  ()   ->    Dict:
			a__     = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     )
			return image






@require_torch
class       UpperCamelCase__  (		unittest.TestCase     ):







	@cached_property
	def 						__a   (				self			:   int		):




				'''simple docstring'''
				return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase		) if is_vision_available() else None







	@slow
	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				a__     = LayoutLMvaModel.from_pretrained("microsoft/layoutlmv3-base"		).to(lowerCamelCase		)

				a__     = self.default_image_processor
				a__     = prepare_img()
				a__     = image_processor(images=lowerCamelCase , return_tensors="pt"		).pixel_values.to(lowerCamelCase		)

				a__     = torch.tensor([[1, 2]]		)
				a__     = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]		).unsqueeze(0		)

				# forward pass
				a__     = model(
				    input_ids=input_ids.to(lowerCamelCase		) , bbox=bbox.to(lowerCamelCase		) , pixel_values=pixel_values.to(lowerCamelCase		) , )

				# verify the logits
				a__     = torch.Size((1, 1_9_9, 7_6_8)		)
				self.assertEqual(outputs.last_hidden_state.shape , lowerCamelCase		)

				a__     = torch.tensor(
				    [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]		).to(lowerCamelCase		)

				self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3] , lowerCamelCase , atol=1e-4		)		)



