





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_        =							logging.get_logger(__name__)

lowerCAmelCase_        =							{
    '''microsoft/layoutlmv3-base''': '''https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json''',
}



class    __lowerCAmelCase			(     _a	):
			lowerCamelCase_	:	Dict			     = '''layoutlmv3'''






			def __init__(self   ,				__magic_name__=5_0265   ,				__magic_name__=768   ,				__magic_name__=12   ,				__magic_name__=12   ,				__magic_name__=3072   ,				__magic_name__="gelu"   ,				__magic_name__=0.1   ,				__magic_name__=0.1   ,				__magic_name__=512   ,				__magic_name__=2   ,				__magic_name__=0.02   ,				__magic_name__=1e-5   ,				__magic_name__=1   ,				__magic_name__=0   ,				__magic_name__=2   ,				__magic_name__=1024   ,				__magic_name__=128   ,				__magic_name__=128   ,				__magic_name__=True   ,				__magic_name__=32   ,				__magic_name__=128   ,				__magic_name__=64   ,				__magic_name__=256   ,				__magic_name__=True   ,				__magic_name__=True   ,				__magic_name__=True   ,				__magic_name__=224   ,				__magic_name__=3   ,				__magic_name__=16   ,				__magic_name__=None   ,				**__magic_name__   ,				)	->  List[Any]:


							'''simple docstring'''



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




