

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


A_       						=							logging.get_logger(__name__)

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





class 							_snake_case (       _a						):
   _A       :     List[Any]       =					'''blip_2_vision_model'''
   def __init__( self			:      Optional[int]     ,SCREAMING_SNAKE_CASE__			:      Optional[Any]=1_408     ,SCREAMING_SNAKE_CASE__			:      str=6_144     ,SCREAMING_SNAKE_CASE__			:      Optional[Any]=39     ,SCREAMING_SNAKE_CASE__			:      Dict=16     ,SCREAMING_SNAKE_CASE__			:      Optional[int]=224     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]=14     ,SCREAMING_SNAKE_CASE__			:      Dict="gelu"     ,SCREAMING_SNAKE_CASE__			:      List[Any]=0.00_001     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]=0.0     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]=1e-10     ,SCREAMING_SNAKE_CASE__			:      List[Any]=True     ,**SCREAMING_SNAKE_CASE__			:      Tuple     ,):
     super().__init__(**SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:Any						      =					hidden_size
     SCREAMING_SNAKE_CASE:List[str]						      =					intermediate_size
     SCREAMING_SNAKE_CASE:str						      =					num_hidden_layers
     SCREAMING_SNAKE_CASE:str						      =					num_attention_heads
     SCREAMING_SNAKE_CASE:Any						      =					patch_size
     SCREAMING_SNAKE_CASE:int						      =					image_size
     SCREAMING_SNAKE_CASE:Tuple						      =					initializer_range
     SCREAMING_SNAKE_CASE:Dict						      =					attention_dropout
     SCREAMING_SNAKE_CASE:Any						      =					layer_norm_eps
     SCREAMING_SNAKE_CASE:Tuple						      =					hidden_act
     SCREAMING_SNAKE_CASE:List[str]						      =					qkv_bias







   @classmethod
   def     __UpperCamelCase						( cls			:      int     ,SCREAMING_SNAKE_CASE__			:      Union[str, os.PathLike]     ,**SCREAMING_SNAKE_CASE__			:      Dict							):
     cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE      ,    SCREAMING_SNAKE_CASE:List[str]						      =					cls.get_config_dict(SCREAMING_SNAKE_CASE__     ,**SCREAMING_SNAKE_CASE__							)

     # get the vision config dict if we are loading from Blip2Config
     if config_dict.get("model_type"							) == "blip-2":
       SCREAMING_SNAKE_CASE: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 							_snake_case (       _a						):
   _A       :     int       =					'''blip_2_qformer'''
   def __init__( self			:      Any     ,SCREAMING_SNAKE_CASE__			:      str=30_522     ,SCREAMING_SNAKE_CASE__			:      Optional[Any]=768     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]=12     ,SCREAMING_SNAKE_CASE__			:      Tuple=12     ,SCREAMING_SNAKE_CASE__			:      int=3_072     ,SCREAMING_SNAKE_CASE__			:      Tuple="gelu"     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]=0.1     ,SCREAMING_SNAKE_CASE__			:      Tuple=0.1     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]=512     ,SCREAMING_SNAKE_CASE__			:      List[str]=0.02     ,SCREAMING_SNAKE_CASE__			:      List[str]=1e-12     ,SCREAMING_SNAKE_CASE__			:      Optional[int]=0     ,SCREAMING_SNAKE_CASE__			:      Dict="absolute"     ,SCREAMING_SNAKE_CASE__			:      List[str]=2     ,SCREAMING_SNAKE_CASE__			:      List[str]=1_408     ,**SCREAMING_SNAKE_CASE__			:      Any     ,):
     super().__init__(pad_token_id=SCREAMING_SNAKE_CASE__     ,**SCREAMING_SNAKE_CASE__							)

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







   @classmethod
   def     __UpperCamelCase						( cls			:      int     ,SCREAMING_SNAKE_CASE__			:      Union[str, os.PathLike]     ,**SCREAMING_SNAKE_CASE__			:      Dict							):
     cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE      ,    SCREAMING_SNAKE_CASE:Union[str, Any]						      =					cls.get_config_dict(SCREAMING_SNAKE_CASE__     ,**SCREAMING_SNAKE_CASE__							)

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





