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

Refactoring and fixes minor bugs in light API#62

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 intomainfromminor
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
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
1 change: 1 addition & 0 deletions_doc/api/index.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@ API
array_api
graph_api
light_api
translate_api
npx_core_api
npx_functions
npx_jit_eager
Expand Down
46 changes: 3 additions & 43 deletions_doc/api/light_api.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,17 +11,10 @@ start

.. autofunction:: onnx_array_api.light_api.start

translate
+++++++++

.. autofunction:: onnx_array_api.light_api.translate

make_helper
+++++++++++
g
+

.. autofunction:: onnx_array_api.light_api.make_helper.make_node_extended

.. autofunction:: onnx_array_api.light_api.make_helper.make_ref_attribute
.. autofunction:: onnx_array_api.light_api.g

Classes for the Light API
=========================
Expand DownExpand Up@@ -69,39 +62,6 @@ Vars
:members:
:inherited-members:

Classes for the Translater
==========================

BaseEmitter
+++++++++++

.. autoclass:: onnx_array_api.light_api.base_emitter.BaseEmitter
:members:

EventType
+++++++++

.. autoclass:: onnx_array_api.light_api.base_emitter.EventType
:members:

InnerEmitter
++++++++++++

.. autoclass:: onnx_array_api.light_api.inner_emitter.InnerEmitter
:members:

LightEmitter
++++++++++++

.. autoclass:: onnx_array_api.light_api.light_emitter.LightEmitter
:members:

Translater
++++++++++

.. autoclass:: onnx_array_api.light_api.translate.Translater
:members:

Available operators
===================

Expand Down
52 changes: 52 additions & 0 deletions_doc/api/translate_api.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
============================
onnx_array_api.translate_api
============================


Main API
========

translate
+++++++++

.. autofunction:: onnx_array_api.translate_api.translate

make_helper
+++++++++++

.. autofunction:: onnx_array_api.translate_api.make_helper.make_node_extended

.. autofunction:: onnx_array_api.translate_api.make_helper.make_ref_attribute

Classes for the Translater
==========================

BaseEmitter
+++++++++++

.. autoclass:: onnx_array_api.translate_api.base_emitter.BaseEmitter
:members:

EventType
+++++++++

.. autoclass:: onnx_array_api.translate_api.base_emitter.EventType
:members:

InnerEmitter
++++++++++++

.. autoclass:: onnx_array_api.translate_api.inner_emitter.InnerEmitter
:members:

LightEmitter
++++++++++++

.. autoclass:: onnx_array_api.translate_api.light_emitter.LightEmitter
:members:

Translater
++++++++++

.. autoclass:: onnx_array_api.translate_api.translate.Translater
:members:
4 changes: 2 additions & 2 deletions_unittests/ut_light_api/test_backend_export.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,8 +22,8 @@
from onnx.numpy_helper import from_array, to_array
from onnx.backend.base import Device, DeviceType
from onnx_array_api.reference import ExtendedReferenceEvaluator
from onnx_array_api.light_api.make_helper import make_node_extended
from onnx_array_api.light_api import translate
from onnx_array_api.translate_api.make_helper import make_node_extended
from onnx_array_api.translate_api import translate
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot

verbosity = 10 if "-v" in sys.argv or "--verbose" in sys.argv else 0
Expand Down
20 changes: 18 additions & 2 deletions_unittests/ut_light_api/test_light_api.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -211,7 +211,7 @@ def test_neg(self):
self.assertIsInstance(v, Var)
self.assertEqual(["X"], v.parent.input_names)
s = str(v)
self.assertEqual("X:FLOAT", s)
self.assertEqual("X:FLOAT:[]", s)
onx = start().vin("X").Neg().rename("Y").vout().to_onnx()
self.assertIsInstance(onx, ModelProto)
ref = ReferenceEvaluator(onx)
Expand DownExpand Up@@ -510,7 +510,23 @@ def ah(self):
expected = (a > 0).astype(int).astype(np.float32).reshape((-1, 1))
self.assertEqualArray(expected, got)

def test_input_shape(self):
kernel = (np.arange(9) + 1).reshape(3, 3).astype(np.float32)
model = (
start()
.vin("X", shape=[None, None])
.cst(kernel[np.newaxis, np.newaxis, ...])
.rename("W")
.bring("X", "W")
.Conv(pads=[1, 1, 1, 1])
.rename("Y")
.vout(shape=[])
.to_onnx()
)
i = str(model.graph.input[0]).replace("\n", "").replace(" ", "")
self.assertNotIn("shape{}", i)


if __name__ == "__main__":
TestLightApi().test_domain()
TestLightApi().test_add()
unittest.main(verbosity=2)
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,8 +5,9 @@
from onnx.defs import onnx_opset_version
from onnx.reference import ReferenceEvaluator
from onnx_array_api.ext_test_case import ExtTestCase
from onnx_array_api.light_api import start, translate, g
from onnx_array_api.light_api.base_emitter import EventType
from onnx_array_api.light_api import start, g
from onnx_array_api.translate_api import translate
from onnx_array_api.translate_api.base_emitter import EventType

OPSET_API = min(19, onnx_opset_version() - 1)

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,7 +15,8 @@
)
from onnx.checker import check_model
from onnx_array_api.ext_test_case import ExtTestCase
from onnx_array_api.light_api import start, translate
from onnx_array_api.light_api import start
from onnx_array_api.translate_api import translate

OPSET_API = min(19, onnx_opset_version() - 1)

Expand DownExpand Up@@ -335,7 +336,7 @@ def _run(cls, code):
import onnx
import onnx.helper
import onnx.numpy_helper
import onnx_array_api.light_api.make_helper
import onnx_array_api.translate_api.make_helper
import onnx.reference.custom_element_types

def from_array_extended(tensor, name=None):
Expand All@@ -362,7 +363,7 @@ def from_array_extended(tensor, name=None):
globs = onnx.__dict__.copy()
globs.update(onnx.helper.__dict__)
globs.update(onnx.numpy_helper.__dict__)
globs.update(onnx_array_api.light_api.make_helper.__dict__)
globs.update(onnx_array_api.translate_api.make_helper.__dict__)
globs.update(onnx.reference.custom_element_types.__dict__)
globs["from_array_extended"] = from_array_extended
locs = {}
Expand Down
2 changes: 1 addition & 1 deletiononnx_array_api/_command_lines_parser.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -56,7 +56,7 @@ def get_parser_translate() -> ArgumentParser:


def _cmd_translate(argv: List[Any]):
from .light_api import translate
from .translate_api import translate

parser = get_parser_translate()
args = parser.parse_args(argv[1:])
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -81,9 +81,17 @@ def elem_type_int(elem_type: ELEMENT_TYPE) -> int:
return np_dtype_to_tensor_dtype(elem_type)


def make_shape(shape: TensorShapeProto) -> SHAPE_TYPE:
def _pick_dim(d, empty_dim):
if d.dim_value:
return d.dim_value
if d.dim_param:
return d.dim_param
return empty_dim


