Skip to main content

safemodels

Cryptographically-secure proof-of-concept for verifying the provenance of ML models.

This library is a thought experiment into what securing the supply chain of ML models could look like. It's built on top of safetensors. You should probably read the blog post for more context!

Installation

$ pip install safemodels

Usage

Hashing

from safemodels import safe_hash
from huggingface_hub import hf_hub_download as dl

st = dl("gpt2", filename="model.safetensors")
pt = dl("gpt2", filename="pytorch_model.bin")

assert safe_hash(st) == safe_hash(pt) == 'd6c60a3126ef088e5f8fdaa332da56d552da966a'

Signing

from safemodels import SafeModel
from huggingface_hub import hf_hub_download as dl

st = dl("gpt2", filename="model.safetensors")
sm = SafeModel.from_safetensor(st)
# or
st, sm = SafeModel.from_hf("gpt2", version="main")

sm.sign_safetensor(st) # backwards-compatible rewrite of file

Verification

>>> from safemodels import init, Issuer
>>> from huggingface_hub import hf_hub_download
>>>
>>> init(Issuer(identity="EleutherAI", issuer="https://auth.huggingface.com")
>>>
>>> hf_hub_download("EleuterAI/gpt-j-6B", filename="model.safetensors")
Downloading model.safetensors: 100%|███| 548M/548M [00:14<00:00, 39.2MB/s]
211it [00:00, 4785.46it/s]
Error: none of the expected identities matched what was in the certificate, got subjects [EleuterAI] with issuer https://auth.huggingface.com
Traceback (most recent call last):
  ...
safemodels.InvalidSignature: Loaded a safetensor with an invalid signature!

safetensor Metadata

from safemodels.utils.safetensors import extract_metadata, update_meta
from huggingface_hub import hf_hub_download as dl

st = dl("gpt2", filename="model.safetensors")

print(extract_metadata(st))
# {'format': 'pt'}

update_meta(st, {"hello": "world"})

print(extract_metadata(st))
# {'format': 'pt', 'hello': 'world'}

Metadata

Release files for safemodels 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for safemodels 0.2.0
File Size Uploaded Provenance
safemodels-0.2.0.tar.gz 6.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for safemodels 0.2.0
File Interpreter ABI Platform
safemodels-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.9 kB

Release files / safemodels-0.2.0.tar.gz

Download URL safemodels-0.2.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
67fef8be56dafc75e24b013b6c0d0a0e67265f7a300eaf30a13997cd777f52e9
BLAKE2b-256 checksum
How to use checksums
02910aa05308a763e12543eec172ee982150c1b2c0d1de6e5fedea422399fd16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.0 Darwin/22.1.0

Release files / safemodels-0.2.0-py3-none-any.whl

Download URL safemodels-0.2.0-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4b9b6a313a9b00b9c7c77437f1be27edd1ac1eb6f9bb817eee8501349824b2d8
BLAKE2b-256 checksum
How to use checksums
8041f31a10ec088a1a9f7d312551246440e839f0555a514910e0fb2883171813
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.0 Darwin/22.1.0

Security for safemodels 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page