


import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
class 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					int					       =						['''image_processor''', '''tokenizer''']
      snake_case__    :					Optional[int]					       =						'''AutoImageProcessor'''
      snake_case__    :					int					       =						'''AutoTokenizer'''
      def __init__(	self    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Dict=None					,							SCREAMING_SNAKE_CASE__    :	List[Any]=None					,							**SCREAMING_SNAKE_CASE__    :	Tuple					)		->      Optional[Any]:
             a_ :		Optional[Any]							=  None
             if "feature_extractor" in kwargs:
                    warnings.warn(
                        'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                        ' instead.'					,							SCREAMING_SNAKE_CASE__					,							)
                    a_ :		Dict							=  kwargs.pop('feature_extractor'					)

             a_ :		List[str]							=  image_processor if image_processor is not None else feature_extractor
             if image_processor is None:
                    raise ValueError('You need to specify an `image_processor`.'					)
             if tokenizer is None:
                    raise ValueError('You need to specify a `tokenizer`.'					)

             super().__init__(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
             a_ :		Union[str, Any]							=  self.image_processor
             a_ :		Union[str, Any]							=  False
      def __call__(	self    :	Optional[Any]					,							*SCREAMING_SNAKE_CASE__    :	List[Any]					,							**SCREAMING_SNAKE_CASE__    :	Tuple					)		->      Optional[int]:
             # For backward compatibility
             if self._in_target_context_manager:
                    return self.current_processor(*SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)

             a_ :		Dict							=  kwargs.pop('images'					,							SCREAMING_SNAKE_CASE__					)
             a_ :		Optional[int]							=  kwargs.pop('text'					,							SCREAMING_SNAKE_CASE__					)
             if len(SCREAMING_SNAKE_CASE__					) > 0:
                    a_ :		Optional[Any]							=  args[0]
                    a_ :		int							=  args[1:]

             if images is None and text is None:
                    raise ValueError('You need to specify either an `images` or `text` input to process.'					)

             if images is not None:
                    a_ :		List[str]							=  self.image_processor(SCREAMING_SNAKE_CASE__					,							*SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)
             if text is not None:
                    a_ :		Dict							=  self.tokenizer(SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)

             if text is None:
                    return inputs
             elif images is None:
                    return encodings
             else:
                    a_ :		Optional[int]							=  encodings['input_ids']
                    return inputs
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					,							*SCREAMING_SNAKE_CASE__    :	Optional[Any]					,							**SCREAMING_SNAKE_CASE__    :	Optional[Any]					)		->      Optional[int]:
             return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	int					,							*SCREAMING_SNAKE_CASE__    :	List[Any]					,							**SCREAMING_SNAKE_CASE__    :	List[str]					)		->      Union[str, Any]:
             return self.tokenizer.decode(*SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)
      @contextmanager
      def 	SCREAMING_SNAKE_CASE							(	self    :	int					)		->      Optional[int]:
             warnings.warn(
                 '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
                 'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
                 'your images inputs, or in a separate call.'					)
             a_ :		Any							=  True
             a_ :		Optional[int]							=  self.tokenizer
             yield
             a_ :		List[Any]							=  self.image_processor
             a_ :		Optional[int]							=  False
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	Any=False					,							SCREAMING_SNAKE_CASE__    :	Dict=None					)		->      int:
             if added_vocab is None:
                    a_ :		Optional[Any]							=  self.tokenizer.get_added_vocab()

             a_ :		List[Any]							=  {}

             while tokens:
                    a_ :		int							=  re.search(r'<s_(.*?)>'					,							SCREAMING_SNAKE_CASE__					,							re.IGNORECASE					)
                    if start_token is None:
                           break
                    a_ :		Dict							=  start_token.group(1					)
                    a_ :		Union[str, Any]							=  re.search(rF"""</s_{key}>"""					,							SCREAMING_SNAKE_CASE__					,							re.IGNORECASE					)
                    a_ :		Any							=  start_token.group()
                    if end_token is None:
                           a_ :		str							=  tokens.replace(SCREAMING_SNAKE_CASE__					,							''					)
                    else:
                           a_ :		Any							=  end_token.group()
                           a_ :		Tuple							=  re.escape(SCREAMING_SNAKE_CASE__					)
                           a_ :		Union[str, Any]							=  re.escape(SCREAMING_SNAKE_CASE__					)
                           a_ :		int							=  re.search(F"""{start_token_escaped}(.*?){end_token_escaped}"""					,							SCREAMING_SNAKE_CASE__					,							re.IGNORECASE					)
                           if content is not None:
                                  a_ :		int							=  content.group(1					).strip()
                                  if r"<s_" in content and r"</s_" in content:  # non-leaf node
                                         a_ :		Optional[int]							=  self.tokenajson(SCREAMING_SNAKE_CASE__					,							is_inner_value=SCREAMING_SNAKE_CASE__					,							added_vocab=SCREAMING_SNAKE_CASE__					)
                                         if value:
                                                if len(SCREAMING_SNAKE_CASE__					) == 1:
                                                       a_ :		Optional[Any]							=  value[0]
                                                a_ :		int							=  value
                                  else:  # leaf nodes
                                         a_ :		Tuple							=  []
                                         for leaf in content.split(r'<sep/>'					):
                                                a_ :		Tuple							=  leaf.strip()
                                                if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
                                                       a_ :		Union[str, Any]							=  leaf[1:-2]  # for categorical special tokens
                                                output[key].append(SCREAMING_SNAKE_CASE__					)
                                         if len(output[key]					) == 1:
                                                a_ :		Dict							=  output[key][0]

                           a_ :		Dict							=  tokens[tokens.find(SCREAMING_SNAKE_CASE__					) + len(SCREAMING_SNAKE_CASE__					) :].strip()
                           if tokens[:6] == r"<sep/>":  # non-leaf nodes
                                  return [output] + self.tokenajson(tokens[6:]					,							is_inner_value=SCREAMING_SNAKE_CASE__					,							added_vocab=SCREAMING_SNAKE_CASE__					)

             if len(SCREAMING_SNAKE_CASE__					):
                    return [output] if is_inner_value else output
             else:
                    return [] if is_inner_value else {"text_sequence": tokens}
      @property
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					)		->      Tuple:
             warnings.warn(
                 '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'					,							SCREAMING_SNAKE_CASE__					,							)
             return self.image_processor_class

      @property
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[int]					)		->      Optional[int]:
             warnings.warn(
                 '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'					,							SCREAMING_SNAKE_CASE__					,							)
             return self.image_processor






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_				:			str   =	{
    'Salesforce/instruct-blip-flan-t5': 'https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json',
}
class 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					int					       =						'''instructblip_vision_model'''
      def __init__(	self    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	List[Any]=1_4_0_8					,							SCREAMING_SNAKE_CASE__    :	int=6_1_4_4					,							SCREAMING_SNAKE_CASE__    :	Dict=3_9					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=1_6					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=2_2_4					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=1_4					,							SCREAMING_SNAKE_CASE__    :	List[Any]="gelu"					,							SCREAMING_SNAKE_CASE__    :	List[str]=1E-6					,							SCREAMING_SNAKE_CASE__    :	Tuple=0.0					,							SCREAMING_SNAKE_CASE__    :	List[str]=1E-10					,							SCREAMING_SNAKE_CASE__    :	str=True					,							**SCREAMING_SNAKE_CASE__    :	Dict					,							)		->      int:
             super().__init__(**SCREAMING_SNAKE_CASE__					)

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

      @classmethod
      def 	SCREAMING_SNAKE_CASE							(	cls    :	Any					,							SCREAMING_SNAKE_CASE__    :	Union[str, os.PathLike]					,							**SCREAMING_SNAKE_CASE__    :	List[str]					)		->      "PretrainedConfig":
             cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE__					)

             a_							,		a_ :		Dict							=  cls.get_config_dict(SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)

             # get the vision config dict if we are loading from InstructBlipConfig
             if config_dict.get('model_type'					) == "instructblip":
                    a_ :		List[Any]							=  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 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					List[Any]					       =						'''instructblip_qformer'''
      def __init__(	self    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=3_0_5_2_2					,							SCREAMING_SNAKE_CASE__    :	str=7_6_8					,							SCREAMING_SNAKE_CASE__    :	List[str]=1_2					,							SCREAMING_SNAKE_CASE__    :	Any=1_2					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=3_0_7_2					,							SCREAMING_SNAKE_CASE__    :	List[str]="gelu"					,							SCREAMING_SNAKE_CASE__    :	Dict=0.1					,							SCREAMING_SNAKE_CASE__    :	int=0.1					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=5_1_2					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=0.02					,							SCREAMING_SNAKE_CASE__    :	int=1E-12					,							SCREAMING_SNAKE_CASE__    :	Dict=0					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]="absolute"					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=2					,							SCREAMING_SNAKE_CASE__    :	str=1_4_0_8					,							**SCREAMING_SNAKE_CASE__    :	Dict					,							)		->      Optional[Any]:
             super().__init__(pad_token_id=SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)

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

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

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

             # get the qformer config dict if we are loading from InstructBlipConfig
             if config_dict.get('model_type'					) == "instructblip":
                    a_ :		Dict							=  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 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					Union[str, Any]					       =						'''instructblip'''
      snake_case__    :					List[str]					       =						True
      def __init__(	self    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=None					,							SCREAMING_SNAKE_CASE__    :	Dict=None					,							SCREAMING_SNAKE_CASE__    :	List[Any]=None					,							SCREAMING_SNAKE_CASE__    :	int=3_2					,							**SCREAMING_SNAKE_CASE__    :	Union[str, Any]					)		->      Tuple:
             super().__init__(**SCREAMING_SNAKE_CASE__					)

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

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

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

             a_ :		int							=  InstructBlipVisionConfig(**SCREAMING_SNAKE_CASE__					)
             a_ :		int							=  InstructBlipQFormerConfig(**SCREAMING_SNAKE_CASE__					)
             a_ :		Optional[Any]							=  text_config['model_type'] if 'model_type' in text_config else 'opt'
             a_ :		List[Any]							=  CONFIG_MAPPING[text_model_type](**SCREAMING_SNAKE_CASE__					)

             a_ :		Tuple							=  self.text_config.tie_word_embeddings
             a_ :		List[Any]							=  self.text_config.is_encoder_decoder

             a_ :		Optional[Any]							=  num_query_tokens
             a_ :		int							=  self.vision_config.hidden_size
             a_ :		List[str]							=  self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
             a_ :		Optional[int]							=  1.0
             a_ :		Any							=  0.02
      @classmethod
      def 	SCREAMING_SNAKE_CASE							(	cls    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	InstructBlipVisionConfig					,							SCREAMING_SNAKE_CASE__    :	InstructBlipQFormerConfig					,							SCREAMING_SNAKE_CASE__    :	PretrainedConfig					,							**SCREAMING_SNAKE_CASE__    :	List[str]					,							)		->      Any:
             return cls(
                 vision_config=vision_config.to_dict()					,							qformer_config=qformer_config.to_dict()					,							text_config=text_config.to_dict()					,							**SCREAMING_SNAKE_CASE__					,							)

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


