Salesforce/codegen2-7B_P
26222
1# coding=utf-82# Copyright 2022 Salesforce authors, The EleutherAI, and HuggingFace Teams. All rights reserved.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15""" CodeGen model configuration"""16from collections import OrderedDict17from typing import Any, List, Mapping, Optional18 19from transformers import PreTrainedTokenizer, TensorType, is_torch_available20from transformers.configuration_utils import PretrainedConfig21from transformers.onnx import OnnxConfigWithPast, PatchingSpec22from transformers.utils import logging23 24 25logger = logging.get_logger(__name__)26 27 28CODEGEN_PRETRAINED_CONFIG_ARCHIVE_MAP = {29 "Salesforce/codegen-350M-nl": "https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json",30 "Salesforce/codegen-350M-multi": "https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json",31 "Salesforce/codegen-350M-mono": "https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json",32 "Salesforce/codegen-2B-nl": "https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json",33 "Salesforce/codegen-2B-multi": "https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json",34 "Salesforce/codegen-2B-mono": "https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json",35 "Salesforce/codegen-6B-nl": "https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json",36 "Salesforce/codegen-6B-multi": "https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json",37 "Salesforce/codegen-6B-mono": "https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json",38 "Salesforce/codegen-16B-nl": "https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json",39 "Salesforce/codegen-16B-multi": "https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json",40 "Salesforce/codegen-16B-mono": "https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json",41}42 43 44class CodeGenConfig(PretrainedConfig):45 r"""46 This is the configuration class to store the configuration of a [`CodeGenModel`]. It is used to instantiate a47 CodeGen model according to the specified arguments, defining the model architecture. Instantiating a configuration48 with the defaults will yield a similar configuration to that of the CodeGen49 [Salesforce/codegen-2B-mono](https://huggingface.co/Salesforce/codegen-2B-mono) architecture. Configuration objects50 inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from51 [`PretrainedConfig`] for more information.52 53 Args:54 vocab_size (`int`, *optional*, defaults to 50400):55 Vocabulary size of the CodeGen model. Defines the number of different tokens that can be represented by the56 `inputs_ids` passed when calling [`CodeGenModel`].57 n_positions (`int`, *optional*, defaults to 2048):58 The maximum sequence length that this model might ever be used with. Typically set this to something large59 just in case (e.g., 512 or 1024 or 2048).60 n_embd (`int`, *optional*, defaults to 4096):61 Dimensionality of the embeddings and hidden states.62 n_layer (`int`, *optional*, defaults to 28):63 Number of hidden layers in the Transformer encoder.64 n_head (`int`, *optional*, defaults to 16):65 Number of attention heads for each attention layer in the Transformer encoder.66 rotary_dim (`int`, *optional*, defaults to 64):67 Number of dimensions in the embedding that Rotary Position Embedding is applied to.68 n_inner (`int`, *optional*, defaults to None):69 Dimensionality of the inner feed-forward layers. `None` will set it to 4 times n_embd70 activation_function (`str`, *optional*, defaults to `"gelu_new"`):71 Activation function, to be selected in the list `["relu", "silu", "gelu", "tanh", "gelu_new"]`.72 resid_pdrop (`float`, *optional*, defaults to 0.1):73 The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.74 embd_pdrop (`int`, *optional*, defaults to 0.1):75 The dropout ratio for the embeddings.76 attn_pdrop (`float`, *optional*, defaults to 0.1):77 The dropout ratio for the attention.78 layer_norm_epsilon (`float`, *optional*, defaults to 1e-5):79 The epsilon to use in the layer normalization layers.80 initializer_range (`float`, *optional*, defaults to 0.02):81 The standard deviation of the truncated_normal_initializer for initializing all weight matrices.82 scale_attn_weights (`bool`, *optional*, defaults to `True`):83 Scale attention weights by dividing by sqrt(hidden_size).84 use_cache (`bool`, *optional*, defaults to `True`):85 Whether or not the model should return the last key/values attentions (not used by all models).86 87 Example:88 89 ```python90 >>> from transformers import CodeGenModel, CodeGenConfig91 92 >>> # Initializing a CodeGen 6B configuration93 >>> configuration = CodeGenConfig()94 95 >>> # Initializing a model from the configuration96 >>> model = CodeGenModel(configuration)97 98 >>> # Accessing the model configuration99 >>> configuration = model.config100 ```"""101 model_type = "codegen"102 attribute_map = {103 "max_position_embeddings": "n_positions",104 "hidden_size": "n_embd",105 "num_attention_heads": "n_head",106 "num_hidden_layers": "n_layer",107 }108 109 def __init__(110 self,111 vocab_size=50400,112 n_positions=2048,113 n_ctx=2048,114 n_embd=4096,115 n_layer=28,116 n_head=16,117 rotary_dim=64,118 n_inner=None,119 activation_function="gelu_new",120 resid_pdrop=0.0,121 embd_pdrop=0.0,122 attn_pdrop=0.0,123 layer_norm_epsilon=1e-5,124 initializer_range=0.02,125 scale_attn_weights=True,126 use_cache=True,127 bos_token_id=50256,128 eos_token_id=50256,129 tie_word_embeddings=False,130 **kwargs131 ):132 self.vocab_size = vocab_size133 self.n_ctx = n_ctx134 self.n_positions = n_positions135 self.n_embd = n_embd136 self.n_layer = n_layer137 self.n_head = n_head138 self.n_inner = n_inner139 self.rotary_dim = rotary_dim140 self.activation_function = activation_function141 self.resid_pdrop = resid_pdrop142 self.embd_pdrop = embd_pdrop143 self.attn_pdrop = attn_pdrop144 self.layer_norm_epsilon = layer_norm_epsilon145 self.initializer_range = initializer_range146 self.scale_attn_weights = scale_attn_weights147 self.use_cache = use_cache148 149 self.bos_token_id = bos_token_id150 self.eos_token_id = eos_token_id151 152 super().__init__(153 bos_token_id=bos_token_id, eos_token_id=eos_token_id, tie_word_embeddings=tie_word_embeddings, **kwargs154 )155 156 157# Copied from transformers.models.gpt2.configuration_gpt2.GPT2OnnxConfig158class CodeGenOnnxConfig(OnnxConfigWithPast):159 def __init__(160 self,161 config: PretrainedConfig,162 task: str = "default",163 patching_specs: List[PatchingSpec] = None,164 use_past: bool = False,165 ):166 super().__init__(config, task=task, patching_specs=patching_specs, use_past=use_past)167 if not getattr(self._config, "pad_token_id", None):168 # TODO: how to do that better?169 self._config.pad_token_id = 0170 171 @property172 def inputs(self) -> Mapping[str, Mapping[int, str]]:173 common_inputs = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}})174 if self.use_past:175 self.fill_with_past_key_values_(common_inputs, direction="inputs")176 common_inputs["attention_mask"] = {0: "batch", 1: "past_sequence + sequence"}177 else:178 common_inputs["attention_mask"] = {0: "batch", 1: "sequence"}179 180 return common_inputs181 182 @property183 def num_layers(self) -> int:184 return self._config.n_layer185 186 @property187 def num_attention_heads(self) -> int:188 return self._config.n_head189 190 def generate_dummy_inputs(191 self,192 tokenizer: PreTrainedTokenizer,193 batch_size: int = -1,194 seq_length: int = -1,195 is_pair: bool = False,196 framework: Optional[TensorType] = None,197 ) -> Mapping[str, Any]:198 common_inputs = super(OnnxConfigWithPast, self).generate_dummy_inputs(199 tokenizer, batch_size=batch_size, seq_length=seq_length, is_pair=is_pair, framework=framework200 )201 202 # We need to order the input in the way they appears in the forward()203 ordered_inputs = OrderedDict({"input_ids": common_inputs["input_ids"]})204 205 # Need to add the past_keys206 if self.use_past:207 if not is_torch_available():208 raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed.")209 else:210 import torch211 212 batch, seqlen = common_inputs["input_ids"].shape213 # Not using the same length for past_key_values214 past_key_values_length = seqlen + 2215 past_shape = (216 batch,217 self.num_attention_heads,218 past_key_values_length,219 self._config.hidden_size // self.num_attention_heads,220 )221 ordered_inputs["past_key_values"] = [222 (torch.zeros(past_shape), torch.zeros(past_shape)) for _ in range(self.num_layers)223 ]224 225 ordered_inputs["attention_mask"] = common_inputs["attention_mask"]226 if self.use_past:227 mask_dtype = ordered_inputs["attention_mask"].dtype228 ordered_inputs["attention_mask"] = torch.cat(229 [ordered_inputs["attention_mask"], torch.ones(batch, past_key_values_length, dtype=mask_dtype)], dim=1230 )231 232 return ordered_inputs233 234 @property235 def default_onnx_opset(self) -> int:236 return 13