Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
77 changes: 77 additions & 0 deletions _doc/sphinxdoc/source/_exts/generate_onnx_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Extension for sphinx to display the onnx nodes.
"""
from docutils import nodes
from docutils.parsers.rst import Directive
from docutils.statemachine import StringList
import sphinx
from sphinx.util.nodes import nested_parse_with_titles
from tabulate import tabulate
from mlprodict.npy.xop_factory import _dynamic_class_creation


class SupportedOnnxOpsDirective(Directive):
"""
Automatically displays the list of supported ONNX models
*skl2onnx* can use to build converters.
"""
required_arguments = False
optional_arguments = 0
final_argument_whitespace = True
option_spec = {}
has_content = False

def run(self):
cls = _dynamic_class_creation()
cls_name = [(c.__name__, c) for c in cls]
rows = []
sorted_cls_name = list(sorted(cls_name))
main = nodes.container()

def make_ref(cl):
return ":ref:`l-xop-onnx-{}`".format(cl.__name__)

table = []
cut = len(sorted_cls_name) // 3 + \
(1 if len(sorted_cls_name) % 3 else 0)
for i in range(cut):
row = []
row.append(make_ref(sorted_cls_name[i][1]))
if i + cut < len(sorted_cls_name):
row.append(make_ref(sorted_cls_name[i + cut][1]))
if i + cut * 2 < len(sorted_cls_name):
row.append(make_ref(sorted_cls_name[i + cut * 2][1]))
else:
row.append('')
else:
row.append('')
row.append('')
table.append(row)

rst = tabulate(table, tablefmt="rst")
rows = rst.split("\n")

node = nodes.container()
st = StringList(rows)
nested_parse_with_titles(self.state, st, node)
main += node

rows.append('')
for name, cl in sorted_cls_name:
rows = []
rows.append('.. _l-xop-onnx-{}:'.format(cl.__name__))
rows.append('')
rows.append(cl.__name__)
rows.append('=' * len(cl.__name__))
rows.append('')
rows.append(
".. autoclass:: mlprodict.npy.xop.xop_auto_import_.{}".format(name))
st = StringList(rows)
node = nodes.container()
nested_parse_with_titles(self.state, st, node)
main += node


def setup(app):
app.add_directive('supported-onnx-ops', SupportedOnnxOpsDirective)
return {'version': sphinx.__display_version__, 'parallel_read_safe': True}
1 change: 1 addition & 0 deletions _doc/sphinxdoc/source/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This is a summary of functions this modules provides.
onnx_conv
sklapi
npy
xop

**ONNX runtime**

Expand Down
48 changes: 48 additions & 0 deletions _doc/sphinxdoc/source/api/xop.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

.. _l-xop-onnxpy:

Create ONNX graphs
==================

.. contents::
:local:

Example
+++++++

Converters
++++++++++

API
+++

.. autosignature:: mlprodict.npy.xop.ClassFactory

.. autosignature:: mlprodict.npy.xop.dynamic_class_creation

.. autosignature:: mlprodict.npy.xops_variable.Variable

.. autosignature:: mlprodict.npy.xop_ops._GraphBuilder

.. autosignature:: mlprodict.npy.xop_ops.OnnxOperator

.. autosignature:: mlprodict.npy.xop_ops.OnnxOperatorItem

.. autosignature:: mlprodict.npy.xop_opset.OnnxReduceSumApi11

.. autosignature:: mlprodict.npy.xop_opset.OnnxSplitApi11

.. autosignature:: mlprodict.npy.xop_opset.OnnxSqueezeApi11

.. autosignature:: mlprodict.npy.xop_opset.OnnxUnsqueezeApi11

.. autosignature:: mlprodict.npy.xop_opset.OnnxReduceL2_typed

.. autosignature:: mlprodict.npy.xop_opset.OnnxReshapeApi13

Available ONNX operators
++++++++++++++++++++++++

.. toctree::

xop_supported
5 changes: 5 additions & 0 deletions _doc/sphinxdoc/source/api/xop_supported.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

Supported ONNX operators
========================

.. supported-onnx-ops::
3 changes: 3 additions & 0 deletions _doc/sphinxdoc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
try:
import generate_visual_graphs
import generate_automated_pages
import generate_onnx_ops
except ImportError: # pragma: no cover
this = os.path.dirname(__file__)
sys.path.append(os.path.join(this, '_exts'))
import generate_visual_graphs
import generate_automated_pages
import generate_onnx_ops


sys.path.insert(0, os.path.abspath(os.path.join(os.path.split(__file__)[0])))
Expand All @@ -44,6 +46,7 @@
'sphinxcontrib.blockdiag',
'generate_automated_pages',
'generate_visual_graphs',
'generate_onnx_ops',
])

html_css_files = ['my-styles.css']
Expand Down
27 changes: 27 additions & 0 deletions _unittests/ut_cli/test_cli_dynamic_doc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
@brief test tree node (time=23s)
"""
import unittest
from pyquickhelper.loghelper import BufferedPrint
from pyquickhelper.pycode import ExtTestCase
from mlprodict.__main__ import main


class TestCliDynamicDoc(ExtTestCase):

def test_cli_onnx_code_help(self):
st = BufferedPrint()
main(args=["dynamic_doc", "--help"], fLOG=st.fprint)
res = str(st)
self.assertIn("Generates", res)

def test_cli_onnx_code(self):
st = BufferedPrint()
main(args=["dynamic_doc", '--verbose', '1'], fLOG=st.fprint)
res = str(st)
if len(res) > 0:
self.assertIn("Abs", res)


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion _unittests/ut_cli/test_cli_onnx_code.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
@brief test tree node (time=10s)
@brief test tree node (time=15s)
"""
import os
import unittest
Expand Down
Loading