Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
sdpython merged 9 commits intomainfromdoc5
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from1 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
PrevPrevious commit
NextNext commit
update titles
  • Loading branch information
@xadupre
xadupre committedNov 7, 2023
commit16e697e370db0ab83a41398fc957f76e41cb2934
2 changes: 1 addition & 1 deletionCHANGELOGS.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsREADME.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 forONNX
onnx-array-api:APIs to createONNX 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 DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@

onnx-array-api:(Numpy) Array API forONNX
onnx-array-api:APIs to createONNX 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 importto_dot
from onnx_array_api.plotting.text_plot importonnx_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 DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletiononnx_array_api/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
# coding: utf-8
"""
(Numpy) Array API forONNX.
APIs to createONNX Graphs.
"""

__version__ = "0.1.2"
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp