
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


lowercase   =	logging.get_logger(__name__)


lowercase   =	{
    '''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    __A(					UpperCAmelCase							):
					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       :  List[str] ,       __UpperCamelCase       :  Dict=5_0_4_0_0 ,       __UpperCamelCase       :  Union[str, Any]=2_0_4_8 ,       __UpperCamelCase       :  List[Any]=2_0_4_8 ,       __UpperCamelCase       :  Dict=4_0_9_6 ,       __UpperCamelCase       :  Tuple=2_8 ,       __UpperCamelCase       :  Optional[Any]=1_6 ,       __UpperCamelCase       :  Dict=6_4 ,       __UpperCamelCase       :  Optional[Any]=None ,       __UpperCamelCase       :  List[Any]="gelu_new" ,       __UpperCamelCase       :  str=0.0 ,       __UpperCamelCase       :  Optional[Any]=0.0 ,       __UpperCamelCase       :  Tuple=0.0 ,       __UpperCamelCase       :  Dict=1E-5 ,       __UpperCamelCase       :  Union[str, Any]=0.02 ,       __UpperCamelCase       :  Union[str, Any]=True ,       __UpperCamelCase       :  Dict=5_0_2_5_6 ,       __UpperCamelCase       :  Dict=5_0_2_5_6 ,       __UpperCamelCase       :  int=False ,       **__UpperCamelCase       :  Optional[Any] ,       ):
												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    __A(					UpperCAmelCase							):

					def __init__(						self       :  int ,       __UpperCamelCase       :  PretrainedConfig ,       __UpperCamelCase       :  str = "default" ,       __UpperCamelCase       :  List[PatchingSpec] = None ,       __UpperCamelCase       :  bool = False ,       ):
												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  lowercase__   (						self       :  Tuple ):
												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  lowercase__   (						self       :  Dict ):
												return self._config.n_layer

					@property
					def  lowercase__   (						self       :  Dict ):
												return self._config.n_head

					def  lowercase__   (						self       :  Tuple ,       __UpperCamelCase       :  PreTrainedTokenizer ,       __UpperCamelCase       :  int = -1 ,       __UpperCamelCase       :  int = -1 ,       __UpperCamelCase       :  bool = False ,       __UpperCamelCase       :  Optional[TensorType] = None ,       ):
												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  lowercase__   (						self       :  List[str] ):
												return 1_3







import importlib.metadata
import operator
import re
import sys
from typing import Optional

from packaging import version


lowercase   =	{
    '''<''': operator.lt,
    '''<=''': operator.le,
    '''==''': operator.eq,
    '''!=''': operator.ne,
    '''>=''': operator.ge,
    '''>''': operator.gt,
}



def   __lowerCAmelCase    (				UpperCAmelCase__					:   Tuple      ,					UpperCAmelCase__					:   int      ,					UpperCAmelCase__					:   Optional[int]      ,					UpperCAmelCase__					:   Optional[int]      ,					UpperCAmelCase__					:   Optional[int]      ,					UpperCAmelCase__					:   int							)	->     Union[str, Any]:
							if got_ver is None or want_ver is None:
														raise ValueError(
														    F'''Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider'''
														    F''' reinstalling {pkg}.'''							)
							if not ops[op](version.parse(UpperCAmelCase__							)      ,					version.parse(UpperCAmelCase__							)							):
														raise ImportError(
														    F'''{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}'''							)



def   __lowerCAmelCase    (				UpperCAmelCase__					:   str      ,					UpperCAmelCase__					:   Optional[str] = None							)	->     None:
							lowerCamelCase_					=       F'''\n{hint}''' if hint is not None else """"""

							# non-versioned check
							if re.match(R"""^[\w_\-\d]+$"""      ,					UpperCAmelCase__							):
														lowerCamelCase_	, lowerCamelCase_	, lowerCamelCase_					=       requirement, None, None
							else:
														lowerCamelCase_					=       re.findall(R"""^([^!=<>\s]+)([\s!=<>]{1,2}.+)"""      ,					UpperCAmelCase__							)
														if not match:
																					raise ValueError(
																					    """requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but"""
																					    F''' got {requirement}'''							)
														lowerCamelCase_	, lowerCamelCase_					=       match[0]
														lowerCamelCase_					=       want_full.split(""","""							)  # there could be multiple requirements
														lowerCamelCase_					=       {}
														for w in want_range:
																					lowerCamelCase_					=       re.findall(R"""^([\s!=<>]{1,2})(.+)"""      ,					UpperCAmelCase__							)
																					if not match:
																												raise ValueError(
																												    """requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,"""
																												    F''' but got {requirement}'''							)
																					lowerCamelCase_	, lowerCamelCase_					=       match[0]
																					lowerCamelCase_					=       want_ver
																					if op not in ops:
																												raise ValueError(F'''{requirement}: need one of {list(ops.keys()							)}, but got {op}'''							)

    # special case
							if pkg == "python":
														lowerCamelCase_					=       """.""".join([str(UpperCAmelCase__							) for x in sys.version_info[:3]]							)
														for op, want_ver in wanted.items():
																					_compare_versions(UpperCAmelCase__      ,					UpperCAmelCase__      ,					UpperCAmelCase__      ,					UpperCAmelCase__      ,					UpperCAmelCase__      ,					UpperCAmelCase__							)
														return

							# check if any version is installed
							try:
														lowerCamelCase_					=       importlib.metadata.version(UpperCAmelCase__							)
							except importlib.metadata.PackageNotFoundError:
														raise importlib.metadata.PackageNotFoundError(
														    F'''The \'{requirement}\' distribution was not found and is required by this application. {hint}'''							)

							# check that the right version is installed if version number or a range was provided
							if want_ver is not None:
														for op, want_ver in wanted.items():
																					_compare_versions(UpperCAmelCase__      ,					UpperCAmelCase__      ,					UpperCAmelCase__      ,					UpperCAmelCase__      ,					UpperCAmelCase__      ,					UpperCAmelCase__							)



def   __lowerCAmelCase    (				UpperCAmelCase__					:   List[str]							)	->     Dict:
							lowerCamelCase_					=       """Try: pip install transformers -U or pip install -e '.[dev]' if you're working with git main"""
							return require_version(UpperCAmelCase__      ,					UpperCAmelCase__							)





