



from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
    import PIL


__UpperCAmelCase          =       logging.get_logger(__name__)



class 				lowerCAmelCase_		(  a__							):
       UpperCAmelCase__				:					int        =  ["pixel_values"]
       def __init__(			self,			SCREAMING_SNAKE_CASE_ = True,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = PILImageResampling.BICUBIC,			SCREAMING_SNAKE_CASE_ = True,			SCREAMING_SNAKE_CASE_ = 1 / 255,			SCREAMING_SNAKE_CASE_ = True,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = True,			**SCREAMING_SNAKE_CASE_,			)     ->		None:
          super().__init__(**SCREAMING_SNAKE_CASE_       )
          UpperCamelCase    :  str							=	size if size is not None else {'height': 384, 'width': 384}
          UpperCamelCase    :  Optional[int]							=	get_size_dict(SCREAMING_SNAKE_CASE_,			default_to_square=SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  List[str]							=	do_resize
          UpperCamelCase    :  str							=	size
          UpperCamelCase    :  Tuple							=	resample
          UpperCamelCase    :  Any							=	do_rescale
          UpperCamelCase    :  Optional[int]							=	rescale_factor
          UpperCamelCase    :  Optional[int]							=	do_normalize
          UpperCamelCase    :  Optional[int]							=	image_mean if image_mean is not None else OPENAI_CLIP_MEAN
          UpperCamelCase    :  Tuple							=	image_std if image_std is not None else OPENAI_CLIP_STD
          UpperCamelCase    :  Dict							=	do_convert_rgb
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = PILImageResampling.BICUBIC,			SCREAMING_SNAKE_CASE_ = None,			**SCREAMING_SNAKE_CASE_,			)     ->		np.ndarray:
          UpperCamelCase    :  Dict							=	get_size_dict(SCREAMING_SNAKE_CASE_,			default_to_square=SCREAMING_SNAKE_CASE_       )
          if "height" not in size or "width" not in size:
             raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}"""       )
          UpperCamelCase    :  List[Any]							=	(size['height'], size['width'])
          return resize(SCREAMING_SNAKE_CASE_,			size=SCREAMING_SNAKE_CASE_,			resample=SCREAMING_SNAKE_CASE_,			data_format=SCREAMING_SNAKE_CASE_,			**SCREAMING_SNAKE_CASE_       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = None,			**SCREAMING_SNAKE_CASE_,			)     ->		Any:
          return rescale(SCREAMING_SNAKE_CASE_,			scale=SCREAMING_SNAKE_CASE_,			data_format=SCREAMING_SNAKE_CASE_,			**SCREAMING_SNAKE_CASE_       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = None,			**SCREAMING_SNAKE_CASE_,			)     ->		np.ndarray:
          return normalize(SCREAMING_SNAKE_CASE_,			mean=SCREAMING_SNAKE_CASE_,			std=SCREAMING_SNAKE_CASE_,			data_format=SCREAMING_SNAKE_CASE_,			**SCREAMING_SNAKE_CASE_       )






       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = ChannelDimension.FIRST,			**SCREAMING_SNAKE_CASE_,			)     ->		PIL.Image.Image:
          UpperCamelCase    :  List[str]							=	do_resize if do_resize is not None else self.do_resize
          UpperCamelCase    :  Tuple							=	resample if resample is not None else self.resample
          UpperCamelCase    :  Optional[Any]							=	do_rescale if do_rescale is not None else self.do_rescale
          UpperCamelCase    :  Any							=	rescale_factor if rescale_factor is not None else self.rescale_factor
          UpperCamelCase    :  Union[str, Any]							=	do_normalize if do_normalize is not None else self.do_normalize
          UpperCamelCase    :  Union[str, Any]							=	image_mean if image_mean is not None else self.image_mean
          UpperCamelCase    :  int							=	image_std if image_std is not None else self.image_std
          UpperCamelCase    :  Optional[Any]							=	do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

          UpperCamelCase    :  Optional[Any]							=	size if size is not None else self.size
          UpperCamelCase    :  List[Any]							=	get_size_dict(SCREAMING_SNAKE_CASE_,			default_to_square=SCREAMING_SNAKE_CASE_       )

          UpperCamelCase    :  Optional[Any]							=	make_list_of_images(SCREAMING_SNAKE_CASE_       )

          if not valid_images(SCREAMING_SNAKE_CASE_       ):
             raise ValueError(
                 'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
                 'torch.Tensor, tf.Tensor or jax.ndarray.'       )

          if do_resize and size is None or resample is None:
             raise ValueError('Size and resample must be specified if do_resize is True.'       )

          if do_rescale and rescale_factor is None:
             raise ValueError('Rescale factor must be specified if do_rescale is True.'       )

          if do_normalize and (image_mean is None or image_std is None):
             raise ValueError('Image mean and std must be specified if do_normalize is True.'       )

          # PIL RGBA images are converted to RGB
          if do_convert_rgb:
             UpperCamelCase    :  Dict							=	[convert_to_rgb(SCREAMING_SNAKE_CASE_       ) for image in images]

          # All transformations expect numpy arrays.
          UpperCamelCase    :  Optional[Any]							=	[to_numpy_array(SCREAMING_SNAKE_CASE_       ) for image in images]

          if do_resize:
             UpperCamelCase    :  Optional[int]							=	[self.resize(image=SCREAMING_SNAKE_CASE_,			size=SCREAMING_SNAKE_CASE_,			resample=SCREAMING_SNAKE_CASE_       ) for image in images]

          if do_rescale:
             UpperCamelCase    :  str							=	[self.rescale(image=SCREAMING_SNAKE_CASE_,			scale=SCREAMING_SNAKE_CASE_       ) for image in images]

          if do_normalize:
             UpperCamelCase    :  List[Any]							=	[self.normalize(image=SCREAMING_SNAKE_CASE_,			mean=SCREAMING_SNAKE_CASE_,			std=SCREAMING_SNAKE_CASE_       ) for image in images]

          UpperCamelCase    :  List[str]							=	[to_channel_dimension_format(SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       ) for image in images]

          UpperCamelCase    :  str							=	BatchFeature(data={'pixel_values': images},			tensor_type=SCREAMING_SNAKE_CASE_       )

          return encoded_outputs








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


__UpperCAmelCase          =       logging.get_logger(__name__)


__UpperCAmelCase          =       {
    '''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_		(  a__							):
       UpperCAmelCase__				:					List[str]        =  "codegen"
       UpperCAmelCase__				:					str        =  {
           "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_0400,			SCREAMING_SNAKE_CASE_=2048,			SCREAMING_SNAKE_CASE_=2048,			SCREAMING_SNAKE_CASE_=4096,			SCREAMING_SNAKE_CASE_=28,			SCREAMING_SNAKE_CASE_=16,			SCREAMING_SNAKE_CASE_=64,			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_0256,			SCREAMING_SNAKE_CASE_=5_0256,			SCREAMING_SNAKE_CASE_=False,			**SCREAMING_SNAKE_CASE_,			)     ->		Tuple:
          UpperCamelCase    :  Tuple							=	vocab_size
          UpperCamelCase    :  Optional[int]							=	n_ctx
          UpperCamelCase    :  Optional[int]							=	n_positions
          UpperCamelCase    :  List[str]							=	n_embd
          UpperCamelCase    :  Dict							=	n_layer
          UpperCamelCase    :  int							=	n_head
          UpperCamelCase    :  Union[str, Any]							=	n_inner
          UpperCamelCase    :  int							=	rotary_dim
          UpperCamelCase    :  Optional[Any]							=	activation_function
          UpperCamelCase    :  Optional[int]							=	resid_pdrop
          UpperCamelCase    :  Union[str, Any]							=	embd_pdrop
          UpperCamelCase    :  Optional[Any]							=	attn_pdrop
          UpperCamelCase    :  List[str]							=	layer_norm_epsilon
          UpperCamelCase    :  Union[str, Any]							=	initializer_range
          UpperCamelCase    :  str							=	use_cache

          UpperCamelCase    :  Dict							=	bos_token_id
          UpperCamelCase    :  Union[str, 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_		(  a__							):
       def __init__(			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_ = "default",			SCREAMING_SNAKE_CASE_ = None,			SCREAMING_SNAKE_CASE_ = False,			)     ->		List[str]:
          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 					snake_case_       (			self       )     ->		Mapping[str, Mapping[int, str]]:
          UpperCamelCase    :  Tuple							=	OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}       )
          if self.use_past:
             self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE_,			direction='inputs'       )
             UpperCamelCase    :  List[Any]							=	{0: 'batch', 1: 'past_sequence + sequence'}
          else:
             UpperCamelCase    :  Optional[int]							=	{0: 'batch', 1: 'sequence'}

          return common_inputs
       @property
       def 					snake_case_       (			self       )     ->		int:
          return self._config.n_layer
       @property
       def 					snake_case_       (			self       )     ->		int:
          return self._config.n_head
       def 					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]:
          UpperCamelCase    :  Tuple							=	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    :  Optional[int]							=	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    :  List[str]							=	common_inputs['input_ids'].shape
                # Not using the same length for past_key_values
                UpperCamelCase    :  List[Any]							=	seqlen + 2
                UpperCamelCase    :  List[str]							=	(
                    batch,
                    self.num_attention_heads,
                    past_key_values_length,
                    self._config.hidden_size // self.num_attention_heads,
                )
                UpperCamelCase    :  str							=	[
                    (torch.zeros(SCREAMING_SNAKE_CASE_       ), torch.zeros(SCREAMING_SNAKE_CASE_       )) for _ in range(self.num_layers       )
                ]

          UpperCamelCase    :  List[Any]							=	common_inputs['attention_mask']
          if self.use_past:
             UpperCamelCase    :  Optional[Any]							=	ordered_inputs['attention_mask'].dtype
             UpperCamelCase    :  List[Any]							=	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 					snake_case_       (			self       )     ->		int:
          return 13



