




from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
						from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


UpperCamelCase__ :   str						=							logging.get_logger(__name__)

UpperCamelCase__ :   Any						=							{
    '''bigscience/bloom''': '''https://huggingface.co/bigscience/bloom/resolve/main/config.json''',
    '''bigscience/bloom-560m''': '''https://huggingface.co/bigscience/bloom-560m/blob/main/config.json''',
    '''bigscience/bloom-1b1''': '''https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json''',
    '''bigscience/bloom-1b7''': '''https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json''',
    '''bigscience/bloom-3b''': '''https://huggingface.co/bigscience/bloom-3b/blob/main/config.json''',
    '''bigscience/bloom-7b1''': '''https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json''',
}







class 				lowerCAmelCase_       (					lowerCamelCase_							):
				__a     :       Dict						=     "bloom"
				__a     :       Any						=     ["past_key_values"]
				__a     :       Union[str, Any]						=     {
				    "num_hidden_layers": "n_layer",
				    "num_attention_heads": "n_head",
				}






				def __init__(			self      ,snake_case__=250880      ,snake_case__=64      ,snake_case__=2      ,snake_case__=8      ,snake_case__=1E-5      ,snake_case__=0.02      ,snake_case__=True      ,snake_case__=1      ,snake_case__=2      ,snake_case__=False      ,snake_case__=0.0      ,snake_case__=0.0      ,snake_case__=1      ,snake_case__=False      ,**snake_case__      ,):
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      vocab_size
							# Backward compatibility with n_embed kwarg
							SCREAMING_SNAKE_CASE_   : Tuple					  =      kwargs.pop('n_embed'      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : int					  =      hidden_size if n_embed is None else n_embed
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      n_layer
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      n_head
							SCREAMING_SNAKE_CASE_   : List[str]					  =      layer_norm_epsilon
							SCREAMING_SNAKE_CASE_   : Tuple					  =      initializer_range
							SCREAMING_SNAKE_CASE_   : List[str]					  =      use_cache
							SCREAMING_SNAKE_CASE_   : int					  =      pretraining_tp
							SCREAMING_SNAKE_CASE_   : int					  =      apply_residual_connection_post_layernorm
							SCREAMING_SNAKE_CASE_   : Tuple					  =      hidden_dropout
							SCREAMING_SNAKE_CASE_   : List[str]					  =      attention_dropout

							SCREAMING_SNAKE_CASE_   : str					  =      bos_token_id
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      eos_token_id
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      slow_but_exact

							super().__init__(bos_token_id=snake_case__      ,eos_token_id=snake_case__      ,**snake_case__ )







class 				lowerCAmelCase_       (					lowerCamelCase_							):
				__a     :       List[Any]						=     version.parse("1.12"							)






				def __init__(			self      ,snake_case__      ,snake_case__ = "default"      ,snake_case__ = None      ,snake_case__ = False      ,):
							super().__init__(snake_case__      ,task=snake_case__      ,patching_specs=snake_case__      ,use_past=snake_case__ )
							if not getattr(self._config      ,'pad_token_id'      ,snake_case__ ):
										# TODO: how to do that better?
										SCREAMING_SNAKE_CASE_   : str					  =      0






				@property
				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Dict					  =      OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}} )
							if self.use_past:
										# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
										self.fill_with_past_key_values_(snake_case__      ,direction='inputs'      ,inverted_values_shape=snake_case__ )
										SCREAMING_SNAKE_CASE_   : Optional[int]					  =      {0: 'batch', 1: 'past_sequence + sequence'}
							else:
										SCREAMING_SNAKE_CASE_   : int					  =      {0: 'batch', 1: 'sequence'}

							return common_inputs






				@property
				def 		snake_case	(			self ):
							return self._config.n_layer






				@property
				def 		snake_case	(			self ):
							return self._config.n_head






				@property
				def 		snake_case	(			self ):
							return 1E-3






				def 		snake_case	(			self      ,snake_case__      ,snake_case__ = -1      ,snake_case__ = -1      ,snake_case__ = False      ,snake_case__ = None      ,):
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      super(snake_case__      ,self ).generate_dummy_inputs(
							    snake_case__      ,batch_size=snake_case__      ,seq_length=snake_case__      ,is_pair=snake_case__      ,framework=snake_case__ )

							# We need to order the input in the way they appears in the forward()
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      OrderedDict({'input_ids': common_inputs['input_ids']} )

							# Need to add the past_keys
							if self.use_past:
										if not is_torch_available():
													raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' )
										else:
													import torch

													SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : List[Any]					  =      common_inputs['input_ids'].shape
													# Not using the same length for past_key_values
													SCREAMING_SNAKE_CASE_   : List[Any]					  =      seqlen + 2
													SCREAMING_SNAKE_CASE_   : Any					  =      self._config.hidden_size // self.num_attention_heads
													SCREAMING_SNAKE_CASE_   : List[str]					  =      (
													    batch * self.num_attention_heads,
													    head_dim,
													    past_key_values_length,
													)
													SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      (
													    batch * self.num_attention_heads,
													    past_key_values_length,
													    head_dim,
													)
													SCREAMING_SNAKE_CASE_   : Dict					  =      [
													    (torch.zeros(snake_case__ ), torch.zeros(snake_case__ )) for _ in range(self.num_layers )
													]

							SCREAMING_SNAKE_CASE_   : Tuple					  =      common_inputs['attention_mask']
							if self.use_past:
										SCREAMING_SNAKE_CASE_   : Any					  =      ordered_inputs['attention_mask'].dtype
										SCREAMING_SNAKE_CASE_   : List[Any]					  =      torch.cat(
										    [ordered_inputs['attention_mask'], torch.ones(snake_case__      ,snake_case__      ,dtype=snake_case__ )]      ,dim=1 )

							return ordered_inputs






				@property
				def 		snake_case	(			self ):
							return 13









