Skip to main content

Spox

CI Documentation Status

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.

Download files

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

Source Distribution

spox-0.18.0.tar.gz (591.2 kB view details)

Uploaded Source

Built Distribution

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

spox-0.18.0-py3-none-any.whl (365.0 kB view details)

Uploaded Python 3

File details

Details for the file spox-0.18.0.tar.gz.

File metadata

  • Download URL: spox-0.18.0.tar.gz
  • Upload date:
  • Size: 591.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for spox-0.18.0.tar.gz
Algorithm Hash digest
SHA256 f87c05cbf4d0d4b50464d5a1dd4fa34ec00f83bc11b0c06eac4e6fb1a4f3e5ad
MD5 1437b8209f00534350aca33f0bdd2438
BLAKE2b-256 a7c7d116b6306517d0a21bee7e92fe9867b96228c760ae8e388f1b3964fb8927

See more details on using hashes here.

Provenance

The following attestation bundles were made for spox-0.18.0.tar.gz:

Publisher: build_and_publish.yml on Quantco/spox

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

File details

Details for the file spox-0.18.0-py3-none-any.whl.

File metadata

  • Download URL: spox-0.18.0-py3-none-any.whl
  • Upload date:
  • Size: 365.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for spox-0.18.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a04a95d5828b76313c16f1fce170fc67a0de5011433bc6d737a279f7c5c664d
MD5 85024e0045e8aaee6d43464fe0361310
BLAKE2b-256 748416b894255dd0f497ad9df4660f93344a9587fd5c506582fb03f7dbc5d27e

See more details on using hashes here.

Provenance

The following attestation bundles were made for spox-0.18.0-py3-none-any.whl:

Publisher: build_and_publish.yml on Quantco/spox

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

Release history Release notifications | RSS feed

This release

0.18.0 This release

2 files

0.17.2

2 files

0.17.1

2 files

0.17.0

2 files

0.16.0

2 files

0.15.0

2 files

0.14.1

2 files

0.12.1

2 files

0.12.0

2 files

0.11.0

2 files

0.10.3

2 files

0.10.2

2 files

0.10.1

2 files

0.10.0

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

0.8.0

2 files

0.7.0

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

Supported by

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