Skip to content

First documentation of the light API #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update titles
  • Loading branch information
xadupre committed Nov 7, 2023
commit 16e697e370db0ab83a41398fc957f76e41cb2934
2 changes: 1 addition & 1 deletion CHANGELOGS.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Change Logs
===========

0.2.0
0.1.2
+++++

* :pr:`42`: first sketch for a very simple API to create onnx graph in one or two lines
Expand Down
31 changes: 29 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.. image:: https://github.com/sdpython/onnx-array-api/raw/main/_doc/_static/logo.png
:width: 120

onnx-array-api: (Numpy) Array API for ONNX
onnx-array-api: APIs to create ONNX Graphs
==========================================

.. image:: https://dev.azure.com/xavierdupre3/onnx-array-api/_apis/build/status/sdpython.onnx-array-api
Expand All @@ -29,7 +29,9 @@ onnx-array-api: (Numpy) Array API for ONNX
.. 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 a numpy API for ONNX.
**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.
Expand Down Expand Up @@ -111,6 +113,31 @@ It supports eager mode as well:
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 <https://pypi.org/project/onnx-array-api/>`_
and its documentation is published at
Expand Down
41 changes: 35 additions & 6 deletions _doc/index.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

onnx-array-api: (Numpy) Array API for ONNX
onnx-array-api: APIs to create ONNX Graphs
==========================================

.. image:: https://dev.azure.com/xavierdupre3/onnx-array-api/_apis/build/status/sdpython.onnx-array-api
Expand All @@ -26,10 +26,8 @@ onnx-array-api: (Numpy) Array API for ONNX
.. 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 a numpy API for ONNX.
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.
**onnx-array-api** implements APIs to create custom ONNX graphs.
The objective is to speed up the implementation of converter libraries.

.. toctree::
:maxdepth: 1
Expand All @@ -47,6 +45,8 @@ well as to execute it.
CHANGELOGS
license

**Numpy API**

Sources available on
`github/onnx-array-api <https://github.com/sdpython/onnx-array-api>`_.

Expand All @@ -57,7 +57,7 @@ Sources available on

import numpy as np # A
from onnx_array_api.npx import absolute, jit_onnx
from onnx_array_api.plotting.dot_plot import to_dot
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot

def l1_loss(x, y):
return absolute(x - y).sum()
Expand All @@ -78,6 +78,8 @@ Sources available on
res = jitted_myloss(x, y)
print(res)

print(onnx_simple_text_plot(jitted_myloss.get_onnx()))

.. gdot::
:script: DOT-SECTION
:process:
Expand Down Expand Up @@ -106,3 +108,30 @@ Sources available on
y = np.array([[0.11, 0.22], [0.33, 0.44]], dtype=np.float32)
res = jitted_myloss(x, y)
print(to_dot(jitted_myloss.get_onnx()))

**Light API**

.. runpython::
:showcode:

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()
)

print(onnx_simple_text_plot(model))
60 changes: 60 additions & 0 deletions _doc/tutorial/onnx_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,66 @@ an existing onnx models or to merge models coming from different packages.
Sometimes, they are just not available, only onnx is.
Let's see how it looks like a very simply example.

Use torch or tensorflow
=======================

:epkg:`pytorch` offers the possibility to convert any function
implemented with pytorch function into onnx with :epkg:`torch.onnx`.
A couple of examples.

.. code-block:: python

import torch
import torch.nn


class MyModel(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(2, 2)

def forward(self, x, bias=None):
out = self.linear(x)
out = out + bias
return out

model = MyModel()
kwargs = {"bias": 3.}
args = (torch.randn(2, 2, 2),)

export_output = torch.onnx.dynamo_export(
model,
*args,
**kwargs).save("my_simple_model.onnx")

.. code-block:: python

from typing import Dict, Tuple
import torch
import torch.onnx


def func_with_nested_input_structure(
x_dict: Dict[str, torch.Tensor],
y_tuple: Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]],
):
if "a" in x_dict:
x = x_dict["a"]
elif "b" in x_dict:
x = x_dict["b"]
else:
x = torch.randn(3)

y1, (y2, y3) = y_tuple

return x + y1 + y2 + y3

x_dict = {"a": torch.tensor(1.)}
y_tuple = (torch.tensor(2.), (torch.tensor(3.), torch.tensor(4.)))
export_output = torch.onnx.dynamo_export(func_with_nested_input_structure, x_dict, y_tuple)

print(export_output.adapt_torch_inputs_to_onnx(x_dict, y_tuple))

Euclidian distance
==================

Expand Down
2 changes: 1 addition & 1 deletion onnx_array_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: utf-8
"""
(Numpy) Array API for ONNX.
APIs to create ONNX Graphs.
"""

__version__ = "0.1.2"
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy