Skip to content

Commit 70d6f64

Browse files
authored
First documentation of the light API (#44)
* documentation * remove rstcheck, too many bugs * add missing link * fix unit test * fix documentation * unstable * windows * update titles * minorchanges
1 parent daf4402 commit 70d6f64

22 files changed

+1012
-61
lines changed

.github/workflows/rstcheck.yml

-27
This file was deleted.

CHANGELOGS.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Change Logs
22
===========
33

4-
0.2.0
4+
0.1.2
55
+++++
66

77
* :pr:`42`: first sketch for a very simple API to create onnx graph in one or two lines

README.rst

+29-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.. image:: https://github.com/sdpython/onnx-array-api/raw/main/_doc/_static/logo.png
33
:width: 120
44

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

88
.. image:: https://dev.azure.com/xavierdupre3/onnx-array-api/_apis/build/status/sdpython.onnx-array-api
@@ -29,7 +29,9 @@ onnx-array-api: (Numpy) Array API for ONNX
2929
.. image:: https://codecov.io/gh/sdpython/onnx-array-api/branch/main/graph/badge.svg?token=Wb9ZGDta8J
3030
:target: https://codecov.io/gh/sdpython/onnx-array-api
3131

32-
**onnx-array-api** implements a numpy API for ONNX.
32+
**onnx-array-api** implements APIs to create custom ONNX graphs.
33+
The objective is to speed up the implementation of converter libraries.
34+
The first one matches **numpy API**.
3335
It gives the user the ability to convert functions written
3436
following the numpy API to convert that function into ONNX as
3537
well as to execute it.
@@ -111,6 +113,31 @@ It supports eager mode as well:
111113
l2_loss=[0.002]
112114
[0.042]
113115

116+
The second API ir **Light API** tends to do every thing in one line.
117+
The euclidean distance looks like the following:
118+
119+
::
120+
121+
import numpy as np
122+
from onnx_array_api.light_api import start
123+
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot
124+
125+
model = (
126+
start()
127+
.vin("X")
128+
.vin("Y")
129+
.bring("X", "Y")
130+
.Sub()
131+
.rename("dxy")
132+
.cst(np.array([2], dtype=np.int64), "two")
133+
.bring("dxy", "two")
134+
.Pow()
135+
.ReduceSum()
136+
.rename("Z")
137+
.vout()
138+
.to_onnx()
139+
)
140+
114141
The library is released on
115142
`pypi/onnx-array-api <https://pypi.org/project/onnx-array-api/>`_
116143
and its documentation is published at

_doc/api/docs.rst

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
validation.docs
2+
===============
3+
4+
make_euclidean
5+
++++++++++++++
6+
7+
.. autofunction:: onnx_array_api.validation.docs.make_euclidean

_doc/api/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ API
2222
tools
2323
profiling
2424
f8
25+
docs

_doc/api/light_api.rst

+2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ Var
2424

2525
.. autoclass:: onnx_array_api.light_api.Var
2626
:members:
27+
:inherited-members:
2728

2829
Vars
2930
====
3031

3132
.. autoclass:: onnx_array_api.light_api.Vars
3233
:members:
34+
:inherited-members:

_doc/conf.py

+13
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,34 @@
114114
"https://data-apis.org/array-api/",
115115
("2022.12/API_specification/generated/array_api.{0}.html", 1),
116116
),
117+
"ast": "https://docs.python.org/3/library/ast.html",
117118
"cProfile.Profile": "https://docs.python.org/3/library/profile.html#profile.Profile",
118119
"DOT": "https://graphviz.org/doc/info/lang.html",
120+
"inner API": "https://onnx.ai/onnx/intro/python.html",
119121
"JIT": "https://en.wikipedia.org/wiki/Just-in-time_compilation",
120122
"onnx": "https://onnx.ai/onnx/",
123+
"onnx.helper": "https://onnx.ai/onnx/api/helper.html",
121124
"ONNX": "https://onnx.ai/",
125+
"ONNX Operators": "https://onnx.ai/onnx/operators/",
122126
"onnxruntime": "https://onnxruntime.ai/",
127+
"onnxruntime-training": "https://onnxruntime.ai/docs/get-started/training-on-device.html",
123128
"numpy": "https://numpy.org/",
124129
"numba": "https://numba.pydata.org/",
125130
"onnx-array-api": ("https://sdpython.github.io/doc/onnx-array-api/dev/"),
131+
"onnxscript": "https://github.com/microsoft/onnxscript",
126132
"pyinstrument": "https://github.com/joerick/pyinstrument",
127133
"python": "https://www.python.org/",
134+
"pytorch": "https://pytorch.org/",
135+
"reverse Polish notation": "https://en.wikipedia.org/wiki/Reverse_Polish_notation",
128136
"scikit-learn": "https://scikit-learn.org/stable/",
129137
"scipy": "https://scipy.org/",
138+
"sklearn-onnx": "https://onnx.ai/sklearn-onnx/",
139+
"spox": "https://github.com/Quantco/spox",
130140
"sphinx-gallery": "https://github.com/sphinx-gallery/sphinx-gallery",
141+
"tensorflow": "https://www.tensorflow.org/",
142+
"tensorflow-onnx": "https://github.com/onnx/tensorflow-onnx",
131143
"torch": "https://pytorch.org/docs/stable/torch.html",
144+
"torch.onnx": "https://pytorch.org/docs/stable/onnx.html",
132145
#
133146
"C_OrtValue": (
134147
"http://www.xavierdupre.fr/app/onnxcustom/helpsphinx/"

_doc/index.rst

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

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

55
.. image:: https://dev.azure.com/xavierdupre3/onnx-array-api/_apis/build/status/sdpython.onnx-array-api
@@ -26,10 +26,8 @@ onnx-array-api: (Numpy) Array API for ONNX
2626
.. image:: https://codecov.io/gh/sdpython/onnx-array-api/branch/main/graph/badge.svg?token=Wb9ZGDta8J
2727
:target: https://codecov.io/gh/sdpython/onnx-array-api
2828

29-
**onnx-array-api** implements a numpy API for ONNX.
30-
It gives the user the ability to convert functions written
31-
following the numpy API to convert that function into ONNX as
32-
well as to execute it.
29+
**onnx-array-api** implements APIs to create custom ONNX graphs.
30+
The objective is to speed up the implementation of converter libraries.
3331

3432
.. toctree::
3533
:maxdepth: 1
@@ -47,6 +45,8 @@ well as to execute it.
4745
CHANGELOGS
4846
license
4947

48+
**Numpy API**
49+
5050
Sources available on
5151
`github/onnx-array-api <https://github.com/sdpython/onnx-array-api>`_.
5252

@@ -57,7 +57,7 @@ Sources available on
5757

5858
import numpy as np # A
5959
from onnx_array_api.npx import absolute, jit_onnx
60-
from onnx_array_api.plotting.dot_plot import to_dot
60+
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot
6161

6262
def l1_loss(x, y):
6363
return absolute(x - y).sum()
@@ -78,6 +78,8 @@ Sources available on
7878
res = jitted_myloss(x, y)
7979
print(res)
8080

81+
print(onnx_simple_text_plot(jitted_myloss.get_onnx()))
82+
8183
.. gdot::
8284
:script: DOT-SECTION
8385
:process:
@@ -106,3 +108,30 @@ Sources available on
106108
y = np.array([[0.11, 0.22], [0.33, 0.44]], dtype=np.float32)
107109
res = jitted_myloss(x, y)
108110
print(to_dot(jitted_myloss.get_onnx()))
111+
112+
**Light API**
113+
114+
.. runpython::
115+
:showcode:
116+
117+
import numpy as np
118+
from onnx_array_api.light_api import start
119+
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot
120+
121+
model = (
122+
start()
123+
.vin("X")
124+
.vin("Y")
125+
.bring("X", "Y")
126+
.Sub()
127+
.rename("dxy")
128+
.cst(np.array([2], dtype=np.int64), "two")
129+
.bring("dxy", "two")
130+
.Pow()
131+
.ReduceSum()
132+
.rename("Z")
133+
.vout()
134+
.to_onnx()
135+
)
136+
137+
print(onnx_simple_text_plot(model))

_doc/tech/aapi.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
.. _l-array-api-painpoint:
12

2-
Difficulty to implement an an Array API for ONNX
3-
================================================
3+
Difficulty to implement an Array API for ONNX
4+
=============================================
45

56
Implementing the full array API is not always easy with :epkg:`onnx`.
67
Python is not strongly typed and many different types can be used

_doc/tutorial/index.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ Tutorial
66
.. toctree::
77
:maxdepth: 1
88

9-
overview
9+
onnx_api
10+
light_api
11+
numpy_api
1012
benchmarks

_doc/tutorial/light_api.rst

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.. _l-light-api:
2+
3+
==========================================
4+
Light API for ONNX: everything in one line
5+
==========================================
6+
7+
It is inspired from the :epkg:`reverse Polish notation`.
8+
Following example implements the euclidean distance.
9+
This API tries to keep it simple and intuitive to short functions.
10+
11+
.. runpython::
12+
:showcode:
13+
14+
import numpy as np
15+
from onnx_array_api.light_api import start
16+
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot
17+
18+
model = (
19+
start()
20+
.vin("X")
21+
.vin("Y")
22+
.bring("X", "Y")
23+
.Sub()
24+
.rename("dxy")
25+
.cst(np.array([2], dtype=np.int64), "two")
26+
.bring("dxy", "two")
27+
.Pow()
28+
.ReduceSum()
29+
.rename("Z")
30+
.vout()
31+
.to_onnx()
32+
)
33+
34+
print(onnx_simple_text_plot(model))
35+
36+
There are two kinds of methods, the graph methods, playing with the graph structure,
37+
and the methods for operators starting with an upper letter.
38+
39+
Graph methods
40+
=============
41+
42+
Any graph must start with function :func:`start <onnx_array_api.light_api.start>`.
43+
It is usually following by `vin` to add an input.
44+
45+
* bring (:meth:`Var.bring <onnx_array_api.light_api.Var.bring>`,
46+
:meth:`Vars.bring <onnx_array_api.light_api.Vars.bring>`):
47+
assembles multiple results into a set before calling an operator taking mulitple inputs,
48+
* cst (:meth:`Var.cst <onnx_array_api.light_api.Var.cst>`,
49+
:meth:`Vars.cst <onnx_array_api.light_api.Vars.cst>`):
50+
adds a constant tensor to the graph,
51+
* rename (:meth:`Var.rename <onnx_array_api.light_api.Var.rename>`,
52+
:meth:`Vars.rename <onnx_array_api.light_api.Vars.rename>`):
53+
renames or give a name to a variable in order to call it later.
54+
* vin (:meth:`Var.vin <onnx_array_api.light_api.Var.vin>`,
55+
:meth:`Vars.vin <onnx_array_api.light_api.Vars.vin>`):
56+
adds an input to the graph,
57+
* vout (:meth:`Var.vout <onnx_array_api.light_api.Var.vout>`,
58+
:meth:`Vars.vout <onnx_array_api.light_api.Vars.vout>`):
59+
declares an existing result as an output.
60+
61+
These methods are implemented in class :class:`onnx_array_api.light_api.var.BaseVar`
62+
63+
Operator methods
64+
================
65+
66+
They are described in :epkg:`ONNX Operators` and redefined in a stable API
67+
so that the definition should not change depending on this opset.
68+
:class:`onnx_array_api.light_api.Var` defines all operators taking only one input.
69+
:class:`onnx_array_api.light_api.Vars` defines all other operators.
70+
71+
Numpy methods
72+
=============
73+
74+
Numpy users expect methods such as `reshape`, property `shape` or
75+
operator `+` to be available as well and that the case. They are
76+
defined in class :class:`Var <onnx_array_api.light_api.Var>` or
77+
:class:`Vars <onnx_array_api.light_api.Vars>` depending on the number of
78+
inputs they require. Their name starts with a lower letter.

_doc/tutorial/overview.rst renamed to _doc/tutorial/numpy_api.rst

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _l-numpy-api-onnx:
2+
13
==================
24
Numpy API for ONNX
35
==================
@@ -19,6 +21,8 @@ loss functions for example without knowing too much about ONNX.
1921
The first version (onnx==1.15) does not support control flow yet (test and loops).
2022
There is no easy syntax for that yet and the main challenge is to deal with local context.
2123

24+
You read :ref:`l-array-api-painpoint` as well.
25+
2226
Overview
2327
========
2428

0 commit comments

Comments
 (0)