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

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


lowerCamelCase  =logging.get_logger(__name__)


lowerCamelCase  ={
    "Salesforce/codegen-350M-nl": "https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json",
    "Salesforce/codegen-350M-multi": "https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json",
    "Salesforce/codegen-350M-mono": "https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json",
    "Salesforce/codegen-2B-nl": "https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json",
    "Salesforce/codegen-2B-multi": "https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json",
    "Salesforce/codegen-2B-mono": "https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json",
    "Salesforce/codegen-6B-nl": "https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json",
    "Salesforce/codegen-6B-multi": "https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json",
    "Salesforce/codegen-6B-mono": "https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json",
    "Salesforce/codegen-16B-nl": "https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json",
    "Salesforce/codegen-16B-multi": "https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json",
    "Salesforce/codegen-16B-mono": "https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json",
}




class 					_lowerCamelCase			(   UpperCamelCase_			):





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		 =	'''codegen'''
     SCREAMING_SNAKE_CASE_		 =	{
         '''max_position_embeddings''': '''n_positions''',
         '''hidden_size''': '''n_embd''',
         '''num_attention_heads''': '''n_head''',
         '''num_hidden_layers''': '''n_layer''',
     }






     def __init__(				self    ,    __SCREAMING_SNAKE_CASE=5_0_4_0_0    ,    __SCREAMING_SNAKE_CASE=2_0_4_8    ,    __SCREAMING_SNAKE_CASE=2_0_4_8    ,    __SCREAMING_SNAKE_CASE=4_0_9_6    ,    __SCREAMING_SNAKE_CASE=2_8    ,    __SCREAMING_SNAKE_CASE=1_6    ,    __SCREAMING_SNAKE_CASE=6_4    ,    __SCREAMING_SNAKE_CASE=None    ,    __SCREAMING_SNAKE_CASE="gelu_new"    ,    __SCREAMING_SNAKE_CASE=0.0    ,    __SCREAMING_SNAKE_CASE=0.0    ,    __SCREAMING_SNAKE_CASE=0.0    ,    __SCREAMING_SNAKE_CASE=1e-5    ,    __SCREAMING_SNAKE_CASE=0.02    ,    __SCREAMING_SNAKE_CASE=True    ,    __SCREAMING_SNAKE_CASE=5_0_2_5_6    ,    __SCREAMING_SNAKE_CASE=5_0_2_5_6    ,    __SCREAMING_SNAKE_CASE=False    ,    **__SCREAMING_SNAKE_CASE    ,    )	-> Optional[Any]:




           """simple docstring"""







           UpperCamelCase__     : Union[str, Any]											=					vocab_size
           UpperCamelCase__     : Tuple											=					n_ctx
           UpperCamelCase__     : Any											=					n_positions
           UpperCamelCase__     : str											=					n_embd
           UpperCamelCase__     : Union[str, Any]											=					n_layer
           UpperCamelCase__     : Any											=					n_head
           UpperCamelCase__     : Optional[Any]											=					n_inner
           UpperCamelCase__     : Tuple											=					rotary_dim
           UpperCamelCase__     : Union[str, Any]											=					activation_function
           UpperCamelCase__     : Any											=					resid_pdrop
           UpperCamelCase__     : Union[str, Any]											=					embd_pdrop
           UpperCamelCase__     : Any											=					attn_pdrop
           UpperCamelCase__     : Tuple											=					layer_norm_epsilon
           UpperCamelCase__     : str											=					initializer_range
           UpperCamelCase__     : Any											=					use_cache

           UpperCamelCase__     : Optional[int]											=					bos_token_id
           UpperCamelCase__     : Optional[Any]											=					eos_token_id

           super().__init__(
               bos_token_id=__SCREAMING_SNAKE_CASE    ,    eos_token_id=__SCREAMING_SNAKE_CASE    ,    tie_word_embeddings=__SCREAMING_SNAKE_CASE    ,    **__SCREAMING_SNAKE_CASE	)




