{"id":15693245,"url":"https://github.com/sdpython/onnx-array-api","last_synced_at":"2025-05-07T23:43:18.454Z","repository":{"id":143463347,"uuid":"615291125","full_name":"sdpython/onnx-array-api","owner":"sdpython","description":"Array APIs to write ONNX Graphs","archived":false,"fork":false,"pushed_at":"2025-05-06T09:55:15.000Z","size":1593,"stargazers_count":11,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-07T23:42:59.464Z","etag":null,"topics":["array-api","machine-learning","numpy","onnx"],"latest_commit_sha":null,"homepage":"https://sdpython.github.io/doc/onnx-array-api/dev/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sdpython.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOGS.rst","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-17T11:16:57.000Z","updated_at":"2025-04-08T10:30:53.000Z","dependencies_parsed_at":"2023-11-07T17:38:36.232Z","dependency_job_id":"19998e67-637f-424f-9596-50fbdbac24f5","html_url":"https://github.com/sdpython/onnx-array-api","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdpython%2Fonnx-array-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdpython%2Fonnx-array-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdpython%2Fonnx-array-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdpython%2Fonnx-array-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sdpython","download_url":"https://codeload.github.com/sdpython/onnx-array-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252973617,"owners_count":21834105,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array-api","machine-learning","numpy","onnx"],"created_at":"2024-10-03T18:42:23.550Z","updated_at":"2025-05-07T23:43:18.423Z","avatar_url":"https://github.com/sdpython.png","language":"Python","readme":"\n.. image:: https://github.com/sdpython/onnx-array-api/raw/main/_doc/_static/logo.png\n    :width: 120\n\nonnx-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 \u003chttps://pypi.org/project/onnx-array-api/\u003e`_\nand its documentation is published at\n`APIs to create ONNX Graphs \u003chttps://sdpython.github.io/doc/onnx-array-api/dev/\u003e`_.\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) -\u003e r__0\n      Abs(r__0) -\u003e r__1\n        ReduceSum(r__1, keepdims=0) -\u003e 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\u003chttps://en.wikipedia.org/wiki/Reverse_Polish_notation\u003e`_.\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","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsdpython%2Fonnx-array-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsdpython%2Fonnx-array-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsdpython%2Fonnx-array-api/lists"}