onnx-extended


Nameonnx-extended JSON
Version 0.2.4 PyPI version JSON
download
home_pagehttps://github.com/sdpython/onnx-extended
SummaryExtends the list of supported operators in onnx reference implementation and onnxruntime, or implements faster versions in C++.
upload_time2024-01-03 18:34:43
maintainer
docs_urlNone
authorXavier Dupré
requires_python>=3.9
licenseCopyright (c) 2023, Xavier Dupré Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords onnx onnxruntime cuda openmp cmake cython pybind11
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
.. image:: https://github.com/sdpython/onnx-extended/raw/main/_doc/_static/logo.png
    :width: 120

onnx-extended: extensions for onnx and onnxruntime
==================================================

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

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

.. image:: http://img.shields.io/github/issues/sdpython/onnx-extended.png
    :alt: GitHub Issues
    :target: https://github.com/sdpython/onnx-extended/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-extended
    :target: https://github.com/sdpython/onnx-extended/
    :alt: size

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

**onnx-extended** extends the list of supported operators in onnx
reference implementation and `onnxruntime
<https://github.com/microsoft/onnxruntime>`_,
or implements faster versions in C++.
Documentation `onnx-extended
<https://sdpython.github.io/doc/onnx-extended/dev/>`_.
Source are available on `github/onnx-extended
<https://github.com/sdpython/onnx-extended/>`_.

Use a C++ implementation of existing operators
++++++++++++++++++++++++++++++++++++++++++++++

.. code-block:: python

    import timeit
    import numpy as np
    from onnx import TensorProto
    from onnx.helper import (
        make_graph,
        make_model,
        make_node,
        make_opsetid,
        make_tensor_value_info,
    )
    from onnx.reference import ReferenceEvaluator
    from onnxruntime import InferenceSession
    from onnx_extended.ext_test_case import measure_time
    from onnx_extended.reference import CReferenceEvaluator


    X = make_tensor_value_info("X", TensorProto.FLOAT, [None, None, None, None])
    Y = make_tensor_value_info("Y", TensorProto.FLOAT, [None, None, None, None])
    B = make_tensor_value_info("B", TensorProto.FLOAT, [None, None, None, None])
    W = make_tensor_value_info("W", TensorProto.FLOAT, [None, None, None, None])
    node = make_node(
        "Conv",
        ["X", "W", "B"],
        ["Y"],
        pads=[1, 1, 1, 1],
        dilations=[1, 1],
        strides=[2, 2],
    )
    graph = make_graph([node], "g", [X, W, B], [Y])
    onnx_model = make_model(graph, opset_imports=[make_opsetid("", 16)])

    sH, sW = 64, 64
    X = np.arange(sW * sH).reshape((1, 1, sH, sW)).astype(np.float32)
    W = np.ones((1, 1, 3, 3), dtype=np.float32)
    B = np.array([[[[0]]]], dtype=np.float32)

    sess1 = ReferenceEvaluator(onnx_model)
    sess2 = CReferenceEvaluator(onnx_model)  # 100 times faster

    expected = sess1.run(None, {"X": X, "W": W, "B": B})[0]
    got = sess2.run(None, {"X": X, "W": W, "B": B})[0]
    diff = np.abs(expected - got).max()
    print(f"difference: {diff}")

    f1 = lambda: sess1.run(None, {"X": X, "W": W, "B": B})[0]
    f2 = lambda: sess2.run(None, {"X": X, "W": W, "B": B})[0]
    print("onnx:", timeit.timeit(f1, globals=globals(), number=5))
    print("onnx-extended:", timeit.timeit(f2, globals=globals(), number=5))

::

    difference: 0.0
    onnx: 0.024006774998269975
    onnx-extended: 0.0002316169993719086

Build with CUDA, openmp, eigen, onnxruntime
+++++++++++++++++++++++++++++++++++++++++++

