
'''simple docstring'''







from dataclasses import dataclass
from typing import Tuple

import numpy as np
import torch






@dataclass
class  lowerCAmelCase__   :
					lowerCAmelCase_        =     42  # [batch_size x 3]
					lowerCAmelCase_        =     42  # [batch_size x 3]
					lowerCAmelCase_        =     42  # [batch_size x 3]
					lowerCAmelCase_        =     42  # [batch_size x 3]
					lowerCAmelCase_        =     42
					lowerCAmelCase_        =     42
					lowerCAmelCase_        =     42
					lowerCAmelCase_        =     42
					lowerCAmelCase_        =     42






					def 	_snake_case    (   self		):


											"""simple docstring"""





											assert self.x.shape[0] == self.y.shape[0] == self.z.shape[0] == self.origin.shape[0]
											assert self.x.shape[1] == self.y.shape[1] == self.z.shape[1] == self.origin.shape[1] == 3
											assert len(self.x.shape		) == len(self.y.shape		) == len(self.z.shape		) == len(self.origin.shape		) == 2






					def 	_snake_case    (   self		):


											"""simple docstring"""





											return torch.from_numpy(np.array([self.width, self.height]				,							dtype=np.floataa		)		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											return torch.from_numpy(np.array([self.x_fov, self.y_fov]				,							dtype=np.floataa		)		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       torch.arange(self.height * self.width		)
											lowercase_	:     Union[str, Any]													=       torch.stack(
											    [
											        pixel_indices % self.width,
											        torch.div(__SCREAMING_SNAKE_CASE				,							self.width				,							rounding_mode='''trunc'''		),
											    ]				,							axis=1				,							)
											return coords






					@property
					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_						, *lowercase_	:     str													=       self.shape
											lowercase_	:     List[Any]													=       int(np.prod(__SCREAMING_SNAKE_CASE		)		)

											lowercase_	:     Union[str, Any]													=       self.get_image_coords()
											lowercase_	:     Tuple													=       torch.broadcast_to(coords.unsqueeze(0		)				,							[batch_size * inner_batch_size, *coords.shape]		)
											lowercase_	:     Dict													=       self.get_camera_rays(__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Tuple													=       rays.view(__SCREAMING_SNAKE_CASE				,							inner_batch_size * self.height * self.width				,							2				,							3		)

											return rays






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_						, *lowercase_						, lowercase_	:     str													=       coords.shape
											assert n_coords == 2
											assert batch_size == self.origin.shape[0]

											lowercase_	:     Any													=       coords.view(__SCREAMING_SNAKE_CASE				,							-1				,							2		)

											lowercase_	:     str													=       self.resolution()
											lowercase_	:     Dict													=       self.fov()

											lowercase_	:     Optional[Any]													=       (flat.float() / (res - 1)) * 2 - 1
											lowercase_	:     Any													=       fracs * torch.tan(fov / 2		)

											lowercase_	:     Optional[int]													=       fracs.view(__SCREAMING_SNAKE_CASE				,							-1				,							2		)
											lowercase_	:     Optional[Any]													=       (
											    self.z.view(__SCREAMING_SNAKE_CASE				,							1				,							3		)
											    + self.x.view(__SCREAMING_SNAKE_CASE				,							1				,							3		) * fracs[:, :, :1]
											    + self.y.view(__SCREAMING_SNAKE_CASE				,							1				,							3		) * fracs[:, :, 1:]
											)
											lowercase_	:     List[str]													=       directions / directions.norm(dim=-1				,							keepdim=__SCREAMING_SNAKE_CASE		)
											lowercase_	:     str													=       torch.stack(
											    [
											        torch.broadcast_to(self.origin.view(__SCREAMING_SNAKE_CASE				,							1				,							3		)				,							[batch_size, directions.shape[1], 3]		),
											        directions,
											    ]				,							dim=2				,							)
											return rays.view(__SCREAMING_SNAKE_CASE				,							*__SCREAMING_SNAKE_CASE				,							2				,							3		)






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											assert width * self.height == height * self.width, "The aspect ratio should not change."
											return DifferentiableProjectiveCamera(
											    origin=self.origin				,							x=self.x				,							y=self.y				,							z=self.z				,							width=__SCREAMING_SNAKE_CASE				,							height=__SCREAMING_SNAKE_CASE				,							x_fov=self.x_fov				,							y_fov=self.y_fov				,							)







def 					snake_case_		(				__SCREAMING_SNAKE_CASE					: int							):



						"""simple docstring"""





						lowercase_	:     Any													=       []
						lowercase_	:     List[str]													=       []
						lowercase_	:     int													=       []
						lowercase_	:     str													=       []
						for theta in np.linspace(0      ,      2 * np.pi      ,      num=20							):
												lowercase_	:     Dict													=       np.array([np.sin(__SCREAMING_SNAKE_CASE							), np.cos(__SCREAMING_SNAKE_CASE							), -0.5]							)
												z /= np.sqrt(np.sum(z**2							)							)
												lowercase_	:     Tuple													=       -z * 4
												lowercase_	:     str													=       np.array([np.cos(__SCREAMING_SNAKE_CASE							), -np.sin(__SCREAMING_SNAKE_CASE							), 0.0]							)
												lowercase_	:     Optional[Any]													=       np.cross(__SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE							)
												origins.append(__SCREAMING_SNAKE_CASE							)
												xs.append(__SCREAMING_SNAKE_CASE							)
												ys.append(__SCREAMING_SNAKE_CASE							)
												zs.append(__SCREAMING_SNAKE_CASE							)
						return DifferentiableProjectiveCamera(
						    origin=torch.from_numpy(np.stack(__SCREAMING_SNAKE_CASE      ,      axis=0							)							).float()      ,      x=torch.from_numpy(np.stack(__SCREAMING_SNAKE_CASE      ,      axis=0							)							).float()      ,      y=torch.from_numpy(np.stack(__SCREAMING_SNAKE_CASE      ,      axis=0							)							).float()      ,      z=torch.from_numpy(np.stack(__SCREAMING_SNAKE_CASE      ,      axis=0							)							).float()      ,      width=__SCREAMING_SNAKE_CASE      ,      height=__SCREAMING_SNAKE_CASE      ,      x_fov=0.7      ,      y_fov=0.7      ,      shape=(1, len(__SCREAMING_SNAKE_CASE							))      ,      )





'''simple docstring'''







import copy
import os
from typing import Union

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


_lowercase :  Optional[Any]    							=       logging.get_logger(__name__)

_lowercase :  List[str]    							=       {
    "google/pix2struct-textcaps-base": (
        "https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"
    ),
}






class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_text_model'''
					lowerCAmelCase_        =     ['''past_key_values''']
					lowerCAmelCase_        =     {
					    '''hidden_size''': '''hidden_size''',
					    '''num_attention_heads''': '''num_heads''',
					    '''num_hidden_layers''': '''num_layers''',
					}






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=5_02_44				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							__SCREAMING_SNAKE_CASE=0.1				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=1				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											lowercase_	:     Any													=       vocab_size
											lowercase_	:     Tuple													=       hidden_size
											lowercase_	:     Optional[Any]													=       d_kv
											lowercase_	:     List[str]													=       d_ff
											lowercase_	:     List[str]													=       num_layers
											lowercase_	:     Optional[Any]													=       num_heads
											lowercase_	:     Union[str, Any]													=       relative_attention_num_buckets
											lowercase_	:     Optional[int]													=       relative_attention_max_distance
											lowercase_	:     Union[str, Any]													=       dropout_rate
											lowercase_	:     Dict													=       layer_norm_epsilon
											lowercase_	:     Dict													=       initializer_factor
											lowercase_	:     List[Any]													=       use_cache

											lowercase_	:     Optional[int]													=       eos_token_id
											lowercase_	:     Optional[int]													=       decoder_start_token_id

											# for backwards compatibility
											lowercase_	:     Any													=       dense_act_fn

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






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     Optional[int]													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

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







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_vision_model'''






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=1E-1_0				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=40_96				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Union[str, Any]													=       hidden_size
											lowercase_	:     Any													=       patch_embed_hidden_size
											lowercase_	:     List[Any]													=       d_ff
											lowercase_	:     Dict													=       dropout_rate
											lowercase_	:     Any													=       num_hidden_layers
											lowercase_	:     Any													=       num_attention_heads
											lowercase_	:     int													=       initializer_range
											lowercase_	:     Dict													=       initializer_factor
											lowercase_	:     Dict													=       attention_dropout
											lowercase_	:     Optional[Any]													=       layer_norm_eps
											lowercase_	:     str													=       dense_act_fn
											lowercase_	:     Dict													=       seq_len
											lowercase_	:     List[Any]													=       relative_attention_num_buckets
											lowercase_	:     int													=       relative_attention_max_distance
											lowercase_	:     Optional[int]													=       d_kv






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     str													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

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







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct'''
					lowerCAmelCase_        =     True






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=0.02				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





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

											if text_config is None:
																	lowercase_	:     Optional[Any]													=       {}
																	logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''		)

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

											lowercase_	:     str													=       PixaStructTextConfig(**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Tuple													=       PixaStructVisionConfig(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[Any]													=       self.text_config.decoder_start_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.pad_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.eos_token_id

											lowercase_	:     int													=       initializer_factor
											lowercase_	:     Any													=       initializer_range

											lowercase_	:     str													=       self.initializer_range
											lowercase_	:     str													=       self.initializer_range

											lowercase_	:     int													=       is_vqa






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       copy.deepcopy(self.__dict__		)
											lowercase_	:     Any													=       self.text_config.to_dict()
											lowercase_	:     Optional[Any]													=       self.vision_config.to_dict()
											lowercase_	:     Optional[int]													=       self.__class__.model_type
											return output



