onnx-array-api


Nameonnx-array-api JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/sdpython/onnx-array-api
SummaryArray (and numpy) API for ONNX
upload_time2024-03-01 15:34:39
maintainer
docs_urlNone
authorXavier Dupré
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            onnx-array-api: APIs to create ONNX Graphs
==========================================

.. image:: https://dev.azure.com/xavierdupre3/onnx-array-api/_apis/build/status/sdpython.onnx-array-api
    :target: https://dev.azure.com/xavierdupre3/onnx-array-api/

.. image:: https://badge.fury.io/py/onnx-array-api.svg
    :target: http://badge.fury.io/py/onnx-array-api

.. image:: http://img.shields.io/github/issues/sdpython/onnx-array-api.png
    :alt: GitHub Issues
    :target: https://github.com/sdpython/onnx-array-api/issues

.. image:: https://img.shields.io/badge/license-MIT-blue.svg
    :alt: MIT License
    :target: https://opensource.org/license/MIT/

.. image:: https://img.shields.io/github/repo-size/sdpython/onnx-array-api
    :target: https://github.com/sdpython/onnx-array-api/
    :alt: size

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
    :target: https://github.com/psf/black

.. 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 APIs to create custom ONNX graphs.
The objective is to speed up the implementation of converter libraries.
The library is released on
`pypi/onnx-array-api <https://pypi.org/project/onnx-array-api/>`_
and its documentation is published at
`APIs to create ONNX Graphs <https://sdpython.github.io/doc/onnx-array-api/dev/>`_.

Numpy API
+++++++++

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.

.. code-block:: python

    import numpy as np
    from onnx_array_api.npx import absolute, jit_onnx
    from onnx_array_api.plotting.text_plot import onnx_simple_text_plot

    def l1_loss(x, y):
        return absolute(x - y).sum()


    def l2_loss(x, y):
        return ((x - y) ** 2).sum()


    def myloss(x, y):
        return l1_loss(x[:, 0], y[:, 0]) + l2_loss(x[:, 1], y[:, 1])


    jitted_myloss = jit_onnx(myloss)

    x = np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32)
    y = np.array([[0.11, 0.22], [0.33, 0.44]], dtype=np.float32)

    res = jitted_myloss(x, y)
    print(res)

    print(onnx_simple_text_plot(jitted_myloss.get_onnx()))

::

    [0.042]
    opset: domain='' version=18
    input: name='x0' type=dtype('float32') shape=['', '']
    input: name='x1' type=dtype('float32') shape=['', '']
    Sub(x0, x1) -> r__0
      Abs(r__0) -> r__1
        ReduceSum(r__1, keepdims=0) -> r__2
    output: name='r__2' type=dtype('float32') shape=None

It supports eager mode as well:

.. code-block:: python

    import numpy as np
    from onnx_array_api.npx import absolute, eager_onnx


    def l1_loss(x, y):
        err = absolute(x - y).sum()
        print(f"l1_loss={err.numpy()}")
        return err


    def l2_loss(x, y):
        err = ((x - y) ** 2).sum()
        print(f"l2_loss={err.numpy()}")
        return err


    def myloss(x, y):
        return l1_loss(x[:, 0], y[:, 0]) + l2_loss(x[:, 1], y[:, 1])


    eager_myloss = eager_onnx(myloss)

    x = np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32)
    y = np.array([[0.11, 0.22], [0.33, 0.44]], dtype=np.float32)

    res = eager_myloss(x, y)
    print(res)

::

    l1_loss=[0.04]
    l2_loss=[0.002]
    [0.042]

Light API
+++++++++

The second API or **Light API** tends to do every thing in one line.
It is inspired from the `Reverse Polish Notation
<https://en.wikipedia.org/wiki/Reverse_Polish_notation>`_.
The euclidean distance looks like the following:

.. code-block:: python

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

GraphBuilder API
++++++++++++++++

Almost every converting library (converting a machine learned model to ONNX) is implementing
its own graph builder and customizes it for its needs.
It handles some frequent tasks such as giving names to intermediate
results, loading, saving onnx models. It can be used as well to extend an existing graph.

