

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




  def __init__(				self							,   lowerCAmelCase							,   lowerCAmelCase=2							,   lowerCAmelCase=3							,   lowerCAmelCase=4							,   lowerCAmelCase=2							,   lowerCAmelCase=7							,   lowerCAmelCase=True							,   lowerCAmelCase=True							,   lowerCAmelCase=True							,   lowerCAmelCase=True							,   lowerCAmelCase=99							,   lowerCAmelCase=36							,   lowerCAmelCase=3							,   lowerCAmelCase=4							,   lowerCAmelCase=37							,   lowerCAmelCase="gelu"							,   lowerCAmelCase=0.1							,   lowerCAmelCase=0.1							,   lowerCAmelCase=512							,   lowerCAmelCase=16							,   lowerCAmelCase=2							,   lowerCAmelCase=0.02							,   lowerCAmelCase=6							,   lowerCAmelCase=6							,   lowerCAmelCase=3							,   lowerCAmelCase=4							,   lowerCAmelCase=None							,   lowerCAmelCase=1_000							,   )  -> Tuple:
        '''simple docstring'''






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

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




  def   A__     (				self					)  -> int:
        '''simple docstring'''






        _lowercase	       =ids_tensor([self.batch_size, self.text_seq_length]							,   self.vocab_size					)

        _lowercase	       =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]:
                          _lowercase	       =bbox[i, j, 3]
                          _lowercase	       =bbox[i, j, 1]
                          _lowercase	       =t
                    if bbox[i, j, 2] < bbox[i, j, 0]:
                          _lowercase	       =bbox[i, j, 2]
                          _lowercase	       =bbox[i, j, 0]
                          _lowercase	       =t

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

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

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

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

        _lowercase	       =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							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase					)  -> str:
        '''simple docstring'''






        _lowercase	       =LayoutLMvaModel(config=_A					)
        model.to(_A					)
        model.eval()

        # text + image
        _lowercase	       =model(_A							,   pixel_values=_A					)
        _lowercase	       =model(
            _A							,   bbox=_A							,   pixel_values=_A							,   attention_mask=_A							,   token_type_ids=_A					)
        _lowercase	       =model(_A							,   bbox=_A							,   pixel_values=_A							,   token_type_ids=_A					)
        _lowercase	       =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
        _lowercase	       =model(_A					)
        self.parent.assertEqual(
            result.last_hidden_state.shape							,   (self.batch_size, self.text_seq_length, self.hidden_size)					)

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




  def   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase					)  -> Tuple:
        '''simple docstring'''






        _lowercase	       =self.num_labels
        _lowercase	       =LayoutLMvaForSequenceClassification(_A					)
        model.to(_A					)
        model.eval()
        _lowercase	       =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   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase					)  -> List[str]:
        '''simple docstring'''






        _lowercase	       =self.num_labels
        _lowercase	       =LayoutLMvaForTokenClassification(config=_A					)
        model.to(_A					)
        model.eval()
        _lowercase	       =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   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase					)  -> List[Any]:
        '''simple docstring'''






        _lowercase	       =LayoutLMvaForQuestionAnswering(config=_A					)
        model.to(_A					)
        model.eval()
        _lowercase	       =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   A__     (				self					)  -> Tuple:
        '''simple docstring'''






        _lowercase	       =self.prepare_config_and_inputs()
        (
            _lowercase				
        )	       =config_and_inputs
        _lowercase	       ={
            '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  __lowerCAmelCase     (					snake_case__			,			snake_case__			,			unittest.TestCase							):
  _a									=  False
  _a									=  False
  _a									=  False

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




  def   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase					)  -> Any:
        '''simple docstring'''






        return True




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






        _lowercase	       =LayoutLMvaModelTester(self					)
        _lowercase	       =ConfigTester(self							,   config_class=_A							,   hidden_size=37					)




  def   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase=False					)  -> Union[str, Any]:
        '''simple docstring'''






        _lowercase	       =copy.deepcopy(_A					)
        if model_class in get_values(_A					):
              _lowercase	       ={
                  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					):
                    _lowercase	       =torch.ones(self.model_tester.batch_size							,   dtype=torch.long							,   device=_A					)
              elif model_class in get_values(_A					):
                    _lowercase	       =torch.zeros(
                        self.model_tester.batch_size							,   dtype=torch.long							,   device=_A					)
                    _lowercase	       =torch.zeros(
                        self.model_tester.batch_size							,   dtype=torch.long							,   device=_A					)
              elif model_class in [
                  *get_values(_A					),
              ]:
                    _lowercase	       =torch.zeros(
                        self.model_tester.batch_size							,   dtype=torch.long							,   device=_A					)
              elif model_class in [
                  *get_values(_A					),
              ]:
                    _lowercase	       =torch.zeros(
                        (self.model_tester.batch_size, self.model_tester.text_seq_length)							,   dtype=torch.long							,   device=_A							,   )

        return inputs_dict




  def   A__     (				self					)  -> str:
        '''simple docstring'''






        self.config_tester.run_common_tests()




  def   A__     (				self					)  -> Dict:
        '''simple docstring'''






        _lowercase	       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_A					)




  def   A__     (				self					)  -> Dict:
        '''simple docstring'''






        _lowercase	       =self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
              _lowercase	       =type
              self.model_tester.create_and_check_model(*_A					)




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






        _lowercase	       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*_A					)




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






        _lowercase	       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*_A					)




  def   A__     (				self					)  -> Tuple:
        '''simple docstring'''






        _lowercase	       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*_A					)




  @slow
  def   A__     (				self					)  -> Dict:
        '''simple docstring'''






        for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _lowercase	       =LayoutLMvaModel.from_pretrained(_A					)
              self.assertIsNotNone(_A					)
