


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


SCREAMING_SNAKE_CASE :   Union[str, Any]							      =				logging.get_logger(__name__)


SCREAMING_SNAKE_CASE :   Optional[Any]							      =				{
    "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     ):
       lowercase_				:			Optional[int]									=    """codegen"""
       lowercase_				:			Optional[int]									=    {
           """max_position_embeddings""": """n_positions""",
           """hidden_size""": """n_embd""",
           """num_attention_heads""": """n_head""",
           """num_hidden_layers""": """n_layer""",
       }
       def __init__(      self,   lowerCamelCase=5_04_00,   lowerCamelCase=20_48,   lowerCamelCase=20_48,   lowerCamelCase=40_96,   lowerCamelCase=28,   lowerCamelCase=16,   lowerCamelCase=64,   lowerCamelCase=None,   lowerCamelCase="gelu_new",   lowerCamelCase=0.0,   lowerCamelCase=0.0,   lowerCamelCase=0.0,   lowerCamelCase=1E-5,   lowerCamelCase=0.0_2,   lowerCamelCase=True,   lowerCamelCase=5_02_56,   lowerCamelCase=5_02_56,   lowerCamelCase=False,   **lowerCamelCase,   ) ->     Union[str, Any]:




           """simple docstring"""

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

           _lowercase :							Tuple   	=	bos_token_id
           _lowercase :							Any   	=	eos_token_id

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






class _lowerCamelCase(		_a     ):
       def __init__(      self,   lowerCamelCase,   lowerCamelCase = "default",   lowerCamelCase = None,   lowerCamelCase = False,   ) ->     Union[str, Any]:




           """simple docstring"""

           super().__init__(lowerCamelCase,   task=lowerCamelCase,   patching_specs=lowerCamelCase,   use_past=lowerCamelCase)
           if not getattr(self._config,   'pad_token_id',   lowerCamelCase):
               # TODO: how to do that better?
               _lowercase :							str   	=	0
       @property
       def     UpperCamelCase  (      self) ->     Mapping[str, Mapping[int, str]]:




           """simple docstring"""

           _lowercase :							Dict   	=	OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}})
           if self.use_past:
               self.fill_with_past_key_values_(lowerCamelCase,   direction='inputs')
               _lowercase :							Optional[int]   	=	{0: 'batch', 1: 'past_sequence + sequence'}
           else:
               _lowercase :							Optional[int]   	=	{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,   lowerCamelCase,   lowerCamelCase = -1,   lowerCamelCase = -1,   lowerCamelCase = False,   lowerCamelCase = None,   ) ->     Mapping[str, Any]:




           """simple docstring"""

           _lowercase :							Optional[int]   	=	super(lowerCamelCase,   self).generate_dummy_inputs(
               lowerCamelCase,   batch_size=lowerCamelCase,   seq_length=lowerCamelCase,   is_pair=lowerCamelCase,   framework=lowerCamelCase)

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

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

           _lowercase :							Tuple   	=	common_inputs['attention_mask']
           if self.use_past:
               _lowercase :							Union[str, Any]   	=	ordered_inputs['attention_mask'].dtype
               _lowercase :							Optional[Any]   	=	torch.cat(
                   [ordered_inputs['attention_mask'], torch.ones(lowerCamelCase,   lowerCamelCase,   dtype=lowerCamelCase)],   dim=1)

           return ordered_inputs




       @property
       def     UpperCamelCase  (      self) ->     int:




           """simple docstring"""

           return 13






import dataclasses
import json
import warnings
from dataclasses import dataclass, field
from time import time
from typing import List

from ..utils import logging


SCREAMING_SNAKE_CASE :   Dict							      =				logging.get_logger(__name__)



def        UpperCamelCase_(   lowerCamelCase_=None							,				lowerCamelCase_=None				)					->		Tuple:
    return field(default_factory=lambda: default							,				metadata=lowerCamelCase_				)






