


'''simple docstring'''






import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase       =							logging.get_logger(__name__)

__UpperCAmelCase       =							{
    '''google/pix2struct-textcaps-base''': (
        '''https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json'''
    ),
}




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Tuple     						=				"pix2struct_text_model"
							lowercase__      :       Optional[int]     						=				["past_key_values"]
							lowercase__      :       int     						=				{
							    "hidden_size": "hidden_size",
							    "num_attention_heads": "num_heads",
							    "num_hidden_layers": "num_layers",
							}







							def __init__(		self ,   lowerCamelCase_=5_02_44 ,   lowerCamelCase_=7_68 ,   lowerCamelCase_=64 ,   lowerCamelCase_=20_48 ,   lowerCamelCase_=12 ,   lowerCamelCase_=12 ,   lowerCamelCase_=32 ,   lowerCamelCase_=1_28 ,   lowerCamelCase_=0.1 ,   lowerCamelCase_=1e-6 ,   lowerCamelCase_=1.0 ,   lowerCamelCase_="gelu_new" ,   lowerCamelCase_=0 ,   lowerCamelCase_=False ,   lowerCamelCase_=0 ,   lowerCamelCase_=1 ,   lowerCamelCase_=False ,   lowerCamelCase_=True ,   **lowerCamelCase_ ,   )	->							Any:
														lowerCAmelCase__	       =     vocab_size
														lowerCAmelCase__	       =     hidden_size
														lowerCAmelCase__	       =     d_kv
														lowerCAmelCase__	       =     d_ff
														lowerCAmelCase__	       =     num_layers
														lowerCAmelCase__	       =     num_heads
														lowerCAmelCase__	       =     relative_attention_num_buckets
														lowerCAmelCase__	       =     relative_attention_max_distance
														lowerCAmelCase__	       =     dropout_rate
														lowerCAmelCase__	       =     layer_norm_epsilon
														lowerCAmelCase__	       =     initializer_factor
														lowerCAmelCase__	       =     use_cache

														lowerCAmelCase__	       =     eos_token_id
														lowerCAmelCase__	       =     decoder_start_token_id

														# for backwards compatibility
														lowerCAmelCase__	       =     dense_act_fn

														super().__init__(
														    pad_token_id=lowerCamelCase_ ,   eos_token_id=lowerCamelCase_ ,   decoder_start_token_id=lowerCamelCase_ ,   tie_word_embeddings=lowerCamelCase_ ,   is_decoder=lowerCamelCase_ ,   **lowerCamelCase_ ,   )







							@classmethod
							def   __SCREAMING_SNAKE_CASE							(		cls ,   lowerCamelCase_ ,   **lowerCamelCase_       )	->							"PretrainedConfig":
														cls._set_token_in_kwargs(lowerCamelCase_       )

														lowerCAmelCase__       , lowerCAmelCase__	       =     cls.get_config_dict(lowerCamelCase_ ,   **lowerCamelCase_       )

														# get the text config dict if we are loading from Pix2StructConfig
														if config_dict.get('''model_type'''       ) == "pix2struct":
																					lowerCAmelCase__	       =     config_dict['''text_config''']

														if "model_type" in config_dict and hasattr(cls ,   '''model_type'''       ) and config_dict["model_type"] != cls.model_type:
																					logger.warning(
																					    F"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """
																					    F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""       )

														return cls.from_dict(lowerCamelCase_ ,   **lowerCamelCase_       )




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Optional[Any]     						=				"pix2struct_vision_model"







							def __init__(		self ,   lowerCamelCase_=7_68 ,   lowerCamelCase_=7_68 ,   lowerCamelCase_=20_48 ,   lowerCamelCase_=64 ,   lowerCamelCase_=12 ,   lowerCamelCase_=12 ,   lowerCamelCase_="gelu_new" ,   lowerCamelCase_=1e-6 ,   lowerCamelCase_=0.0 ,   lowerCamelCase_=0.0 ,   lowerCamelCase_=1e-10 ,   lowerCamelCase_=1.0 ,   lowerCamelCase_=40_96 ,   lowerCamelCase_=32 ,   lowerCamelCase_=1_28 ,   **lowerCamelCase_ ,   )	->							int:
														super().__init__(**lowerCamelCase_       )

														lowerCAmelCase__	       =     hidden_size
														lowerCAmelCase__	       =     patch_embed_hidden_size
														lowerCAmelCase__	       =     d_ff
														lowerCAmelCase__	       =     dropout_rate
														lowerCAmelCase__	       =     num_hidden_layers
														lowerCAmelCase__	       =     num_attention_heads
														lowerCAmelCase__	       =     initializer_range
														lowerCAmelCase__	       =     initializer_factor
														lowerCAmelCase__	       =     attention_dropout
														lowerCAmelCase__	       =     layer_norm_eps
														lowerCAmelCase__	       =     dense_act_fn
														lowerCAmelCase__	       =     seq_len
														lowerCAmelCase__	       =     relative_attention_num_buckets
														lowerCAmelCase__	       =     relative_attention_max_distance
														lowerCAmelCase__	       =     d_kv







							@classmethod
							def   __SCREAMING_SNAKE_CASE							(		cls ,   lowerCamelCase_ ,   **lowerCamelCase_       )	->							"PretrainedConfig":
														cls._set_token_in_kwargs(lowerCamelCase_       )

														lowerCAmelCase__       , lowerCAmelCase__	       =     cls.get_config_dict(lowerCamelCase_ ,   **lowerCamelCase_       )

														# get the vision config dict if we are loading from Pix2StructConfig
														if config_dict.get('''model_type'''       ) == "pix2struct":
																					lowerCAmelCase__	       =     config_dict['''vision_config''']

														if "model_type" in config_dict and hasattr(cls ,   '''model_type'''       ) and config_dict["model_type"] != cls.model_type:
																					logger.warning(
																					    F"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """
																					    F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""       )

														return cls.from_dict(lowerCamelCase_ ,   **lowerCamelCase_       )




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Optional[Any]     						=				"pix2struct"
							lowercase__      :       int     						=				True







							def __init__(		self ,   lowerCamelCase_=None ,   lowerCamelCase_=None ,   lowerCamelCase_=1.0 ,   lowerCamelCase_=0.02 ,   lowerCamelCase_=False ,   lowerCamelCase_=False ,   lowerCamelCase_=True ,   **lowerCamelCase_ ,   )	->							Any:
														super().__init__(tie_word_embeddings=lowerCamelCase_ ,   is_encoder_decoder=lowerCamelCase_ ,   **lowerCamelCase_       )

														if text_config is None:
																					lowerCAmelCase__	       =     {}
																					logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''       )

														if vision_config is None:
																					lowerCAmelCase__	       =     {}
																					logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.'''       )

														lowerCAmelCase__	       =     PixaStructTextConfig(**lowerCamelCase_       )
														lowerCAmelCase__	       =     PixaStructVisionConfig(**lowerCamelCase_       )

														lowerCAmelCase__	       =     self.text_config.decoder_start_token_id
														lowerCAmelCase__	       =     self.text_config.pad_token_id
														lowerCAmelCase__	       =     self.text_config.eos_token_id

														lowerCAmelCase__	       =     initializer_factor
														lowerCAmelCase__	       =     initializer_range

														lowerCAmelCase__	       =     self.initializer_range
														lowerCAmelCase__	       =     self.initializer_range

														lowerCAmelCase__	       =     is_vqa







							@classmethod
							def   __SCREAMING_SNAKE_CASE							(		cls ,   lowerCamelCase_ ,   lowerCamelCase_ ,   **lowerCamelCase_       )	->							Any:
														return cls(text_config=text_config.to_dict() ,   vision_config=vision_config.to_dict() ,   **lowerCamelCase_       )







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							List[str]:
														lowerCAmelCase__	       =     copy.deepcopy(self.__dict__       )
														lowerCAmelCase__	       =     self.text_config.to_dict()
														lowerCAmelCase__	       =     self.vision_config.to_dict()
														lowerCAmelCase__	       =     self.__class__.model_type
														return output



'''simple docstring'''






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers
from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roformer import RoFormerTokenizer
from .tokenization_utils import JiebaPreTokenizer


__UpperCAmelCase       =							logging.get_logger(__name__)

__UpperCAmelCase       =							{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

__UpperCAmelCase       =							{
    '''vocab_file''': {
        '''junnyu/roformer_chinese_small''': '''https://huggingface.co/junnyu/roformer_chinese_small/resolve/main/vocab.txt''',
        '''junnyu/roformer_chinese_base''': '''https://huggingface.co/junnyu/roformer_chinese_base/resolve/main/vocab.txt''',
        '''junnyu/roformer_chinese_char_small''': (
            '''https://huggingface.co/junnyu/roformer_chinese_char_small/resolve/main/vocab.txt'''
        ),
        '''junnyu/roformer_chinese_char_base''': (
            '''https://huggingface.co/junnyu/roformer_chinese_char_base/resolve/main/vocab.txt'''
        ),
        '''junnyu/roformer_small_discriminator''': (
            '''https://huggingface.co/junnyu/roformer_small_discriminator/resolve/main/vocab.txt'''
        ),
        '''junnyu/roformer_small_generator''': (
            '''https://huggingface.co/junnyu/roformer_small_generator/resolve/main/vocab.txt'''
        ),
    }
}

