


"""simple docstring"""






from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin






@dataclass
class      A(  lowerCAmelCase__		):

		"""simple docstring"""





		A    =							42






class      A(  lowerCAmelCase__				,							lowerCAmelCase__		):

		"""simple docstring"""





		@register_to_config
		def __init__( self					,				SCREAMING_SNAKE_CASE__ = 16					,				SCREAMING_SNAKE_CASE__ = 88					,				SCREAMING_SNAKE_CASE__ = None					,				SCREAMING_SNAKE_CASE__ = None					,				SCREAMING_SNAKE_CASE__ = 1					,				SCREAMING_SNAKE_CASE__ = 0.0					,				SCREAMING_SNAKE_CASE__ = 32					,				SCREAMING_SNAKE_CASE__ = None					,				SCREAMING_SNAKE_CASE__ = False					,				SCREAMING_SNAKE_CASE__ = None					,				SCREAMING_SNAKE_CASE__ = "geglu"					,				SCREAMING_SNAKE_CASE__ = True					,				SCREAMING_SNAKE_CASE__ = True					,				)      ->	int:

					"""simple docstring"""



					super().__init__()
					_UpperCamelCase					:Dict					     =	num_attention_heads
					_UpperCamelCase					:Tuple					     =	attention_head_dim
					_UpperCamelCase					:int					     =	num_attention_heads * attention_head_dim

					_UpperCamelCase					:List[Any]					     =	in_channels

					_UpperCamelCase					:int					     =	torch.nn.GroupNorm(num_groups=snake_case_					,				num_channels=snake_case_					,				eps=1E-6					,				affine=snake_case_   )
					_UpperCamelCase					:Dict					     =	nn.Linear(snake_case_					,				snake_case_   )

					# 3. Define transformers blocks
					_UpperCamelCase					:List[Any]					     =	nn.ModuleList(
					    [
					        BasicTransformerBlock(
					            snake_case_					,				snake_case_					,				snake_case_					,				dropout=snake_case_					,				cross_attention_dim=snake_case_					,				activation_fn=snake_case_					,				attention_bias=snake_case_					,				double_self_attention=snake_case_					,				norm_elementwise_affine=snake_case_					,				)
					        for d in range(snake_case_   )
					    ]   )

					_UpperCamelCase					:Optional[Any]					     =	nn.Linear(snake_case_					,				snake_case_   )





		def   _UpperCamelCase( self					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__=1					,				SCREAMING_SNAKE_CASE__=None					,				SCREAMING_SNAKE_CASE__ = True					,				)      ->	Any:

					"""simple docstring"""



					_UpperCamelCase     ,      _UpperCamelCase     ,      _UpperCamelCase     ,      _UpperCamelCase					:int					     =	hidden_states.shape
					_UpperCamelCase					:Any					     =	batch_frames // num_frames

					_UpperCamelCase					:Dict					     =	hidden_states

					_UpperCamelCase					:List[str]					     =	hidden_states[None, :].reshape(snake_case_					,				snake_case_					,				snake_case_					,				snake_case_					,				snake_case_   )
					_UpperCamelCase					:str					     =	hidden_states.permute(0					,				2					,				1					,				3					,				4   )

					_UpperCamelCase					:Tuple					     =	self.norm(snake_case_   )
					_UpperCamelCase					:Union[str, Any]					     =	hidden_states.permute(0					,				3					,				4					,				2					,				1   ).reshape(batch_size * height * width					,				snake_case_					,				snake_case_   )

					_UpperCamelCase					:Optional[Any]					     =	self.proj_in(snake_case_   )

					# 2. Blocks
					for block in self.transformer_blocks:
								_UpperCamelCase					:Union[str, Any]					     =	block(
								    snake_case_					,				encoder_hidden_states=snake_case_					,				timestep=snake_case_					,				cross_attention_kwargs=snake_case_					,				class_labels=snake_case_					,				)

					# 3. Output
					_UpperCamelCase					:str					     =	self.proj_out(snake_case_   )
					_UpperCamelCase					:Optional[Any]					     =	(
					    hidden_states[None, None, :]
					    .reshape(snake_case_					,				snake_case_					,				snake_case_					,				snake_case_					,				snake_case_   )
					    .permute(0					,				3					,				4					,				1					,				2   )
					    .contiguous()
					)
					_UpperCamelCase					:Union[str, Any]					     =	hidden_states.reshape(snake_case_					,				snake_case_					,				snake_case_					,				snake_case_   )

					_UpperCamelCase					:Union[str, Any]					     =	hidden_states + residual

					if not return_dict:
								return (output,)

					return TransformerTemporalModelOutput(sample=snake_case_   )





