-
Notifications
You must be signed in to change notification settings - Fork 51
Add safetensors reader #555
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
Open
nenb
wants to merge
5
commits into
zarr-developers:main
Choose a base branch
from
nenb:add-safetensors-reader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8030453
Create SafeTensors reader implementation
nenb 6d37b17
Address Tom comments
nenb 2b87662
Add notebook for illustration
nenb 2a76187
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 87afb34
Add support for authorization header
nenb 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # SafeTensors Reader User Guide | ||
|
|
||
| The SafeTensors reader in VirtualiZarr allows you to reference tensors stored in SafeTensors files. This guide explains how to use the reader effectively. | ||
|
|
||
| ## What is SafeTensors Format? | ||
|
|
||
| SafeTensors is a file format developed by HuggingFace for storing tensors (multidimensional arrays) | ||
| that offers several advantages: | ||
| - Safe: No use of pickle, eliminating security concerns | ||
| - Efficient: Zero-copy access for fast loading | ||
| - Simple: Straightforward binary format with JSON header | ||
| - Language-agnostic: Available across Python, Rust, C++, and JavaScript | ||
|
|
||
| The format consists of: | ||
| - 8 bytes (header size): little-endian uint64 containing the size of the header | ||
| - JSON header: Contains metadata for all tensors (shapes, dtypes, offsets) | ||
| - Binary data: Contiguous tensor data | ||
|
|
||
| ## How VirtualiZarr's SafeTensors Reader Works | ||
|
|
||
| VirtualiZarr's SafeTensors reader allows you to: | ||
| - Create "virtual" Zarr stores pointing to chunks of data inside SafeTensors files | ||
| - Open the virtual zarr stores as xarray DataArrays with named dimensions | ||
| - Access specific slices of tensors from cloud storage | ||
| - Preserve metadata from the original SafeTensors file | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| Opening a SafeTensors file is straightforward: | ||
|
|
||
| ```python | ||
| import virtualizarr as vz | ||
|
|
||
| # Open a SafeTensors file | ||
| vds = vz.open_virtual_dataset("model.safetensors") | ||
|
|
||
| # Access tensors as xarray variables | ||
| weight = vds["weight"] | ||
| bias = vds["bias"] | ||
| ``` | ||
|
|
||
| ## Custom Dimension Names | ||
|
|
||
| By default, dimensions are named generically (e.g., "weight_dim_0", "weight_dim_1"). You can provide custom dimension names for better semantics: | ||
|
|
||
| ```python | ||
| # Define custom dimension names | ||
| custom_dims = { | ||
| "weight": ["input_dims", "output_dims"], | ||
| "bias": ["output_dims"] | ||
| } | ||
nenb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Open with custom dimension names | ||
| vds = vz.open_virtual_dataset( | ||
| "model.safetensors", | ||
| virtual_backend_kwargs={"dimension_names": custom_dims} | ||
| ) | ||
|
|
||
| # Now dimensions have meaningful names | ||
| print(vds["weight"].dims) # ('input_dims', 'output_dims') | ||
| print(vds["bias"].dims) # ('output_dims',) | ||
| ``` | ||
|
|
||
| ## Loading Specific Variables | ||
|
|
||
| You can specify which variables to load as eager arrays instead of virtual references: | ||
|
|
||
| ```python | ||
| # Load specific variables as eager arrays | ||
| vds = vz.open_virtual_dataset( | ||
| "model_weights.safetensors", | ||
| loadable_variables=["small_tensor1", "small_tensor2"] | ||
| ) | ||
|
|
||
| # These will be loaded as regular numpy arrays | ||
| small_tensor1 = vds["small_tensor1"] | ||
| # Large tensors remain virtual references | ||
| large_tensor = vds["large_tensor"] | ||
| ``` | ||
|
|
||
| ## Working with Remote Files | ||
|
|
||
| The SafeTensors reader supports reading from the HuggingFace Hub: | ||
| ```python | ||
| # HuggingFace Hub | ||
| vds = vz.open_virtual_dataset( | ||
| "https://huggingface.co/openai-community/gpt2/model.safetensors", | ||
| virtual_backend_kwargs={"revision": "main"} | ||
| ) | ||
| ``` | ||
|
|
||
| It supports reading from object storage: | ||
|
|
||
| ```python | ||
| # S3 | ||
| vds = vz.open_virtual_dataset( | ||
| "s3://my-bucket/model.safetensors", | ||
| reader_options={ | ||
| "storage_options": { | ||
| "key": "ACCESS_KEY", | ||
| "secret": "SECRET_KEY", | ||
| "region_name": "us-west-2" | ||
| } | ||
| } | ||
| ) | ||
| ``` | ||
|
|
||
| ## Accessing Metadata | ||
|
|
||
| SafeTensors files can contain metadata at the file level and tensor level: | ||
|
|
||
| ```python | ||
| # Access file-level metadata | ||
| print(vds.attrs) # File-level metadata | ||
|
|
||
| # Access tensor-specific metadata | ||
| print(vds["weight"].attrs) # Tensor-specific metadata | ||
|
|
||
| # Access original SafeTensors dtype information | ||
| original_dtype = vds["weight"].attrs["original_safetensors_dtype"] | ||
| print(f"Original dtype: {original_dtype}") | ||
| ``` | ||
|
|
||
| ## Known Limitations | ||
|
|
||
| ### Performance Considerations | ||
| - Very large tensors (>1GB) are treated as a single chunk, which may impact memory usage when accessing small slices | ||
| - Files with thousands of tiny tensors may have overhead due to metadata handling | ||
|
|
||
| ## Best Practices | ||
|
|
||
| - **For large tensors**: Use slicing to access only the portions you need | ||
| - **For remote files**: Use appropriate credentials and optimize access patterns | ||
| - **For many small tensors**: Consider loading them eagerly using `loadable_variables` | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.