






from dataclasses import dataclass
from typing import Dict, Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .attention_processor import AttentionProcessor, AttnProcessor
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, DiagonalGaussianDistribution, Encoder


@dataclass
class      lowercase__     (			_UpperCAmelCase	):
	a_         =42





class      lowercase__     (			_UpperCAmelCase,					_UpperCAmelCase	):
	a_         =True

	@register_to_config
	def __init__(							self		,    __UpperCAmelCase = 3		,    __UpperCAmelCase = 3		,    __UpperCAmelCase = ("DownEncoderBlock2D",)		,    __UpperCAmelCase = ("UpDecoderBlock2D",)		,    __UpperCAmelCase = (64,)		,    __UpperCAmelCase = 1		,    __UpperCAmelCase = "silu"		,    __UpperCAmelCase = 4		,    __UpperCAmelCase = 32		,    __UpperCAmelCase = 32		,    __UpperCAmelCase = 0.18_215		,    )->					Tuple:







							'''simple docstring'''





							super().__init__()

							# pass init params to Encoder
							lowerCAmelCase__       =						Encoder(
							    in_channels=__UpperCAmelCase		,    out_channels=__UpperCAmelCase		,    down_block_types=__UpperCAmelCase		,    block_out_channels=__UpperCAmelCase		,    layers_per_block=__UpperCAmelCase		,    act_fn=__UpperCAmelCase		,    norm_num_groups=__UpperCAmelCase		,    double_z=__UpperCAmelCase		,    )

							# pass init params to Decoder
							lowerCAmelCase__       =						Decoder(
							    in_channels=__UpperCAmelCase		,    out_channels=__UpperCAmelCase		,    up_block_types=__UpperCAmelCase		,    block_out_channels=__UpperCAmelCase		,    layers_per_block=__UpperCAmelCase		,    norm_num_groups=__UpperCAmelCase		,    act_fn=__UpperCAmelCase		,    )

							lowerCAmelCase__       =						nn.Convad(2 * latent_channels		,    2 * latent_channels		,    1       )
							lowerCAmelCase__       =						nn.Convad(__UpperCAmelCase		,    __UpperCAmelCase		,    1       )

							lowerCAmelCase__       =						False
							lowerCAmelCase__       =						False

							# only relevant if vae tiling is enabled
							lowerCAmelCase__       =						self.config.sample_size
							lowerCAmelCase__       =						(
							    self.config.sample_size[0]
							    if isinstance(self.config.sample_size		,    (list, tuple)       )
							    else self.config.sample_size
							)
							lowerCAmelCase__       =						int(sample_size / (2 ** (len(self.config.block_out_channels       ) - 1))       )
							lowerCAmelCase__       =						0.25

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase		,    __UpperCAmelCase=False       )->					Optional[Any]:







							'''simple docstring'''





							if isinstance(__UpperCAmelCase		,    (Encoder, Decoder)       ):
													lowerCAmelCase__       =						value

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase = True       )->					Tuple:







							'''simple docstring'''





							lowerCAmelCase__       =						use_tiling

	def 		UpperCAmelCase  (							self       )->					Union[str, Any]:







							'''simple docstring'''





							self.enable_tiling(__UpperCAmelCase       )

	def 		UpperCAmelCase  (							self       )->					str:







							'''simple docstring'''





							lowerCAmelCase__       =						True

	def 		UpperCAmelCase  (							self       )->					Optional[int]:







							'''simple docstring'''





							lowerCAmelCase__       =						False

	@property
	# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
	def 		UpperCAmelCase  (							self       )->					Dict[str, AttentionProcessor]:







							'''simple docstring'''





							lowerCAmelCase__       =						{}

							def fn_recursive_add_processors(__UpperCAmelCase		,    __UpperCAmelCase		,    __UpperCAmelCase       ):
													if hasattr(__UpperCAmelCase		,    "set_processor"       ):
																			lowerCAmelCase__       =						module.processor

													for sub_name, child in module.named_children():
																			fn_recursive_add_processors(F"{name}.{sub_name}"		,    __UpperCAmelCase		,    __UpperCAmelCase       )

													return processors

							for name, module in self.named_children():
													fn_recursive_add_processors(__UpperCAmelCase		,    __UpperCAmelCase		,    __UpperCAmelCase       )

							return processors

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase       )->					str:







							'''simple docstring'''





							lowerCAmelCase__       =						len(self.attn_processors.keys()       )

							if isinstance(__UpperCAmelCase		,    __UpperCAmelCase       ) and len(__UpperCAmelCase       ) != count:
													raise ValueError(
													    F"A dict of processors was passed, but the number of processors {len(__UpperCAmelCase       )} does not match the"
													    F" number of attention layers: {count}. Please make sure to pass {count} processor classes."       )

							def fn_recursive_attn_processor(__UpperCAmelCase		,    __UpperCAmelCase		,    __UpperCAmelCase       ):
													if hasattr(__UpperCAmelCase		,    "set_processor"       ):
																			if not isinstance(__UpperCAmelCase		,    __UpperCAmelCase       ):
																									module.set_processor(__UpperCAmelCase       )
																			else:
																									module.set_processor(processor.pop(F"{name}.processor"       )       )

													for sub_name, child in module.named_children():
																			fn_recursive_attn_processor(F"{name}.{sub_name}"		,    __UpperCAmelCase		,    __UpperCAmelCase       )

							for name, module in self.named_children():
													fn_recursive_attn_processor(__UpperCAmelCase		,    __UpperCAmelCase		,    __UpperCAmelCase       )

	def 		UpperCAmelCase  (							self       )->					List[Any]:







							'''simple docstring'''





							self.set_attn_processor(AttnProcessor()       )

	@apply_forward_hook
	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase		,    __UpperCAmelCase = True       )->					AutoencoderKLOutput:







							'''simple docstring'''





							if self.use_tiling and (x.shape[-1] > self.tile_sample_min_size or x.shape[-2] > self.tile_sample_min_size):
													return self.tiled_encode(__UpperCAmelCase		,    return_dict=__UpperCAmelCase       )

							if self.use_slicing and x.shape[0] > 1:
													lowerCAmelCase__       =						[self.encoder(__UpperCAmelCase       ) for x_slice in x.split(1       )]
													lowerCAmelCase__       =						torch.cat(__UpperCAmelCase       )
							else:
													lowerCAmelCase__       =						self.encoder(__UpperCAmelCase       )

							lowerCAmelCase__       =						self.quant_conv(__UpperCAmelCase       )
							lowerCAmelCase__       =						DiagonalGaussianDistribution(__UpperCAmelCase       )

							if not return_dict:
													return (posterior,)

							return AutoencoderKLOutput(latent_dist=__UpperCAmelCase       )

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase		,    __UpperCAmelCase = True       )->					Union[DecoderOutput, torch.FloatTensor]:







							'''simple docstring'''





							if self.use_tiling and (z.shape[-1] > self.tile_latent_min_size or z.shape[-2] > self.tile_latent_min_size):
													return self.tiled_decode(__UpperCAmelCase		,    return_dict=__UpperCAmelCase       )

							lowerCAmelCase__       =						self.post_quant_conv(__UpperCAmelCase       )
							lowerCAmelCase__       =						self.decoder(__UpperCAmelCase       )

							if not return_dict:
													return (dec,)

							return DecoderOutput(sample=__UpperCAmelCase       )

	@apply_forward_hook
	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase		,    __UpperCAmelCase = True       )->					Union[DecoderOutput, torch.FloatTensor]:







							'''simple docstring'''





							if self.use_slicing and z.shape[0] > 1:
													lowerCAmelCase__       =						[self._decode(__UpperCAmelCase       ).sample for z_slice in z.split(1       )]
													lowerCAmelCase__       =						torch.cat(__UpperCAmelCase       )
							else:
													lowerCAmelCase__       =						self._decode(__UpperCAmelCase       ).sample

							if not return_dict:
													return (decoded,)

							return DecoderOutput(sample=__UpperCAmelCase       )

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase		,    __UpperCAmelCase		,    __UpperCAmelCase       )->					List[str]:







							'''simple docstring'''





							lowerCAmelCase__       =						min(a.shape[2]		,    b.shape[2]		,    __UpperCAmelCase       )
							for y in range(__UpperCAmelCase       ):
													lowerCAmelCase__       =						a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
							return b

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase		,    __UpperCAmelCase		,    __UpperCAmelCase       )->					Optional[int]:







							'''simple docstring'''





							lowerCAmelCase__       =						min(a.shape[3]		,    b.shape[3]		,    __UpperCAmelCase       )
							for x in range(__UpperCAmelCase       ):
													lowerCAmelCase__       =						a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
							return b

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase		,    __UpperCAmelCase = True       )->					AutoencoderKLOutput:







							'''simple docstring'''





							lowerCAmelCase__       =						int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)       )
							lowerCAmelCase__       =						int(self.tile_latent_min_size * self.tile_overlap_factor       )
							lowerCAmelCase__       =						self.tile_latent_min_size - blend_extent

							# Split the image into 512x512 tiles and encode them separately.
							lowerCAmelCase__       =						[]
							for i in range(0		,    x.shape[2]		,    __UpperCAmelCase       ):
													lowerCAmelCase__       =						[]
													for j in range(0		,    x.shape[3]		,    __UpperCAmelCase       ):
																			lowerCAmelCase__       =						x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
																			lowerCAmelCase__       =						self.encoder(__UpperCAmelCase       )
																			lowerCAmelCase__       =						self.quant_conv(__UpperCAmelCase       )
																			row.append(__UpperCAmelCase       )
													rows.append(__UpperCAmelCase       )
							lowerCAmelCase__       =						[]
							for i, row in enumerate(__UpperCAmelCase       ):
													lowerCAmelCase__       =						[]
													for j, tile in enumerate(__UpperCAmelCase       ):
																			# blend the above tile and the left tile
																			# to the current tile and add the current tile to the result row
																			if i > 0:
																									lowerCAmelCase__       =						self.blend_v(rows[i - 1][j]		,    __UpperCAmelCase		,    __UpperCAmelCase       )
																			if j > 0:
																									lowerCAmelCase__       =						self.blend_h(row[j - 1]		,    __UpperCAmelCase		,    __UpperCAmelCase       )
																			result_row.append(tile[:, :, :row_limit, :row_limit]       )
													result_rows.append(torch.cat(__UpperCAmelCase		,    dim=3       )       )

							lowerCAmelCase__       =						torch.cat(__UpperCAmelCase		,    dim=2       )
							lowerCAmelCase__       =						DiagonalGaussianDistribution(__UpperCAmelCase       )

							if not return_dict:
													return (posterior,)

							return AutoencoderKLOutput(latent_dist=__UpperCAmelCase       )

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase		,    __UpperCAmelCase = True       )->					Union[DecoderOutput, torch.FloatTensor]:







							'''simple docstring'''





							lowerCAmelCase__       =						int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)       )
							lowerCAmelCase__       =						int(self.tile_sample_min_size * self.tile_overlap_factor       )
							lowerCAmelCase__       =						self.tile_sample_min_size - blend_extent

							# Split z into overlapping 64x64 tiles and decode them separately.
							# The tiles have an overlap to avoid seams between tiles.
							lowerCAmelCase__       =						[]
							for i in range(0		,    z.shape[2]		,    __UpperCAmelCase       ):
													lowerCAmelCase__       =						[]
													for j in range(0		,    z.shape[3]		,    __UpperCAmelCase       ):
																			lowerCAmelCase__       =						z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
																			lowerCAmelCase__       =						self.post_quant_conv(__UpperCAmelCase       )
																			lowerCAmelCase__       =						self.decoder(__UpperCAmelCase       )
																			row.append(__UpperCAmelCase       )
													rows.append(__UpperCAmelCase       )
							lowerCAmelCase__       =						[]
							for i, row in enumerate(__UpperCAmelCase       ):
													lowerCAmelCase__       =						[]
													for j, tile in enumerate(__UpperCAmelCase       ):
																			# blend the above tile and the left tile
																			# to the current tile and add the current tile to the result row
																			if i > 0:
																									lowerCAmelCase__       =						self.blend_v(rows[i - 1][j]		,    __UpperCAmelCase		,    __UpperCAmelCase       )
																			if j > 0:
																									lowerCAmelCase__       =						self.blend_h(row[j - 1]		,    __UpperCAmelCase		,    __UpperCAmelCase       )
																			result_row.append(tile[:, :, :row_limit, :row_limit]       )
													result_rows.append(torch.cat(__UpperCAmelCase		,    dim=3       )       )

							lowerCAmelCase__       =						torch.cat(__UpperCAmelCase		,    dim=2       )
							if not return_dict:
													return (dec,)

							return DecoderOutput(sample=__UpperCAmelCase       )


	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase		,    __UpperCAmelCase = False		,    __UpperCAmelCase = True		,    __UpperCAmelCase = None		,    )->					Union[DecoderOutput, torch.FloatTensor]:







							'''simple docstring'''





							lowerCAmelCase__       =						sample
							lowerCAmelCase__       =						self.encode(__UpperCAmelCase       ).latent_dist
							if sample_posterior:
													lowerCAmelCase__       =						posterior.sample(generator=__UpperCAmelCase       )
							else:
													lowerCAmelCase__       =						posterior.mode()
							lowerCAmelCase__       =						self.decode(__UpperCAmelCase       ).sample

							if not return_dict:
													return (dec,)

							return DecoderOutput(sample=__UpperCAmelCase       )














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


a_							=    logging.get_logger(__name__)


a_							=    {
    '''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      lowercase__     (			_UpperCAmelCase	):
	a_         ="""codegen"""
	a_         ={
	    """max_position_embeddings""": """n_positions""",
	    """hidden_size""": """n_embd""",
	    """num_attention_heads""": """n_head""",
	    """num_hidden_layers""": """n_layer""",
	}

	def __init__(							self		,    __UpperCAmelCase=50400		,    __UpperCAmelCase=2048		,    __UpperCAmelCase=2048		,    __UpperCAmelCase=4096		,    __UpperCAmelCase=28		,    __UpperCAmelCase=16		,    __UpperCAmelCase=64		,    __UpperCAmelCase=None		,    __UpperCAmelCase="gelu_new"		,    __UpperCAmelCase=0.0		,    __UpperCAmelCase=0.0		,    __UpperCAmelCase=0.0		,    __UpperCAmelCase=1E-5		,    __UpperCAmelCase=0.02		,    __UpperCAmelCase=True		,    __UpperCAmelCase=50256		,    __UpperCAmelCase=50256		,    __UpperCAmelCase=False		,    **__UpperCAmelCase		,    )->					Optional[int]:







							'''simple docstring'''





							lowerCAmelCase__       =						vocab_size
							lowerCAmelCase__       =						n_ctx
							lowerCAmelCase__       =						n_positions
							lowerCAmelCase__       =						n_embd
							lowerCAmelCase__       =						n_layer
							lowerCAmelCase__       =						n_head
							lowerCAmelCase__       =						n_inner
							lowerCAmelCase__       =						rotary_dim
							lowerCAmelCase__       =						activation_function
							lowerCAmelCase__       =						resid_pdrop
							lowerCAmelCase__       =						embd_pdrop
							lowerCAmelCase__       =						attn_pdrop
							lowerCAmelCase__       =						layer_norm_epsilon
							lowerCAmelCase__       =						initializer_range
							lowerCAmelCase__       =						use_cache

							lowerCAmelCase__       =						bos_token_id
							lowerCAmelCase__       =						eos_token_id

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





class      lowercase__     (			_UpperCAmelCase	):

	def __init__(							self		,    __UpperCAmelCase		,    __UpperCAmelCase = "default"		,    __UpperCAmelCase = None		,    __UpperCAmelCase = False		,    )->					Any:







							'''simple docstring'''





							super().__init__(__UpperCAmelCase		,    task=__UpperCAmelCase		,    patching_specs=__UpperCAmelCase		,    use_past=__UpperCAmelCase       )
							if not getattr(self._config		,    "pad_token_id"		,    __UpperCAmelCase       ):
													# TODO: how to do that better?
													lowerCAmelCase__       =						0

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







							'''simple docstring'''





							lowerCAmelCase__       =						OrderedDict({"input_ids": {0: "batch", 1: "sequence"}}       )
							if self.use_past:
													self.fill_with_past_key_values_(__UpperCAmelCase		,    direction="inputs"       )
													lowerCAmelCase__       =						{0: "batch", 1: "past_sequence + sequence"}
							else:
													lowerCAmelCase__       =						{0: "batch", 1: "sequence"}

							return common_inputs

	@property
	def 		UpperCAmelCase  (							self       )->					int:







							'''simple docstring'''





							return self._config.n_layer

	@property
	def 		UpperCAmelCase  (							self       )->					int:







							'''simple docstring'''





							return self._config.n_head

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







							'''simple docstring'''





							lowerCAmelCase__       =						super(__UpperCAmelCase		,    self       ).generate_dummy_inputs(
							    __UpperCAmelCase		,    batch_size=__UpperCAmelCase		,    seq_length=__UpperCAmelCase		,    is_pair=__UpperCAmelCase		,    framework=__UpperCAmelCase       )

							# We need to order the input in the way they appears in the forward()
							lowerCAmelCase__       =						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

																			lowerCAmelCase__      ,							lowerCAmelCase__       =						common_inputs["input_ids"].shape
																			# Not using the same length for past_key_values
																			lowerCAmelCase__       =						seqlen + 2
																			lowerCAmelCase__       =						(
																			    batch,
																			    self.num_attention_heads,
																			    past_key_values_length,
																			    self._config.hidden_size // self.num_attention_heads,
																			)
																			lowerCAmelCase__       =						[
																			    (torch.zeros(__UpperCAmelCase       ), torch.zeros(__UpperCAmelCase       )) for _ in range(self.num_layers       )
																			]

							lowerCAmelCase__       =						common_inputs["attention_mask"]
							if self.use_past:
													lowerCAmelCase__       =						ordered_inputs["attention_mask"].dtype
													lowerCAmelCase__       =						torch.cat(
													    [ordered_inputs["attention_mask"], torch.ones(__UpperCAmelCase		,    __UpperCAmelCase		,    dtype=__UpperCAmelCase       )]		,    dim=1       )

							return ordered_inputs


	@property
	def 		UpperCAmelCase  (							self       )->					int:







							'''simple docstring'''





							return 13






