Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sdpython/onnx-array-api
Array APIs to write ONNX Graphs
https://github.com/sdpython/onnx-array-api
array-api machine-learning numpy onnx
Last synced: about 2 months ago
JSON representation
Array APIs to write ONNX Graphs
- Host: GitHub
- URL: https://github.com/sdpython/onnx-array-api
- Owner: sdpython
- License: mit
- Created: 2023-03-17T11:16:57.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-08T21:58:27.000Z (9 months ago)
- Last Synced: 2024-04-18T01:53:57.265Z (8 months ago)
- Topics: array-api, machine-learning, numpy, onnx
- Language: Python
- Homepage: https://sdpython.github.io/doc/onnx-array-api/dev/
- Size: 1.52 MB
- Stars: 7
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOGS.rst
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
.. image:: https://github.com/sdpython/onnx-array-api/raw/main/_doc/_static/logo.png
:width: 120onnx-array-api: APIs to create ONNX Graphs
==========================================.. image:: https://dev.azure.com/xavierdupre3/onnx-array-api/_apis/build/status/sdpython.onnx-array-api
:target: https://dev.azure.com/xavierdupre3/onnx-array-api/.. image:: https://badge.fury.io/py/onnx-array-api.svg
:target: http://badge.fury.io/py/onnx-array-api.. image:: http://img.shields.io/github/issues/sdpython/onnx-array-api.png
:alt: GitHub Issues
:target: https://github.com/sdpython/onnx-array-api/issues.. image:: https://img.shields.io/badge/license-MIT-blue.svg
:alt: MIT License
:target: https://opensource.org/license/MIT/.. image:: https://img.shields.io/github/repo-size/sdpython/onnx-array-api
:target: https://github.com/sdpython/onnx-array-api/
:alt: size.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black.. image:: https://codecov.io/gh/sdpython/onnx-array-api/branch/main/graph/badge.svg?token=Wb9ZGDta8J
:target: https://codecov.io/gh/sdpython/onnx-array-api**onnx-array-api** implements APIs to create custom ONNX graphs.
The objective is to speed up the implementation of converter libraries.
The library is released on
`pypi/onnx-array-api `_
and its documentation is published at
`APIs to create ONNX Graphs `_.Numpy API
+++++++++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... code-block:: python
import numpy as np
from onnx_array_api.npx import absolute, jit_onnx
from onnx_array_api.plotting.text_plot import onnx_simple_text_plotdef 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=NoneIt supports eager mode as well:
.. code-block:: python
import numpy as np
from onnx_array_api.npx import absolute, eager_onnxdef l1_loss(x, y):
err = absolute(x - y).sum()
print(f"l1_loss={err.numpy()}")
return errdef l2_loss(x, y):
err = ((x - y) ** 2).sum()
print(f"l2_loss={err.numpy()}")
return errdef 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]Light API
+++++++++The second API or **Light API** tends to do every thing in one line.
It is inspired from the `Reverse Polish Notation
`_.
The euclidean distance looks like the following:.. code-block:: python
import numpy as np
from onnx_array_api.light_api import start
from onnx_array_api.plotting.text_plot import onnx_simple_text_plotmodel = (
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()
)GraphBuilder API
++++++++++++++++Almost every converting library (converting a machine learned model to ONNX) is implementing
its own graph builder and customizes it for its needs.
It handles some frequent tasks such as giving names to intermediate
results, loading, saving onnx models. It can be used as well to extend an existing graph... code-block:: python
import numpy as np
from onnx_array_api.graph_api import GraphBuilderg = GraphBuilder()
g.make_tensor_input("X", np.float32, (None, None))
g.make_tensor_input("Y", np.float32, (None, None))
r1 = g.make_node("Sub", ["X", "Y"]) # the name given to the output is given by the class,
# it ensures the name is unique
init = g.make_initializer(np.array([2], dtype=np.int64)) # the class automatically
# converts the array to a tensor
r2 = g.make_node("Pow", [r1, init])
g.make_node("ReduceSum", [r2], outputs=["Z"]) # the output name is given because
# the user wants to choose the name
g.make_tensor_output("Z", np.float32, (None, None))onx = g.to_onnx() # final conversion to onnx