__UpperCAmelCase       =							{
    '''junnyu/roformer_chinese_small''': 1_536,
    '''junnyu/roformer_chinese_base''': 1_536,
    '''junnyu/roformer_chinese_char_small''': 512,
    '''junnyu/roformer_chinese_char_base''': 512,
    '''junnyu/roformer_small_discriminator''': 128,
    '''junnyu/roformer_small_generator''': 128,
}


__UpperCAmelCase       =							{
    '''junnyu/roformer_chinese_small''': {'''do_lower_case''': True},
    '''junnyu/roformer_chinese_base''': {'''do_lower_case''': True},
    '''junnyu/roformer_chinese_char_small''': {'''do_lower_case''': True},
    '''junnyu/roformer_chinese_char_base''': {'''do_lower_case''': True},
    '''junnyu/roformer_small_discriminator''': {'''do_lower_case''': True},
    '''junnyu/roformer_small_generator''': {'''do_lower_case''': True},
}




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       int     						=				VOCAB_FILES_NAMES
							lowercase__      :       Optional[int]     						=				PRETRAINED_VOCAB_FILES_MAP
							lowercase__      :       Any     						=				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							lowercase__      :       Union[str, Any]     						=				PRETRAINED_INIT_CONFIGURATION
							lowercase__      :       Tuple     						=				RoFormerTokenizer







							def __init__(		self ,   lowerCamelCase_=None ,   lowerCamelCase_=None ,   lowerCamelCase_=True ,   lowerCamelCase_="[UNK]" ,   lowerCamelCase_="[SEP]" ,   lowerCamelCase_="[PAD]" ,   lowerCamelCase_="[CLS]" ,   lowerCamelCase_="[MASK]" ,   lowerCamelCase_=True ,   lowerCamelCase_=None ,   **lowerCamelCase_ ,   )	->							Tuple:
														super().__init__(
														    lowerCamelCase_ ,   tokenizer_file=lowerCamelCase_ ,   do_lower_case=lowerCamelCase_ ,   unk_token=lowerCamelCase_ ,   sep_token=lowerCamelCase_ ,   pad_token=lowerCamelCase_ ,   cls_token=lowerCamelCase_ ,   mask_token=lowerCamelCase_ ,   tokenize_chinese_chars=lowerCamelCase_ ,   strip_accents=lowerCamelCase_ ,   **lowerCamelCase_ ,   )

														lowerCAmelCase__	       =     json.loads(self.backend_tokenizer.normalizer.__getstate__()       )
														if (
														    pre_tok_state.get('''lowercase''' ,   lowerCamelCase_       ) != do_lower_case
														    or pre_tok_state.get('''strip_accents''' ,   lowerCamelCase_       ) != strip_accents
														):
																					lowerCAmelCase__	       =     getattr(lowerCamelCase_ ,   pre_tok_state.pop('''type'''       )       )
																					lowerCAmelCase__	       =     do_lower_case
																					lowerCAmelCase__	       =     strip_accents
																					lowerCAmelCase__	       =     pre_tok_class(**lowerCamelCase_       )

														lowerCAmelCase__	       =     do_lower_case







							def __getstate__(		self       )	->							Any:
														lowerCAmelCase__	       =     self.__dict__.copy()
														lowerCAmelCase__	       =     BertPreTokenizer()
														return state







							def __setstate__(		self ,   lowerCamelCase_       )	->							List[Any]:
														lowerCAmelCase__	       =     d
														lowerCAmelCase__	       =     self.__dict__['''_tokenizer'''].get_vocab()
														lowerCAmelCase__	       =     PreTokenizer.custom(JiebaPreTokenizer(lowerCamelCase_       )       )







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_=None       )	->							Union[str, Any]:
														lowerCAmelCase__	       =     [self.cls_token_id] + token_ids_a + [self.sep_token_id]

														if token_ids_a:
																					output += token_ids_a + [self.sep_token_id]

														return output







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = None       )	->							List[int]:
														lowerCAmelCase__	       =     [self.sep_token_id]
														lowerCAmelCase__	       =     [self.cls_token_id]
														if token_ids_a is None:
																					return len(cls + token_ids_a + sep       ) * [0]
														return len(cls + token_ids_a + sep       ) * [0] + len(token_ids_a + sep       ) * [1]







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_ = None       )	->							Tuple[str]:
														lowerCAmelCase__	       =     self._tokenizer.model.save(lowerCamelCase_ ,   name=lowerCamelCase_       )
														return tuple(lowerCamelCase_       )







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_=None ,   lowerCamelCase_=None ,   lowerCamelCase_=False ,   **lowerCamelCase_ ,   )	->							Union[str, Any]:
														lowerCAmelCase__	       =     BertPreTokenizer()
														return super().save_pretrained(lowerCamelCase_ ,   lowerCamelCase_ ,   lowerCamelCase_ ,   lowerCamelCase_ ,   **lowerCamelCase_       )