.. code-block:: python

    import numpy as np
    from onnx_array_api.graph_api  import GraphBuilder

    g = GraphBuilder()
    g.make_tensor_input("X", np.float32, (None, None))
    g.make_tensor_input("Y", np.float32, (None, None))
    r1 = g.make_node("Sub", ["X", "Y"])  # the name given to the output is given by the class,
                                         # it ensures the name is unique
    init = g.make_initializer(np.array([2], dtype=np.int64))  # the class automatically
                                                              # converts the array to a tensor
    r2 = g.make_node("Pow", [r1, init])
    g.make_node("ReduceSum", [r2], outputs=["Z"])  # the output name is given because
                                                   # the user wants to choose the name
    g.make_tensor_output("Z", np.float32, (None, None))

    onx = g.to_onnx()  # final conversion to onnx

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/sdpython/onnx-array-api",
    "name": "onnx-array-api",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Xavier Dupr\u00e9",
    "author_email": "xavier.dupre@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/18/1c/f438c3665567582fc2e3cdddfd31edcc1713143285ba5b7809269672548b/onnx-array-api-0.2.0.tar.gz",
    "platform": null,
    "description": "onnx-array-api: APIs to create ONNX Graphs\n==========================================\n\n.. image:: https://dev.azure.com/xavierdupre3/onnx-array-api/_apis/build/status/sdpython.onnx-array-api\n    :target: https://dev.azure.com/xavierdupre3/onnx-array-api/\n\n.. image:: https://badge.fury.io/py/onnx-array-api.svg\n    :target: http://badge.fury.io/py/onnx-array-api\n\n.. image:: http://img.shields.io/github/issues/sdpython/onnx-array-api.png\n    :alt: GitHub Issues\n    :target: https://github.com/sdpython/onnx-array-api/issues\n\n.. image:: https://img.shields.io/badge/license-MIT-blue.svg\n    :alt: MIT License\n    :target: https://opensource.org/license/MIT/\n\n.. image:: https://img.shields.io/github/repo-size/sdpython/onnx-array-api\n    :target: https://github.com/sdpython/onnx-array-api/\n    :alt: size\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n    :target: https://github.com/psf/black\n\n.. image:: https://codecov.io/gh/sdpython/onnx-array-api/branch/main/graph/badge.svg?token=Wb9ZGDta8J \n    :target: https://codecov.io/gh/sdpython/onnx-array-api\n\n**onnx-array-api** implements APIs to create custom ONNX graphs.\nThe objective is to speed up the implementation of converter libraries.\nThe library is released on\n`pypi/onnx-array-api <https://pypi.org/project/onnx-array-api/>`_\nand its documentation is published at\n`APIs to create ONNX Graphs <https://sdpython.github.io/doc/onnx-array-api/dev/>`_.\n\nNumpy API\n+++++++++\n\nThe first one matches **numpy API**.\nIt gives the user the ability to convert functions written\nfollowing the numpy API to convert that function into ONNX as\nwell as to execute it.\n\n.. code-block:: python\n\n    import numpy as np\n    from onnx_array_api.npx import absolute, jit_onnx\n    from onnx_array_api.plotting.text_plot import onnx_simple_text_plot\n\n    def l1_loss(x, y):\n        return absolute(x - y).sum()\n\n\n    def l2_loss(x, y):\n        return ((x - y) ** 2).sum()\n\n\n    def myloss(x, y):\n        return l1_loss(x[:, 0], y[:, 0]) + l2_loss(x[:, 1], y[:, 1])\n\n\n    jitted_myloss = jit_onnx(myloss)\n\n    x = np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32)\n    y = np.array([[0.11, 0.22], [0.33, 0.44]], dtype=np.float32)\n\n    res = jitted_myloss(x, y)\n    print(res)\n\n    print(onnx_simple_text_plot(jitted_myloss.get_onnx()))\n\n::\n\n    [0.042]\n    opset: domain='' version=18\n    input: name='x0' type=dtype('float32') shape=['', '']\n    input: name='x1' type=dtype('float32') shape=['', '']\n    Sub(x0, x1) -> r__0\n      Abs(r__0) -> r__1\n        ReduceSum(r__1, keepdims=0) -> r__2\n    output: name='r__2' type=dtype('float32') shape=None\n\nIt supports eager mode as well:\n\n.. code-block:: python\n\n    import numpy as np\n    from onnx_array_api.npx import absolute, eager_onnx\n\n\n    def l1_loss(x, y):\n        err = absolute(x - y).sum()\n        print(f\"l1_loss={err.numpy()}\")\n        return err\n\n\n    def l2_loss(x, y):\n        err = ((x - y) ** 2).sum()\n        print(f\"l2_loss={err.numpy()}\")\n        return err\n\n\n    def myloss(x, y):\n        return l1_loss(x[:, 0], y[:, 0]) + l2_loss(x[:, 1], y[:, 1])\n\n\n    eager_myloss = eager_onnx(myloss)\n\n    x = np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32)\n    y = np.array([[0.11, 0.22], [0.33, 0.44]], dtype=np.float32)\n\n    res = eager_myloss(x, y)\n    print(res)\n\n::\n\n    l1_loss=[0.04]\n    l2_loss=[0.002]\n    [0.042]\n\nLight API\n+++++++++\n\nThe second API or **Light API** tends to do every thing in one line.\nIt is inspired from the `Reverse Polish Notation\n<https://en.wikipedia.org/wiki/Reverse_Polish_notation>`_.\nThe euclidean distance looks like the following:\n\n.. code-block:: python\n\n    import numpy as np\n    from onnx_array_api.light_api import start\n    from onnx_array_api.plotting.text_plot import onnx_simple_text_plot\n\n    model = (\n        start()\n        .vin(\"X\")\n        .vin(\"Y\")\n        .bring(\"X\", \"Y\")\n        .Sub()\n        .rename(\"dxy\")\n        .cst(np.array([2], dtype=np.int64), \"two\")\n        .bring(\"dxy\", \"two\")\n        .Pow()\n        .ReduceSum()\n        .rename(\"Z\")\n        .vout()\n        .to_onnx()\n    )    \n\nGraphBuilder API\n++++++++++++++++\n\nAlmost every converting library (converting a machine learned model to ONNX) is implementing\nits own graph builder and customizes it for its needs.\nIt handles some frequent tasks such as giving names to intermediate\nresults, loading, saving onnx models. It can be used as well to extend an existing graph.\n\n.. code-block:: python\n\n    import numpy as np\n    from onnx_array_api.graph_api  import GraphBuilder\n\n    g = GraphBuilder()\n    g.make_tensor_input(\"X\", np.float32, (None, None))\n    g.make_tensor_input(\"Y\", np.float32, (None, None))\n    r1 = g.make_node(\"Sub\", [\"X\", \"Y\"])  # the name given to the output is given by the class,\n                                         # it ensures the name is unique\n    init = g.make_initializer(np.array([2], dtype=np.int64))  # the class automatically\n                                                              # converts the array to a tensor\n    r2 = g.make_node(\"Pow\", [r1, init])\n    g.make_node(\"ReduceSum\", [r2], outputs=[\"Z\"])  # the output name is given because\n                                                   # the user wants to choose the name\n    g.make_tensor_output(\"Z\", np.float32, (None, None))\n\n    onx = g.to_onnx()  # final conversion to onnx\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Array (and numpy) API for ONNX",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/sdpython/onnx-array-api"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f7f65342cd3d337b1b41cff09fcfefc4d52e4bf9dc873d131584ae92cf104261",
                "md5": "dea1b47431adf8f538a8b2310a0e8e9a",
                "sha256": "dc5679037c374a4a71ef3ce9ff93b5ae958ee28d06b55f07a385cd0c76a9a87a"
            },
            "downloads": -1,
            "filename": "onnx_array_api-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "dea1b47431adf8f538a8b2310a0e8e9a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 229381,
            "upload_time": "2024-03-01T15:34:37",
            "upload_time_iso_8601": "2024-03-01T15:34:37.393842Z",
            "url": "https://files.pythonhosted.org/packages/f7/f6/5342cd3d337b1b41cff09fcfefc4d52e4bf9dc873d131584ae92cf104261/onnx_array_api-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "181cf438c3665567582fc2e3cdddfd31edcc1713143285ba5b7809269672548b",
                "md5": "97171977826b120eba3f913a6d0c0f12",
                "sha256": "93c000b9258f5ad3c233a58d0252512646588881bab1ff4b45c12a13551b7eaf"
            },
            "downloads": -1,
            "filename": "onnx-array-api-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "97171977826b120eba3f913a6d0c0f12",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 207211,
            "upload_time": "2024-03-01T15:34:39",
            "upload_time_iso_8601": "2024-03-01T15:34:39.500258Z",
            "url": "https://files.pythonhosted.org/packages/18/1c/f438c3665567582fc2e3cdddfd31edcc1713143285ba5b7809269672548b/onnx-array-api-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-01 15:34:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sdpython",
    "github_project": "onnx-array-api",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "onnx-array-api"
}
        
Elapsed time: 0.82919s