{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Eager mode evaluation\n\nAn *onnxscript* function can be executed directly as a Python function (for example,\nwith a Python debugger). This is useful for debugging an *onnxscript* function definition.\nThis execution makes use of a backend implementation of the ONNX ops used in the function\ndefinition. Currently, the backend implementation uses onnxruntime to execute each op\ninvocation. This mode of execution is referred to as *eager mode evaluation*.\n\nThe example below illustrates this. We first define an *onnxscript* function:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nfrom onnxscript import FLOAT, script\nfrom onnxscript import opset15 as op\n\n\n@script()\ndef linear(A: FLOAT[\"N\", \"K\"], W: FLOAT[\"K\", \"M\"], Bias: FLOAT[\"M\"]) -> FLOAT[\"N\", \"M\"]:  # noqa: F821\n    T1 = op.MatMul(A, W)\n    T2 = op.Add(T1, Bias)\n    Y = op.Relu(T2)\n    return Y"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create inputs for evaluating the function:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "np.random.seed(0)\nm = 4\nk = 16\nn = 4\na = np.random.rand(k, m).astype(\"float32\").T\nw = np.random.rand(n, k).astype(\"float32\").T\nb = np.random.rand(n).astype(\"float32\").T"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Evaluate the function:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(linear(a, w, b))"
      ]
    }
  ],
  "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.17"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}