



import copy
import os
from typing import Union

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


A__	      =       logging.get_logger(__name__)

A__	      =       {
    """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						,				_snake_case=50244						,				_snake_case=768						,				_snake_case=64						,				_snake_case=2048						,				_snake_case=12						,				_snake_case=12						,				_snake_case=32						,				_snake_case=128						,				_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						,				):



									"""simple docstring"""





									_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=_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 							snake_case   (							cls						,				_snake_case						,				**_snake_case		):



									"""simple docstring"""





									cls._set_token_in_kwargs(_snake_case		)

									_lowerCAmelCase	,	_lowerCAmelCase						=    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":
														_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(_snake_case						,				**_snake_case		)





class 					__lowerCAmelCase (     lowerCamelCase__       ):
				__lowerCamelCase =				'''pix2struct_vision_model'''




				def __init__(							self						,				_snake_case=768						,				_snake_case=768						,				_snake_case=2048						,				_snake_case=64						,				_snake_case=12						,				_snake_case=12						,				_snake_case="gelu_new"						,				_snake_case=1e-6						,				_snake_case=0.0						,				_snake_case=0.0						,				_snake_case=1e-10						,				_snake_case=1.0						,				_snake_case=4096						,				_snake_case=32						,				_snake_case=128						,				**_snake_case						,				):



									"""simple docstring"""





									super().__init__(**_snake_case		)

									_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 							snake_case   (							cls						,				_snake_case						,				**_snake_case		):



									"""simple docstring"""





									cls._set_token_in_kwargs(_snake_case		)

									_lowerCAmelCase	,	_lowerCAmelCase						=    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":
														_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(_snake_case						,				**_snake_case		)





class 					__lowerCAmelCase (     lowerCamelCase__       ):
				__lowerCamelCase =				'''pix2struct'''
				__lowerCamelCase =				True




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



									"""simple docstring"""





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

									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(**_snake_case		)
									_lowerCAmelCase						=    PixaStructVisionConfig(**_snake_case		)

									_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 							snake_case   (							cls						,				_snake_case						,				_snake_case						,				**_snake_case		):



									"""simple docstring"""





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




				def 							snake_case   (							self		):



									"""simple docstring"""





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







def 					a__			(				SCREAMING_SNAKE_CASE    :    float				,      SCREAMING_SNAKE_CASE    :    float				,      SCREAMING_SNAKE_CASE    :    int				):


  '''simple docstring'''







  if principal <= 0:
    raise Exception("Principal borrowed must be > 0"				)
  if rate_per_annum < 0:
    raise Exception("Rate of interest must be >= 0"				)
  if years_to_repay <= 0 or not isinstance(SCREAMING_SNAKE_CASE				,      SCREAMING_SNAKE_CASE				):
    raise Exception("Years to repay must be an integer > 0"				)

  # Yearly rate is divided by 12 to get monthly rate
  lowerCAmelCase     :     Tuple								=							rate_per_annum / 1_2

  # Years to repay is multiplied by 12 to get number of payments as payment is monthly
  lowerCAmelCase     :     List[Any]								=							years_to_repay * 1_2

  return (
      principal
      * rate_per_month
      * (1 + rate_per_month) ** number_of_payments
      / ((1 + rate_per_month) ** number_of_payments - 1)
  )


if __name__ == "__main__":
     import doctest

     doctest.testmod()





