{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# ONNX Runtime Backend for ONNX\n\n*ONNX Runtime* extends the\n[onnx backend API](https://github.com/onnx/onnx/blob/main/docs/ImplementingAnOnnxBackend.md)\nto run predictions using this runtime.\nLet's use the API to compute the prediction\nof a simple logistic regression model.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom onnx import load\n\nimport onnxruntime.backend as backend"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The device depends on how the package was compiled,\nGPU or CPU.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from onnxruntime import datasets, get_device\nfrom onnxruntime.capi.onnxruntime_pybind11_state import InvalidArgument\n\ndevice = get_device()\n\nname = datasets.get_example(\"logreg_iris.onnx\")\nmodel = load(name)\n\nrep = backend.prepare(model, device)\nx = np.array([[-1.0, -2.0]], dtype=np.float32)\ntry:\n    label, proba = rep.run(x)\n    print(\"label={}\".format(label))\n    print(\"probabilities={}\".format(proba))\nexcept (RuntimeError, InvalidArgument) as e:\n    print(e)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The backend can also directly load the model\nwithout using *onnx*.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "rep = backend.prepare(name, device)\nx = np.array([[-1.0, -2.0]], dtype=np.float32)\ntry:\n    label, proba = rep.run(x)\n    print(\"label={}\".format(label))\n    print(\"probabilities={}\".format(proba))\nexcept (RuntimeError, InvalidArgument) as e:\n    print(e)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The backend API is implemented by other frameworks\nand makes it easier to switch between multiple runtimes\nwith the same API.\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}