import copy
import os
from typing import Union

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


a			:			Optional[int]												=					logging.get_logger(__name__)

a			:			Dict												=					{
    '''google/pix2struct-textcaps-base''': (
        '''https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json'''
    ),
}
class 						lowerCamelCase_							(					lowerCAmelCase__					):



					'''simple docstring'''







					__UpperCAmelCase							  =					"pix2struct_text_model"
					__UpperCAmelCase							  =					["past_key_values"]
					__UpperCAmelCase							  =					{
					    "hidden_size": "hidden_size",
					    "num_attention_heads": "num_heads",
					    "num_hidden_layers": "num_layers",
					}






					def __init__(					self			,							snake_case_=5_0_2_4_4			,							snake_case_=7_6_8			,							snake_case_=6_4			,							snake_case_=2_0_4_8			,							snake_case_=1_2			,							snake_case_=1_2			,							snake_case_=3_2			,							snake_case_=1_2_8			,							snake_case_=0.1			,							snake_case_=1e-6			,							snake_case_=1.0			,							snake_case_="gelu_new"			,							snake_case_=0			,							snake_case_=False			,							snake_case_=0			,							snake_case_=1			,							snake_case_=False			,							snake_case_=True			,							**snake_case_			,							) ->      Optional[int]:







								'''simple docstring'''

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

								__lowercase			       = eos_token_id
								__lowercase			       = decoder_start_token_id

								# for backwards compatibility
								__lowercase			       = dense_act_fn

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






					@classmethod
					def A   (					cls			,							snake_case_			,							**snake_case_    ) ->      "PretrainedConfig":







								'''simple docstring'''

								cls._set_token_in_kwargs(snake_case_    )

								__lowercase       ,			__lowercase			       = cls.get_config_dict(snake_case_			,							**snake_case_    )

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




class 						lowerCamelCase_							(					lowerCAmelCase__					):



					'''simple docstring'''







					__UpperCAmelCase							  =					"pix2struct_vision_model"






					def __init__(					self			,							snake_case_=7_6_8			,							snake_case_=7_6_8			,							snake_case_=2_0_4_8			,							snake_case_=6_4			,							snake_case_=1_2			,							snake_case_=1_2			,							snake_case_="gelu_new"			,							snake_case_=1e-6			,							snake_case_=0.0			,							snake_case_=0.0			,							snake_case_=1e-1_0			,							snake_case_=1.0			,							snake_case_=4_0_9_6			,							snake_case_=3_2			,							snake_case_=1_2_8			,							**snake_case_			,							) ->      Union[str, Any]:







								'''simple docstring'''

								super().__init__(**snake_case_    )

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






					@classmethod
					def A   (					cls			,							snake_case_			,							**snake_case_    ) ->      "PretrainedConfig":







								'''simple docstring'''

								cls._set_token_in_kwargs(snake_case_    )

								__lowercase       ,			__lowercase			       = cls.get_config_dict(snake_case_			,							**snake_case_    )

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




class 						lowerCamelCase_							(					lowerCAmelCase__					):



					'''simple docstring'''







					__UpperCAmelCase							  =					"pix2struct"
					__UpperCAmelCase							  =					True






					def __init__(					self			,							snake_case_=None			,							snake_case_=None			,							snake_case_=1.0			,							snake_case_=0.0_2			,							snake_case_=False			,							snake_case_=False			,							snake_case_=True			,							**snake_case_			,							) ->      Optional[int]:







								'''simple docstring'''

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

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

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

								__lowercase			       = PixaStructTextConfig(**snake_case_    )
								__lowercase			       = PixaStructVisionConfig(**snake_case_    )

								__lowercase			       = self.text_config.decoder_start_token_id
								__lowercase			       = self.text_config.pad_token_id
								__lowercase			       = self.text_config.eos_token_id

								__lowercase			       = initializer_factor
								__lowercase			       = initializer_range

								__lowercase			       = self.initializer_range
								__lowercase			       = self.initializer_range

								__lowercase			       = is_vqa






					@classmethod
					def A   (					cls			,							snake_case_			,							snake_case_			,							**snake_case_    ) ->      List[str]:







								'''simple docstring'''

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






					def A   (					self    ) ->      Tuple:







								'''simple docstring'''

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


