



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

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





	def  _lowercase(  self    )			->			Optional[int]:
								UpperCAmelCase     :		Tuple							     =		ids_tensor([self.batch_size, self.text_seq_length]		,		self.vocab_size    )

								UpperCAmelCase     :		str							     =		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]:
																													UpperCAmelCase     :		List[Any]							     =		bbox[i, j, 3]
																													UpperCAmelCase     :		List[Any]							     =		bbox[i, j, 1]
																													UpperCAmelCase     :		Optional[Any]							     =		t
																						if bbox[i, j, 2] < bbox[i, j, 0]:
																													UpperCAmelCase     :		Union[str, Any]							     =		bbox[i, j, 2]
																													UpperCAmelCase     :		int							     =		bbox[i, j, 0]
																													UpperCAmelCase     :		Optional[Any]							     =		t

								UpperCAmelCase     :		Union[str, Any]							     =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

								UpperCAmelCase     :		List[Any]							     =		None
								if self.use_input_mask:
															UpperCAmelCase     :		List[Any]							     =		random_attention_mask([self.batch_size, self.text_seq_length]    )

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

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

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

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





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		A		,		A		,		A		,		A    )			->			Optional[Any]:
								UpperCAmelCase     :		Optional[Any]							     =		LayoutLMvaModel(config=A    )
								model.to(A    )
								model.eval()

								# text + image
								UpperCAmelCase     :		Tuple							     =		model(A		,		pixel_values=A    )
								UpperCAmelCase     :		List[Any]							     =		model(
								    A		,		bbox=A		,		pixel_values=A		,		attention_mask=A		,		token_type_ids=A    )
								UpperCAmelCase     :		List[Any]							     =		model(A		,		bbox=A		,		pixel_values=A		,		token_type_ids=A    )
								UpperCAmelCase     :		Optional[Any]							     =		model(A		,		bbox=A		,		pixel_values=A    )

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

								# text only
								UpperCAmelCase     :		str							     =		model(A    )
								self.parent.assertEqual(
								    result.last_hidden_state.shape		,		(self.batch_size, self.text_seq_length, self.hidden_size)    )

								# image only
								UpperCAmelCase     :		str							     =		model(pixel_values=A    )
								self.parent.assertEqual(
								    result.last_hidden_state.shape		,		(self.batch_size, self.image_seq_length, self.hidden_size)    )





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		A		,		A		,		A		,		A    )			->			Optional[int]:
								UpperCAmelCase     :		Any							     =		self.num_labels
								UpperCAmelCase     :		int							     =		LayoutLMvaForSequenceClassification(A    )
								model.to(A    )
								model.eval()
								UpperCAmelCase     :		List[str]							     =		model(
								    A		,		bbox=A		,		pixel_values=A		,		attention_mask=A		,		token_type_ids=A		,		labels=A		,		)
								self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.num_labels)    )





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		A		,		A		,		A		,		A    )			->			Tuple:
								UpperCAmelCase     :		Union[str, Any]							     =		self.num_labels
								UpperCAmelCase     :		str							     =		LayoutLMvaForTokenClassification(config=A    )
								model.to(A    )
								model.eval()
								UpperCAmelCase     :		List[str]							     =		model(
								    A		,		bbox=A		,		pixel_values=A		,		attention_mask=A		,		token_type_ids=A		,		labels=A		,		)
								self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.text_seq_length, self.num_labels)    )





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		A		,		A		,		A		,		A    )			->			Optional[Any]:
								UpperCAmelCase     :		Union[str, Any]							     =		LayoutLMvaForQuestionAnswering(config=A    )
								model.to(A    )
								model.eval()
								UpperCAmelCase     :		int							     =		model(
								    A		,		bbox=A		,		pixel_values=A		,		attention_mask=A		,		token_type_ids=A		,		start_positions=A		,		end_positions=A		,		)
								self.parent.assertEqual(result.start_logits.shape		,		(self.batch_size, self.seq_length)    )
								self.parent.assertEqual(result.end_logits.shape		,		(self.batch_size, self.seq_length)    )






	def  _lowercase(  self    )			->			Optional[int]:
								UpperCAmelCase     :		Optional[Any]							     =		self.prepare_config_and_inputs()
								(
								    (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     (
								    UpperCAmelCase
								)     ,     
								)     :		Union[str, Any]							     =		config_and_inputs
								UpperCAmelCase     :		Union[str, Any]							     =		{
								    """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_ (							__magic_name__      ,     __magic_name__      ,     unittest.TestCase			):
	lowercase          = False
	lowercase          = False
	lowercase          = False

	lowercase          = (
	    (
	        LayoutLMvaModel,
	        LayoutLMvaForSequenceClassification,
	        LayoutLMvaForTokenClassification,
	        LayoutLMvaForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	lowercase          = (
	    {'document-question-answering': LayoutLMvaForQuestionAnswering, 'feature-extraction': LayoutLMvaModel}
	    if is_torch_available()
	    else {}
	)





	def  _lowercase(  self		,		A		,		A		,		A		,		A		,		A    )			->			Optional[Any]:
								# `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  _lowercase(  self    )			->			Dict:
								UpperCAmelCase     :		Optional[int]							     =		LayoutLMvaModelTester(self    )
								UpperCAmelCase     :		List[str]							     =		ConfigTester(self		,		config_class=A		,		hidden_size=37    )





	def  _lowercase(  self		,		A		,		A		,		A=False    )			->			Any:
								UpperCAmelCase     :		Tuple							     =		copy.deepcopy(A    )
								if model_class in get_values(A    ):
															UpperCAmelCase     :		Optional[int]							     =		{
															    k: v.unsqueeze(1    ).expand(-1		,		self.model_tester.num_choices		,		-1    ).contiguous()
															    if isinstance(A		,		torch.Tensor    ) and v.ndim > 1
															    else v
															    for k, v in inputs_dict.items()
															}
								if return_labels:
															if model_class in get_values(A    ):
																						UpperCAmelCase     :		Tuple							     =		torch.ones(self.model_tester.batch_size		,		dtype=torch.long		,		device=A    )
															elif model_class in get_values(A    ):
																						UpperCAmelCase     :		Union[str, Any]							     =		torch.zeros(
																						    self.model_tester.batch_size		,		dtype=torch.long		,		device=A    )
																						UpperCAmelCase     :		Union[str, Any]							     =		torch.zeros(
																						    self.model_tester.batch_size		,		dtype=torch.long		,		device=A    )
															elif model_class in [
															    *get_values(A    ),
															]:
																						UpperCAmelCase     :		Union[str, Any]							     =		torch.zeros(
																						    self.model_tester.batch_size		,		dtype=torch.long		,		device=A    )
															elif model_class in [
															    *get_values(A    ),
															]:
																						UpperCAmelCase     :		Optional[Any]							     =		torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.text_seq_length)		,		dtype=torch.long		,		device=A		,		)

								return inputs_dict





	def  _lowercase(  self    )			->			Union[str, Any]:
								self.config_tester.run_common_tests()





	def  _lowercase(  self    )			->			Union[str, Any]:
								UpperCAmelCase     :		List[str]							     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*A    )





	def  _lowercase(  self    )			->			Any:
								UpperCAmelCase     :		List[str]							     =		self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
															UpperCAmelCase     :		Any							     =		type
															self.model_tester.create_and_check_model(*A    )





	def  _lowercase(  self    )			->			List[Any]:
								UpperCAmelCase     :		List[str]							     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*A    )





	def  _lowercase(  self    )			->			int:
								UpperCAmelCase     :		int							     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*A    )





	def  _lowercase(  self    )			->			Tuple:
								UpperCAmelCase     :		Optional[Any]							     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*A    )






	@slow
	def  _lowercase(  self    )			->			Optional[Any]:
								for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															UpperCAmelCase     :		List[str]							     =		LayoutLMvaModel.from_pretrained(A    )
															self.assertIsNotNone(A    )




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


@require_torch
class 			UpperCamelCase_ (							unittest.TestCase			):





	@cached_property
	def  _lowercase(  self    )			->			int:
								return LayoutLMvaImageProcessor(apply_ocr=A    ) if is_vision_available() else None






	@slow
	def  _lowercase(  self    )			->			Any:
								UpperCAmelCase     :		Tuple							     =		LayoutLMvaModel.from_pretrained("""microsoft/layoutlmv3-base"""    ).to(A    )

								UpperCAmelCase     :		int							     =		self.default_image_processor
								UpperCAmelCase     :		Optional[Any]							     =		prepare_img()
								UpperCAmelCase     :		Optional[Any]							     =		image_processor(images=A		,		return_tensors="""pt"""    ).pixel_values.to(A    )

								UpperCAmelCase     :		str							     =		torch.tensor([[1, 2]]    )
								UpperCAmelCase     :		Union[str, Any]							     =		torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]    ).unsqueeze(0    )

								# forward pass
								UpperCAmelCase     :		Tuple							     =		model(
								    input_ids=input_ids.to(A    )		,		bbox=bbox.to(A    )		,		pixel_values=pixel_values.to(A    )		,		)

								# verify the logits
								UpperCAmelCase     :		Tuple							     =		torch.Size((1, 199, 768)    )
								self.assertEqual(outputs.last_hidden_state.shape		,		A    )

								UpperCAmelCase     :		Optional[Any]							     =		torch.tensor(
								    [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]    ).to(A    )

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






'''simple docstring'''




# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce



def       __lowerCamelCase  (					_lowercase		)      ->			Tuple:
							return (torch.arange(state.num_processes		) + 1.0 + (state.num_processes * state.process_index)).to(state.device		)



def       __lowerCamelCase  (					_lowercase		)      ->			List[Any]:
							UpperCAmelCase     :		Any							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		Union[str, Any]							     =		gather(_lowercase		)
							assert gathered_tensor.tolist() == list(range(1	,				state.num_processes**2 + 1		)		)



def       __lowerCamelCase  (					_lowercase		)      ->			Optional[int]:
							UpperCAmelCase     :		Any							     =		[state.process_index]
							UpperCAmelCase     :		Union[str, Any]							     =		gather_object(_lowercase		)
							assert len(_lowercase		) == state.num_processes, F'''{gathered_obj}, {len(_lowercase		)} != {state.num_processes}'''
							assert gathered_obj == list(range(state.num_processes		)		), F'''{gathered_obj} != {list(range(state.num_processes		)		)}'''



def       __lowerCamelCase  (					_lowercase		)      ->			List[Any]:
							UpperCAmelCase     :		Optional[int]							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		List[str]							     =		broadcast(_lowercase		)
							assert broadcasted_tensor.shape == torch.Size([state.num_processes]		)
							assert broadcasted_tensor.tolist() == list(range(1	,				state.num_processes + 1		)		)



def       __lowerCamelCase  (					_lowercase		)      ->			Tuple:
							# We need to pad the tensor with one more element if we are the main process
							# to ensure that we can pad
							if state.is_main_process:
														UpperCAmelCase     :		Optional[Any]							     =		torch.arange(state.num_processes + 1		).to(state.device		)
							else:
														UpperCAmelCase     :		Tuple							     =		torch.arange(state.num_processes		).to(state.device		)
							UpperCAmelCase     :		Optional[Any]							     =		pad_across_processes(_lowercase		)
							assert padded_tensor.shape == torch.Size([state.num_processes + 1]		)
							if not state.is_main_process:
														assert padded_tensor.tolist() == list(range(0	,				state.num_processes		)		) + [0]



def       __lowerCamelCase  (					_lowercase		)      ->			Dict:
							# For now runs on only two processes
							if state.num_processes != 2:
														return
							UpperCAmelCase     :		Optional[Any]							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		Optional[Any]							     =		reduce(_lowercase	,				"""sum"""		)
							UpperCAmelCase     :		Optional[Any]							     =		torch.tensor([4.0, 6]		).to(state.device		)
							assert torch.allclose(_lowercase	,				_lowercase		), F'''{reduced_tensor} != {truth_tensor}'''



def       __lowerCamelCase  (					_lowercase		)      ->			Optional[Any]:
							# For now runs on only two processes
							if state.num_processes != 2:
														return
							UpperCAmelCase     :		Tuple							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		Optional[int]							     =		reduce(_lowercase	,				"""mean"""		)
							UpperCAmelCase     :		str							     =		torch.tensor([2.0, 3]		).to(state.device		)
							assert torch.allclose(_lowercase	,				_lowercase		), F'''{reduced_tensor} != {truth_tensor}'''



def       __lowerCamelCase  (					_lowercase		)      ->			Optional[int]:
							# For xla_spawn (TPUs)
							main()



def       __lowerCamelCase  (					)      ->			int:
							UpperCAmelCase     :		List[Any]							     =		PartialState()
							state.print(F'''State: {state}'''		)
							state.print("""testing gather"""		)
							test_gather(_lowercase		)
							state.print("""testing gather_object"""		)
							test_gather_object(_lowercase		)
							state.print("""testing broadcast"""		)
							test_broadcast(_lowercase		)
							state.print("""testing pad_across_processes"""		)
							test_pad_across_processes(_lowercase		)
							state.print("""testing reduce_sum"""		)
							test_reduce_sum(_lowercase		)
							state.print("""testing reduce_mean"""		)
							test_reduce_mean(_lowercase		)


if __name__ == "__main__":
							main()