class 					_lowerCamelCase			(   UpperCamelCase_			):





     """simple docstring"""







     def __init__(				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE = "default"    ,    __SCREAMING_SNAKE_CASE = None    ,    __SCREAMING_SNAKE_CASE = False    ,    )	-> str:




           """simple docstring"""







           super().__init__(__SCREAMING_SNAKE_CASE    ,    task=__SCREAMING_SNAKE_CASE    ,    patching_specs=__SCREAMING_SNAKE_CASE    ,    use_past=__SCREAMING_SNAKE_CASE	)
           if not getattr(self._config    ,    '''pad_token_id'''    ,    __SCREAMING_SNAKE_CASE	):
                 # TODO: how to do that better?
                 UpperCamelCase__     : str											=					0






     @property
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Mapping[str, Mapping[int, str]]:




           """simple docstring"""







           UpperCamelCase__     : Union[str, Any]											=					OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}	)
           if self.use_past:
                 self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE    ,    direction='''inputs'''	)
                 UpperCamelCase__     : Tuple											=					{0: '''batch''', 1: '''past_sequence + sequence'''}
           else:
                 UpperCamelCase__     : Dict											=					{0: '''batch''', 1: '''sequence'''}

           return common_inputs






     @property
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> int:




           """simple docstring"""







           return self._config.n_layer






     @property
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> int:




           """simple docstring"""







           return self._config.n_head






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE = -1    ,    __SCREAMING_SNAKE_CASE = -1    ,    __SCREAMING_SNAKE_CASE = False    ,    __SCREAMING_SNAKE_CASE = None    ,    )	-> Mapping[str, Any]:




           """simple docstring"""







           UpperCamelCase__     : Optional[Any]											=					super(__SCREAMING_SNAKE_CASE    ,    self	).generate_dummy_inputs(
               __SCREAMING_SNAKE_CASE    ,    batch_size=__SCREAMING_SNAKE_CASE    ,    seq_length=__SCREAMING_SNAKE_CASE    ,    is_pair=__SCREAMING_SNAKE_CASE    ,    framework=__SCREAMING_SNAKE_CASE	)

           # We need to order the input in the way they appears in the forward()
           UpperCamelCase__     : List[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

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

           UpperCamelCase__     : List[str]											=					common_inputs['''attention_mask''']
           if self.use_past:
                 UpperCamelCase__     : int											=					ordered_inputs['''attention_mask'''].dtype
                 UpperCamelCase__     : str											=					torch.cat(
                     [ordered_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    dtype=__SCREAMING_SNAKE_CASE	)]    ,    dim=1	)

           return ordered_inputs






     @property
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> int:




           """simple docstring"""







           return 1_3




from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
    from PIL import Image

    from ..image_utils import load_image

if is_tf_available():
    from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING

if is_torch_available():
    import torch

    from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING

lowerCamelCase  =logging.get_logger(__name__)




