recipe: safetensors 0.8.0#108
Merged
Merged
Conversation
Rust/PyO3 (maturin) recipe, tokenizers archetype minus the C++: the 0.8.0 sdist vendors the Rust core (safetensors/) next to the pyo3 bindings (bindings/python/), builds abi3-py310, and needs zero patches. Pure-Rust dep tree (memmap2, serde_json) - no libc++_shared host dep; the metal.rs Apple-silicon path is cfg-gated to macOS and compiles out on iOS/Android. Full 6-slice matrix green; android .so links only libpython/libdl/libc, 16KB-aligned. Unlocks model2vec (numpy-only static embeddings). Tests cover the 0.8.0 TensorSpec bytes API (constructor takes python-style dtype names, headers report format codes), the mmap safe_open path, and the numpy round-trip downstream consumers use.
The tester app installs flet + pytest + the recipe's Requires-Dist only, so a zero-runtime-dep recipe (safetensors) can't exercise integration paths on-device — its numpy test could only ever skip. stage_recipe.sh now expands an optional recipes/<name>/tests/requirements.txt (one PEP 508 spec per line, comments/blanks skipped) into the generated pyproject via a __TEST_DEPS__ token line in the template. Template expansion is a bash line loop with printf rather than sed: specs legitimately contain double quotes in markers, which have no safe portable sed-RHS escaping. Deps are emitted as TOML literal strings, so single quotes inside a spec are rejected with an error. Token matches are exact-line, so comments mentioning a token pass through verbatim. Empty-array expansion is guarded for macOS bash 3.2 under set -u.
# Conflicts: # tests/recipe-tester/README.md # tests/recipe-tester/pyproject.toml.tpl # tests/recipe-tester/stage_recipe.sh
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 a
safetensorsrecipe — safe, fast tensor serialization on both platforms.Closes flet-dev/flet#6688.
Shape
safetensors is a Rust/PyO3 (maturin) package with a published sdist, so this is a plain minimal-Rust recipe: zero patches, no
sourceoverride, noexcluded_arches— it builds for every Android ABI (incl.armeabi-v7a) and all three iOS slices. The only meta beyond name/version is the standard_PYTHON_SYSCONFIGDATA_NAMEcross-compile env var.Testing
tests/test_safetensors.pyruns three on-device checks: a bytes-levelserialize/deserializeround-trip through the Rust core (TensorSpec, no framework dep), aserialize_fileround-trip, and thenumpy+safe_open(framework="numpy")round-trip that downstream consumers (model2vec, ONNX inference) actually use.numpyis an upstream extra (safetensors[numpy]), not aRequires-Dist, so it's declared as a test-only dep via meta.yamltest.requires— installed for the on-device run without becoming a wheel dependency.Consumer notes (usage & recommendations)
safetensorsloads and saves model weights/embeddings as a safe, zero-copy file format — none of pickle's arbitrary-code-execution risk, and fast mmap reads that pull one tensor at a time.Install
safetensorsitself has no required dependencies, but the usable mobile path needsnumpy, so add both:Example
Save a couple of tensors, reload them via
safe_open, and show the result on screen. Note the file goes into Flet's storage: the app bundle is read-only on device, so write toFLET_APP_STORAGE_TEMP/_DATA, not the working directory.On device this shows
emb: shape [2, 3], sum 15.0/bias: shape [3], sum 3.0.Recommendations
numpy—torch/tensorflow/paddlearen't available as mobile wheels. Loading withframework="pt"fails at runtime because torch isn't installed; convert weights to the numpy view (or ONNX) ahead of time.safe_open+get_tensoroverload_filefor large files — it mmaps and reads tensors on demand instead of pulling everything into RAM at once..safetensorsis the weights/embeddings format for the on-device ML stack — pair it withonnxruntime+numpyfor inference, ormodel2vecfor static embeddings.