






"""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


UpperCamelCase_	:				Any										= logging.get_logger(__name__)

UpperCamelCase_	:				Optional[Any]										= {
    """microsoft/layoutlmv3-base""": """https://huggingface.co/microsoft/layoutlmv3-base/resolve/main/config.json""",
}


class      _lowercase (     lowerCAmelCase						):
      _a   :     List[Any]								=   '''layoutlmv3'''


      def __init__(    self				:	Union[str, Any]							,   a				:	Any=5_0_2_6_5							,   a				:	int=7_6_8							,   a				:	List[Any]=1_2							,   a				:	List[Any]=1_2							,   a				:	Any=3_0_7_2							,   a				:	Dict="gelu"							,   a				:	Optional[int]=0.1							,   a				:	List[str]=0.1							,   a				:	Optional[Any]=5_1_2							,   a				:	int=2							,   a				:	Union[str, Any]=0.0_2							,   a				:	Union[str, Any]=1e-5							,   a				:	Optional[Any]=1							,   a				:	int=0							,   a				:	str=2							,   a				:	str=1_0_2_4							,   a				:	List[Any]=1_2_8							,   a				:	List[Any]=1_2_8							,   a				:	str=True							,   a				:	Union[str, Any]=3_2							,   a				:	List[str]=1_2_8							,   a				:	List[str]=6_4							,   a				:	Tuple=2_5_6							,   a				:	str=True							,   a				:	Union[str, Any]=True							,   a				:	str=True							,   a				:	List[str]=2_2_4							,   a				:	Dict=3							,   a				:	Any=1_6							,   a				:	int=None							,   **a				:	Union[str, Any]							,   ):





            """simple docstring"""







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



class      _lowercase (     lowerCAmelCase						):
      _a   :     Any								=   version.parse('''1.12'''						)


      @property
      def _UpperCamelCase						(    self				:	List[Any]						):





            """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 _UpperCamelCase						(    self				:	Optional[Any]						):





            """simple docstring"""







            return 1e-5


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





            """simple docstring"""







            return 1_2







      def _UpperCamelCase						(    self				:	Tuple							,   a				:	"ProcessorMixin"							,   a				:	int = -1							,   a				:	int = -1							,   a				:	bool = False							,   a				:	Optional["TensorType"] = None							,   a				:	int = 3							,   a				:	int = 4_0							,   a				:	int = 4_0							,   ):





            """simple docstring"""







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

            # 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(
                a							,   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      :    Union[str, Any]  			=processor.tokenizer.num_special_tokens_to_add(a						)
            __snake_case      :    Dict  			=compute_effective_axis_dimension(
                a							,   fixed_dimension=OnnxConfig.default_fixed_sequence							,   num_token_to_add=a						)
            # Generate dummy inputs according to compute batch and sequence
            __snake_case      :    Union[str, Any]  			=[[''' '''.join([processor.tokenizer.unk_token]						) * seq_length]] * batch_size

            # Generate dummy bounding boxes
            __snake_case      :    List[Any]  			=[[[4_8, 8_4, 7_3, 1_2_8]]] * 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      :    int  			=self._generate_dummy_images(a							,   a							,   a							,   a						)

            __snake_case      :    List[Any]  			=dict(
                processor(
                    a							,   text=a							,   boxes=a							,   return_tensors=a							,   )						)

            return inputs










"""simple docstring"""





import json
import os
import unittest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class      _lowercase (     lowerCAmelCase		,  unittest.TestCase						):
      _a   :     List[str]								=   MgpstrTokenizer
      _a   :     int								=   False
      _a   :     List[str]								=   {}
      _a   :     Optional[Any]								=   False


      def _UpperCamelCase						(    self				:	str						):





            """simple docstring"""







            super().setUp()

            # fmt: off
            __snake_case      :    Union[str, Any]  			=['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z''']
            # fmt: on
            __snake_case      :    Optional[Any]  			=dict(zip(a							,   range(len(a						)						)						)						)

            __snake_case      :    int  			=os.path.join(self.tmpdirname							,   VOCAB_FILES_NAMES['''vocab_file''']						)
            with open(self.vocab_file							,   '''w'''							,   encoding='''utf-8'''						) as fp:
                  fp.write(json.dumps(a						) + '''\n'''						)


      def _UpperCamelCase						(    self				:	Any							,   **a				:	int						):





            """simple docstring"""







            return MgpstrTokenizer.from_pretrained(self.tmpdirname							,   **a						)


      def _UpperCamelCase						(    self				:	int							,   a				:	Any						):





            """simple docstring"""







            __snake_case      :    Dict  			='''tester'''
            __snake_case      :    str  			='''tester'''
            return input_text, output_text


      @unittest.skip('''MGP-STR always lower cases letters.'''						)
      def _UpperCamelCase						(    self				:	Any						):





            """simple docstring"""







            pass


      def _UpperCamelCase						(    self				:	Any						):





            """simple docstring"""







            __snake_case      :    List[Any]  			=self.get_tokenizers(do_lower_case=a						)
            for tokenizer in tokenizers:
                  with self.subTest(f'''{tokenizer.__class__.__name__}'''						):
                        __snake_case      :    List[Any]  			='''[SPECIAL_TOKEN]'''

                        tokenizer.add_special_tokens({'''cls_token''': special_token}						)
                        __snake_case      :    Tuple  			=tokenizer.encode([special_token]							,   add_special_tokens=a						)
                        self.assertEqual(len(a						)							,   1						)

                        __snake_case      :    int  			=tokenizer.decode(a							,   skip_special_tokens=a						)
                        self.assertTrue(special_token not in decoded						)


      def _UpperCamelCase						(    self				:	Tuple						):





            """simple docstring"""







            __snake_case      :    Union[str, Any]  			=self.get_tokenizers()
            for tokenizer in tokenizers:
                  with self.subTest(f'''{tokenizer.__class__.__name__}'''						):
                        __snake_case			,				__snake_case      :    Tuple  			=self.get_input_output_texts(a						)

                        __snake_case      :    int  			=tokenizer.tokenize(a						)
                        __snake_case      :    str  			=tokenizer.convert_tokens_to_ids(a						)
                        __snake_case      :    Union[str, Any]  			=tokenizer.encode(a							,   add_special_tokens=a						)
                        self.assertListEqual(a							,   a						)

                        __snake_case      :    Tuple  			=tokenizer.convert_ids_to_tokens(a						)
                        self.assertNotEqual(len(a						)							,   0						)
                        __snake_case      :    Optional[Any]  			=tokenizer.decode(a						)
                        self.assertIsInstance(a							,   a						)

                        self.assertEqual(text_a.replace(''' '''							,   ''''''						)							,   a						)


      @unittest.skip('''MGP-STR tokenizer only handles one sequence.'''						)
      def _UpperCamelCase						(    self				:	List[Any]						):





            """simple docstring"""







            pass







      @unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer'''						)
      def _UpperCamelCase						(    self				:	List[str]						):





            """simple docstring"""







            pass