class 							_snake_case (       _a						):
   _A       :     Dict       =					'''blip-2'''
   _A       :     int       =					True
   def __init__( self			:      Optional[int]     ,SCREAMING_SNAKE_CASE__			:      Optional[int]=None     ,SCREAMING_SNAKE_CASE__			:      int=None     ,SCREAMING_SNAKE_CASE__			:      List[Any]=None     ,SCREAMING_SNAKE_CASE__			:      Optional[int]=32     ,**SCREAMING_SNAKE_CASE__			:      Union[str, Any]							):
     super().__init__(**SCREAMING_SNAKE_CASE__							)

     if vision_config is None:
       SCREAMING_SNAKE_CASE:Optional[int]						      =					{}
       logger.info("vision_config is None. initializing the Blip2VisionConfig with default values."							)

     if qformer_config is None:
       SCREAMING_SNAKE_CASE:Optional[int]						      =					{}
       logger.info("qformer_config is None. Initializing the Blip2QFormerConfig with default values."							)

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

     SCREAMING_SNAKE_CASE:Union[str, Any]						      =					BlipaVisionConfig(**SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Any						      =					BlipaQFormerConfig(**SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:List[str]						      =					text_config["model_type"] if "model_type" in text_config else "opt"
     SCREAMING_SNAKE_CASE:Optional[int]						      =					CONFIG_MAPPING[text_model_type](**SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:Optional[int]						      =					self.text_config.tie_word_embeddings
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					self.text_config.is_encoder_decoder

     SCREAMING_SNAKE_CASE:Dict						      =					num_query_tokens
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					self.vision_config.hidden_size
     SCREAMING_SNAKE_CASE:str						      =					self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
     SCREAMING_SNAKE_CASE:List[str]						      =					1.0
     SCREAMING_SNAKE_CASE:Dict						      =					0.02
   @classmethod
   def     __UpperCamelCase						( cls			:      Any     ,SCREAMING_SNAKE_CASE__			:      BlipaVisionConfig     ,SCREAMING_SNAKE_CASE__			:      BlipaQFormerConfig     ,SCREAMING_SNAKE_CASE__			:      PretrainedConfig     ,**SCREAMING_SNAKE_CASE__			:      Tuple     ,):
     return cls(
         vision_config=vision_config.to_dict()     ,qformer_config=qformer_config.to_dict()     ,text_config=text_config.to_dict()     ,**SCREAMING_SNAKE_CASE__     ,)







   def     __UpperCamelCase						( self			:      Union[str, Any]							):
     SCREAMING_SNAKE_CASE:str						      =					copy.deepcopy(self.__dict__							)
     SCREAMING_SNAKE_CASE:Union[str, Any]						      =					self.vision_config.to_dict()
     SCREAMING_SNAKE_CASE:Dict						      =					self.qformer_config.to_dict()
     SCREAMING_SNAKE_CASE:int						      =					self.text_config.to_dict()
     SCREAMING_SNAKE_CASE:Optional[int]						      =					self.__class__.model_type
     return output









'''simple docstring'''

import inspect
import re
from hashlib import shaaaa
from typing import Dict, List

from .arrow import arrow
from .audiofolder import audiofolder
from .csv import csv
from .imagefolder import imagefolder
from .json import json
from .pandas import pandas
from .parquet import parquet
from .sql import sql  # noqa F401
from .text import text





def    A_  ( snake_case ):
  SCREAMING_SNAKE_CASE:List[Any]						      =					[]
  for line in lines:
    SCREAMING_SNAKE_CASE:Any						      =					re.sub(r"#.*"      ,       ""      ,       snake_case )  # remove comments
    if line:
      filtered_lines.append(snake_case )
  SCREAMING_SNAKE_CASE:int						      =					"\n".join(snake_case )

  # Make a hash from all this code
  SCREAMING_SNAKE_CASE:str						      =					full_str.encode("utf-8" )
  return shaaaa(snake_case ).hexdigest()


# get importable module names and hash for caching
A_       						=							{
    "csv": (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())),
    "json": (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())),
    "pandas": (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())),
    "parquet": (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())),
    "arrow": (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())),
    "text": (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())),
    "imagefolder": (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())),
    "audiofolder": (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())),
}

# Used to infer the module to use based on the data files extensions
A_       						=							{
    ".csv": ("csv", {}),
    ".tsv": ("csv", {"sep": "\t"}),
    ".json": ("json", {}),
    ".jsonl": ("json", {}),
    ".parquet": ("parquet", {}),
    ".arrow": ("arrow", {}),
    ".txt": ("text", {}),
}
_EXTENSION_TO_MODULE.update({ext: ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext: ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
A_       						=							{"imagefolder", "audiofolder"}

# Used to filter data files based on extensions given a module name
A_       						=							{}
for _ext, (_module, _) in _EXTENSION_TO_MODULE.items():
    _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext)

_MODULE_TO_EXTENSIONS["imagefolder"].append(".zip")


_MODULE_TO_EXTENSIONS["audiofolder"].append(".zip")