@dataclass
class _lowerCamelCase:
       lowercase_				:			List[str]									=    list_field(
           default=[],				metadata={
               """help""": (
                   """Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version"""
                   """ of all available models"""
               )
           },				)

       lowercase_				:			List[int]									=    list_field(
           default=[8],				metadata={"""help""": """List of batch sizes for which memory and time performance will be evaluated"""}     )

       lowercase_				:			List[int]									=    list_field(
           default=[8, 32, 1_28, 5_12],				metadata={"""help""": """List of sequence lengths for which memory and time performance will be evaluated"""},				)

       lowercase_				:			bool									=    field(
           default=_a,				metadata={"""help""": """Whether to benchmark inference of model. Inference can be disabled via --no-inference."""},				)
       lowercase_				:			bool									=    field(
           default=_a,				metadata={"""help""": """Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."""},				)
       lowercase_				:			bool									=    field(
           default=_a,				metadata={"""help""": """Whether to run on available tpu devices. TPU can be disabled via --no-tpu."""}     )
       lowercase_				:			bool									=    field(default=_a,				metadata={"""help""": """Use FP16 to accelerate inference."""}     )
       lowercase_				:			bool									=    field(default=_a,				metadata={"""help""": """Benchmark training of model"""}     )
       lowercase_				:			bool									=    field(default=_a,				metadata={"""help""": """Verbose memory tracing"""}     )
       lowercase_				:			bool									=    field(
           default=_a,				metadata={"""help""": """Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."""},				)
       lowercase_				:			bool									=    field(
           default=_a,				metadata={
               """help""": """Whether to perform memory measurements. Memory measurements can be disabled via --no-memory"""
           },				)
       lowercase_				:			bool									=    field(default=_a,				metadata={"""help""": """Trace memory line by line"""}     )
       lowercase_				:			bool									=    field(default=_a,				metadata={"""help""": """Save result to a CSV file"""}     )
       lowercase_				:			bool									=    field(default=_a,				metadata={"""help""": """Save all print statements in a log file"""}     )
       lowercase_				:			bool									=    field(default=_a,				metadata={"""help""": """Whether to print environment information"""}     )
       lowercase_				:			bool									=    field(
           default=_a,				metadata={
               """help""": (
                   """Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use"""
                   """ multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled"""
                   """ for debugging / testing and on TPU."""
               )
           },				)
       lowercase_				:			str									=    field(
           default=F'''inference_time_{round(time()     )}.csv''',				metadata={"""help""": """CSV filename used if saving time results to csv."""},				)
       lowercase_				:			str									=    field(
           default=F'''inference_memory_{round(time()     )}.csv''',				metadata={"""help""": """CSV filename used if saving memory results to csv."""},				)
       lowercase_				:			str									=    field(
           default=F'''train_time_{round(time()     )}.csv''',				metadata={"""help""": """CSV filename used if saving time results to csv for training."""},				)
       lowercase_				:			str									=    field(
           default=F'''train_memory_{round(time()     )}.csv''',				metadata={"""help""": """CSV filename used if saving memory results to csv for training."""},				)
       lowercase_				:			str									=    field(
           default=F'''env_info_{round(time()     )}.csv''',				metadata={"""help""": """CSV filename used if saving environment information."""},				)
       lowercase_				:			str									=    field(
           default=F'''log_{round(time()     )}.csv''',				metadata={"""help""": """Log filename used if print statements are saved in log."""},				)
       lowercase_				:			int									=    field(default=3,				metadata={"""help""": """Times an experiment will be run."""}     )
       lowercase_				:			bool									=    field(
           default=_a,				metadata={
               """help""": (
                   """Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain"""
                   """ model weights."""
               )
           },				)
       def     UpperCamelCase  (      self) ->     Union[str, Any]:




           """simple docstring"""

           warnings.warn(
               F'''The class {self.__class__} is deprecated. Hugging Face Benchmarking utils'''
               ' are deprecated in general and it is advised to use external Benchmarking libraries '
               ' to benchmark Transformer models.',   lowerCamelCase,   )
       def     UpperCamelCase  (      self) ->     Optional[Any]:




           """simple docstring"""

           return json.dumps(dataclasses.asdict(self),   indent=2)
       @property
       def     UpperCamelCase  (      self) ->     List[str]:




           """simple docstring"""

           if len(self.models) <= 0:
               raise ValueError(
                   'Please make sure you provide at least one model name / model identifier, *e.g.* `--models'
                   ' bert-base-cased` or `args.models = [\'bert-base-cased\'].')
           return self.models




       @property
       def     UpperCamelCase  (      self) ->     Dict:




           """simple docstring"""

           if not self.multi_process:
               return False
           elif self.is_tpu:
               logger.info('Multiprocessing is currently not possible on TPU.')
               return False
           else:
               return True


