






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






 '''simple docstring'''




 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.02				,	_A=6				,	_A=6				,	_A=3				,	_A=4				,	_A=None				,	_A=1000				,	)			->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase					:   Union[str, Any]      	=					parent
     _UpperCAmelCase					:   List[Any]      	=					batch_size
     _UpperCAmelCase					:   List[str]      	=					num_channels
     _UpperCAmelCase					:   Union[str, Any]      	=					image_size
     _UpperCAmelCase					:   Optional[Any]      	=					patch_size
     _UpperCAmelCase					:   Tuple      	=					text_seq_length
     _UpperCAmelCase					:   List[Any]      	=					is_training
     _UpperCAmelCase					:   Dict      	=					use_input_mask
     _UpperCAmelCase					:   Any      	=					use_token_type_ids
     _UpperCAmelCase					:   List[str]      	=					use_labels
     _UpperCAmelCase					:   Tuple      	=					vocab_size
     _UpperCAmelCase					:   List[str]      	=					hidden_size
     _UpperCAmelCase					:   str      	=					num_hidden_layers
     _UpperCAmelCase					:   Dict      	=					num_attention_heads
     _UpperCAmelCase					:   Optional[int]      	=					intermediate_size
     _UpperCAmelCase					:   str      	=					hidden_act
     _UpperCAmelCase					:   List[str]      	=					hidden_dropout_prob
     _UpperCAmelCase					:   Tuple      	=					attention_probs_dropout_prob
     _UpperCAmelCase					:   Any      	=					max_position_embeddings
     _UpperCAmelCase					:   Optional[int]      	=					type_vocab_size
     _UpperCAmelCase					:   str      	=					type_sequence_label_size
     _UpperCAmelCase					:   Any      	=					initializer_range
     _UpperCAmelCase					:   Optional[Any]      	=					coordinate_size
     _UpperCAmelCase					:   Optional[int]      	=					shape_size
     _UpperCAmelCase					:   str      	=					num_labels
     _UpperCAmelCase					:   List[Any]      	=					num_choices
     _UpperCAmelCase					:   Tuple      	=					scope
     _UpperCAmelCase					:   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)
     _UpperCAmelCase					:   List[str]      	=					text_seq_length
     _UpperCAmelCase					:   Any      	=					(image_size // patch_size) ** 2 + 1
     _UpperCAmelCase					:   Tuple      	=					self.text_seq_length + self.image_seq_length


 def 			snake_case__					(						self)			->		Any:

     """simple docstring"""



     _UpperCAmelCase					:   int      	=					ids_tensor([self.batch_size, self.text_seq_length]				,	self.vocab_size)

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

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

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

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

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

     _UpperCAmelCase					:   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 			snake_case__					(						self				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A)			->		Any:

     """simple docstring"""



     _UpperCAmelCase					:   int      	=					LayoutLMvaModel(config=snake_case__)
     model.to(snake_case__)
     model.eval()

     # text + image
     _UpperCAmelCase					:   Tuple      	=					model(snake_case__				,	pixel_values=snake_case__)
     _UpperCAmelCase					:   int      	=					model(
         snake_case__				,	bbox=snake_case__				,	pixel_values=snake_case__				,	attention_mask=snake_case__				,	token_type_ids=snake_case__)
     _UpperCAmelCase					:   Union[str, Any]      	=					model(snake_case__				,	bbox=snake_case__				,	pixel_values=snake_case__				,	token_type_ids=snake_case__)
     _UpperCAmelCase					:   int      	=					model(snake_case__				,	bbox=snake_case__				,	pixel_values=snake_case__)

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

     # text only
     _UpperCAmelCase					:   Optional[Any]      	=					model(snake_case__)
     self.parent.assertEqual(
         result.last_hidden_state.shape				,	(self.batch_size, self.text_seq_length, self.hidden_size))

     # image only
     _UpperCAmelCase					:   Optional[int]      	=					model(pixel_values=snake_case__)
     self.parent.assertEqual(
         result.last_hidden_state.shape				,	(self.batch_size, self.image_seq_length, self.hidden_size))


 def 			snake_case__					(						self				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A)			->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase					:   Any      	=					self.num_labels
     _UpperCAmelCase					:   List[Any]      	=					LayoutLMvaForSequenceClassification(snake_case__)
     model.to(snake_case__)
     model.eval()
     _UpperCAmelCase					:   Union[str, Any]      	=					model(
         snake_case__				,	bbox=snake_case__				,	pixel_values=snake_case__				,	attention_mask=snake_case__				,	token_type_ids=snake_case__				,	labels=snake_case__				,	)
     self.parent.assertEqual(result.logits.shape				,	(self.batch_size, self.num_labels))


 def 			snake_case__					(						self				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A)			->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase					:   str      	=					self.num_labels
     _UpperCAmelCase					:   Union[str, Any]      	=					LayoutLMvaForTokenClassification(config=snake_case__)
     model.to(snake_case__)
     model.eval()
     _UpperCAmelCase					:   Union[str, Any]      	=					model(
         snake_case__				,	bbox=snake_case__				,	pixel_values=snake_case__				,	attention_mask=snake_case__				,	token_type_ids=snake_case__				,	labels=snake_case__				,	)
     self.parent.assertEqual(result.logits.shape				,	(self.batch_size, self.text_seq_length, self.num_labels))


 def 			snake_case__					(						self				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A				,	_A)			->		Dict:

     """simple docstring"""



     _UpperCAmelCase					:   Dict      	=					LayoutLMvaForQuestionAnswering(config=snake_case__)
     model.to(snake_case__)
     model.eval()
     _UpperCAmelCase					:   Union[str, Any]      	=					model(
         snake_case__				,	bbox=snake_case__				,	pixel_values=snake_case__				,	attention_mask=snake_case__				,	token_type_ids=snake_case__				,	start_positions=snake_case__				,	end_positions=snake_case__				,	)
     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 			snake_case__					(						self)			->		List[str]:

     """simple docstring"""



     _UpperCAmelCase					:   Any      	=					self.prepare_config_and_inputs()
     (
         (
         _UpperCAmelCase							
     )	,			(
         _UpperCAmelCase							
     )	,			(
         _UpperCAmelCase							
     )	,			(
         _UpperCAmelCase							
     )	,			(
         _UpperCAmelCase							
     )	,			(
         _UpperCAmelCase							
     )	,			(
         _UpperCAmelCase							
     )	,			(
         _UpperCAmelCase							
     )	,										
     )					:   Optional[int]      	=					config_and_inputs
     _UpperCAmelCase					:   Tuple      	=					{
         '''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 				A_     (    SCREAMING_SNAKE_CASE_			, SCREAMING_SNAKE_CASE_			, unittest.TestCase      ):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE				:      Dict			=       False
 _SCREAMING_SNAKE_CASE				:      Optional[Any]			=       False
 _SCREAMING_SNAKE_CASE				:      Optional[Any]			=       False

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


 def 			snake_case__					(						self				,	_A				,	_A				,	_A				,	_A				,	_A)			->		str:

     """simple docstring"""



     return True


 def 			snake_case__					(						self)			->		Any:

     """simple docstring"""



     _UpperCAmelCase					:   Optional[int]      	=					LayoutLMvaModelTester(self)
     _UpperCAmelCase					:   Optional[int]      	=					ConfigTester(self				,	config_class=snake_case__				,	hidden_size=37)


 def 			snake_case__					(						self				,	_A				,	_A				,	_A=False)			->		str:

     """simple docstring"""



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

     return inputs_dict


 def 			snake_case__					(						self)			->		List[str]:

     """simple docstring"""



     self.config_tester.run_common_tests()


 def 			snake_case__					(						self)			->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase					:   List[str]      	=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*snake_case__)


 def 			snake_case__					(						self)			->		Any:

     """simple docstring"""



     _UpperCAmelCase					:   Optional[int]      	=					self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
         _UpperCAmelCase					:   Dict      	=					type
         self.model_tester.create_and_check_model(*snake_case__)


 def 			snake_case__					(						self)			->		Any:

     """simple docstring"""



     _UpperCAmelCase					:   Tuple      	=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*snake_case__)


 def 			snake_case__					(						self)			->		Any:

     """simple docstring"""



     _UpperCAmelCase					:   Union[str, Any]      	=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*snake_case__)


 def 			snake_case__					(						self)			->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase					:   str      	=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*snake_case__)


 @slow
 def 			snake_case__					(						self)			->		List[Any]:

     """simple docstring"""



     for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _UpperCAmelCase					:   Any      	=					LayoutLMvaModel.from_pretrained(snake_case__)
         self.assertIsNotNone(snake_case__)



def   _lowerCamelCase						(    )   ->       List[Any]:
    _UpperCAmelCase					:   Any      	=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
    return image


@require_torch
class 				A_     (    unittest.TestCase      ):






 '''simple docstring'''




 @cached_property
 def 			snake_case__					(						self)			->		Any:

     """simple docstring"""



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


 @slow
 def 			snake_case__					(						self)			->		Tuple:

     """simple docstring"""



     _UpperCAmelCase					:   Union[str, Any]      	=					LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base''').to(snake_case__)

     _UpperCAmelCase					:   Dict      	=					self.default_image_processor
     _UpperCAmelCase					:   Dict      	=					prepare_img()
     _UpperCAmelCase					:   Tuple      	=					image_processor(images=snake_case__				,	return_tensors='''pt''').pixel_values.to(snake_case__)

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

     # forward pass
     _UpperCAmelCase					:   int      	=					model(
         input_ids=input_ids.to(snake_case__)				,	bbox=bbox.to(snake_case__)				,	pixel_values=pixel_values.to(snake_case__)				,	)

     # verify the logits
     _UpperCAmelCase					:   Any      	=					torch.Size((1, 199, 768))
     self.assertEqual(outputs.last_hidden_state.shape				,	snake_case__)

     _UpperCAmelCase					:   int      	=					torch.tensor(
         [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]).to(snake_case__)

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





def 	lowerCamelCase					(	UpperCamelCase     :				int			,       UpperCamelCase     :				int							)   ->    str:
		if number < 0 or shift_amount < 0:
				raise ValueError('both inputs must be positive integers'							)

		_lowerCamelCase				   =  str(bin(UpperCamelCase							)							)
		binary_number += "0" * shift_amount
		return binary_number


def 	lowerCamelCase					(	UpperCamelCase     :				int			,       UpperCamelCase     :				int							)   ->    str:
		if number < 0 or shift_amount < 0:
				raise ValueError('both inputs must be positive integers'							)

		_lowerCamelCase				   =  str(bin(UpperCamelCase							)							)[2:]
		if shift_amount >= len(UpperCamelCase							):
				return "0b0"
		_lowerCamelCase				   =  binary_number[: len(UpperCamelCase							) - shift_amount]
		return "0b" + shifted_binary_number


def 	lowerCamelCase					(	UpperCamelCase     :				int			,       UpperCamelCase     :				int							)   ->    str:
		if number >= 0:  # Get binary representation of positive number
				_lowerCamelCase				   =  '0' + str(bin(UpperCamelCase							)							).strip('-'							)[2:]
		else:  # Get binary (2's complement) representation of negative number
				_lowerCamelCase				   =  len(bin(UpperCamelCase							)[3:]							)  # Find 2's complement of number
				_lowerCamelCase				   =  bin(abs(UpperCamelCase							) - (1 << binary_number_length)							)[3:]
				_lowerCamelCase				   =  (
				    '1' + '0' * (binary_number_length - len(UpperCamelCase							)) + binary_number
				)

		if shift_amount >= len(UpperCamelCase							):
				return "0b" + binary_number[0] * len(UpperCamelCase							)
		return (
		    "0b"
		    + binary_number[0] * shift_amount
		    + binary_number[: len(UpperCamelCase							) - shift_amount]
		)


if __name__ == "__main__":
				import doctest

				doctest.testmod()