


'''simple docstring'''
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import TensorType, logging


if TYPE_CHECKING:
						from ...onnx.config import PatchingSpec
						from ...tokenization_utils_base import PreTrainedTokenizerBase


__magic_name__ : List[Any]					=logging.get_logger(__name__)

__magic_name__ : List[Any]					={
    'allenai/longformer-base-4096': 'https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json',
    'allenai/longformer-large-4096': 'https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json',
    'allenai/longformer-large-4096-finetuned-triviaqa': (
        'https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json'
    ),
    'allenai/longformer-base-4096-extra.pos.embd.only': (
        'https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json'
    ),
    'allenai/longformer-large-4096-extra.pos.embd.only': (
        'https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json'
    ),
}






class 		UpperCamelCase_		(       __lowercase ):





						"""simple docstring"""




						UpperCAmelCase__						:							Tuple       =  "longformer"
						def __init__(			self					:       Tuple						,					_lowerCamelCase					:       Optional[Any] = 5_12						,					_lowerCamelCase					:       int = 2						,					_lowerCamelCase					:       List[str] = 1						,					_lowerCamelCase					:       List[str] = 0						,					_lowerCamelCase					:       Optional[int] = 2						,					_lowerCamelCase					:       Any = 3_05_22						,					_lowerCamelCase					:       int = 7_68						,					_lowerCamelCase					:       Tuple = 12						,					_lowerCamelCase					:       Union[str, Any] = 12						,					_lowerCamelCase					:       Union[str, Any] = 30_72						,					_lowerCamelCase					:       Union[str, Any] = "gelu"						,					_lowerCamelCase					:       Optional[Any] = 0.1						,					_lowerCamelCase					:       List[str] = 0.1						,					_lowerCamelCase					:       str = 5_12						,					_lowerCamelCase					:       int = 2						,					_lowerCamelCase					:       Any = 0.02						,					_lowerCamelCase					:       str = 1e-12						,					_lowerCamelCase					:       Optional[int] = False						,					**_lowerCamelCase					:       Optional[int]						,					)				->       List[str]:
										super().__init__(pad_token_id=_lowerCamelCase						,					**_lowerCamelCase	)

										__magic_name__							=			attention_window
										__magic_name__							=			sep_token_id
										__magic_name__							=			bos_token_id
										__magic_name__							=			eos_token_id
										__magic_name__							=			vocab_size
										__magic_name__							=			hidden_size
										__magic_name__							=			num_hidden_layers
										__magic_name__							=			num_attention_heads
										__magic_name__							=			hidden_act
										__magic_name__							=			intermediate_size
										__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__							=			onnx_export






class 		UpperCamelCase_		(       __lowercase ):





						"""simple docstring"""




						def __init__(			self					:       Union[str, Any]						,					_lowerCamelCase					:       str						,					_lowerCamelCase					:       int = "default"						,					_lowerCamelCase					:       Union[str, Any] = None	)				->       Tuple:
										super().__init__(_lowerCamelCase						,					_lowerCamelCase						,					_lowerCamelCase	)
										__magic_name__							=			True
						@property
						def        __A       (			self					:       List[Any]	)				->       Mapping[str, Mapping[int, str]]:

										if self.task == "multiple-choice":
														__magic_name__							=			{0: "batch", 1: "choice", 2: "sequence"}
										else:
														__magic_name__							=			{0: "batch", 1: "sequence"}
										return OrderedDict(
										    [
										        ("input_ids", dynamic_axis),
										        ("attention_mask", dynamic_axis),
										        ("global_attention_mask", dynamic_axis),
										    ]	)
						@property
						def        __A       (			self					:       Optional[int]	)				->       Mapping[str, Mapping[int, str]]:
										__magic_name__							=			super().outputs
										if self.task == "default":
														__magic_name__							=			{0: "batch"}
										return outputs
						@property
						def        __A       (			self					:       int	)				->       float:
										return 1e-4
						@property
						def        __A       (			self					:       List[str]	)				->       int:
										return max(super().default_onnx_opset						,					14	)

						def        __A       (			self					:       str						,					_lowerCamelCase					:       int						,					_lowerCamelCase					:       Tuple = -1						,					_lowerCamelCase					:       Any = -1						,					_lowerCamelCase					:       Tuple = False						,					_lowerCamelCase					:       Optional[Any] = None						,					)				->       Mapping[str, Any]:
										__magic_name__							=			super().generate_dummy_inputs(
										    preprocessor=_lowerCamelCase						,					batch_size=_lowerCamelCase						,					seq_length=_lowerCamelCase						,					is_pair=_lowerCamelCase						,					framework=_lowerCamelCase	)
										import torch

										# for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64)
										# makes the export fail randomly
										__magic_name__							=			torch.zeros_like(inputs["input_ids"]	)
										# make every second token global
										__magic_name__							=			1

										return inputs