@add_end_docstrings(UpperCamelCase_			)
class 					_lowerCamelCase			(   UpperCamelCase_			):





     """simple docstring"""







     def __init__(				self    ,    *__SCREAMING_SNAKE_CASE    ,    **__SCREAMING_SNAKE_CASE	)	-> Dict:




           """simple docstring"""







           super().__init__(*__SCREAMING_SNAKE_CASE    ,    **__SCREAMING_SNAKE_CASE	)
           requires_backends(self    ,    '''vision'''	)
           self.check_model_type(
               TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == '''tf''' else MODEL_FOR_VISION_2_SEQ_MAPPING	)






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE=None    ,    __SCREAMING_SNAKE_CASE=None    ,    __SCREAMING_SNAKE_CASE=None	)	-> List[Any]:




           """simple docstring"""







           UpperCamelCase__     : Tuple											=					{}
           UpperCamelCase__     : int											=					{}

           if prompt is not None:
                 UpperCamelCase__     : int											=					prompt

           if generate_kwargs is not None:
                 UpperCamelCase__     : int											=					generate_kwargs
           if max_new_tokens is not None:
                 if "generate_kwargs" not in forward_kwargs:
                       UpperCamelCase__     : Union[str, Any]											=					{}
                 if "max_new_tokens" in forward_kwargs["generate_kwargs"]:
                       raise ValueError(
                           '''\'max_new_tokens\' is defined twice, once in \'generate_kwargs\' and once as a direct parameter,'''
                           ''' please use only one'''	)
                 UpperCamelCase__     : Dict											=					max_new_tokens
           return preprocess_params, forward_kwargs, {}






     def __call__(				self    ,    __SCREAMING_SNAKE_CASE    ,    **__SCREAMING_SNAKE_CASE	)	-> List[str]:




           """simple docstring"""







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






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE=None	)	-> List[Any]:




           """simple docstring"""







           UpperCamelCase__     : List[Any]											=					load_image(__SCREAMING_SNAKE_CASE	)

           if prompt is not None:
                 if not isinstance(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	):
                       raise ValueError(
                           F'''Received an invalid text input, got - {type(__SCREAMING_SNAKE_CASE	)} - but expected a single string. '''
                           '''Note also that one single text can be provided for conditional image to text generation.'''	)

                 UpperCamelCase__     : Optional[int]											=					self.model.config.model_type

                 if model_type == "git":
                       UpperCamelCase__     : Optional[int]											=					self.image_processor(images=__SCREAMING_SNAKE_CASE    ,    return_tensors=self.framework	)
                       UpperCamelCase__     : str											=					self.tokenizer(text=__SCREAMING_SNAKE_CASE    ,    add_special_tokens=__SCREAMING_SNAKE_CASE	).input_ids
                       UpperCamelCase__     : Dict											=					[self.tokenizer.cls_token_id] + input_ids
                       UpperCamelCase__     : int											=					torch.tensor(__SCREAMING_SNAKE_CASE	).unsqueeze(0	)
                       model_inputs.update({'''input_ids''': input_ids}	)

                 elif model_type == "pix2struct":
                       UpperCamelCase__     : Tuple											=					self.image_processor(images=__SCREAMING_SNAKE_CASE    ,    header_text=__SCREAMING_SNAKE_CASE    ,    return_tensors=self.framework	)

                 elif model_type != "vision-encoder-decoder":
                       # vision-encoder-decoder does not support conditional generation
                       UpperCamelCase__     : int											=					self.image_processor(images=__SCREAMING_SNAKE_CASE    ,    return_tensors=self.framework	)
                       UpperCamelCase__     : int											=					self.tokenizer(__SCREAMING_SNAKE_CASE    ,    return_tensors=self.framework	)
                       model_inputs.update(__SCREAMING_SNAKE_CASE	)

                 else:
                       raise ValueError(F'''Model type {model_type} does not support conditional text generation'''	)

           else:
                 UpperCamelCase__     : str											=					self.image_processor(images=__SCREAMING_SNAKE_CASE    ,    return_tensors=self.framework	)

           if self.model.config.model_type == "git" and prompt is None:
                 UpperCamelCase__     : Optional[Any]											=					None

           return model_inputs






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE=None	)	-> Optional[Any]:




           """simple docstring"""







           if (
               "input_ids" in model_inputs
               and isinstance(model_inputs['''input_ids''']    ,    __SCREAMING_SNAKE_CASE	)
               and all(x is None for x in model_inputs['''input_ids''']	)
           ):
                 UpperCamelCase__     : Union[str, Any]											=					None

           if generate_kwargs is None:
                 UpperCamelCase__     : Optional[Any]											=					{}
           # FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py`
           #  parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas
           #  the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name`
           #  in the `_prepare_model_inputs` method.
           UpperCamelCase__     : Any											=					model_inputs.pop(self.model.main_input_name	)
           UpperCamelCase__     : Any											=					self.model.generate(__SCREAMING_SNAKE_CASE    ,    **__SCREAMING_SNAKE_CASE    ,    **__SCREAMING_SNAKE_CASE	)
           return model_outputs






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE	)	-> int:




           """simple docstring"""







           UpperCamelCase__     : Union[str, Any]											=					[]
           for output_ids in model_outputs:
                 UpperCamelCase__     : str											=					{
                     '''generated_text''': self.tokenizer.decode(
                         __SCREAMING_SNAKE_CASE    ,    skip_special_tokens=__SCREAMING_SNAKE_CASE    ,    )
                 }
                 records.append(__SCREAMING_SNAKE_CASE	)
           return records


