Skip to content
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

[Model] FalconMamba Support #9325

Merged
merged 10 commits into from
Oct 21, 2024

Conversation

dhiaEddineRhaiem
Copy link
Contributor

@dhiaEddineRhaiem dhiaEddineRhaiem commented Oct 13, 2024

This PR adds the support for FalconMamba in VLLM.
In summary , FalconMamba is the same Mamba class without the feed forward block and with the B_C_dt_rms LayerNorm.
Note that FalconMamba is now supported in transformers>=4.45.
I tried with this model https://huggingface.co/tiiuae/falcon-mamba-7b-instruct/ and it worked well locally.
Fixes: #7478

BEFORE SUBMITTING, PLEASE READ THE CHECKLIST BELOW AND FILL IN THE DESCRIPTION ABOVE


PR Checklist (Click to Expand)

Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.

PR Title and Classification

Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:

  • [Bugfix] for bug fixes.
  • [CI/Build] for build or continuous integration improvements.
  • [Doc] for documentation fixes and improvements.
  • [Model] for adding a new model or improving an existing model. Model name should appear in the title.
  • [Frontend] For changes on the vLLM frontend (e.g., OpenAI API server, LLM class, etc.)
  • [Kernel] for changes affecting CUDA kernels or other compute kernels.
  • [Core] for changes in the core vLLM logic (e.g., LLMEngine, AsyncLLMEngine, Scheduler, etc.)
  • [Hardware][Vendor] for hardware-specific changes. Vendor name should appear in the prefix (e.g., [Hardware][AMD]).
  • [Misc] for PRs that do not fit the above categories. Please use this sparingly.

Note: If the PR spans more than one category, please include all relevant prefixes.

Code Quality

The PR need to meet the following code quality standards:

  • We adhere to Google Python style guide and Google C++ style guide.
  • Pass all linter checks. Please use format.sh to format your code.
  • The code need to be well-documented to ensure future contributors can easily understand the code.
  • Include sufficient tests to ensure the project to stay correct and robust. This includes both unit tests and integration tests.
  • Please add documentation to docs/source/ if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.

Adding or changing kernels

Each custom kernel needs a schema and one or more implementations to be registered with PyTorch.

  • Make sure custom ops are registered following PyTorch guidelines: Custom C++ and CUDA Operators and The Custom Operators Manual
  • Custom operations that return Tensors require meta-functions. Meta-functions should be implemented and registered in python so that dynamic dims can be handled automatically. See above documents for a description of meta-functions.
  • Use torch.libary.opcheck() to test the function registration and meta-function for any registered ops. See tests/kernels for examples.
  • When changing the C++ signature of an existing op, the schema must be updated to reflect the changes.
  • If a new custom type is needed, see the following document: Custom Class Support in PT2.

Notes for Large Changes

Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with rfc-required and might not go through the PR.

What to Expect for the Reviews

The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:

  • After the PR is submitted, the PR will be assigned to a reviewer. Every reviewer will pick up the PRs based on their expertise and availability.
  • After the PR is assigned, the reviewer will provide status update every 2-3 days. If the PR is not reviewed within 7 days, please feel free to ping the reviewer or the vLLM team.
  • After the review, the reviewer will put an action-required label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR.
  • Please respond to all comments within a reasonable time frame. If a comment isn't clear or you disagree with a suggestion, feel free to ask for clarification or discuss the suggestion.

Thank You

Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!

Copy link

👋 Hi! Thank you for contributing to the vLLM project.
Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can do one of these:

  • Add ready label to the PR
  • Enable auto-merge.

🚀

hidden_size: int,
eps: float = 1e-6,
var_hidden_size: Optional[int] = None,
is_learnable: bool = True) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RMSNorm weights are non learnable for FalconMamba model.
The idea is to add support for non learnable RMSNorm weights, so we can benefit from the same forward types of this class.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain this a bit more? It seems like this might have been done to work around some issues that popped up during weight loading. Is that right?

And am I right that the weights will always be 1.0 for Falcon Mamba, i.e. we could skip the application of the weights for dt_layernorm, b_layernorm, and c_layernorm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The idea is to register weights as parameters when they are learnable and register them as buffers whenever they are not so that they will not be included in the state_dict of the model.
    the same logic is applied here https://pytorch.org/docs/stable/_modules/torch/nn/modules/normalization.html#RMSNorm (pytorch implementation of RMSNorm)

2.Yes , you are right.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation -- I think it would be better to handle this in FalconMambaForCausalLM.load_weights, since it's a special case that only applies to FalconMamba currently.

In load_weights, could you add a condition to check if dt_layernorm, b_layernorm, or c_layernorm is in the name? If this is the case, we can set the weight loader to a function that explicitly sets all of the elements to 1.0, which will make things explicitly clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review

I managed to integrate FalconMamba inside mamba.py.

for rmsnorm , i reveretd the changes , but i think there is no need to handle dt_layernorm, b_layernorm, or c_layernorm inside load_weights since they have been initialised as nn.parameters(torch.ones(hidden_size)) inside RMSNorm initial implementation which is compatible with FalconMamba dt,b,c rmsnorms.

Copy link
Collaborator

@tlrmchlsmth tlrmchlsmth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, thanks for the contribution!

I left a few inline comments. Biggest thing for me is to understand what's happening with the layernorm weight to see if this is the right approach.

