-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Safetensors support. #381
Merged
Merged
Safetensors support. #381
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
80c0066
Dummy attempt to use safetensors.
Narsil 7210f3d
Cleaned up.
Narsil 0582a50
Released version.
Narsil 5077f30
Reworked the TensorCollection.
Narsil ee6d197
Readd boolean.
Narsil babb2e8
Testing the safetensors feature.
Narsil 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 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 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 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,44 @@ | ||
//! Demonstrates how to save and load arrays with safetensors | ||
|
||
#[cfg(feature = "safetensors")] | ||
fn main() { | ||
use ::safetensors::SafeTensors; | ||
use dfdx::{ | ||
prelude::*, | ||
tensor::{AsArray, Cpu}, | ||
}; | ||
use memmap2::MmapOptions; | ||
let dev: Cpu = Default::default(); | ||
|
||
type Model = (Linear<5, 10>, Linear<10, 5>); | ||
let model = dev.build_module::<Model, f32>(); | ||
model | ||
.save_safetensors("model.safetensors") | ||
.expect("Failed to save model"); | ||
|
||
let mut model2 = dev.build_module::<Model, f32>(); | ||
model2 | ||
.load_safetensors("model.safetensors") | ||
.expect("Failed to load model"); | ||
|
||
assert_eq!(model.0.weight.array(), model2.0.weight.array()); | ||
|
||
// ADVANCED USAGE to load pre-existing models | ||
|
||
// wget -O gpt2.safetensors https://huggingface.co/gpt2/resolve/main/model.safetensors | ||
|
||
let mut gpt2 = dev.build_module::<Linear<728, 50257>, f32>(); | ||
let filename = "gpt2.safetensors"; | ||
let f = std::fs::File::open(filename).expect("Couldn't read file, have you downloaded gpt2 ? `wget -O gpt2.safetensors https://huggingface.co/gpt2/resolve/main/model.safetensors`"); | ||
let buffer = unsafe { MmapOptions::new().map(&f).expect("Could not mmap") }; | ||
let tensors = SafeTensors::deserialize(&buffer).expect("Couldn't read safetensors file"); | ||
|
||
gpt2.weight | ||
.load_safetensor(&tensors, "wte.weight") | ||
.expect("Could not load tensor"); | ||
} | ||
|
||
#[cfg(not(feature = "safetensors"))] | ||
fn main() { | ||
panic!("Use the 'safetensors' feature to run this example"); | ||
} |
This file contains 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
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.
Awesome example