


'''simple docstring'''







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


if TYPE_CHECKING:
       from ...processing_utils import ProcessorMixin
       from ...utils import TensorType


lowerCAmelCase     :  Dict											=    logging.get_logger(__name__)

lowerCAmelCase     :  str											=    {
    """microsoft/layoutlmv3-base""": """https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json""",
}
class 						UpperCamelCase__       (			_a						):



     """simple docstring"""






     __magic_name__           =     '''layoutlmv3'''







     def __init__(			self   ,						snake_case__=5_0265   ,						snake_case__=768   ,						snake_case__=12   ,						snake_case__=12   ,						snake_case__=3072   ,						snake_case__="gelu"   ,						snake_case__=0.1   ,						snake_case__=0.1   ,						snake_case__=512   ,						snake_case__=2   ,						snake_case__=0.02   ,						snake_case__=1E-5   ,						snake_case__=1   ,						snake_case__=0   ,						snake_case__=2   ,						snake_case__=1024   ,						snake_case__=128   ,						snake_case__=128   ,						snake_case__=True   ,						snake_case__=32   ,						snake_case__=128   ,						snake_case__=64   ,						snake_case__=256   ,						snake_case__=True   ,						snake_case__=True   ,						snake_case__=True   ,						snake_case__=224   ,						snake_case__=3   ,						snake_case__=16   ,						snake_case__=None   ,						**snake_case__   ,						):

            '''simple docstring'''

            super().__init__(
                vocab_size=snake_case__   ,						hidden_size=snake_case__   ,						num_hidden_layers=snake_case__   ,						num_attention_heads=snake_case__   ,						intermediate_size=snake_case__   ,						hidden_act=snake_case__   ,						hidden_dropout_prob=snake_case__   ,						attention_probs_dropout_prob=snake_case__   ,						max_position_embeddings=snake_case__   ,						type_vocab_size=snake_case__   ,						initializer_range=snake_case__   ,						layer_norm_eps=snake_case__   ,						pad_token_id=snake_case__   ,						bos_token_id=snake_case__   ,						eos_token_id=snake_case__   ,						**snake_case__   ,						)
            _lowerCAmelCase     :				Tuple       =							max_ad_position_embeddings
            _lowerCAmelCase     :				Dict       =							coordinate_size
            _lowerCAmelCase     :				Tuple       =							shape_size
            _lowerCAmelCase     :				Optional[int]       =							has_relative_attention_bias
            _lowerCAmelCase     :				List[Any]       =							rel_pos_bins
            _lowerCAmelCase     :				Union[str, Any]       =							max_rel_pos
            _lowerCAmelCase     :				List[str]       =							has_spatial_attention_bias
            _lowerCAmelCase     :				Optional[Any]       =							rel_ad_pos_bins
            _lowerCAmelCase     :				str       =							max_rel_ad_pos
            _lowerCAmelCase     :				str       =							text_embed
            _lowerCAmelCase     :				Dict       =							visual_embed
            _lowerCAmelCase     :				Tuple       =							input_size
            _lowerCAmelCase     :				int       =							num_channels
            _lowerCAmelCase     :				int       =							patch_size
            _lowerCAmelCase     :				Dict       =							classifier_dropout
