A framework for constructing ONNX computational graphs.
Project description
Spox
Spox makes it easy to construct ONNX models through clean and idiomatic Python code.
Why use Spox?
A common application of ONNX is converting models from various frameworks. This requires replicating their runtime behaviour with ONNX operators. In the past this has been a major challenge. Based on our experience, we designed Spox from the ground up to make the process of writing converters (and ONNX models in general) as easy as possible.
Spox's features include:
- Eager operator validation and type inference
- Errors with Python tracebacks to offending operators
- First-class support for subgraphs (control flow)
- A lean and predictable API
Installation
Spox releases are available on PyPI:
pip install spox
There is also a package available on conda-forge:
conda install spox
Quick start
In Spox, you primarily interact with Var objects - variables - which are placeholders for runtime values.
The initial Var objects, which represent the arguments of a model (the model inputs in ONNX nomenclature), are created with an explicit type using the argument(Type) -> Var function. The possible types include Tensor, Sequence, and Optional.
All further Var objects are created by calling functions which take existing Var objects as inputs and produce new Var objects as outputs. Spox determines the Var.type for these eagerly to allow validation.
Spox provides such functions for all operators in the standard. They are grouped by domain and version in the spox.opset submodule.
The final onnx.ModelProto object is built by passing input and output Vars for the model to the spox.build function.
Below is an example for defining an ONNX graph which computes the geometric mean of two inputs. Make sure to consult the Spox documentation to find more details and tutorials.
import onnx
from spox import argument, build, Tensor, Var
# Import operators from the ai.onnx domain at version 17
from spox.opset.ai.onnx import v17 as op
def geometric_mean(x: Var, y: Var) -> Var:
# use the standard Sqrt and Mul
return op.sqrt(op.mul(x, y))
# Create typed model inputs. Each tensor is of rank 1
# and has the runtime-determined length 'N'.
a = argument(Tensor(float, ('N',)))
b = argument(Tensor(float, ('N',)))
# Perform operations on `Var`s
c = geometric_mean(a, b)
# Build an `onnx.ModelProto` for the given inputs and outputs.
model: onnx.ModelProto = build(inputs={'a': a, 'b': b}, outputs={'c': c})
Credits
Original designed and developed by @jbachurski with the supervision of @cbourjau.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spox-0.15.0.tar.gz.
File metadata
- Download URL: spox-0.15.0.tar.gz
- Upload date:
- Size: 507.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13f367607735c4327c7f623555c790786901a0bc11c44f8074124ec8b5e937ac
|
|
| MD5 |
01410b121d1175fe6f4c3edd6410b275
|
|
| BLAKE2b-256 |
061964fa829bf8c80cb3948296f2bde900f03b7c97914e348483af792a1a2040
|
Provenance
The following attestation bundles were made for spox-0.15.0.tar.gz:
Publisher:
build_and_publish.yml on Quantco/spox
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spox-0.15.0.tar.gz -
Subject digest:
13f367607735c4327c7f623555c790786901a0bc11c44f8074124ec8b5e937ac - Sigstore transparency entry: 230551196
- Sigstore integration time:
-
Permalink:
Quantco/spox@ce8737229cb857d4276c39f87fdad41552c1b5f0 -
Branch / Tag:
refs/tags/0.15.0 - Owner: https://github.com/Quantco
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build_and_publish.yml@ce8737229cb857d4276c39f87fdad41552c1b5f0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file spox-0.15.0-py3-none-any.whl.
File metadata
- Download URL: spox-0.15.0-py3-none-any.whl
- Upload date:
- Size: 275.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1ccfce174d604cafff2afe1b42fc429b03ebbd57cb8d14a0a7f2a868974b83c
|
|
| MD5 |
0587d28434ac7af93c4046087b0cd8d5
|
|
| BLAKE2b-256 |
8c466bdbd731ca53df50953a6871b4fc1cb4e75f8258ea6039cee2640a375cb6
|
Provenance
The following attestation bundles were made for spox-0.15.0-py3-none-any.whl:
Publisher:
build_and_publish.yml on Quantco/spox
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spox-0.15.0-py3-none-any.whl -
Subject digest:
d1ccfce174d604cafff2afe1b42fc429b03ebbd57cb8d14a0a7f2a868974b83c - Sigstore transparency entry: 230551199
- Sigstore integration time:
-
Permalink:
Quantco/spox@ce8737229cb857d4276c39f87fdad41552c1b5f0 -
Branch / Tag:
refs/tags/0.15.0 - Owner: https://github.com/Quantco
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build_and_publish.yml@ce8737229cb857d4276c39f87fdad41552c1b5f0 -
Trigger Event:
release
-
Statement type: