
'''simple docstring'''







import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
					import tensorflow as tf

					from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
					from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

lowercase    :							List[Any]      =					logging.get_logger(__name__)


class 						__UpperCAmelCase      (					enum.Enum						):
							__lowercase      		=       0
							__lowercase      		=       1


@add_end_docstrings(__lowerCamelCase						)
class 						__UpperCAmelCase      (					__lowerCamelCase						):
							__lowercase      		=       """generated"""


							def __init__(       self    ,   *lowerCAmelCase_    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								super().__init__(*lowerCAmelCase_    ,   **lowerCAmelCase_  )

								self.check_model_type(
								    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
								    if self.framework == 'tf'
								    else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING  )


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_=None    ,   **lowerCAmelCase_    ,   ):





								"""simple docstring"""






								_snake_case   						=		{}
								if truncation is not None:
									_snake_case   						=		truncation

								_snake_case   						=		generate_kwargs

								_snake_case   						=		{}
								if return_tensors is not None and return_type is None:
									_snake_case   						=		ReturnType.TENSORS if return_tensors else ReturnType.TEXT
								if return_type is not None:
									_snake_case   						=		return_type

								if clean_up_tokenization_spaces is not None:
									_snake_case   						=		clean_up_tokenization_spaces

								if stop_sequence is not None:
									_snake_case   						=		self.tokenizer.encode(lowerCAmelCase_    ,   add_special_tokens=lowerCAmelCase_  )
									if len(lowerCAmelCase_  ) > 1:
										warnings.warn(
										    'Stopping on a multiple token sequence is not yet supported on transformers. The first token of'
										    ' the stop sequence will be used as the stop sequence string in the interim.'  )
									_snake_case   						=		stop_sequence_ids[0]

								return preprocess_params, forward_params, postprocess_params


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								return True


							def  lowerCamelCase				(       self    ,   *lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		self.model.config.prefix if self.model.config.prefix is not None else ''
								if isinstance(args[0]    ,   lowerCAmelCase_  ):
									if self.tokenizer.pad_token_id is None:
										raise ValueError('Please make sure that the tokenizer has a pad_token_id when using a batch input'  )
									_snake_case   						=		([prefix + arg for arg in args[0]],)
									_snake_case   						=		True

								elif isinstance(args[0]    ,   lowerCAmelCase_  ):
									_snake_case   						=		(prefix + args[0],)
									_snake_case   						=		False
								else:
									raise ValueError(
									    F' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`'  )
								_snake_case   						=		self.tokenizer(*lowerCAmelCase_    ,   padding=lowerCAmelCase_    ,   truncation=lowerCAmelCase_    ,   return_tensors=self.framework  )
								# This is produced by tokenizers but is an invalid generate kwargs
								if "token_type_ids" in inputs:
									del inputs["token_type_ids"]
								return inputs


							def __call__(       self    ,   *lowerCAmelCase_    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		super().__call__(*lowerCAmelCase_    ,   **lowerCAmelCase_  )
								if (
								    isinstance(args[0]    ,   lowerCAmelCase_  )
								    and all(isinstance(lowerCAmelCase_    ,   lowerCAmelCase_  ) for el in args[0]  )
								    and all(len(lowerCAmelCase_  ) == 1 for res in result  )
								):
									return [res[0] for res in result]
								return result


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_=TruncationStrategy.DO_NOT_TRUNCATE    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		self._parse_and_tokenize(lowerCAmelCase_    ,   truncation=lowerCAmelCase_    ,   **lowerCAmelCase_  )
								return inputs


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								if self.framework == "pt":
									_snake_case						, _snake_case   						=		model_inputs['input_ids'].shape
								elif self.framework == "tf":
									_snake_case						, _snake_case   						=		tf.shape(model_inputs['input_ids']  ).numpy()

								_snake_case   						=		generate_kwargs.get('min_length'    ,   self.model.config.min_length  )
								_snake_case   						=		generate_kwargs.get('max_length'    ,   self.model.config.max_length  )
								self.check_inputs(lowerCAmelCase_    ,   generate_kwargs['min_length']    ,   generate_kwargs['max_length']  )
								_snake_case   						=		self.model.generate(**lowerCAmelCase_    ,   **lowerCAmelCase_  )
								_snake_case   						=		output_ids.shape[0]
								if self.framework == "pt":
									_snake_case   						=		output_ids.reshape(lowerCAmelCase_    ,   out_b // in_b    ,   *output_ids.shape[1:]  )
								elif self.framework == "tf":
									_snake_case   						=		tf.reshape(lowerCAmelCase_    ,   (in_b, out_b // in_b, *output_ids.shape[1:])  )
								return {"output_ids": output_ids}




							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_=ReturnType.TEXT    ,   lowerCAmelCase_=False  ):





								"""simple docstring"""






								_snake_case   						=		[]
								for output_ids in model_outputs["output_ids"][0]:
									if return_type == ReturnType.TENSORS:
										_snake_case   						=		{F'{self.return_name}_token_ids': output_ids}
									elif return_type == ReturnType.TEXT:
										_snake_case   						=		{
										    F'{self.return_name}_text': self.tokenizer.decode(
										        lowerCAmelCase_    ,   skip_special_tokens=lowerCAmelCase_    ,   clean_up_tokenization_spaces=lowerCAmelCase_    ,   )
										}
									records.append(lowerCAmelCase_  )
								return records


@add_end_docstrings(__lowerCamelCase						)
class 						__UpperCAmelCase      (					__lowerCamelCase						):
							__lowercase      		=       """summary"""


							def __call__(       self    ,   *lowerCAmelCase_    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								return super().__call__(*lowerCAmelCase_    ,   **lowerCAmelCase_  )




							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								if max_length < min_length:
									logger.warning(F'Your min_length={min_length} must be inferior than your max_length={max_length}.'  )

								if input_length < max_length:
									logger.warning(
									    F'Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is '
									    'a summarization task, where outputs shorter than the input are typically wanted, you might '
									    F'consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})'  )


@add_end_docstrings(__lowerCamelCase						)
class 						__UpperCAmelCase      (					__lowerCamelCase						):
							__lowercase      		=       """translation"""


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								if input_length > 0.9 * max_length:
									logger.warning(
									    F'Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider '
									    'increasing your max_length manually, e.g. translator(\'...\', max_length=400)'  )
								return True


							def  lowerCamelCase				(       self    ,   *lowerCAmelCase_    ,   lowerCAmelCase_=TruncationStrategy.DO_NOT_TRUNCATE    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_=None  ):





								"""simple docstring"""






								if getattr(self.tokenizer    ,   '_build_translation_inputs'    ,   lowerCAmelCase_  ):
									return self.tokenizer._build_translation_inputs(
									    *lowerCAmelCase_    ,   return_tensors=self.framework    ,   truncation=lowerCAmelCase_    ,   src_lang=lowerCAmelCase_    ,   tgt_lang=lowerCAmelCase_  )
								else:
									return super()._parse_and_tokenize(*lowerCAmelCase_    ,   truncation=lowerCAmelCase_  )


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_=None    ,   lowerCAmelCase_=None    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case						, _snake_case						, _snake_case   						=		super()._sanitize_parameters(**lowerCAmelCase_  )
								if src_lang is not None:
									_snake_case   						=		src_lang
								if tgt_lang is not None:
									_snake_case   						=		tgt_lang
								if src_lang is None and tgt_lang is None:
									# Backward compatibility, direct arguments use is preferred.
									_snake_case   						=		kwargs.get('task'    ,   self.task  )
									_snake_case   						=		task.split('_'  )
									if task and len(lowerCAmelCase_  ) == 4:
										# translation, XX, to YY
										_snake_case   						=		items[1]
										_snake_case   						=		items[3]
								return preprocess_params, forward_params, postprocess_params




							def __call__(       self    ,   *lowerCAmelCase_    ,   **lowerCAmelCase_  ):





								"""simple docstring"""






								return super().__call__(*lowerCAmelCase_    ,   **lowerCAmelCase_  )



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	      = logging.get_logger(__name__)

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







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     Optional[Any]							 =       """blip_2_vision_model"""





	def __init__(       self		,    snake_case=1408		,    snake_case=6144		,    snake_case=39		,    snake_case=16		,    snake_case=224		,    snake_case=14		,    snake_case="gelu"		,    snake_case=0.00_001		,    snake_case=0.0		,    snake_case=1E-10		,    snake_case=True		,    **snake_case		,    ):
		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 	SCREAMING_SNAKE_CASE__						(       cls		,    snake_case		,    **snake_case       ):
		cls._set_token_in_kwargs(snake_case       )

		lowercase  ,					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        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     int							 =       """blip_2_qformer"""





	def __init__(       self		,    snake_case=3_0522		,    snake_case=768		,    snake_case=12		,    snake_case=12		,    snake_case=3072		,    snake_case="gelu"		,    snake_case=0.1		,    snake_case=0.1		,    snake_case=512		,    snake_case=0.02		,    snake_case=1E-12		,    snake_case=0		,    snake_case="absolute"		,    snake_case=2		,    snake_case=1408		,    **snake_case		,    ):
		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 	SCREAMING_SNAKE_CASE__						(       cls		,    snake_case		,    **snake_case       ):
		cls._set_token_in_kwargs(snake_case       )

		lowercase  ,					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        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     List[Any]							 =       """blip-2"""
	_UpperCamelCase     :     str							 =       True





	def __init__(       self		,    snake_case=None		,    snake_case=None		,    snake_case=None		,    snake_case=32		,    **snake_case       ):
		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 	SCREAMING_SNAKE_CASE__						(       cls		,    snake_case		,    snake_case		,    snake_case		,    **snake_case		,    ):
		return cls(
		    vision_config=vision_config.to_dict()		,    qformer_config=qformer_config.to_dict()		,    text_config=text_config.to_dict()		,    **snake_case		,    )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		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

