

'''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


lowercase__         =      logging.get_logger(__name__)

lowercase__         =      {
    "salesforce/blip2-opt-2.7b": "https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json",
}




class 	A_			(    _snake_case							):






     '''simple docstring'''



     UpperCAmelCase_						:     List[Any]  		=  """blip_2_vision_model"""


     def __init__(		self			:					Dict					,      lowercase_			:					Union[str, Any]=1_408					,      lowercase_			:					Dict=6_144					,      lowercase_			:					Optional[int]=39					,      lowercase_			:					Union[str, Any]=16					,      lowercase_			:					List[str]=224					,      lowercase_			:					Tuple=14					,      lowercase_			:					Union[str, Any]="gelu"					,      lowercase_			:					Any=0.0_0001					,      lowercase_			:					List[str]=0.0					,      lowercase_			:					Optional[Any]=1E-10					,      lowercase_			:					str=True					,      **lowercase_			:					List[Any]					,      )						->     Dict:
      super().__init__(**lowercase_	)

      UpperCAmelCase	:     Any						 =			hidden_size
      UpperCAmelCase	:     Optional[int]						 =			intermediate_size
      UpperCAmelCase	:     List[str]						 =			num_hidden_layers
      UpperCAmelCase	:     Dict						 =			num_attention_heads
      UpperCAmelCase	:     List[str]						 =			patch_size
      UpperCAmelCase	:     Union[str, Any]						 =			image_size
      UpperCAmelCase	:     List[Any]						 =			initializer_range
      UpperCAmelCase	:     Dict						 =			attention_dropout
      UpperCAmelCase	:     Optional[int]						 =			layer_norm_eps
      UpperCAmelCase	:     Any						 =			hidden_act
      UpperCAmelCase	:     Optional[int]						 =			qkv_bias




     @classmethod
     def        UpperCAmelCase_	(		cls			:					Optional[Any]					,      lowercase_			:					Union[str, os.PathLike]					,      **lowercase_			:					str	)						->     "PretrainedConfig":
      cls._set_token_in_kwargs(lowercase_	)

      UpperCAmelCase ,			UpperCAmelCase	:     Any						 =			cls.get_config_dict(lowercase_					,      **lowercase_	)

      # get the vision config dict if we are loading from Blip2Config
      if config_dict.get('model_type'	) == "blip-2":
       UpperCAmelCase	:     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(lowercase_					,      **lowercase_	)




class 	A_			(    _snake_case							):






     '''simple docstring'''



     UpperCAmelCase_						:     List[Any]  		=  """blip_2_qformer"""


     def __init__(		self			:					List[str]					,      lowercase_			:					Optional[int]=30_522					,      lowercase_			:					Union[str, Any]=768					,      lowercase_			:					Optional[Any]=12					,      lowercase_			:					Dict=12					,      lowercase_			:					Dict=3_072					,      lowercase_			:					Any="gelu"					,      lowercase_			:					Any=0.1					,      lowercase_			:					List[str]=0.1					,      lowercase_			:					List[Any]=512					,      lowercase_			:					Any=0.02					,      lowercase_			:					List[str]=1E-12					,      lowercase_			:					Optional[int]=0					,      lowercase_			:					List[Any]="absolute"					,      lowercase_			:					Optional[int]=2					,      lowercase_			:					Dict=1_408					,      **lowercase_			:					List[str]					,      )						->     Optional[int]:
      super().__init__(pad_token_id=lowercase_					,      **lowercase_	)

      UpperCAmelCase	:     Union[str, Any]						 =			vocab_size
      UpperCAmelCase	:     int						 =			hidden_size
      UpperCAmelCase	:     str						 =			num_hidden_layers
      UpperCAmelCase	:     Tuple						 =			num_attention_heads
      UpperCAmelCase	:     Dict						 =			hidden_act
      UpperCAmelCase	:     Optional[Any]						 =			intermediate_size
      UpperCAmelCase	:     int						 =			hidden_dropout_prob
      UpperCAmelCase	:     Dict						 =			attention_probs_dropout_prob
      UpperCAmelCase	:     List[Any]						 =			max_position_embeddings
      UpperCAmelCase	:     List[Any]						 =			initializer_range
      UpperCAmelCase	:     str						 =			layer_norm_eps
      UpperCAmelCase	:     Tuple						 =			position_embedding_type
      UpperCAmelCase	:     Any						 =			cross_attention_frequency
      UpperCAmelCase	:     Union[str, Any]						 =			encoder_hidden_size




     @classmethod
     def        UpperCAmelCase_	(		cls			:					Optional[int]					,      lowercase_			:					Union[str, os.PathLike]					,      **lowercase_			:					Tuple	)						->     "PretrainedConfig":
      cls._set_token_in_kwargs(lowercase_	)

      UpperCAmelCase ,			UpperCAmelCase	:     List[Any]						 =			cls.get_config_dict(lowercase_					,      **lowercase_	)

      # get the qformer config dict if we are loading from Blip2Config
      if config_dict.get('model_type'	) == "blip-2":
       UpperCAmelCase	:     Optional[Any]						 =			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(lowercase_					,      **lowercase_	)




class 	A_			(    _snake_case							):






     '''simple docstring'''



     UpperCAmelCase_						:     Optional[Any]  		=  """blip-2"""
     UpperCAmelCase_						:     str  		=  True


     def __init__(		self			:					int					,      lowercase_			:					Tuple=None					,      lowercase_			:					Tuple=None					,      lowercase_			:					Optional[int]=None					,      lowercase_			:					List[Any]=32					,      **lowercase_			:					List[Any]	)						->     Any:
      super().__init__(**lowercase_	)

      if vision_config is None:
       UpperCAmelCase	:     Dict						 =			{}
       logger.info('vision_config is None. initializing the Blip2VisionConfig with default values.'	)

      if qformer_config is None:
       UpperCAmelCase	:     Tuple						 =			{}
       logger.info('qformer_config is None. Initializing the Blip2QFormerConfig with default values.'	)

      if text_config is None:
       UpperCAmelCase	:     List[str]						 =			{}
       logger.info('text_config is None. Initializing the text config with default values (`OPTConfig`).'	)

      UpperCAmelCase	:     List[str]						 =			BlipaVisionConfig(**lowercase_	)
      UpperCAmelCase	:     Tuple						 =			BlipaQFormerConfig(**lowercase_	)
      UpperCAmelCase	:     Tuple						 =			text_config['model_type'] if 'model_type' in text_config else 'opt'
      UpperCAmelCase	:     Optional[int]						 =			CONFIG_MAPPING[text_model_type](**lowercase_	)

      UpperCAmelCase	:     List[str]						 =			self.text_config.tie_word_embeddings
      UpperCAmelCase	:     Optional[int]						 =			self.text_config.is_encoder_decoder

      UpperCAmelCase	:     List[str]						 =			num_query_tokens
      UpperCAmelCase	:     Any						 =			self.vision_config.hidden_size
      UpperCAmelCase	:     Dict						 =			self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
      UpperCAmelCase	:     Dict						 =			1.0
      UpperCAmelCase	:     Optional[int]						 =			0.02


     @classmethod
     def        UpperCAmelCase_	(		cls			:					Tuple					,      lowercase_			:					BlipaVisionConfig					,      lowercase_			:					BlipaQFormerConfig					,      lowercase_			:					PretrainedConfig					,      **lowercase_			:					Dict					,      )						->     int:
      return cls(
          vision_config=vision_config.to_dict()					,      qformer_config=qformer_config.to_dict()					,      text_config=text_config.to_dict()					,      **lowercase_					,      )




     def        UpperCAmelCase_	(		self			:					Optional[int]	)						->     Optional[int]:
      UpperCAmelCase	:     str						 =			copy.deepcopy(self.__dict__	)
      UpperCAmelCase	:     Optional[Any]						 =			self.vision_config.to_dict()
      UpperCAmelCase	:     Union[str, Any]						 =			self.qformer_config.to_dict()
      UpperCAmelCase	:     List[Any]						 =			self.text_config.to_dict()
      UpperCAmelCase	:     Optional[int]						 =			self.__class__.model_type
      return output







'''simple docstring'''

import copy
from typing import Dict, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING
from ..detr import DetrConfig
from ..swin import SwinConfig


lowercase__         =      {
    "facebook/maskformer-swin-base-ade": (
        "https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json"
    )
    # See all MaskFormer models at https://huggingface.co/models?filter=maskformer
}

lowercase__         =      logging.get_logger(__name__)




class 	A_			(    _snake_case							):






     '''simple docstring'''



     UpperCAmelCase_						:     Any  		=  """maskformer"""
     UpperCAmelCase_						:     Union[str, Any]  		=  {"""hidden_size""": """mask_feature_size"""}
     UpperCAmelCase_						:     Union[str, Any]  		=  ["""resnet""", """swin"""]
     UpperCAmelCase_						:     Optional[int]  		=  ["""detr"""]


     def __init__(		self			:					Union[str, Any]					,      lowercase_			:					int = 256					,      lowercase_			:					int = 256					,      lowercase_			:					float = 0.1					,      lowercase_			:					bool = False					,      lowercase_			:					Optional[Dict] = None					,      lowercase_			:					Optional[Dict] = None					,      lowercase_			:					float = 0.02					,      lowercase_			:					float = 1.0					,      lowercase_			:					float = 1.0					,      lowercase_			:					float = 1.0					,      lowercase_			:					float = 20.0					,      lowercase_			:					Optional[bool] = None					,      **lowercase_			:					int					,      )						->     Optional[Any]:
      if backbone_config is None:
       # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k
       UpperCAmelCase	:     List[Any]						 =			SwinConfig(
           image_size=384					,      in_channels=3					,      patch_size=4					,      embed_dim=128					,      depths=[2, 2, 18, 2]					,      num_heads=[4, 8, 16, 32]					,      window_size=12					,      drop_path_rate=0.3					,      out_features=['stage1', 'stage2', 'stage3', 'stage4']					,      )

      if isinstance(lowercase_					,      lowercase_	):
       UpperCAmelCase	:     List[Any]						 =			backbone_config.pop('model_type'	)
       UpperCAmelCase	:     Optional[Any]						 =			CONFIG_MAPPING[backbone_model_type]
       UpperCAmelCase	:     List[Any]						 =			config_class.from_dict(lowercase_	)

      # verify that the backbone is supported
      if backbone_config.model_type not in self.backbones_supported:
       logger.warning_once(
           f"""Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. """
           f"""Supported model types: {",".join(self.backbones_supported	)}"""	)

      if decoder_config is None:
       # fall back to https://huggingface.co/facebook/detr-resnet-50
       UpperCAmelCase	:     Dict						 =			DetrConfig()
      else:
       # verify that the decoder is supported
       UpperCAmelCase	:     Tuple						 =			(
           decoder_config.pop('model_type'	) if isinstance(lowercase_					,      lowercase_	) else decoder_config.model_type
       )
       if decoder_type not in self.decoders_supported:
        raise ValueError(
            f"""Transformer Decoder {decoder_type} not supported, please use one of"""
            f""" {",".join(self.decoders_supported	)}"""	)
       if isinstance(lowercase_					,      lowercase_	):
        UpperCAmelCase	:     Union[str, Any]						 =			CONFIG_MAPPING[decoder_type]
        UpperCAmelCase	:     str						 =			config_class.from_dict(lowercase_	)

      UpperCAmelCase	:     int						 =			backbone_config
      UpperCAmelCase	:     Tuple						 =			decoder_config
      # main feature dimension for the model
      UpperCAmelCase	:     Union[str, Any]						 =			fpn_feature_size
      UpperCAmelCase	:     Optional[Any]						 =			mask_feature_size
      # initializer
      UpperCAmelCase	:     Union[str, Any]						 =			init_std
      UpperCAmelCase	:     List[str]						 =			init_xavier_std
      # Hungarian matcher && loss
      UpperCAmelCase	:     int						 =			cross_entropy_weight
      UpperCAmelCase	:     Tuple						 =			dice_weight
      UpperCAmelCase	:     int						 =			mask_weight
      UpperCAmelCase	:     Any						 =			use_auxiliary_loss
      UpperCAmelCase	:     Tuple						 =			no_object_weight
      UpperCAmelCase	:     str						 =			output_auxiliary_logits

      UpperCAmelCase	:     Dict						 =			self.decoder_config.encoder_attention_heads
      UpperCAmelCase	:     Union[str, Any]						 =			self.decoder_config.num_hidden_layers
      super().__init__(**lowercase_	)


     @classmethod
     def        UpperCAmelCase_	(		cls			:					Any					,      lowercase_			:					PretrainedConfig					,      lowercase_			:					PretrainedConfig					,      **lowercase_			:					Optional[Any]	)						->     str:
      return cls(
          backbone_config=lowercase_					,      decoder_config=lowercase_					,      **lowercase_					,      )




     def        UpperCAmelCase_	(		self			:					Optional[Any]	)						->     Dict[str, any]:
      UpperCAmelCase	:     Optional[int]						 =			copy.deepcopy(self.__dict__	)
      UpperCAmelCase	:     List[str]						 =			self.backbone_config.to_dict()
      UpperCAmelCase	:     Any						 =			self.decoder_config.to_dict()
      UpperCAmelCase	:     Union[str, Any]						 =			self.__class__.model_type
      return output