The package also contains some dummy examples on how to
build with C++ functions (`pybind11 <https://github.com/pybind/pybind11>`_,
`cython <https://cython.org/>`_),
with `openmp <https://www.openmp.org/>`_,
`eigen <https://eigen.tuxfamily.org/index.php>`_
with or without CUDA. It also shows how to create a custom operator
for *onnxruntime* in C++.

The version released on `pypi/onnx-extended <https://pypi.org/project/onnx-extended/>`_
only works on CPU. It needs to be manually built to enable
the code using CUDA. The build will automatically link with CUDA if it is found.
If not, some extensions might not be available.

::

    python setup.py build_ext --inplace
    # pip install -e .

It is possible to use a specific version of CUDA:

::

    python setup.py build_ext --inplace --cuda-version=11.8
    # or (not working yet)
    # pip install -e . --config-settings="--cuda-version=11.8"
    # pip install -e . --global-option="--cuda-version=11.8"
    export USE_CUDA=11.8
    pip install -e .

`NVTX <https://github.com/NVIDIA/NVTX>`_
can be enabled with the following command:

::

    python setup.py build_ext --inplace --use_nvtx 1
    # or (not working yet)
    # pip install -e . --config-settings="--use_nvtx=1"
    pip install -e . --global-option "--use_nvtx=1"

Experimental cython binding for onnxruntime
+++++++++++++++++++++++++++++++++++++++++++

The python onnxruntime package relies on pybind11 to expose
its functionalities. *onnx-extended* tries to build a cython wrapper
around the C/C++ API of onnxruntime. cython relies on python C API
and is faster than pybind11. This different may be significant when
onnxruntime is used on small graphs and tensors.

Custom kernels for onnxruntime
++++++++++++++++++++++++++++++

onnxruntime provides an API to add custom implementation
for existing or new onnx operators. An example for CPU.