class    __lowerCAmelCase			(     _a	):
			lowerCamelCase_	:	Optional[int]			     = version.parse('''1.12'''	)






			@property
			def      lowerCamelCase    (self       )	->  Mapping[str, Mapping[int, str]]:


							'''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      lowerCamelCase    (self       )	->  float:


							'''simple docstring'''



							return 1e-5






			@property
			def      lowerCamelCase    (self       )	->  int:


							'''simple docstring'''



							return 12






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__ = -1   ,				__magic_name__ = -1   ,				__magic_name__ = False   ,				__magic_name__ = None   ,				__magic_name__ = 3   ,				__magic_name__ = 40   ,				__magic_name__ = 40   ,				)	->  Mapping[str, Any]:


							'''simple docstring'''



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

							# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
							snake_case_		:       Dict           =    compute_effective_axis_dimension(
							    __magic_name__   ,				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
							snake_case_		:       Optional[int]           =    processor.tokenizer.num_special_tokens_to_add(__magic_name__       )
							snake_case_		:       Any           =    compute_effective_axis_dimension(
							    __magic_name__   ,				fixed_dimension=OnnxConfig.default_fixed_sequence   ,				num_token_to_add=__magic_name__       )
							# Generate dummy inputs according to compute batch and sequence
							snake_case_		:       str           =    [[''' '''.join([processor.tokenizer.unk_token]       ) * seq_length]] * batch_size

							# Generate dummy bounding boxes
							snake_case_		:       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)
							snake_case_		:       Optional[int]           =    self._generate_dummy_images(__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )

							snake_case_		:       List[Any]           =    dict(
							    processor(
							        __magic_name__   ,				text=__magic_name__   ,				boxes=__magic_name__   ,				return_tensors=__magic_name__   ,				)       )

							return inputs







import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor

@require_tokenizers
@require_vision
class   lowerCAmelCase				(   unittest.TestCase     ):
 def 	A_      ( self    : str      )     ->      Dict:
     lowerCamelCase__       : Optional[Any]   					=						tempfile.mkdtemp()

     # fmt: off
     lowerCamelCase__       : Tuple   					=						['[UNK]', '[CLS]', '[SEP]', '[PAD]', '[MASK]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing', ',', 'low', 'lowest']
     # fmt: on
     lowerCamelCase__       : int   					=						os.path.join(self.tmpdirname     ,	VOCAB_FILES_NAMES['vocab_file']      )
     with open(self.vocab_file     ,	'w'     ,	encoding='utf-8'      ) as vocab_writer:
         vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]      )      )

     lowerCamelCase__       : Any   					=						{
         'do_resize': True,
         'size': {'height': 18, 'width': 18},
         'do_normalize': True,
         'image_mean': [0.5, 0.5, 0.5],
         'image_std': [0.5, 0.5, 0.5],
     }
     lowerCamelCase__       : int   					=						os.path.join(self.tmpdirname     ,	UpperCAmelCase      )
     with open(self.image_processor_file     ,	'w'     ,	encoding='utf-8'      ) as fp:
         json.dump(UpperCAmelCase     ,	UpperCAmelCase      )
 def 	A_      ( self    : List[str]     ,	**UpperCAmelCase    : str      )     ->      int:
     return BertTokenizer.from_pretrained(self.tmpdirname     ,	**UpperCAmelCase      )
 def 	A_      ( self    : Optional[int]     ,	**UpperCAmelCase    : List[str]      )     ->      List[str]:
     return ViTImageProcessor.from_pretrained(self.tmpdirname     ,	**UpperCAmelCase      )
 def 	A_      ( self    : int      )     ->      Any:
     shutil.rmtree(self.tmpdirname      )
 def 	A_      ( self    : str      )     ->      str:
     lowerCamelCase__       : List[Any]   					=						[np.random.randint(255     ,	size=(3, 30, 400)     ,	dtype=np.uinta      )]

     lowerCamelCase__       : Union[str, Any]   					=						[Image.fromarray(np.moveaxis(UpperCAmelCase     ,	0     ,	-1      )      ) for x in image_inputs]

     return image_inputs
 def 	A_      ( self    : Dict      )     ->      Dict:
     lowerCamelCase__       : List[Any]   					=						self.get_tokenizer()
     lowerCamelCase__       : Tuple   					=						self.get_image_processor()

     lowerCamelCase__       : Union[str, Any]   					=						VisionTextDualEncoderProcessor(tokenizer=UpperCAmelCase     ,	image_processor=UpperCAmelCase      )

     processor.save_pretrained(self.tmpdirname      )
     lowerCamelCase__       : List[str]   					=						VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname      )

     self.assertEqual(processor.tokenizer.get_vocab()     ,	tokenizer.get_vocab()      )
     self.assertIsInstance(processor.tokenizer     ,	(BertTokenizer, BertTokenizerFast)      )

     self.assertEqual(processor.image_processor.to_json_string()     ,	image_processor.to_json_string()      )
     self.assertIsInstance(processor.image_processor     ,	UpperCAmelCase      )
 def 	A_      ( self    : List[str]      )     ->      Tuple:
     lowerCamelCase__       : List[Any]   					=						VisionTextDualEncoderProcessor(
         tokenizer=self.get_tokenizer()     ,	image_processor=self.get_image_processor()      )
     processor.save_pretrained(self.tmpdirname      )

     lowerCamelCase__       : Tuple   					=						self.get_tokenizer(bos_token='(BOS)'     ,	eos_token='(EOS)'      )
     lowerCamelCase__       : int   					=						self.get_image_processor(do_normalize=UpperCAmelCase     ,	padding_value=1.0      )

     lowerCamelCase__       : Union[str, Any]   					=						VisionTextDualEncoderProcessor.from_pretrained(
         self.tmpdirname     ,	bos_token='(BOS)'     ,	eos_token='(EOS)'     ,	do_normalize=UpperCAmelCase     ,	padding_value=1.0      )

     self.assertEqual(processor.tokenizer.get_vocab()     ,	tokenizer_add_kwargs.get_vocab()      )
     self.assertIsInstance(processor.tokenizer     ,	(BertTokenizer, BertTokenizerFast)      )

     self.assertEqual(processor.image_processor.to_json_string()     ,	image_processor_add_kwargs.to_json_string()      )
     self.assertIsInstance(processor.image_processor     ,	UpperCAmelCase      )
 def 	A_      ( self    : Any      )     ->      Tuple:
     lowerCamelCase__       : List[Any]   					=						self.get_image_processor()
     lowerCamelCase__       : Any   					=						self.get_tokenizer()

     lowerCamelCase__       : Optional[int]   					=						VisionTextDualEncoderProcessor(tokenizer=UpperCAmelCase     ,	image_processor=UpperCAmelCase      )

     lowerCamelCase__       : Optional[int]   					=						self.prepare_image_inputs()

     lowerCamelCase__       : int   					=						image_processor(UpperCAmelCase     ,	return_tensors='np'      )
     lowerCamelCase__       : List[str]   					=						processor(images=UpperCAmelCase     ,	return_tensors='np'      )

     for key in input_feat_extract.keys():
         self.assertAlmostEqual(input_feat_extract[key].sum()     ,	input_processor[key].sum()     ,	delta=1e-2      )
 def 	A_      ( self    : Tuple      )     ->      List[Any]:
     lowerCamelCase__       : Dict   					=						self.get_image_processor()
     lowerCamelCase__       : List[str]   					=						self.get_tokenizer()

     lowerCamelCase__       : str   					=						VisionTextDualEncoderProcessor(tokenizer=UpperCAmelCase     ,	image_processor=UpperCAmelCase      )

     lowerCamelCase__       : Tuple   					=						'lower newer'

     lowerCamelCase__       : Tuple   					=						processor(text=UpperCAmelCase      )

     lowerCamelCase__       : Any   					=						tokenizer(UpperCAmelCase      )

     for key in encoded_tok.keys():
         self.assertListEqual(encoded_tok[key]     ,	encoded_processor[key]      )
 def 	A_      ( self    : Optional[int]      )     ->      Any:
     lowerCamelCase__       : Optional[int]   					=						self.get_image_processor()
     lowerCamelCase__       : Tuple   					=						self.get_tokenizer()

     lowerCamelCase__       : Tuple   					=						VisionTextDualEncoderProcessor(tokenizer=UpperCAmelCase     ,	image_processor=UpperCAmelCase      )

     lowerCamelCase__       : str   					=						'lower newer'
     lowerCamelCase__       : Dict   					=						self.prepare_image_inputs()

     lowerCamelCase__       : int   					=						processor(text=UpperCAmelCase     ,	images=UpperCAmelCase      )

     self.assertListEqual(list(inputs.keys()      )     ,	['input_ids', 'token_type_ids', 'attention_mask', 'pixel_values']      )

     # test if it raises when no input is passed
     with self.assertRaises(UpperCAmelCase      ):
         processor()
 def 	A_      ( self    : Optional[Any]      )     ->      List[str]:
     lowerCamelCase__       : Dict   					=						self.get_image_processor()
     lowerCamelCase__       : Union[str, Any]   					=						self.get_tokenizer()

     lowerCamelCase__       : List[str]   					=						VisionTextDualEncoderProcessor(tokenizer=UpperCAmelCase     ,	image_processor=UpperCAmelCase      )

     lowerCamelCase__       : List[str]   					=						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     lowerCamelCase__       : Dict   					=						processor.batch_decode(UpperCAmelCase      )
     lowerCamelCase__       : List[str]   					=						tokenizer.batch_decode(UpperCAmelCase      )

     self.assertListEqual(UpperCAmelCase     ,	UpperCAmelCase      )







 def 	A_      ( self    : Optional[int]      )     ->      List[str]:
     lowerCamelCase__       : Tuple   					=						self.get_image_processor()
     lowerCamelCase__       : List[str]   					=						self.get_tokenizer()

     lowerCamelCase__       : Union[str, Any]   					=						VisionTextDualEncoderProcessor(tokenizer=UpperCAmelCase     ,	image_processor=UpperCAmelCase      )

     lowerCamelCase__       : Dict   					=						'lower newer'
     lowerCamelCase__       : List[Any]   					=						self.prepare_image_inputs()

     lowerCamelCase__       : Union[str, Any]   					=						processor(text=UpperCAmelCase     ,	images=UpperCAmelCase      )

     self.assertListEqual(list(inputs.keys()      )     ,	processor.model_input_names      )





