Skip to main content

Use safetensors with ONNX

Project description

onnx-safetensors

CI PyPI - Version PyPI - Python Version PyPI Downloads Ruff Ruff

ONNX extension for saving to and loading from safetensors 🤗.

Features

  • ✅ Load and save ONNX weights from and to safetensors
  • ✅ Support all ONNX data types, including float8, float4 and 4-bit ints
  • ✅ Allow ONNX backends (including ONNX Runtime) to use safetensors

Install

pip install --upgrade onnx-safetensors

Usage

Load tensors to an ONNX model

[!TIP] You can use safetensors as external data for ONNX.

import os
import onnx
import onnx_safetensors

# Provide your ONNX model here
model: onnx.ModelProto

tensor_file = "path/to/onnx_model/model.safetensors"
base_dir = "path/to/onnx_model"
data_path = "model.safetensors"

# Apply weights from the safetensors file to the model and turn them to in memory tensor
# NOTE: If model size becomes >2GB you will need to offload weights with onnx_safetensors.save_file, or onnx.save with external data options to keep the onnx model valid
model = onnx_safetensors.load_file(model, tensor_file)

# If you want to use the safetensors file in ONNX Runtime:
# Use safetensors as external data in the ONNX model
model_with_external_data = onnx_safetensors.load_file_as_external_data(model, data_path, base_dir=base_dir)

# Save the modified model
# This model is a valid ONNX model using external data from the safetensors file
onnx.save(model_with_external_data, os.path.join(base_dir, "model_using_safetensors.onnx"))

Save weights to a safetensors file

import onnx
import onnx_safetensors

# Provide your ONNX model here
model: onnx.ModelProto
base_dir = "path/to/onnx_model"
data_path = "model.safetensors"

# Offload weights from ONNX model to safetensors file without changing the model
onnx_safetensors.save_file(model, data_path, base_dir=base_dir, replace_data=False)  # Generates model.safetensors

# If you want to use the safetensors file in ONNX Runtime:
# Offload weights from ONNX model to safetensors file and use it as external data for the model by setting replace_data=True
model_with_external_data = onnx_safetensors.save_file(model, data_path, base_dir=base_dir, replace_data=True)

# Save the modified model
# This model is a valid ONNX model using external data from the safetensors file
onnx.save(model_with_external_data, os.path.join(base_dir, "model_using_safetensors.onnx"))

Save an ONNX model with safetensors weights

The save_model function is a convenient way to save both the ONNX model and its weights to separate files:

import onnx_safetensors

# Provide your ONNX model here
model: onnx.ModelProto

# Save model and weights in one step
# This creates model.onnx and model.safetensors
onnx_safetensors.save_model(model, "model.onnx")

# You can also specify a custom name for the weights file
onnx_safetensors.save_model(model, "model.onnx", external_data="weights.safetensors")

Shard large models

For large models, you can automatically shard the weights across multiple safetensors files:

import onnx_safetensors

# Provide your ONNX model here
model: onnx.ModelProto

# Shard the model into multiple files (e.g., 5GB per shard)
# This creates:
# - model.onnx
# - model-00001-of-00003.safetensors
# - model-00002-of-00003.safetensors
# - model-00003-of-00003.safetensors
# - model.safetensors.index.json (index file mapping tensors to shards)
onnx_safetensors.save_model(model, "model.onnx", max_shard_size="5GB")

# You can also use save_file with sharding
onnx_safetensors.save_file(
    model,
    "weights.safetensors",
    base_dir="path/to/save",
    max_shard_size="5GB"
)

The sharding format is compatible with the Hugging Face transformers library, making it easy to share and load models across different frameworks.

Embed ONNX model in a safetensors file

For storage or transfer purposes, you can embed an entire ONNX model (structure and weights) into a single safetensors file:

import onnx_safetensors

# Provide your ONNX model here
model: onnx.ModelProto

# Save the entire model (structure + weights) into a safetensors file
onnx_safetensors.save_safetensors_model(model, "model.safetensors")

# Later, extract the model from the safetensors file
model = onnx_safetensors.extract_safetensors_model("model.safetensors")

# Or extract and save to an ONNX file that references the safetensors file as external data
onnx_safetensors.extract_safetensors_model(
    "model.safetensors",
    output_path="model.onnx"
)

[!NOTE] This format is for storage/transfer only and is not compatible with ONNX Runtime. Use extract_safetensors_model with output_path to create a runnable ONNX model that references the safetensors file as external data.

Command Line Interface

ONNX-safetensors provides a command-line interface for converting ONNX models to use safetensors format:

# Basic conversion
onnx-safetensors convert input.onnx output.onnx

# Convert with sharding (split large models into multiple files)
onnx-safetensors convert input.onnx output.onnx --max-shard-size 5GB

# You can also specify size in MB
onnx-safetensors convert input.onnx output.onnx --max-shard-size 500MB

# Embed an ONNX model into a safetensors file
onnx-safetensors embed input.onnx output.safetensors

The convert command:

  • Loads an ONNX model from the input path
  • Saves it with safetensors external data to the output path
  • Optionally shards large models using --max-shard-size
  • Creates index files automatically when sharding is enabled

The embed command:

  • Loads an ONNX model from the input path
  • Embeds the entire model (structure and weights) into a single safetensors file
  • Useful for storage or transfer purposes
  • Use onnx_safetensors.extract_safetensors_model in Python to extract the model later

Examples

Star History

Star History Chart

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onnx_safetensors-1.6.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

onnx_safetensors-1.6.0-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file onnx_safetensors-1.6.0.tar.gz.

File metadata

  • Download URL: onnx_safetensors-1.6.0.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for onnx_safetensors-1.6.0.tar.gz
Algorithm Hash digest
SHA256 facd5694d9c22539a57da9bb98096dfdce1a8d0427c92bebc4b45922de9eaa01
MD5 693e52a7b034ab5020c9b82adbea0517
BLAKE2b-256 66d97c64598bb563d16e5d626a0eb45e133414e05696f157ac9ed4fd14eb0f24

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnx_safetensors-1.6.0.tar.gz:

Publisher: main.yml on justinchuby/onnx-safetensors

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file onnx_safetensors-1.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for onnx_safetensors-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c125442703acdc42b511a1c03766cc9cc8527d977e2203448e804f4725538858
MD5 ebf2909057e34246fc97e3a3661a9d1d
BLAKE2b-256 0382dc8c5b06e49de221e5655cd78af4ce46977ef5b63771c3e99e30e7ea558c

See more details on using hashes here.

Provenance

The following attestation bundles were made for onnx_safetensors-1.6.0-py3-none-any.whl:

Publisher: main.yml on justinchuby/onnx-safetensors

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page