



"""simple docstring"""
import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


UpperCAmelCase  :						str	=       logging.get_logger(__name__)

UpperCAmelCase  :						Optional[Any]	=       {
    "salesforce/blip2-opt-2.7b": "https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json",
}


class       SCREAMING_SNAKE_CASE__       (							__lowercase      ):
    lowercase__          =      "blip_2_vision_model"

    def __init__( self	: Union[str, Any]       ,     lowerCAmelCase_	: Tuple=1_4_0_8       ,     lowerCAmelCase_	: List[str]=6_1_4_4       ,     lowerCAmelCase_	: Dict=3_9       ,     lowerCAmelCase_	: Any=1_6       ,     lowerCAmelCase_	: Tuple=2_2_4       ,     lowerCAmelCase_	: Optional[Any]=1_4       ,     lowerCAmelCase_	: Dict="gelu"       ,     lowerCAmelCase_	: Any=0.00_001       ,     lowerCAmelCase_	: Union[str, Any]=0.0       ,     lowerCAmelCase_	: Optional[Any]=1E-10       ,     lowerCAmelCase_	: Dict=True       ,     **lowerCAmelCase_	: Tuple       ,     ):







      """simple docstring"""







      super().__init__(**snake_case_)

      lowercase_	    =       hidden_size
      lowercase_	    =       intermediate_size
      lowercase_	    =       num_hidden_layers
      lowercase_	    =       num_attention_heads
      lowercase_	    =       patch_size
      lowercase_	    =       image_size
      lowercase_	    =       initializer_range
      lowercase_	    =       attention_dropout
      lowercase_	    =       layer_norm_eps
      lowercase_	    =       hidden_act
      lowercase_	    =       qkv_bias




    @classmethod
    def     _UpperCAmelCase		( cls	: Tuple       ,     lowerCAmelCase_	: Union[str, os.PathLike]       ,     **lowerCAmelCase_	: int):







      """simple docstring"""







      cls._set_token_in_kwargs(snake_case_)

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

      # get the vision config dict if we are loading from Blip2Config
      if config_dict.get("""model_type""") == "blip-2":
        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       SCREAMING_SNAKE_CASE__       (							__lowercase      ):
    lowercase__          =      "blip_2_qformer"

    def __init__( self	: Dict       ,     lowerCAmelCase_	: Optional[Any]=3_0_5_2_2       ,     lowerCAmelCase_	: Tuple=7_6_8       ,     lowerCAmelCase_	: Optional[int]=1_2       ,     lowerCAmelCase_	: int=1_2       ,     lowerCAmelCase_	: Any=3_0_7_2       ,     lowerCAmelCase_	: Union[str, Any]="gelu"       ,     lowerCAmelCase_	: Dict=0.1       ,     lowerCAmelCase_	: List[str]=0.1       ,     lowerCAmelCase_	: Any=5_1_2       ,     lowerCAmelCase_	: Tuple=0.02       ,     lowerCAmelCase_	: Tuple=1E-12       ,     lowerCAmelCase_	: int=0       ,     lowerCAmelCase_	: int="absolute"       ,     lowerCAmelCase_	: Optional[Any]=2       ,     lowerCAmelCase_	: List[str]=1_4_0_8       ,     **lowerCAmelCase_	: str       ,     ):







      """simple docstring"""







      super().__init__(pad_token_id=snake_case_       ,     **snake_case_)

      lowercase_	    =       vocab_size
      lowercase_	    =       hidden_size
      lowercase_	    =       num_hidden_layers
      lowercase_	    =       num_attention_heads
      lowercase_	    =       hidden_act
      lowercase_	    =       intermediate_size
      lowercase_	    =       hidden_dropout_prob
      lowercase_	    =       attention_probs_dropout_prob
      lowercase_	    =       max_position_embeddings
      lowercase_	    =       initializer_range
      lowercase_	    =       layer_norm_eps
      lowercase_	    =       position_embedding_type
      lowercase_	    =       cross_attention_frequency
      lowercase_	    =       encoder_hidden_size




    @classmethod
    def     _UpperCAmelCase		( cls	: int       ,     lowerCAmelCase_	: Union[str, os.PathLike]       ,     **lowerCAmelCase_	: Tuple):







      """simple docstring"""







      cls._set_token_in_kwargs(snake_case_)

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

      # get the qformer config dict if we are loading from Blip2Config
      if config_dict.get("""model_type""") == "blip-2":
        lowercase_	    =       config_dict['''qformer_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       SCREAMING_SNAKE_CASE__       (							__lowercase      ):
    lowercase__          =      "blip-2"
    lowercase__          =      True

    def __init__( self	: Any       ,     lowerCAmelCase_	: Tuple=None       ,     lowerCAmelCase_	: str=None       ,     lowerCAmelCase_	: Tuple=None       ,     lowerCAmelCase_	: Any=3_2       ,     **lowerCAmelCase_	: Dict):







      """simple docstring"""







      super().__init__(**snake_case_)

      if vision_config is None:
        lowercase_	    =       {}
        logger.info("""vision_config is None. initializing the Blip2VisionConfig with default values.""")

      if qformer_config is None:
        lowercase_	    =       {}
        logger.info("""qformer_config is None. Initializing the Blip2QFormerConfig with default values.""")

      if text_config is None:
        lowercase_	    =       {}
        logger.info("""text_config is None. Initializing the text config with default values (`OPTConfig`).""")

      lowercase_	    =       BlipaVisionConfig(**snake_case_)
      lowercase_	    =       BlipaQFormerConfig(**snake_case_)
      lowercase_	    =       text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
      lowercase_	    =       CONFIG_MAPPING[text_model_type](**snake_case_)

      lowercase_	    =       self.text_config.tie_word_embeddings
      lowercase_	    =       self.text_config.is_encoder_decoder

      lowercase_	    =       num_query_tokens
      lowercase_	    =       self.vision_config.hidden_size
      lowercase_	    =       self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
      lowercase_	    =       1.0
      lowercase_	    =       0.02

    @classmethod
    def     _UpperCAmelCase		( cls	: Optional[int]       ,     lowerCAmelCase_	: BlipaVisionConfig       ,     lowerCAmelCase_	: BlipaQFormerConfig       ,     lowerCAmelCase_	: PretrainedConfig       ,     **lowerCAmelCase_	: Optional[int]       ,     ):







      """simple docstring"""







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




    def     _UpperCAmelCase		( self	: Optional[Any]):







      """simple docstring"""







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



from __future__ import annotations

from fractions import Fraction
from math import gcd, sqrt




def    __UpperCamelCase     (				lowerCAmelCase__							:				int  ):
 __a :  int						=  int(number**0.5  )
 return number == sq * sq




def    __UpperCamelCase     (				lowerCAmelCase__							:				int  ,   lowerCAmelCase__							:				int  ,   lowerCAmelCase__							:				int  ,   lowerCAmelCase__							:				int  ,   lowerCAmelCase__							:				int  ,   lowerCAmelCase__							:				int  ):
 __a :  int						=  x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den
 __a :  int						=  x_den * y_den * z_den
 __a :  int						=  gcd(lowerCAmelCase__  ,   lowerCAmelCase__  )
 top //= hcf
 bottom //= hcf
 return top, bottom




def    __UpperCamelCase     (				lowerCAmelCase__							:				int = 3_5  ):
 __a :  set						=  set()
 __a :  int
 __a :  Fraction						=  Fraction(0  )
 __a :  tuple[int, int]

 for x_num in range(1  ,   order + 1  ):
  for x_den in range(x_num + 1  ,   order + 1  ):
   for y_num in range(1  ,   order + 1  ):
    for y_den in range(y_num + 1  ,   order + 1  ):
     # n=1
     __a :  Any						=  x_num * y_den + x_den * y_num
     __a :  Dict						=  x_den * y_den
     __a :  int						=  gcd(lowerCAmelCase__  ,   lowerCAmelCase__  )
     z_num //= hcf
     z_den //= hcf
     if 0 < z_num < z_den <= order:
      __a :  str						=  add_three(
          lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  )
      unique_s.add(lowerCAmelCase__  )

     # n=2
     __a :  Union[str, Any]						=  (
         x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num
     )
     __a :  Optional[Any]						=  x_den * x_den * y_den * y_den
     if is_sq(lowerCAmelCase__  ) and is_sq(lowerCAmelCase__  ):
      __a :  Any						=  int(sqrt(lowerCAmelCase__  )  )
      __a :  Optional[Any]						=  int(sqrt(lowerCAmelCase__  )  )
      __a :  int						=  gcd(lowerCAmelCase__  ,   lowerCAmelCase__  )
      z_num //= hcf
      z_den //= hcf
      if 0 < z_num < z_den <= order:
       __a :  str						=  add_three(
           lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  )
       unique_s.add(lowerCAmelCase__  )

                    # n=-1
     __a :  List[str]						=  x_num * y_num
     __a :  List[str]						=  x_den * y_num + x_num * y_den
     __a :  str						=  gcd(lowerCAmelCase__  ,   lowerCAmelCase__  )
     z_num //= hcf
     z_den //= hcf
     if 0 < z_num < z_den <= order:
      __a :  Optional[int]						=  add_three(
          lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  )
      unique_s.add(lowerCAmelCase__  )

     # n=2
     __a :  List[str]						=  x_num * x_num * y_num * y_num
     __a :  Optional[int]						=  (
         x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den
     )
     if is_sq(lowerCAmelCase__  ) and is_sq(lowerCAmelCase__  ):
      __a :  Union[str, Any]						=  int(sqrt(lowerCAmelCase__  )  )
      __a :  List[str]						=  int(sqrt(lowerCAmelCase__  )  )
      __a :  Optional[int]						=  gcd(lowerCAmelCase__  ,   lowerCAmelCase__  )
      z_num //= hcf
      z_den //= hcf
      if 0 < z_num < z_den <= order:
       __a :  int						=  add_three(
           lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__  )
       unique_s.add(lowerCAmelCase__  )

 for num, den in unique_s:
  total += Fraction(lowerCAmelCase__  ,   lowerCAmelCase__  )

 return total.denominator + total.numerator


if __name__ == "__main__":
      print(F"""{solution() = }""")