class 						UpperCamelCase__       (			_a						):



     """simple docstring"""






     __magic_name__           =     version.parse("1.12"						)







     @property
     def a						(			self ):

            '''simple docstring'''

            if self.task in ["question-answering", "sequence-classification"]:
                   return OrderedDict(
                       [
                           ('input_ids', {0: 'batch', 1: 'sequence'}),
                           ('attention_mask', {0: 'batch', 1: 'sequence'}),
                           ('bbox', {0: 'batch', 1: 'sequence'}),
                           ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
                       ] )
            else:
                   return OrderedDict(
                       [
                           ('input_ids', {0: 'batch', 1: 'sequence'}),
                           ('bbox', {0: 'batch', 1: 'sequence'}),
                           ('attention_mask', {0: 'batch', 1: 'sequence'}),
                           ('pixel_values', {0: 'batch', 1: 'num_channels'}),
                       ] )







     @property
     def a						(			self ):

            '''simple docstring'''

            return 1E-5







     @property
     def a						(			self ):

            '''simple docstring'''

            return 12







     def a						(			self   ,						snake_case__   ,						snake_case__ = -1   ,						snake_case__ = -1   ,						snake_case__ = False   ,						snake_case__ = None   ,						snake_case__ = 3   ,						snake_case__ = 40   ,						snake_case__ = 40   ,						):

            '''simple docstring'''

            setattr(processor.image_processor   ,						'apply_ocr'   ,						snake_case__ )

            # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
            _lowerCAmelCase     :				Any       =							compute_effective_axis_dimension(
                snake_case__   ,						fixed_dimension=OnnxConfig.default_fixed_batch   ,						num_token_to_add=0 )
            # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
            _lowerCAmelCase     :				List[str]       =							processor.tokenizer.num_special_tokens_to_add(snake_case__ )
            _lowerCAmelCase     :				int       =							compute_effective_axis_dimension(
                snake_case__   ,						fixed_dimension=OnnxConfig.default_fixed_sequence   ,						num_token_to_add=snake_case__ )
            # Generate dummy inputs according to compute batch and sequence
            _lowerCAmelCase     :				Tuple       =							[[' '.join([processor.tokenizer.unk_token] ) * seq_length]] * batch_size

            # Generate dummy bounding boxes
            _lowerCAmelCase     :				Any       =							[[[48, 84, 73, 128]]] * batch_size

            # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
            # batch_size = compute_effective_axis_dimension(batch_size, fixed_dimension=OnnxConfig.default_fixed_batch)
            _lowerCAmelCase     :				Any       =							self._generate_dummy_images(snake_case__   ,						snake_case__   ,						snake_case__   ,						snake_case__ )

            _lowerCAmelCase     :				Any       =							dict(
                processor(
                    snake_case__   ,						text=snake_case__   ,						boxes=snake_case__   ,						return_tensors=snake_case__   ,						) )

            return inputs









import unittest

import numpy as np
import requests

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

    from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
    _SCREAMING_SNAKE_CASE   =  False

if is_vision_available():
    from PIL import Image

    from transformers import PixaStructImageProcessor






class 					SCREAMING_SNAKE_CASE_  (       unittest.TestCase ):
       """simple docstring"""


       def __init__(				self	:str,      snake_case	:int,      snake_case	:Dict=7,      snake_case	:List[Any]=3,      snake_case	:int=18,      snake_case	:List[str]=30,      snake_case	:str=400,      snake_case	:int=None,      snake_case	:List[Any]=True,      snake_case	:Optional[int]=True,      snake_case	:Union[str, Any]=None,      ):



          """simple docstring"""

          _lowercase              =size if size is not None else {'height': 20, 'width': 20}
          _lowercase              =parent
          _lowercase              =batch_size
          _lowercase              =num_channels
          _lowercase              =image_size
          _lowercase              =min_resolution
          _lowercase              =max_resolution
          _lowercase              =size
          _lowercase              =do_normalize
          _lowercase              =do_convert_rgb
          _lowercase              =[512, 1024, 2048, 4096]
          _lowercase              =patch_size if patch_size is not None else {'height': 16, 'width': 16}





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}





       def 				UpperCamelCase__						(				self	:int):



          """simple docstring"""

          _lowercase              ='https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'
          _lowercase              =Image.open(requests.get(snake_case,      stream=snake_case).raw).convert('RGB')
          return raw_image






