Skip to content

[Model] Allow loading from original Mistral format #8168

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

Merged
merged 33 commits into from
Sep 6, 2024

Conversation

patrickvonplaten
Copy link
Contributor

@patrickvonplaten patrickvonplaten commented Sep 4, 2024

Mistral models are usually uploaded in two formats:

This PR allows to load directly from the original consolidated format which should make it easier to directly get new models working in VLLM.

The PR can be tested with:

vllm serve mistralai/Mistral-Nemo-Instruct-2407 --tokenizer_mode mistral --max_model_len 8192 --load_format consolidated

or:

from vllm import LLM, SamplingParams

prompts = [
    "Hello, my name is",
    "The president of the United States is",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95, seed=0)

model_path = "mistralai/Mistral-7B-Instruct-v0.3"
for model in [model_path]:  # or "mistralai/Mistral-7B-Instruct-v0.3"
    llm = LLM(model=model, tokenizer_mode="mistral", max_model_len=8192, load_format="consolidated")
    outputs = llm.generate(prompts, sampling_params)

    for output in outputs:
        prompt = output.prompt
        generated_text = output.outputs[0].text
        print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}"

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.

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

github-actions bot commented Sep 4, 2024

👋 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.

🚀

@patrickvonplaten patrickvonplaten changed the title [Mistral model format] Allow loading consolidated model format [Model] Allow loading from original Mistral format Sep 4, 2024
@patrickvonplaten
Copy link
Contributor Author

/ready

Copy link
Member

@mgoin mgoin left a comment

Choose a reason for hiding this comment

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

I think this is reasonable overall but are these consolidated checkpoints going to be actively used going forward?
We tend to want to conform to HF as a standard, so if these checkpoints don't load in transformers now then I think the community will continue to have interest in having those be converted to canonical "HF-style" checkpoints

Comment on lines 253 to 254
cache_dir: Optional[str],
index_file: str,
Copy link
Member

Choose a reason for hiding this comment

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

super nit: I think it makes more sense to have cache_dir after index_file, similar to how hf_hub_download is called

Copy link
Contributor Author

Choose a reason for hiding this comment

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

happy to change