def make_shape(shape: TensorShapeProto, empty_dim: Optional[Any] = None) -> SHAPE_TYPE:
"Extracts a shape from a tensor type."
if hasattr(shape, "dims"):
res = [(d.dim_value if d.dim_value else d.dim_param) for d in shape.dims]
if hasattr(shape, "dim"):
res = [_pick_dim(d, empty_dim=empty_dim) fori,d inenumerate(shape.dim)]
return tuple(res)
return None
3 changes: 3 additions & 0 deletionsonnx_array_api/graph_api/graph_builder.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -631,6 +631,9 @@ def _build_initializers(self) -> List[TensorProto]:
t = onh.from_array(v, name=k)
res.append(t)
continue
if isinstance(v, TensorProto):
res.append(v)
continue
raise TypeError(
f"Unable to convert initializer {k!r} with type "
f"{type(v)} into a TensorProto."
Expand Down
63 changes: 1 addition & 62 deletionsonnx_array_api/light_api/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
from typing import Dict, Optional
from onnx import ModelProto
from .annotations import domain
from ..annotations import domain
from .model import OnnxGraph, ProtoType
from .translate import Translater
from .var import Var, Vars
from .inner_emitter import InnerEmitter


def start(
Expand DownExpand Up@@ -56,62 +54,3 @@ def g() -> OnnxGraph:
:return: an instance of :class:`onnx_array_api.light_api.OnnxGraph`
"""
return OnnxGraph(proto_type=ProtoType.GRAPH)


def translate(proto: ModelProto, single_line: bool = False, api: str = "light") -> str:
"""
Translates an ONNX proto into a code using :ref:`l-light-api`
to describe the ONNX graph.

:param proto: model to translate
:param single_line: as a single line or not
:param api: API to export into,
default is `"light"` and this is handle by class
:class:`onnx_array_api.light_api.light_emitter.LightEmitter`,
another value is `"onnx"` which is the inner API implemented
in onnx package.
:return: code

.. runpython::
:showcode:

from onnx_array_api.light_api import start, translate

onx = (
start()
.vin("X")
.reshape((-1, 1))
.Transpose(perm=[1, 0])
.rename("Y")
.vout()
.to_onnx()
)
code = translate(onx)
print(code)

The inner API from onnx packahe is also available.

.. runpython::
:showcode:

from onnx_array_api.light_api import start, translate

onx = (
start()
.vin("X")
.reshape((-1, 1))
.Transpose(perm=[1, 0])
.rename("Y")
.vout()
.to_onnx()
)
code = translate(onx, api="onnx")
print(code)
"""
if api == "light":
tr = Translater(proto)
return tr.export(single_line=single_line, as_str=True)
if api == "onnx":
tr = Translater(proto, emitter=InnerEmitter())
return tr.export(as_str=True)
raise ValueError(f"Unexpected value {api!r} for api.")
2 changes: 1 addition & 1 deletiononnx_array_api/light_api/_op_var.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
from typing import List, Optional, Union
from .annotations import AI_ONNX_ML, domain
from ..annotations import AI_ONNX_ML, domain


class OpsVar:
Expand Down
24 changes: 11 additions & 13 deletionsonnx_array_api/light_api/_op_vars.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -49,19 +49,17 @@ def Conv(
pads: Optional[List[int]] = None,
strides: Optional[List[int]] = None,
) -> "Var":
dilations = dilations or []
kernel_shape = kernel_shape or []
pads = pads or []
strides = strides or []
return self.make_node(
"Conv",
*self.vars_,
auto_pad=auto_pad,
dilations=dilations,
group=group,
kernel_shape=kernel_shape,
pads=pads,
strides=strides,
kwargs = {}
if dilations is not None:
kwargs["dilations"] = dilations
if kernel_shape is not None:
kwargs["kernel_shape"] = kernel_shape
if pads is not None:
kwargs["pads"] = pads
if strides is not None:
kwargs["strides"] = strides
return self.make_node(
"Conv", *self.vars_, auto_pad=auto_pad, group=group, **kwargs
)

def ConvInteger(
Expand Down
8 changes: 5 additions & 3 deletionsonnx_array_api/light_api/model.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,7 +14,7 @@
)
from onnx.numpy_helper import from_array
from ..ext_test_case import is_azure, is_windows
from .annotations import (
from ..annotations import (
elem_type_int,
make_shape,
GRAPH_PROTO,
Expand DownExpand Up@@ -180,6 +180,8 @@ def make_output(
:param elem_type: element type (the input is assumed to be a tensor)
:param shape: shape
:return: an instance of ValueInfoProto

If the checker fails, try `shape=[]`.
"""
if not self.has_name(name):
raise ValueError(f"Name {name!r} does not exist.")
Expand DownExpand Up@@ -332,7 +334,7 @@ def _fix_name_tensor_input(
) -> Union[TensorProto, SparseTensorProto, ValueInfoProto]:
obj = self._fix_name_tensor(obj)
shape = make_shape(obj.type.tensor_type.shape)
ifshape is None:
ifnot shape:
tensor_type_proto = make_tensor_type_proto(
obj.type.tensor_type.elem_type, []
)
Expand All@@ -344,7 +346,7 @@ def _fix_name_tensor_output(
) -> Union[TensorProto, SparseTensorProto, ValueInfoProto]:
obj = self._fix_name_tensor(obj)
shape = make_shape(obj.type.tensor_type.shape)
ifshape is None:
ifnot shape:
tensor_type_proto = make_tensor_type_proto(
obj.type.tensor_type.elem_type, []
)
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp