-
Notifications
You must be signed in to change notification settings - Fork 29
[feat] Hybrid Mamba model with Mamba and discrete Mamba 2 layers #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
ad2b8d5
wip
oleksost 5dbc72a
wip
oleksost 5213e9e
WIP
oleksost 01d0fe4
mamba1 block
oleksost ae8f3ca
removed build from remote
oleksost fa6d3bc
removed unneccesary tests
oleksost 963e674
removed unneccesary files
oleksost 0842fcb
test
oleksost 1c81719
test mamba1
oleksost 37ba0d5
tensor dimentions
oleksost 11a5db3
meta init with full model run
oleksost 4af7eb7
training, but having backward issues
oleksost be93749
integration into training pipeline
oleksost dd469bc
mamba2
oleksost ebe1b75
renamed config + skip test
oleksost a4400fd
skip tests if mamba not installed
oleksost c49148c
pre-commits
oleksost 5c8d930
cleanup
oleksost ef6791b
dependencies
oleksost f03dd10
descrete mamba2
oleksost 2414252
Merge branch 'ssm_mamba2' into ssm
oleksost f4d411d
test
oleksost ee86c68
llamba checkpoint converter
oleksost 2561738
cleanup
oleksost ad8a48c
test
oleksost 5243a88
Merge branch 'main' into ssm
oleksost 075a31f
mamba force build
oleksost a788989
mamba force build
oleksost 2700660
mamba force build
oleksost baaf714
causal conv skip build
oleksost 833b586
Merge branch 'main' into ssm
oleksost 9e2897d
docs.yaml
oleksost b231cb8
MTP hardcoded
oleksost 8ccaa28
import nvm
oleksost 864fff2
remove dependency on cartesia
oleksost 7f2b35f
save llamba
oleksost 81c71af
addressed comments
oleksost 7b7ce62
addressed comments
oleksost 776e67b
Merge branch 'main' into ssm
oleksost 3456884
nvm
oleksost b48f68d
renamed block pattern into block layout
oleksost 9a35783
renames
oleksost 32b8aa1
nvm
oleksost 4f9aad0
wip
oleksost 68de5d1
addressed comments
oleksost ebc516a
Merge branch 'main' into ssm
oleksost cb95e52
wip
oleksost 79c9a4b
batch config
oleksost bb3ba66
clean up
oleksost a5297be
nvm
oleksost 2d39857
tests
oleksost df032b5
nvm
oleksost c8fdbb9
identity activation into MLP
oleksost File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
from fast_llm.config import Field, FieldHint, FieldUpdate, check_field, config_class | ||
from fast_llm.engine.base_model.config import BaseModelArchitectureConfig, BaseModelConfig | ||
from fast_llm.functional.config import ActivationType | ||
from fast_llm.layers.common.config import NormalizationArchitectureConfig, NormalizationConfig | ||
from fast_llm.utils import Assert | ||
|
||
|
||
class SSMDimNames: | ||
model_dim = "model_dim" # Model dimension (D) | ||
state_dim = "state_dim" # State dimension (N) | ||
conv_dim = "conv_dim" # Dimension of the conv1d input in mamba layers | ||
inner_dim = "inner_dim" # Inner dimension after expansion | ||
dt_rank = "dt_rank" # Rank of Ξ | ||
inner_proj_mamba = "inner_proj_mamba" # Inner projection dimension for mamba | ||
inner_proj_mamba2 = "inner_proj_mamba2" # Inner projection dimension for mamba2 | ||
x_proj_dim = "x_proj_dim" # X projection dimension | ||
head_dim = "head_dim" # Dimension of the mamba2 head (P) | ||
conv_kernel_size = "conv_kernel_size" # Kernel size of the conv1d in mamba layers | ||
qk_heads = "qk_heads" # Number of QK heads | ||
v_heads = "v_heads" # Number of V heads | ||
|
||
|
||
@config_class() | ||
class SSMArchitectureConfig(BaseModelArchitectureConfig): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please adjust field names for our naming conventions. |
||
_abstract = False | ||
|
||
# Normalization | ||
normalization: NormalizationArchitectureConfig = Field( | ||
default_factory=NormalizationArchitectureConfig, | ||
desc="Configuration for the normalization layers architecture.", | ||
hint=FieldHint.core, | ||
) | ||
|
||
expansion_factor: int = Field( | ||
default=2, desc="Expansion factor for Mamba blocks.", hint=FieldHint.core, valid=check_field(Assert.gt, 0) | ||
) | ||
|
||
state_size: int = Field( | ||
default=16, | ||
desc="State size for Mamba blocks.", | ||
hint=FieldHint.core, | ||
valid=check_field(Assert.gt, 0), | ||
) | ||
conv_kernel_dimension: int = Field( | ||
default=4, | ||
desc="Conv kernel dimension for Mamba blocks.", | ||
hint=FieldHint.core, | ||
valid=check_field(Assert.gt, 0), | ||
) | ||
|
||
# Layer parameters | ||
add_bias_linear: bool = Field( | ||
default=False, | ||
desc="Whether to use bias in SSM layers", | ||
hint=FieldHint.core, | ||
) | ||
|
||
dt_rank: int = Field( | ||
default=None, | ||
desc="Rank of the Ξ projection matrix. If 'None', will be set to ceil(hidden_size/16)", | ||
hint=FieldHint.core, | ||
) | ||
|
||
chunk_size: int = Field( | ||
default=256, | ||
desc="Chunk size for Mamba2 blocks.", | ||
hint=FieldHint.core, | ||
) | ||
|
||
n_qk_heads: int = Field( | ||
default=32, | ||
desc="Number of QK heads for Mamba2 blocks.", | ||
hint=FieldHint.core, | ||
) | ||
|
||
n_v_heads: int = Field( | ||
default=32, | ||
desc="Number of V heads for Mamba2 blocks.", | ||
hint=FieldHint.core, | ||
) | ||
|
||
activation_type: ActivationType = Field( | ||
default=None, | ||
desc="The MLP intermediate activation type. Default: SiLU for gated MLP, GeLU otherwise.", | ||
hint=FieldHint.core, | ||
) | ||
|
||
def _validate(self) -> None: | ||
with self._set_implicit_default(): | ||
if self.activation_type is None: | ||
self.activation_type = ActivationType.silu | ||
if self.dt_rank is None: | ||
self.dt_rank = -1 # set to -1, it will be overwrittem in ssm validation | ||
|
||
super()._validate() | ||
|
||
|
||
@config_class() | ||
class SSMConfig(SSMArchitectureConfig, BaseModelConfig): | ||
"""Configuration for a Structured State Space Model (SSM) layer.""" | ||
|
||
normalization: NormalizationConfig = FieldUpdate(default_factory=NormalizationConfig) | ||
|
||
debug_ssm: bool = Field( | ||
default=False, | ||
desc="debug_ssm", | ||
hint=FieldHint.optional, | ||
) | ||
|
||
dt_min: float = Field( | ||
default=0.001, | ||
desc="Minimum step size for discretization", | ||
hint=FieldHint.core, | ||
valid=check_field(Assert.gt, 0), | ||
) | ||
|
||
dt_max: float = Field( | ||
default=0.1, | ||
desc="Maximum step size for discretization", | ||
hint=FieldHint.core, | ||
valid=check_field(Assert.gt, 0), | ||
) | ||
|
||
dt_init_floor: float = Field( | ||
oleksost marked this conversation as resolved.
Show resolved
Hide resolved
|
||
default=1e-4, | ||
desc="Minimum value for initializing dt", | ||
hint=FieldHint.core, | ||
valid=check_field(Assert.gt, 0), | ||
) | ||
|
||
def _validate(self) -> None: | ||
"""Validate configuration parameters.""" | ||
|
||
super()._validate() | ||
Assert.geq(self.dt_max, self.dt_min) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add support for it in the MLP? (I know it's triton but this one is trivial.)
https://github.com/ServiceNow/Fast-LLM/blob/main/fast_llm/functional/triton/mlp.py Or otherwise prevent it in the config?