from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
						from PIL import Image

						from ..image_utils import load_image

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

UpperCamelCase__ :   List[str]						=							logging.get_logger(__name__)







@add_end_docstrings(lowerCamelCase_							)
class 				lowerCAmelCase_       (					lowerCamelCase_							):






				def __init__(			self      ,*snake_case__      ,**snake_case__ ):
							super().__init__(*snake_case__      ,**snake_case__ )
							self.check_model_type(snake_case__ )






				def 		snake_case	(			self      ,snake_case__=None      ,snake_case__=None      ,snake_case__=None      ,**snake_case__ ):
							SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      {}, {}
							if padding is not None:
										SCREAMING_SNAKE_CASE_   : Any					  =      padding
							if truncation is not None:
										SCREAMING_SNAKE_CASE_   : Tuple					  =      truncation
							if top_k is not None:
										SCREAMING_SNAKE_CASE_   : int					  =      top_k
							return preprocess_params, {}, postprocess_params






				def __call__(			self      ,snake_case__      ,snake_case__ = None      ,**snake_case__ ):
							if isinstance(snake_case__      ,(Image.Image, str) ) and isinstance(snake_case__      ,snake_case__ ):
										SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      {'image': image, 'question': question}
							else:
										SCREAMING_SNAKE_CASE_   : Optional[int]					  =      image
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      super().__call__(snake_case__      ,**snake_case__ )
							return results






				def 		snake_case	(			self      ,snake_case__      ,snake_case__=False      ,snake_case__=False ):
							SCREAMING_SNAKE_CASE_   : List[str]					  =      load_image(inputs['image'] )
							SCREAMING_SNAKE_CASE_   : Dict					  =      self.tokenizer(
							    inputs['question']      ,return_tensors=self.framework      ,padding=snake_case__      ,truncation=snake_case__ )
							SCREAMING_SNAKE_CASE_   : Any					  =      self.image_processor(images=snake_case__      ,return_tensors=self.framework )
							model_inputs.update(snake_case__ )
							return model_inputs






				def 		snake_case	(			self      ,snake_case__ ):
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      self.model(**snake_case__ )
							return model_outputs






				def 		snake_case	(			self      ,snake_case__      ,snake_case__=5 ):
							if top_k > self.model.config.num_labels:
										SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      self.model.config.num_labels

							if self.framework == "pt":
										SCREAMING_SNAKE_CASE_   : Any					  =      model_outputs.logits.sigmoid()[0]
										SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Dict					  =      probs.topk(snake_case__ )
							else:
										raise ValueError(F'Unsupported framework: {self.framework}' )

							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      scores.tolist()
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      ids.tolist()
							return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(snake_case__      ,snake_case__ )]



