SafeTensors serialization for PyTorch models #4163
Open
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.
Adds SafeTensors-based serialization for PyTorch models (addresses #2532) and
implements metadata-driven loading to integrate cleanly with the materializer
workflow (per @bcdurak's feedback).
Changes
safetensorsoptional extra inpyproject.tomlstate_dictto.safetensorswhen available; fallback to.ptwith warningmetadata.json(class_path,serialization_format)TemporaryDirectory+copy_dir()for remote storesload()always returnsnn.Moduleweights.pt,checkpoint.pt, and legacyentire_model.ptNew artifact layout
Metadata
{ "class_path": "my_package.models.MyModel", "serialization_format": "safetensors", "init_args": [], "init_kwargs": {}, "factory_path": null }Why SafeTensors?
Tests
Local run:
pytest tests/unit/integrations/pytorch/materializers/test_pytorch_module_materializer.py -v # 4 passed in 1.88sCoverage:
weights.pt,checkpoint.pt,entire_model.pt)Known limitations (Phase 1)
Zero-argument
__init__()requirement: Models needing config should usea factory method (planned for Phase 2)
Legacy artifacts without metadata (
weights.pt/checkpoint.pt) require:entire_model.ptis loaded and returned as aModuledirectly(no
data_typeneeded)Documentation
Happy to add a short guide covering why/how/limits/troubleshooting.
Which file should I update?
docs/book/component-guide/materializers/pytorch.md(materializer behavior)?docs/book/integration-guide/pytorch.md(integration landing)?Or would you prefer a new section?
Future work (separate PRs)
init_args/init_kwargs/ factory functionsChecklist
ruff check --fix+ruff format)develop