@@ -104,6 +110,56 @@ def get_config(
return config


def load_params_config(model, revision) -> PretrainedConfig:
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why you have this new config? It seems to have the same information you would have in the config.json, just named differently

Copy link
Contributor Author

@patrickvonplaten patrickvonplaten Sep 4, 2024

Choose a reason for hiding this comment

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

The main reason is because the original format is always stored in params.json which accompanies the consolidated.safetensors checkpoints.

Guess there are two problems with config.json

Comment on lines 378 to 396
# Mistral/Llama models can also be loaded with --load-format consolidated
# from consolidated.safetensors checkpoints
consolidated_mapping = {
"layers": "model.layers",
"attention": "self_attn",
"wq": "q_proj",
"wk": "k_proj",
"wv": "v_proj",
"wo": "o_proj",
"attention_norm": "input_layernorm",
"feed_forward": "mlp",
"w1": "gate_proj",
"w2": "down_proj",
"w3": "up_proj",
"ffn_norm": "post_attention_layernorm",
"tok_embeddings": "model.embed_tokens",
"output": "lm_head",
"norm": "model.norm"
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this a standard naming scheme used by Llama models as well? I have only seen Mistral models with these style of checkpoints

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah the original LLama checkpoints have this naming as well: https://github.com/meta-llama/llama/blob/8fac8befd776bc03242fe7bc2236cdb41b6c609c/llama/model.py#L207

(guess most people use the HF format indeed though)

@patrickvonplaten
Copy link
Contributor Author

patrickvonplaten commented Sep 4, 2024

I think this is reasonable overall but are these consolidated checkpoints going to be actively used going forward? We tend to want to conform to HF as a standard, so if these checkpoints don't load in transformers now then I think the community will continue to have interest in having those be converted to canonical "HF-style" checkpoints

Hey @mgoin,

Yes at Mistral we usually only release the consolidated checkpoints to begin with (see table here). The community then usually converts the models to transformers but this takes some time and then it takes even more time to wait for the next transformers release. So yes, I think the consolidated format will surely continued to be used!

I understand that it's easier to just conform to HF-style, but I guess VLLM also has GGUF support and it could make sense to start allowing more and more support for other formats? Guess in the future VLLM can run any nn.Module regardless of whether it's in transformers or not no?

The biggest reason why I think it makes sense to allow also for the consolidated format is so that users could have day-0 inference support for Mistral models for future model releases. VLLM is essentially implementing all the model architectures natively in the model register, so it's just about "correctly" loading the file format no? It's a bit painful to have to adapt mistral format to transformers format to allow inference in VLLM. With mistral-common in VLLM and with consolidated.safetensors format loading, models like Mistral-Nemo should be supported out of the box on day 0.

Happy to adapt the PR in a way that fits the design better!

vllm/config.py Outdated
@@ -744,6 +747,7 @@ class LoadFormat(str, enum.Enum):
SHARDED_STATE = "sharded_state"
GGUF = "gguf"
BITSANDBYTES = "bitsandbytes"
CONSOLIDATED = "consolidated"
Copy link
Contributor

Choose a reason for hiding this comment

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

CONSOLIDATED is a bit broad as a name no ? Especially since there is SHARDED_STATE above, I think it could lead to confusion.

Thoughts on LoadFormat.MISTRAL ? This makes it clear that the intent is to have us support / maintain the integration of our models into vLLM.

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 makes sense!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to "mistral" now - keen to hear what @simon-mo @mgoin think :-)

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 on calling it "MISTRAL" format

Copy link
Member

Choose a reason for hiding this comment

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

I agree, it's better to be explicit if possible

@patrickvonplaten
Copy link
Contributor Author

Thanks for the feedback @timlacroix @mgoin - think everything should be incorporated. Let me know if there is anything else that should be changed!

Copy link
Member

@mgoin mgoin left a comment

Choose a reason for hiding this comment

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

I appreciate your explanation and justification @patrickvonplaten ! I think this is in a good place, my last gripe is with load_params_config since it would be good to future-proof this for other load formats in the future that may require similar changes. Look in the comments for my proposal

Comment on lines 33 to 41
# test that both HF format and mistral format work
load_format = "mistral" if model.endswith("v0.3") else "auto"

with vllm_runner(model,
dtype=dtype,
tokenizer_mode="mistral",
load_format=load_format) as vllm_model:
vllm_outputs = vllm_model.generate_greedy_logprobs(
example_prompts, max_tokens, num_logprobs)
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be best if you specifically made a test for "mistralai/Mistral-7B-Instruct-v0.3" where you test the model load with both "safetensors" and "mistral" (and HF to just ensure reference) since both checkpoint formats are present on that model card.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense! Thanks - will take care of it in a bit

vllm/config.py Outdated
Comment on lines 122 to 123
load_params_config: Load the config from mistral format
(params.json) instead of config.json.
Copy link
Member

Choose a reason for hiding this comment

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

I think you made some good points on supporting checkpoint formats other than HF. Given that assumption, I think it would be best to future-proof the interface of config overrides.

I would like to propose changing this parameter to something like config_format that takes a value from a ConfigFormat enum, defaulting to ConfigFormat.HF_CONFIG or AUTO. Then get_config() can dispatch based on this value to the specified config parsing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes a lot of sense!

patrickvonplaten and others added 3 commits September 6, 2024 01:53
Co-authored-by: Michael Goin <michael@neuralmagic.com>
@mgoin mgoin added the ready ONLY add when PR is ready to merge/full CI is needed label Sep 6, 2024
Copy link
Member

@mgoin mgoin left a comment

Choose a reason for hiding this comment

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

Thanks for working on getting the CI green, looks like you're close. It does seem like AUTO is causing trouble here, so I would be okay with removing it and defaulting to HF if you can't get it stable. Otherwise this LGTM so we can land when green

@patrickvonplaten
Copy link
Contributor Author

patrickvonplaten commented Sep 6, 2024

Thanks for working on getting the CI green, looks like you're close. It does seem like AUTO is causing trouble here, so I would be okay with removing it and defaulting to HF if you can't get it stable. Otherwise this LGTM so we can land when green

Yeah I think it should be fine now (CI is all green). GGUF tests were failing because they didn't default to HF. Fixed it and everything should work fine now I believe! Thanks for guiding me through the PR!

@mgoin mgoin merged commit 29f49cd into vllm-project:main Sep 6, 2024
50 checks passed
dtrifiro pushed a commit to opendatahub-io/vllm that referenced this pull request Sep 12, 2024
Co-authored-by: Michael Goin <michael@neuralmagic.com>
@rosmur
Copy link

rosmur commented Sep 21, 2024

will this work for pixtral as well?

Alvant pushed a commit to compressa-ai/vllm that referenced this pull request Oct 26, 2024
Co-authored-by: Michael Goin <michael@neuralmagic.com>
Signed-off-by: Alvant <alvasian@yandex.ru>
garg-amit pushed a commit to garg-amit/vllm that referenced this pull request Oct 28, 2024
Co-authored-by: Michael Goin <michael@neuralmagic.com>
Signed-off-by: Amit Garg <mitgarg17495@gmail.com>
@ryoungj
Copy link

ryoungj commented Nov 14, 2024

Will this work for the Llama original formats, which, e.g., are in the formats of consolidated.00.pth and params.json?

@sfjhu
Copy link

sfjhu commented Feb 3, 2025

Hi all, I just pulled the latest Docker version and am still getting this error when running Pixtral-12B-2409. Was this fix ever released or am I doing something incorrectly?

LeiWang1999 pushed a commit to LeiWang1999/vllm-bitblas that referenced this pull request Mar 26, 2025
Co-authored-by: Michael Goin <michael@neuralmagic.com>
Signed-off-by: LeiWang1999 <leiwang1999@outlook.com>
No Sign up for free to join this conversation on GitHub. Already have an account? No 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.

7 participants