



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


_lowerCAmelCase										=     {
    '''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
}

_lowerCAmelCase										=     logging.get_logger(__name__)






class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				List[Any]           =				'''maskformer'''
  __lowercase							:				Optional[Any]           =				{'''hidden_size''': '''mask_feature_size'''}
  __lowercase							:				Union[str, Any]           =				['''resnet''', '''swin''']
  __lowercase							:				int           =				['''detr''']




  def __init__(   self  ,__UpperCAmelCase = 256  ,__UpperCAmelCase = 256  ,__UpperCAmelCase = 0.1  ,__UpperCAmelCase = False  ,__UpperCAmelCase = None  ,__UpperCAmelCase = None  ,__UpperCAmelCase = 0.0_2  ,__UpperCAmelCase = 1.0  ,__UpperCAmelCase = 1.0  ,__UpperCAmelCase = 1.0  ,__UpperCAmelCase = 20.0  ,__UpperCAmelCase = None  ,**__UpperCAmelCase  ,)   ->		List[str]:
      if backbone_config is None:
          # fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k
          lowerCAmelCase__			:  Optional[int]											= 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(__UpperCAmelCase  ,__UpperCAmelCase				):
          lowerCAmelCase__			:  List[Any]											= backbone_config.pop("""model_type"""				)
          lowerCAmelCase__			:  Dict											= CONFIG_MAPPING[backbone_model_type]
          lowerCAmelCase__			:  List[Any]											= config_class.from_dict(__UpperCAmelCase				)

      # 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
          lowerCAmelCase__			:  int											= DetrConfig()
      else:
          # verify that the decoder is supported
          lowerCAmelCase__			:  str											= (
              decoder_config.pop("""model_type"""				) if isinstance(__UpperCAmelCase  ,__UpperCAmelCase				) 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(__UpperCAmelCase  ,__UpperCAmelCase				):
              lowerCAmelCase__			:  Optional[Any]											= CONFIG_MAPPING[decoder_type]
              lowerCAmelCase__			:  Optional[int]											= config_class.from_dict(__UpperCAmelCase				)

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

      lowerCAmelCase__			:  str											= self.decoder_config.encoder_attention_heads
      lowerCAmelCase__			:  Any											= self.decoder_config.num_hidden_layers
      super().__init__(**__UpperCAmelCase				)




  @classmethod
  def 							UpperCAmelCase_     (   cls  ,__UpperCAmelCase  ,__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		Optional[int]:
      return cls(
          backbone_config=__UpperCAmelCase  ,decoder_config=__UpperCAmelCase  ,**__UpperCAmelCase  ,)






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








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


_lowerCAmelCase										=     logging.get_logger(__name__)

_lowerCAmelCase										=     {
    '''Salesforce/instruct-blip-flan-t5''': '''https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json''',
}






class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Optional[Any]           =				'''instructblip_vision_model'''




  def __init__(   self  ,__UpperCAmelCase=1408  ,__UpperCAmelCase=6144  ,__UpperCAmelCase=39  ,__UpperCAmelCase=16  ,__UpperCAmelCase=224  ,__UpperCAmelCase=14  ,__UpperCAmelCase="gelu"  ,__UpperCAmelCase=1E-6  ,__UpperCAmelCase=0.0  ,__UpperCAmelCase=1E-10  ,__UpperCAmelCase=True  ,**__UpperCAmelCase  ,)   ->		List[Any]:
      super().__init__(**__UpperCAmelCase				)

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






  @classmethod
  def 							UpperCAmelCase_     (   cls  ,__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		"PretrainedConfig":
      cls._set_token_in_kwargs(__UpperCAmelCase				)

      lowerCAmelCase__				,       lowerCAmelCase__			:  Tuple											= cls.get_config_dict(__UpperCAmelCase  ,**__UpperCAmelCase				)

      # get the vision config dict if we are loading from InstructBlipConfig
      if config_dict.get("""model_type"""				) == "instructblip":
          lowerCAmelCase__			:  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(__UpperCAmelCase  ,**__UpperCAmelCase				)







class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				int           =				'''instructblip_qformer'''




  def __init__(   self  ,__UpperCAmelCase=3_0522  ,__UpperCAmelCase=768  ,__UpperCAmelCase=12  ,__UpperCAmelCase=12  ,__UpperCAmelCase=3072  ,__UpperCAmelCase="gelu"  ,__UpperCAmelCase=0.1  ,__UpperCAmelCase=0.1  ,__UpperCAmelCase=512  ,__UpperCAmelCase=0.0_2  ,__UpperCAmelCase=1E-12  ,__UpperCAmelCase=0  ,__UpperCAmelCase="absolute"  ,__UpperCAmelCase=2  ,__UpperCAmelCase=1408  ,**__UpperCAmelCase  ,)   ->		Tuple:
      super().__init__(pad_token_id=__UpperCAmelCase  ,**__UpperCAmelCase				)

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






  @classmethod
  def 							UpperCAmelCase_     (   cls  ,__UpperCAmelCase  ,**__UpperCAmelCase				)   ->		"PretrainedConfig":
      cls._set_token_in_kwargs(__UpperCAmelCase				)

      lowerCAmelCase__				,       lowerCAmelCase__			:  Any											= cls.get_config_dict(__UpperCAmelCase  ,**__UpperCAmelCase				)

      # get the qformer config dict if we are loading from InstructBlipConfig
      if config_dict.get("""model_type"""				) == "instructblip":
          lowerCAmelCase__			:  Tuple											= 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(__UpperCAmelCase  ,**__UpperCAmelCase				)







class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Any           =				'''instructblip'''
  __lowercase							:				str           =				True




  def __init__(   self  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,__UpperCAmelCase=32  ,**__UpperCAmelCase				)   ->		Any:
      super().__init__(**__UpperCAmelCase				)

      if vision_config is None:
          lowerCAmelCase__			:  Any											= {}
          logger.info("""vision_config is None. initializing the InstructBlipVisionConfig with default values."""				)

      if qformer_config is None:
          lowerCAmelCase__			:  List[str]											= {}
          logger.info("""qformer_config is None. Initializing the InstructBlipQFormerConfig with default values."""				)

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

      lowerCAmelCase__			:  Any											= InstructBlipVisionConfig(**__UpperCAmelCase				)
      lowerCAmelCase__			:  Tuple											= InstructBlipQFormerConfig(**__UpperCAmelCase				)
      lowerCAmelCase__			:  Tuple											= text_config["""model_type"""] if """model_type""" in text_config else """opt"""
      lowerCAmelCase__			:  Any											= CONFIG_MAPPING[text_model_type](**__UpperCAmelCase				)

      lowerCAmelCase__			:  Union[str, Any]											= self.text_config.tie_word_embeddings
      lowerCAmelCase__			:  Any											= self.text_config.is_encoder_decoder

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




  @classmethod
  def 							UpperCAmelCase_     (   cls  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,**__UpperCAmelCase  ,)   ->		Union[str, Any]:
      return cls(
          vision_config=vision_config.to_dict()  ,qformer_config=qformer_config.to_dict()  ,text_config=text_config.to_dict()  ,**__UpperCAmelCase  ,)






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