In a subsequent PR, we should definitely factor out the mixer layer (cc @mzusman if you still plan to tackle it, or I can do it as well)


from ...utils import check_outputs_equal

MODELS = ["tiiuae/falcon-mamba-tiny-dev"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding the test_falcon_mamba.py file, could you add this to the list of models in test_mamba.py?

@@ -11,7 +11,7 @@ Text-only Language Models
^^^^^^^^^^^^^^^^^^^^^^^^^

Text Generation
---------------
---------------
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove spurious whitespace

@@ -156,7 +161,7 @@ Text Generation
- Mamba
- :code:`state-spaces/mamba-130m-hf`, :code:`state-spaces/mamba-790m-hf`, :code:`state-spaces/mamba-2.8b-hf`, etc.
- ✅︎
-
-
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, spurious whitespace

hidden_size: int,
eps: float = 1e-6,
var_hidden_size: Optional[int] = None,
is_learnable: bool = True) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain this a bit more? It seems like this might have been done to work around some issues that popped up during weight loading. Is that right?

And am I right that the weights will always be 1.0 for Falcon Mamba, i.e. we could skip the application of the weights for dt_layernorm, b_layernorm, and c_layernorm?

self.layer_idx = layer_idx
self.config = config
self.mixer = FalconMambaMixer(config, layer_idx)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering: Is there no feed-forward section for FalconMamba?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is no feed forward section in Falcon Mamba , it is a sequence of consecutive mamba1 blocks with extra RMSNorm on input dependant parameters.

Comment on lines 444 to 449
# (param_name, shard_name, shard_id)
("qkv_proj", "q_proj", "q"),
("qkv_proj", "k_proj", "k"),
("qkv_proj", "v_proj", "v"),
("gate_up_proj", "gate_proj", 0),
("gate_up_proj", "up_proj", 1),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no stacked params at all, right? So I think all of this can be deleted

Comment on lines 41 to 46
@dataclass
class FalconMambaCacheParams:
is_prompt: bool = False
conv_state: torch.Tensor = torch.Tensor()
ssm_state: torch.Tensor = torch.Tensor()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this isn't used anywhere, could you remove it? (Needs to be cleaned up in mamba.py too -- I can send a PR to do that)

Comment on lines 324 to 335
packed_modules_mapping = {
"qkv_proj": [
"q_proj",
"k_proj",
"v_proj",
],
}

# LoRA specific attributes
supported_lora_modules = [
"qkv_proj",
"o_proj",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The references to qkv_proj and o_proj should be removed since they aren't relevant to this model

Comment on lines 460 to 461
if ".self_attn." in name:
name = name.replace(".self_attn", "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be deleted as well, no?

Comment on lines 463 to 474
for param_name, weight_name, shard_id in stacked_params_mapping:
if weight_name not in name:
continue
name = name.replace(weight_name, param_name)
# Skip loading extra bias for GPTQ models.
if name.endswith(".bias") and name not in params_dict:
continue
param = params_dict[name]
weight_loader = param.weight_loader
weight_loader(param, loaded_weight, shard_id)
break
else:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then if there are no stacked params, I think this branch of the if statement can be removed.

Copy link
Collaborator

@tlrmchlsmth tlrmchlsmth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about modifying mamba.py to support FalconMamba instead of adding it in its own file? Seems like you would only need to handle the additional layernorms in a couple of spots

@@ -156,7 +161,7 @@ Text Generation
- Mamba
- :code:`state-spaces/mamba-130m-hf`, :code:`state-spaces/mamba-790m-hf`, :code:`state-spaces/mamba-2.8b-hf`, etc.
- ✅︎
-
-
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: spurious whitespace

hidden_size: int,
eps: float = 1e-6,
var_hidden_size: Optional[int] = None,
is_learnable: bool = True) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation -- I think it would be better to handle this in FalconMambaForCausalLM.load_weights, since it's a special case that only applies to FalconMamba currently.

In load_weights, could you add a condition to check if dt_layernorm, b_layernorm, or c_layernorm is in the name? If this is the case, we can set the weight loader to a function that explicitly sets all of the elements to 1.0, which will make things explicitly clear.

Copy link
Collaborator

@tlrmchlsmth tlrmchlsmth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you for the contribution!

@@ -27,7 +27,6 @@ def __init__(
self.variance_epsilon = eps
self.variance_size_override = (None if var_hidden_size == hidden_size
else var_hidden_size)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: best not to introduce whitespace-only changes to files

@tlrmchlsmth tlrmchlsmth added the ready ONLY add when PR is ready to merge/full CI is needed label Oct 20, 2024
@tlrmchlsmth tlrmchlsmth merged commit f6b9729 into vllm-project:main Oct 21, 2024
71 checks passed
charlifu pushed a commit to charlifu/vllm that referenced this pull request Oct 23, 2024
vrdn-23 pushed a commit to vrdn-23/vllm that referenced this pull request Oct 23, 2024
Alvant pushed a commit to compressa-ai/vllm that referenced this pull request Oct 26, 2024
garg-amit pushed a commit to garg-amit/vllm that referenced this pull request Oct 28, 2024
FerdinandZhong pushed a commit to FerdinandZhong/vllm that referenced this pull request Oct 29, 2024
sumitd2 pushed a commit to sumitd2/vllm that referenced this pull request Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Support Falcon Mamba
2 participants