import copy
import os
from typing import Union

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


lowerCamelCase__						=				logging.get_logger(__name__)

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







class 							lowerCAmelCase__   ( __lowercase						):
						UpperCamelCase_				:			Union[str, Any]						=		"pix2struct_text_model"
						UpperCamelCase_				:			str						=		["past_key_values"]
						UpperCamelCase_				:			str						=		{
						    "hidden_size": "hidden_size",
						    "num_attention_heads": "num_heads",
						    "num_hidden_layers": "num_layers",
						}

						def __init__(      self							,				a=5_02_44							,				a=7_68							,				a=64							,				a=20_48							,				a=12							,				a=12							,				a=32							,				a=1_28							,				a=0.1							,				a=1e-6							,				a=1.0							,				a="gelu_new"							,				a=0							,				a=False							,				a=0							,				a=1							,				a=False							,				a=True							,				**a							,				)       ->   Optional[int]:


								'''simple docstring'''

								_UpperCamelCase         =						vocab_size
								_UpperCamelCase         =						hidden_size
								_UpperCamelCase         =						d_kv
								_UpperCamelCase         =						d_ff
								_UpperCamelCase         =						num_layers
								_UpperCamelCase         =						num_heads
								_UpperCamelCase         =						relative_attention_num_buckets
								_UpperCamelCase         =						relative_attention_max_distance
								_UpperCamelCase         =						dropout_rate
								_UpperCamelCase         =						layer_norm_epsilon
								_UpperCamelCase         =						initializer_factor
								_UpperCamelCase         =						use_cache

								_UpperCamelCase         =						eos_token_id
								_UpperCamelCase         =						decoder_start_token_id

								# for backwards compatibility
								_UpperCamelCase         =						dense_act_fn

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







						@classmethod
						def 			A_					(      cls							,				a							,				**a					)       ->   "PretrainedConfig":


								'''simple docstring'''

								cls._set_token_in_kwargs(a					)

								_UpperCamelCase     ,						_UpperCamelCase         =						cls.get_config_dict(a							,				**a					)

								# get the text config dict if we are loading from Pix2StructConfig
								if config_dict.get("""model_type"""					) == "pix2struct":
										_UpperCamelCase         =						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(a							,				**a					)







class 							lowerCAmelCase__   ( __lowercase						):
						UpperCamelCase_				:			int						=		"pix2struct_vision_model"

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


								'''simple docstring'''

								super().__init__(**a					)

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







						@classmethod
						def 			A_					(      cls							,				a							,				**a					)       ->   "PretrainedConfig":


								'''simple docstring'''

								cls._set_token_in_kwargs(a					)

								_UpperCamelCase     ,						_UpperCamelCase         =						cls.get_config_dict(a							,				**a					)

								# get the vision config dict if we are loading from Pix2StructConfig
								if config_dict.get("""model_type"""					) == "pix2struct":
										_UpperCamelCase         =						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(a							,				**a					)







class 							lowerCAmelCase__   ( __lowercase						):
						UpperCamelCase_				:			Dict						=		"pix2struct"
						UpperCamelCase_				:			int						=		True

						def __init__(      self							,				a=None							,				a=None							,				a=1.0							,				a=0.02							,				a=False							,				a=False							,				a=True							,				**a							,				)       ->   Optional[Any]:


								'''simple docstring'''

								super().__init__(tie_word_embeddings=a							,				is_encoder_decoder=a							,				**a					)

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

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

								_UpperCamelCase         =						PixaStructTextConfig(**a					)
								_UpperCamelCase         =						PixaStructVisionConfig(**a					)

								_UpperCamelCase         =						self.text_config.decoder_start_token_id
								_UpperCamelCase         =						self.text_config.pad_token_id
								_UpperCamelCase         =						self.text_config.eos_token_id

								_UpperCamelCase         =						initializer_factor
								_UpperCamelCase         =						initializer_range

								_UpperCamelCase         =						self.initializer_range
								_UpperCamelCase         =						self.initializer_range

								_UpperCamelCase         =						is_vqa

						@classmethod
						def 			A_					(      cls							,				a							,				a							,				**a					)       ->   str:


								'''simple docstring'''

								return cls(text_config=text_config.to_dict()							,				vision_config=vision_config.to_dict()							,				**a					)







						def 			A_					(      self					)       ->   Union[str, Any]:


								'''simple docstring'''

								_UpperCamelCase         =						copy.deepcopy(self.__dict__					)
								_UpperCamelCase         =						self.text_config.to_dict()
								_UpperCamelCase         =						self.vision_config.to_dict()
								_UpperCamelCase         =						self.__class__.model_type
								return output