@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11  ,						reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''  ,						)
@require_torch
@require_vision
class 					SCREAMING_SNAKE_CASE_  (       _a  ,						unittest.TestCase ):
       """simple docstring"""


       __lowerCAmelCase    :		Tuple					=PixaStructImageProcessor if is_vision_available() else None





       def 				UpperCamelCase__						(				self	:str):



          """simple docstring"""

          _lowercase              =PixaStructImageProcessingTester(self)





       @property
       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          return self.image_processor_tester.prepare_image_processor_dict()





       def 				UpperCamelCase__						(				self	:str):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(snake_case,      'do_normalize'))
          self.assertTrue(hasattr(snake_case,      'do_convert_rgb'))





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          _lowercase              =self.image_processor_tester.prepare_dummy_image()

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          _lowercase              =2048

          _lowercase              =image_processor(snake_case,      return_tensors='pt',      max_patches=snake_case)
          self.assertTrue(torch.allclose(inputs.flattened_patches.mean(),      torch.tensor(0.0_6_0_6),      atol=1e-3,      rtol=1e-3))





       def 				UpperCamelCase__						(				self	:int):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowercase              =prepare_image_inputs(self.image_processor_tester,      equal_resolution=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      Image.Image)

          # Test not batched input
          _lowercase              =(
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
             # Test not batched input
             _lowercase              =image_processor(
                 image_inputs[0],      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (1, max_patch, expected_hidden_dim),      )

             # Test batched
             _lowercase              =image_processor(
                 snake_case,      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),      )





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



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowercase              =prepare_image_inputs(self.image_processor_tester,      equal_resolution=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      Image.Image)

          # Test not batched input
          _lowercase              =(
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          _lowercase              =True

          for max_patch in self.image_processor_tester.max_patches:
             # Test not batched input
             with self.assertRaises(snake_case):
                _lowercase              =image_processor(
                    image_inputs[0],      return_tensors='pt',      max_patches=snake_case).flattened_patches

             _lowercase              ='Hello'

             _lowercase              =image_processor(
                 image_inputs[0],      return_tensors='pt',      max_patches=snake_case,      header_text=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (1, max_patch, expected_hidden_dim),      )

             # Test batched
             _lowercase              =image_processor(
                 snake_case,      return_tensors='pt',      max_patches=snake_case,      header_text=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),      )





       def 				UpperCamelCase__						(				self	:int):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random numpy tensors
          _lowercase              =prepare_image_inputs(self.image_processor_tester,      equal_resolution=snake_case,      numpify=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      np.ndarray)

          _lowercase              =(
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
             # Test not batched input
             _lowercase              =image_processor(
                 image_inputs[0],      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (1, max_patch, expected_hidden_dim),      )

             # Test batched
             _lowercase              =image_processor(
                 snake_case,      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),      )





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PyTorch tensors
          _lowercase              =prepare_image_inputs(self.image_processor_tester,      equal_resolution=snake_case,      torchify=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      torch.Tensor)

          # Test not batched input
          _lowercase              =(
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * self.image_processor_tester.num_channels
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
             # Test not batched input
             _lowercase              =image_processor(
                 image_inputs[0],      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (1, max_patch, expected_hidden_dim),      )

             # Test batched
             _lowercase              =image_processor(
                 snake_case,      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),      )






@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11  ,						reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''  ,						)
@require_torch
@require_vision
class 					SCREAMING_SNAKE_CASE_  (       _a  ,						unittest.TestCase ):
       """simple docstring"""


       __lowerCAmelCase    :		Optional[Any]					=PixaStructImageProcessor if is_vision_available() else None





       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          _lowercase              =PixaStructImageProcessingTester(self,      num_channels=4)
          _lowercase              =3





       @property
       def 				UpperCamelCase__						(				self	:str):



          """simple docstring"""

          return self.image_processor_tester.prepare_image_processor_dict()





       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(snake_case,      'do_normalize'))
          self.assertTrue(hasattr(snake_case,      'do_convert_rgb'))





       def 				UpperCamelCase__						(				self	:Optional[Any]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowercase              =prepare_image_inputs(self.image_processor_tester,      equal_resolution=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      Image.Image)

          # Test not batched input
          _lowercase              =(
              (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
              * (self.image_processor_tester.num_channels - 1)
          ) + 2

          for max_patch in self.image_processor_tester.max_patches:
             # Test not batched input
             _lowercase              =image_processor(
                 image_inputs[0],      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (1, max_patch, expected_hidden_dim),      )

             # Test batched
             _lowercase              =image_processor(
                 snake_case,      return_tensors='pt',      max_patches=snake_case).flattened_patches
             self.assertEqual(
                 encoded_images.shape,      (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim),      )