def      a      (			)    ->      Any:





      """simple docstring"""



      _lowercase	       =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
      return image







@require_torch
class  __lowerCAmelCase     (					unittest.TestCase							):




  @cached_property
  def   A__     (				self					)  -> Dict:
        '''simple docstring'''






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




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






        _lowercase	       =LayoutLMvaModel.from_pretrained('microsoft/layoutlmv3-base'					).to(_A					)

        _lowercase	       =self.default_image_processor
        _lowercase	       =prepare_img()
        _lowercase	       =image_processor(images=_A							,   return_tensors='pt'					).pixel_values.to(_A					)

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

        # forward pass
        _lowercase	       =model(
            input_ids=input_ids.to(_A					)							,   bbox=bbox.to(_A					)							,   pixel_values=pixel_values.to(_A					)							,   )

        # verify the logits
        _lowercase	       =torch.Size((1, 199, 768)					)
        self.assertEqual(outputs.last_hidden_state.shape							,   _A					)

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

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









import colorsys

from PIL import Image  # type: ignore







def  _SCREAMING_SNAKE_CASE				(   a					,						a					,						a  )      ->      float:
					__A	:						List[str]       =				x
					__A	:						str       =				y
					for step in range(a  ):  # noqa: B007
										__A	:						Union[str, Any]       =				a * a - b * b + x
										__A	:						Optional[int]       =				2 * a * b + y
										__A	:						List[str]       =				a_new

										# divergence happens for all complex number with an absolute value
										# greater than 4
										if a * a + b * b > 4:
															break
					return step / (max_step - 1)







def  _SCREAMING_SNAKE_CASE				(   a  )      ->      tuple:
					if distance == 1:
										return (0, 0, 0)
					else:
										return (2_55, 2_55, 2_55)







def  _SCREAMING_SNAKE_CASE				(   a  )      ->      tuple:
					if distance == 1:
										return (0, 0, 0)
					else:
										return tuple(round(i * 2_55  ) for i in colorsys.hsv_to_rgb(a					,						1					,						1  )  )







def  _SCREAMING_SNAKE_CASE				(   a = 8_00					,						a = 6_00					,						a = -0.6					,						a = 0					,						a = 3.2					,						a = 50					,						a = True					,						)      ->      Image.Image:
					__A	:						str       =				Image.new('RGB'					,						(image_width, image_height)  )
					__A	:						Dict       =				img.load()

					# loop through the image-coordinates
					for image_x in range(a  ):
										for image_y in range(a  ):
															# determine the figure-coordinates based on the image-coordinates
															__A	:						Dict       =				figure_width / image_width * image_height
															__A	:						Union[str, Any]       =				figure_center_x + (image_x / image_width - 0.5) * figure_width
															__A	:						Optional[Any]       =				figure_center_y + (image_y / image_height - 0.5) * figure_height

															__A	:						Union[str, Any]       =				get_distance(a					,						a					,						a  )

															# color the corresponding pixel based on the selected coloring-function
															if use_distance_color_coding:
																				__A	:						Optional[Any]       =				get_color_coded_rgb(a  )
															else:
																				__A	:						Dict       =				get_black_and_white_rgb(a  )

					return img


if __name__ == "__main__":
				import doctest

				doctest.testmod()

				# colored version, full figure
				UpperCAmelCase						:   str			  =     get_image()

				# uncomment for colored version, different section, zoomed in
				# img = get_image(figure_center_x = -0.6, figure_center_y = -0.4,
				# figure_width = 0.8)

				# uncomment for black and white version, full figure
				# img = get_image(use_distance_color_coding = False)

				# uncomment to save the image
				# img.save("mandelbrot.png")

				img.show()




