-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
[Core] Integrate fastsafetensors loader for loading model weights
#10647
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
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Loading Model weights with fastsafetensors | ||
| =================================================================== | ||
|
|
||
| Using fastsafetensor library enables loading model weights to GPU memory by leveraging GPU direct storage. See https://github.com/foundation-model-stack/fastsafetensors for more details. | ||
| For enabling this feature, set the environment variable ``USE_FASTSAFETENSOR`` to ``true`` |
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 |
|---|---|---|
|
|
@@ -5,4 +5,5 @@ | |
|
|
||
| runai_model_streamer | ||
| tensorizer | ||
| fastsafetensor | ||
| ::: | ||
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
Empty file.
22 changes: 22 additions & 0 deletions
22
tests/fastsafetensors_loader/test_fastsafetensors_loader.py
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,22 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from vllm import SamplingParams | ||
| from vllm.config import LoadFormat | ||
|
|
||
| test_model = "openai-community/gpt2" | ||
|
|
||
| prompts = [ | ||
| "Hello, my name is", | ||
| "The president of the United States is", | ||
| "The capital of France is", | ||
| "The future of AI is", | ||
| ] | ||
| # Create a sampling params object. | ||
| sampling_params = SamplingParams(temperature=0.8, top_p=0.95, seed=0) | ||
|
|
||
|
|
||
| def test_model_loader_download_files(vllm_runner): | ||
| with vllm_runner(test_model, | ||
| load_format=LoadFormat.FASTSAFETENSORS) as llm: | ||
| deserialized_outputs = llm.generate(prompts, sampling_params) | ||
| assert deserialized_outputs |
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,46 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import glob | ||
| import tempfile | ||
|
|
||
| import huggingface_hub.constants | ||
| import torch | ||
|
|
||
| from vllm.model_executor.model_loader.weight_utils import ( | ||
| download_weights_from_hf, fastsafetensors_weights_iterator, | ||
| safetensors_weights_iterator) | ||
|
|
||
|
|
||
| def test_fastsafetensors_model_loader(): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| huggingface_hub.constants.HF_HUB_OFFLINE = False | ||
| download_weights_from_hf("openai-community/gpt2", | ||
| allow_patterns=["*.safetensors"], | ||
| cache_dir=tmpdir) | ||
| safetensors = glob.glob(f"{tmpdir}/**/*.safetensors", recursive=True) | ||
| assert len(safetensors) > 0 | ||
|
|
||
| fastsafetensors_tensors = {} | ||
| hf_safetensors_tensors = {} | ||
|
|
||
| for name, tensor in fastsafetensors_weights_iterator( | ||
| safetensors, True): | ||
| fastsafetensors_tensors[name] = tensor | ||
|
|
||
| for name, tensor in safetensors_weights_iterator(safetensors, True): | ||
| hf_safetensors_tensors[name] = tensor | ||
|
|
||
| assert len(fastsafetensors_tensors) == len(hf_safetensors_tensors) | ||
|
|
||
| for name, fastsafetensors_tensor in fastsafetensors_tensors.items(): | ||
| fastsafetensors_tensor = fastsafetensors_tensor.to('cpu') | ||
| assert fastsafetensors_tensor.dtype == hf_safetensors_tensors[ | ||
| name].dtype | ||
| assert fastsafetensors_tensor.shape == hf_safetensors_tensors[ | ||
| name].shape | ||
| assert torch.all( | ||
| fastsafetensors_tensor.eq(hf_safetensors_tensors[name])) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_fastsafetensors_model_loader() |
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
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.
Uh oh!
There was an error while loading. Please reload this page.