::

    from onnxruntime import InferenceSession, SessionOptions
    from onnx_extended.ortops.optim.cpu import get_ort_ext_libs

    r = get_ort_ext_libs()
    opts = SessionOptions()
    if r is not None:
        opts.register_custom_ops_library(r[0])

    sess_cus = InferenceSession(
        onx_modified.SerializeToString(), opts, providers=["CPUExecutionProvider"]
    )

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/sdpython/onnx-extended",
    "name": "onnx-extended",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "onnx,onnxruntime,CUDA,openmp,cmake,cython,pybind11",
    "author": "Xavier Dupr\u00e9",
    "author_email": "Xavier Dupr\u00e9 <xavier.dupre@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/3a/d5/fa89d405879655c563627812ac09cc74721535fafaca4c998ca5e6507b76/onnx-extended-0.2.4.tar.gz",
    "platform": null,
    "description": "\n.. image:: https://github.com/sdpython/onnx-extended/raw/main/_doc/_static/logo.png\n    :width: 120\n\nonnx-extended: extensions for onnx and onnxruntime\n==================================================\n\n.. image:: https://dev.azure.com/xavierdupre3/onnx-extended/_apis/build/status/sdpython.onnx-extended\n    :target: https://dev.azure.com/xavierdupre3/onnx-extended/\n\n.. image:: https://badge.fury.io/py/onnx-extended.svg\n    :target: http://badge.fury.io/py/onnx-extended\n\n.. image:: http://img.shields.io/github/issues/sdpython/onnx-extended.png\n    :alt: GitHub Issues\n    :target: https://github.com/sdpython/onnx-extended/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-extended\n    :target: https://github.com/sdpython/onnx-extended/\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**onnx-extended** extends the list of supported operators in onnx\nreference implementation and `onnxruntime\n<https://github.com/microsoft/onnxruntime>`_,\nor implements faster versions in C++.\nDocumentation `onnx-extended\n<https://sdpython.github.io/doc/onnx-extended/dev/>`_.\nSource are available on `github/onnx-extended\n<https://github.com/sdpython/onnx-extended/>`_.\n\nUse a C++ implementation of existing operators\n++++++++++++++++++++++++++++++++++++++++++++++\n\n.. code-block:: python\n\n    import timeit\n    import numpy as np\n    from onnx import TensorProto\n    from onnx.helper import (\n        make_graph,\n        make_model,\n        make_node,\n        make_opsetid,\n        make_tensor_value_info,\n    )\n    from onnx.reference import ReferenceEvaluator\n    from onnxruntime import InferenceSession\n    from onnx_extended.ext_test_case import measure_time\n    from onnx_extended.reference import CReferenceEvaluator\n\n\n    X = make_tensor_value_info(\"X\", TensorProto.FLOAT, [None, None, None, None])\n    Y = make_tensor_value_info(\"Y\", TensorProto.FLOAT, [None, None, None, None])\n    B = make_tensor_value_info(\"B\", TensorProto.FLOAT, [None, None, None, None])\n    W = make_tensor_value_info(\"W\", TensorProto.FLOAT, [None, None, None, None])\n    node = make_node(\n        \"Conv\",\n        [\"X\", \"W\", \"B\"],\n        [\"Y\"],\n        pads=[1, 1, 1, 1],\n        dilations=[1, 1],\n        strides=[2, 2],\n    )\n    graph = make_graph([node], \"g\", [X, W, B], [Y])\n    onnx_model = make_model(graph, opset_imports=[make_opsetid(\"\", 16)])\n\n    sH, sW = 64, 64\n    X = np.arange(sW * sH).reshape((1, 1, sH, sW)).astype(np.float32)\n    W = np.ones((1, 1, 3, 3), dtype=np.float32)\n    B = np.array([[[[0]]]], dtype=np.float32)\n\n    sess1 = ReferenceEvaluator(onnx_model)\n    sess2 = CReferenceEvaluator(onnx_model)  # 100 times faster\n\n    expected = sess1.run(None, {\"X\": X, \"W\": W, \"B\": B})[0]\n    got = sess2.run(None, {\"X\": X, \"W\": W, \"B\": B})[0]\n    diff = np.abs(expected - got).max()\n    print(f\"difference: {diff}\")\n\n    f1 = lambda: sess1.run(None, {\"X\": X, \"W\": W, \"B\": B})[0]\n    f2 = lambda: sess2.run(None, {\"X\": X, \"W\": W, \"B\": B})[0]\n    print(\"onnx:\", timeit.timeit(f1, globals=globals(), number=5))\n    print(\"onnx-extended:\", timeit.timeit(f2, globals=globals(), number=5))\n\n::\n\n    difference: 0.0\n    onnx: 0.024006774998269975\n    onnx-extended: 0.0002316169993719086\n\nBuild with CUDA, openmp, eigen, onnxruntime\n+++++++++++++++++++++++++++++++++++++++++++\n\nThe package also contains some dummy examples on how to\nbuild with C++ functions (`pybind11 <https://github.com/pybind/pybind11>`_,\n`cython <https://cython.org/>`_),\nwith `openmp <https://www.openmp.org/>`_,\n`eigen <https://eigen.tuxfamily.org/index.php>`_\nwith or without CUDA. It also shows how to create a custom operator\nfor *onnxruntime* in C++.\n\nThe version released on `pypi/onnx-extended <https://pypi.org/project/onnx-extended/>`_\nonly works on CPU. It needs to be manually built to enable\nthe code using CUDA. The build will automatically link with CUDA if it is found.\nIf not, some extensions might not be available.\n\n::\n\n    python setup.py build_ext --inplace\n    # pip install -e .\n\nIt is possible to use a specific version of CUDA:\n\n::\n\n    python setup.py build_ext --inplace --cuda-version=11.8\n    # or (not working yet)\n    # pip install -e . --config-settings=\"--cuda-version=11.8\"\n    # pip install -e . --global-option=\"--cuda-version=11.8\"\n    export USE_CUDA=11.8\n    pip install -e .\n\n`NVTX <https://github.com/NVIDIA/NVTX>`_\ncan be enabled with the following command:\n\n::\n\n    python setup.py build_ext --inplace --use_nvtx 1\n    # or (not working yet)\n    # pip install -e . --config-settings=\"--use_nvtx=1\"\n    pip install -e . --global-option \"--use_nvtx=1\"\n\nExperimental cython binding for onnxruntime\n+++++++++++++++++++++++++++++++++++++++++++\n\nThe python onnxruntime package relies on pybind11 to expose\nits functionalities. *onnx-extended* tries to build a cython wrapper\naround the C/C++ API of onnxruntime. cython relies on python C API\nand is faster than pybind11. This different may be significant when\nonnxruntime is used on small graphs and tensors.\n\nCustom kernels for onnxruntime\n++++++++++++++++++++++++++++++\n\nonnxruntime provides an API to add custom implementation\nfor existing or new onnx operators. An example for CPU.\n\n::\n\n    from onnxruntime import InferenceSession, SessionOptions\n    from onnx_extended.ortops.optim.cpu import get_ort_ext_libs\n\n    r = get_ort_ext_libs()\n    opts = SessionOptions()\n    if r is not None:\n        opts.register_custom_ops_library(r[0])\n\n    sess_cus = InferenceSession(\n        onx_modified.SerializeToString(), opts, providers=[\"CPUExecutionProvider\"]\n    )\n",
    "bugtrack_url": null,
    "license": "\ufeffCopyright (c) 2023, Xavier Dupr\u00e9  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Extends the list of supported operators in onnx reference implementation and onnxruntime, or implements faster versions in C++.",
    "version": "0.2.4",
    "project_urls": {
        "Changelog": "https://github.com/sdpython/onnx-extended/blob/main/CHANGELOGS.rst",
        "Documentation": "https://sdpython.github.io/doc/onnx-extended/dev/",
        "Homepage": "https://sdpython.github.io/doc/onnx-extended/dev/",
        "Issues": "https://github.com/sdpython/onnx-extended/issues",
        "Repository": "https://github.com/sdpython/onnx-extended/"
    },
    "split_keywords": [
        "onnx",
        "onnxruntime",
        "cuda",
        "openmp",
        "cmake",
        "cython",
        "pybind11"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e7b9a6c01e1b024e79121221f70eea30fd171a960c2d718161cb3598d33ec68a",
                "md5": "38723d4dfe983f524669987f7cc9ec1c",
                "sha256": "b95f6676f424087daf858ea0e6c58a1a3f42e38b8add80934030fd2c9c83c067"
            },
            "downloads": -1,
            "filename": "onnx_extended-0.2.4-cp310-cp310-macosx_10_15_x86_64.whl",
            "has_sig": false,
            "md5_digest": "38723d4dfe983f524669987f7cc9ec1c",
            "packagetype": "bdist_wheel",
            "python_version": "cp310",
            "requires_python": ">=3.9",
            "size": 39043054,
            "upload_time": "2024-01-03T18:34:17",
            "upload_time_iso_8601": "2024-01-03T18:34:17.736290Z",
            "url": "https://files.pythonhosted.org/packages/e7/b9/a6c01e1b024e79121221f70eea30fd171a960c2d718161cb3598d33ec68a/onnx_extended-0.2.4-cp310-cp310-macosx_10_15_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fd8464e89a4b16a96766cdb3635b40c25e353219397899083072bab2378fc0e5",
                "md5": "8bed6f752736d9cb75a01f037a7ef9e5",
                "sha256": "fe98eaa2ff63c5eb18ae6c3a0a9cb560a2069da913fecd245618c29b7064dbc8"
            },
            "downloads": -1,
            "filename": "onnx_extended-0.2.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "8bed6f752736d9cb75a01f037a7ef9e5",
            "packagetype": "bdist_wheel",
            "python_version": "cp310",
            "requires_python": ">=3.9",
            "size": 21053594,
            "upload_time": "2024-01-03T18:34:25",
            "upload_time_iso_8601": "2024-01-03T18:34:25.451516Z",
            "url": "https://files.pythonhosted.org/packages/fd/84/64e89a4b16a96766cdb3635b40c25e353219397899083072bab2378fc0e5/onnx_extended-0.2.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c234fc0ae8dc2faf76406be277c1c6ce9a3c9947958be46a34e7a461369b5378",
                "md5": "9e9872d6bb8f3e80567de6570c7da11c",
                "sha256": "b6643f8e6714874db44c5b8975d7b7a5f34bba20bc774639eb9d86193549afc0"
            },
            "downloads": -1,
            "filename": "onnx_extended-0.2.4-cp310-cp310-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "9e9872d6bb8f3e80567de6570c7da11c",
            "packagetype": "bdist_wheel",
            "python_version": "cp310",
            "requires_python": ">=3.9",
            "size": 4925054,
            "upload_time": "2024-01-03T18:34:28",
            "upload_time_iso_8601": "2024-01-03T18:34:28.791144Z",
            "url": "https://files.pythonhosted.org/packages/c2/34/fc0ae8dc2faf76406be277c1c6ce9a3c9947958be46a34e7a461369b5378/onnx_extended-0.2.4-cp310-cp310-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2144badf45b5853474e7e5f55c1091072eb19ab6c01c279f594f9e85d9789532",
                "md5": "7cd869dc341e93ac1b3e3682d6ad6a78",
                "sha256": "477beefd27ac5b0c97038cb11f53c6b4f0501c1ae324fdbc4f718783f06b50a6"
            },
            "downloads": -1,
            "filename": "onnx_extended-0.2.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "7cd869dc341e93ac1b3e3682d6ad6a78",
            "packagetype": "bdist_wheel",
            "python_version": "cp311",
            "requires_python": ">=3.9",
            "size": 21509789,
            "upload_time": "2024-01-03T18:34:35",
            "upload_time_iso_8601": "2024-01-03T18:34:35.786325Z",
            "url": "https://files.pythonhosted.org/packages/21/44/badf45b5853474e7e5f55c1091072eb19ab6c01c279f594f9e85d9789532/onnx_extended-0.2.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5bbec74a69639ce452aded5a5ec5a05ba265c49da940a69981d20eb68a30a5b4",
                "md5": "d401578aeb4771d590e7170a9b3ecb8f",
                "sha256": "e1027ad886eb0dffca66a893c2817b7d8b2e56460bec966adc227d4b083903b0"
            },
            "downloads": -1,
            "filename": "onnx_extended-0.2.4-cp311-cp311-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "d401578aeb4771d590e7170a9b3ecb8f",
            "packagetype": "bdist_wheel",
            "python_version": "cp311",
            "requires_python": ">=3.9",
            "size": 5383112,
            "upload_time": "2024-01-03T18:34:40",
            "upload_time_iso_8601": "2024-01-03T18:34:40.014038Z",
            "url": "https://files.pythonhosted.org/packages/5b/be/c74a69639ce452aded5a5ec5a05ba265c49da940a69981d20eb68a30a5b4/onnx_extended-0.2.4-cp311-cp311-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3ad5fa89d405879655c563627812ac09cc74721535fafaca4c998ca5e6507b76",
                "md5": "9548592b4a50ca515a1e8172d6915d50",
                "sha256": "12dbc90b2c3ea1059405a98f2d2240f693aa16377553426691bff8f0c446d082"
            },
            "downloads": -1,
            "filename": "onnx-extended-0.2.4.tar.gz",
            "has_sig": false,
            "md5_digest": "9548592b4a50ca515a1e8172d6915d50",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 5383142,
            "upload_time": "2024-01-03T18:34:43",
            "upload_time_iso_8601": "2024-01-03T18:34:43.027254Z",
            "url": "https://files.pythonhosted.org/packages/3a/d5/fa89d405879655c563627812ac09cc74721535fafaca4c998ca5e6507b76/onnx-extended-0.2.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-03 18:34:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sdpython",
    "github_project": "onnx-extended",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "onnx-extended"
}
        
Elapsed time: 0.15701s