Skip to main content

Array (and numpy) API for ONNX

Project description

https://dev.azure.com/xavierdupre3/onnx-array-api/_apis/build/status/sdpython.onnx-array-api https://badge.fury.io/py/onnx-array-api.svg GitHub Issues MIT License size https://img.shields.io/badge/code%20style-black-000000.svg https://codecov.io/gh/sdpython/onnx-array-api/branch/main/graph/badge.svg?token=Wb9ZGDta8J

onnx-array-api implements APIs to create custom ONNX graphs. The objective is to speed up the implementation of converter libraries. The first one matches numpy API. It gives the user the ability to convert functions written following the numpy API to convert that function into ONNX as well as to execute it.

import numpy as np
from onnx_array_api.npx import absolute, jit_onnx
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot

def l1_loss(x, y):
    return absolute(x - y).sum()


def l2_loss(x, y):
    return ((x - y) ** 2).sum()


def myloss(x, y):
    return l1_loss(x[:, 0], y[:, 0]) + l2_loss(x[:, 1], y[:, 1])


jitted_myloss = jit_onnx(myloss)

x = np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32)
y = np.array([[0.11, 0.22], [0.33, 0.44]], dtype=np.float32)

res = jitted_myloss(x, y)
print(res)

print(onnx_simple_text_plot(jitted_myloss.get_onnx()))
[0.042]
opset: domain='' version=18
input: name='x0' type=dtype('float32') shape=['', '']
input: name='x1' type=dtype('float32') shape=['', '']
Sub(x0, x1) -> r__0
  Abs(r__0) -> r__1
    ReduceSum(r__1, keepdims=0) -> r__2
output: name='r__2' type=dtype('float32') shape=None

It supports eager mode as well:

import numpy as np
from onnx_array_api.npx import absolute, eager_onnx


def l1_loss(x, y):
    err = absolute(x - y).sum()
    print(f"l1_loss={err.numpy()}")
    return err


def l2_loss(x, y):
    err = ((x - y) ** 2).sum()
    print(f"l2_loss={err.numpy()}")
    return err


def myloss(x, y):
    return l1_loss(x[:, 0], y[:, 0]) + l2_loss(x[:, 1], y[:, 1])


eager_myloss = eager_onnx(myloss)

x = np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32)
y = np.array([[0.11, 0.22], [0.33, 0.44]], dtype=np.float32)

res = eager_myloss(x, y)
print(res)
l1_loss=[0.04]
l2_loss=[0.002]
[0.042]

The second API ir Light API tends to do every thing in one line. The euclidean distance looks like the following:

import numpy as np
from onnx_array_api.light_api import start
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot

model = (
    start()
    .vin("X")
    .vin("Y")
    .bring("X", "Y")
    .Sub()
    .rename("dxy")
    .cst(np.array([2], dtype=np.int64), "two")
    .bring("dxy", "two")
    .Pow()
    .ReduceSum()
    .rename("Z")
    .vout()
    .to_onnx()
)

The library is released on pypi/onnx-array-api and its documentation is published at (Numpy) Array API for ONNX.

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-array-api-0.1.2.tar.gz (179.6 kB view details)

Uploaded Source

Built Distribution

onnx_array_api-0.1.2-py3-none-any.whl (195.2 kB view details)

Uploaded Python 3

File details

Details for the file onnx-array-api-0.1.2.tar.gz.

File metadata

  • Download URL: onnx-array-api-0.1.2.tar.gz
  • Upload date:
  • Size: 179.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for onnx-array-api-0.1.2.tar.gz
Algorithm Hash digest
SHA256 45c35794e3d8c32917be355f98b2f20fbb1526c6bc50375ad9b32fb1264e5f4e
MD5 20d8c70063111d80f0ca9e2805af73d3
BLAKE2b-256 6fd2ba13fb88770c9ffb676c5f278dd09ecbb532eb785dfaf29e98f34e31d7c8

See more details on using hashes here.

File details

Details for the file onnx_array_api-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for onnx_array_api-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1f4118ca42a56b3581952899dc3e23d8d3520886d4dd2d93cb038396b433786d
MD5 ba38f5fdc371dc234f2633d39ee7c933
BLAKE2b-256 1b93be2289d4deeedd9d030bcea685a723e09dd50340568025f5af202fbcdac9

See more details on using hashes here.

Supported by

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