Skip to content

Commit b08e445

Browse files
authored
[NEW Model] Add mamba (#8513)
* add mamba * update * do not shift * update * push * mamba 单测pass * mamba * update_model_kwargs_for_generation * fix tests * test * test * add tokenizer test
1 parent 8599a53 commit b08e445

File tree

13 files changed

+2023
-3
lines changed

13 files changed

+2023
-3
lines changed

paddlenlp/transformers/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,6 @@
300300
from .qwen2 import *
301301
from .qwen2_moe import *
302302
from .yuan import *
303+
from .mamba.configuration import *
304+
from .mamba.modeling import *
305+
from .mamba.tokenizer import *

paddlenlp/transformers/auto/modeling.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
("Qwen2Moe", "qwen2_moe"),
124124
("Gemma", "gemma"),
125125
("Yuan", "yuan"),
126+
("Mamba", "mamba"),
126127
]
127128
)
128129

paddlenlp/transformers/auto/tokenizer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
("QWenTokenizer", "qwen"),
100100
("GemmaTokenizer", "gemma"),
101101
("YuanTokenizer", "yuan"),
102+
("MambaTokenizer", "mamba"),
102103
]
103104
)
104105

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from .configuration import *
17+
from .modeling import *
18+
from .tokenizer import *
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# coding=utf-8
2+
# Copyright 2024 The HuggingFace Inc. team.
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+
"""MAMBA configuration"""
16+
17+
import math
18+
19+
from ..configuration_utils import PretrainedConfig
20+
21+
__all__ = ["MambaConfig"]
22+
23+
24+
class MambaConfig(PretrainedConfig):
25+
"""
26+
This is the configuration class to store the configuration of a [`MambaModel`]. It is used to instantiate a MAMBA
27+
model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
28+
defaults will yield a similar configuration to that of the MAMBA
29+
[state-spaces/mamba-2.8b](https://huggingface.co/state-spaces/mamba-2.8b) architecture.
30+
31+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
32+
documentation from [`PretrainedConfig`] for more information.
33+
34+
35+
Args:
36+
vocab_size (`int`, *optional*, defaults to 50280):
37+
Vocabulary size of the MAMBA model. Defines the number of different tokens that can be represented by the
38+
`inputs_ids` passed when calling [`MambaModel`].
39+
hidden_size (`int`, *optional*, defaults to 768):
40+
Dimensionality of the embeddings and hidden states.
41+
state_size (`int`, *optional*, defaults to 16): shape of the state space latents.
42+
num_hidden_layers (`int`, *optional*, defaults to 32):
43+
Number of hidden layers in the model.
44+
layer_norm_epsilon (`float`, *optional*, defaults to 1e-05):
45+
The epsilon to use in the layer normalization layers.
46+
pad_token_id (`int`, *optional*, defaults to 0):
47+
Padding token id.
48+
bos_token_id (`int`, *optional*, defaults to 0):
49+
The id of the beginning of sentence token in the vocabulary.
50+
eos_token_id (`int`, *optional*, defaults to 0):
51+
The id of the end of sentence token in the vocabulary.
52+
expand (`int`, *optional*, defaults to 2): Expanding factor used to determine the intermediate size.
53+
conv_kernel (`int`, *optional*, defaults to 4): Size of the convolution kernel.
54+
use_bias (`bool`, *optional*, defaults to `False`):
55+
Whether or not to use bias in ["in_proj", "out_proj"] of the mixer block
56+
use_conv_bias (`bool`, *optional*, defaults to `True`):
57+
Whether or not to use bias in the convolution layer of the mixer block.
58+
hidden_act (`str`, *optional*, defaults to `"silu"`):
59+
The non-linear activation function (function or string) in the decoder.
60+
initializer_range (`float`, *optional*, defaults to 0.1):
61+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
62+
residual_in_fp32 (`bool`, *optional*, defaults to `True`):
63+
Whether or not residuals should be in `float32`. If set to `False` residuals will keep the same `dtype` as the rest of the model
64+
time_step_rank (`Union[int,str]`, *optional*, defaults to `"auto"`):
65+
Rank of the discretization projection matrix. `"auto"` means that it will default to `math.ceil(self.hidden_size / 16)`
66+
time_step_scale (`float`, *optional*, defaults to 1.0):
67+
Scale used used to scale `dt_proj.bias`.
68+
time_step_min (`float`, *optional*, defaults to 0.001):
69+
Minimum `time_step` used to bound `dt_proj.bias`.
70+
time_step_max (`float`, *optional*, defaults to 0.1):
71+
Maximum `time_step` used to bound `dt_proj.bias`.
72+
time_step_init_scheme (`float`, *optional*, defaults to `"random"`):
73+
Init scheme used for `dt_proj.weight`. Should be one of `["random","uniform"]`
74+
time_step_floor (`float`, *optional*, defaults to 0.0001):
75+
Minimum clamping value of the `dt_proj.bias` layer initialization.
76+
rescale_prenorm_residual (`bool`, *optional*, defaults to `False`):
77+
Whether or not to rescale `out_proj` weights when initializing.
78+
use_cache (`bool`, *optional*, defaults to `True`):
79+
Whether or not the cache should be used.
80+
81+
82+
Example:
83+
84+
```python
85+
>>> from paddlenlp.transformers import MambaConfig, MambaModel
86+
87+
>>> # Initializing a Mamba configuration
88+
>>> configuration = MambaConfig()
89+
90+
>>> # Initializing a model (with random weights) from the configuration
91+
>>> model = MambaModel(configuration)
92+
93+
>>> # Accessing the model configuration
94+
>>> configuration = model.config
95+
```"""
96+
97+
model_type = "mamba"
98+
99+
def __init__(
100+
self,
101+
vocab_size=50280,
102+
hidden_size=768,
103+
state_size=16,
104+
num_hidden_layers=32,
105+
layer_norm_epsilon=1e-5,
106+
pad_token_id=0,
107+
bos_token_id=0,
108+
eos_token_id=0,
109+
expand=2,
110+
conv_kernel=4,
111+
use_bias=False,
112+
use_conv_bias=True,
113+
hidden_act="silu",
114+
initializer_range=0.1,
115+
residual_in_fp32=True,
116+
time_step_rank="auto",
117+
time_step_scale=1.0,
118+
time_step_min=0.001,
119+
time_step_max=0.1,
120+
time_step_init_scheme="random",
121+
time_step_floor=1e-4,
122+
rescale_prenorm_residual=False,
123+
use_cache=True,
124+
**kwargs,
125+
):
126+
kwargs["return_dict"] = kwargs.pop("return_dict", True)
127+
super().__init__(bos_token_id=bos_token_id, eos_token_id=eos_token_id, pad_token_id=pad_token_id, **kwargs)
128+
self.vocab_size = vocab_size
129+
self.hidden_size = hidden_size
130+
self.state_size = state_size
131+
self.num_hidden_layers = num_hidden_layers
132+
self.layer_norm_epsilon = layer_norm_epsilon
133+
self.conv_kernel = conv_kernel
134+
self.expand = expand
135+
self.intermediate_size = int(expand * self.hidden_size)
136+
self.bos_token_id = bos_token_id
137+
self.eos_token_id = eos_token_id
138+
self.pad_token_id = pad_token_id
139+
self.use_bias = use_bias
140+
self.use_conv_bias = use_conv_bias
141+
self.hidden_act = hidden_act
142+
self.initializer_range = initializer_range
143+
self.time_step_rank = math.ceil(self.hidden_size / 16) if time_step_rank == "auto" else time_step_rank
144+
self.time_step_scale = time_step_scale
145+
self.time_step_min = time_step_min
146+
self.time_step_max = time_step_max
147+
self.time_step_init_scheme = time_step_init_scheme
148+
self.time_step_floor = time_step_floor
149+
self.rescale_prenorm_residual = rescale_prenorm_residual
150+
self.residual_in_fp32 = residual_in_fp32
151+
self.use_cache = use_cache

0 commit comments

Comments
 (0)