Upload configuration_sdlm.py with huggingface_hub
Browse files- configuration_sdlm.py +147 -0
configuration_sdlm.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2024 The Qwen team, Alibaba Group and the HuggingFace Inc. team. 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 at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# 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 and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
""" Qwen2 model configuration"""
|
| 16 |
+
|
| 17 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 18 |
+
from transformers.utils import logging
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
logger = logging.get_logger(__name__)
|
| 22 |
+
|
| 23 |
+
QWEN2_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
| 24 |
+
"Qwen/Qwen2-7B-beta": "https://huggingface.co/Qwen/Qwen2-7B-beta/resolve/main/config.json",
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
class SDLMQwen2Config(PretrainedConfig):
|
| 28 |
+
r"""
|
| 29 |
+
This is the configuration class to store the configuration of a [`Qwen2Model`]. It is used to instantiate a
|
| 30 |
+
Qwen2 model according to the specified arguments, defining the model architecture. Instantiating a configuration
|
| 31 |
+
with the defaults will yield a similar configuration to that of
|
| 32 |
+
Qwen2-7B-beta [Qwen/Qwen2-7B-beta](https://huggingface.co/Qwen/Qwen2-7B-beta).
|
| 33 |
+
|
| 34 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 35 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
vocab_size (`int`, *optional*, defaults to 151936):
|
| 40 |
+
Vocabulary size of the Qwen2 model. Defines the number of different tokens that can be represented by the
|
| 41 |
+
`inputs_ids` passed when calling [`Qwen2Model`]
|
| 42 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
| 43 |
+
Dimension of the hidden representations.
|
| 44 |
+
intermediate_size (`int`, *optional*, defaults to 22016):
|
| 45 |
+
Dimension of the MLP representations.
|
| 46 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
| 47 |
+
Number of hidden layers in the Transformer encoder.
|
| 48 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
| 49 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 50 |
+
num_key_value_heads (`int`, *optional*, defaults to 32):
|
| 51 |
+
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
|
| 52 |
+
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
|
| 53 |
+
`num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
|
| 54 |
+
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
|
| 55 |
+
by meanpooling all the original heads within that group. For more details checkout [this
|
| 56 |
+
paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to `32`.
|
| 57 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
| 58 |
+
The non-linear activation function (function or string) in the decoder.
|
| 59 |
+
max_position_embeddings (`int`, *optional*, defaults to 32768):
|
| 60 |
+
The maximum sequence length that this model might ever be used with.
|
| 61 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 62 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
| 63 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-06):
|
| 64 |
+
The epsilon used by the rms normalization layers.
|
| 65 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
| 66 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
| 67 |
+
relevant if `config.is_decoder=True`.
|
| 68 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
| 69 |
+
Whether the model's input and output word embeddings should be tied.
|
| 70 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
| 71 |
+
The base period of the RoPE embeddings.
|
| 72 |
+
use_sliding_window (`bool`, *optional*, defaults to `False`):
|
| 73 |
+
Whether to use sliding window attention.
|
| 74 |
+
sliding_window (`int`, *optional*, defaults to 4096):
|
| 75 |
+
Sliding window attention (SWA) window size. If not specified, will default to `4096`.
|
| 76 |
+
max_window_layers (`int`, *optional*, defaults to 28):
|
| 77 |
+
The number of layers that use SWA (Sliding Window Attention). The bottom layers use SWA while the top use full attention.
|
| 78 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 79 |
+
The dropout ratio for the attention probabilities.
|
| 80 |
+
|
| 81 |
+
```python
|
| 82 |
+
>>> from transformers import Qwen2Model, Qwen2Config
|
| 83 |
+
|
| 84 |
+
>>> # Initializing a Qwen2 style configuration
|
| 85 |
+
>>> configuration = Qwen2Config()
|
| 86 |
+
|
| 87 |
+
>>> # Initializing a model from the Qwen2-7B style configuration
|
| 88 |
+
>>> model = Qwen2Model(configuration)
|
| 89 |
+
|
| 90 |
+
>>> # Accessing the model configuration
|
| 91 |
+
>>> configuration = model.config
|
| 92 |
+
```"""
|
| 93 |
+
|
| 94 |
+
model_type = "qwen2"
|
| 95 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
| 96 |
+
|
| 97 |
+
def __init__(
|
| 98 |
+
self,
|
| 99 |
+
vocab_size=151936,
|
| 100 |
+
hidden_size=4096,
|
| 101 |
+
intermediate_size=22016,
|
| 102 |
+
num_hidden_layers=32,
|
| 103 |
+
num_attention_heads=32,
|
| 104 |
+
num_key_value_heads=32,
|
| 105 |
+
hidden_act="silu",
|
| 106 |
+
max_position_embeddings=32768,
|
| 107 |
+
initializer_range=0.02,
|
| 108 |
+
rms_norm_eps=1e-6,
|
| 109 |
+
use_cache=True,
|
| 110 |
+
tie_word_embeddings=False,
|
| 111 |
+
rope_theta=10000.0,
|
| 112 |
+
use_sliding_window=False,
|
| 113 |
+
sliding_window=4096,
|
| 114 |
+
max_window_layers=28,
|
| 115 |
+
attention_dropout=0.0,
|
| 116 |
+
**kwargs,
|
| 117 |
+
):
|
| 118 |
+
self.vocab_size = vocab_size
|
| 119 |
+
self.max_position_embeddings = max_position_embeddings
|
| 120 |
+
self.hidden_size = hidden_size
|
| 121 |
+
self.intermediate_size = intermediate_size
|
| 122 |
+
self.num_hidden_layers = num_hidden_layers
|
| 123 |
+
self.num_attention_heads = num_attention_heads
|
| 124 |
+
self.use_sliding_window = use_sliding_window
|
| 125 |
+
self.sliding_window = sliding_window
|
| 126 |
+
self.max_window_layers = max_window_layers
|
| 127 |
+
|
| 128 |
+
# for backward compatibility
|
| 129 |
+
if num_key_value_heads is None:
|
| 130 |
+
num_key_value_heads = num_attention_heads
|
| 131 |
+
|
| 132 |
+
self.num_key_value_heads = num_key_value_heads
|
| 133 |
+
self.hidden_act = hidden_act
|
| 134 |
+
self.initializer_range = initializer_range
|
| 135 |
+
self.rms_norm_eps = rms_norm_eps
|
| 136 |
+
self.use_cache = use_cache
|
| 137 |
+
self.rope_theta = rope_theta
|
| 138 |
+
self.attention_dropout = attention_dropout
|
| 139 |
+
if kwargs.get('attn_implementation', None) is None:
|
| 140 |
+
self.attn_implementation = kwargs['attn_implementation'] = 'flash_attention_2'
|
| 141 |
+
else:
|
| 142 |
+
self.attn_implementation = kwargs['attn_implementation']
|
| 143 |
+
|
| 144 |
+
super().__init__(
|
| 145 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 146 |
+
**kwargs,
|
| 147 |
+
)
|