Eager mode evaluation¶

An onnxscript function can be executed directly as a Python function (for example, with a Python debugger). This is useful for debugging an onnxscript function definition. This execution makes use of a backend implementation of the ONNX ops used in the function definition. Currently, the backend implementation uses onnxruntime to execute each op invocation. This mode of execution is referred to as eager mode evaluation.

The example below illustrates this. We first define an onnxscript function:

import numpy as np

from onnxscript import FLOAT, script
from onnxscript import opset15 as op


@script()
def linear(A: FLOAT["N", "K"], W: FLOAT["K", "M"], Bias: FLOAT["M"]) -> FLOAT["N", "M"]:  # noqa: F821
    T1 = op.MatMul(A, W)
    T2 = op.Add(T1, Bias)
    Y = op.Relu(T2)
    return Y

Create inputs for evaluating the function:

np.random.seed(0)
m = 4
k = 16
n = 4
a = np.random.rand(k, m).astype("float32").T
w = np.random.rand(n, k).astype("float32").T
b = np.random.rand(n).astype("float32").T

Evaluate the function:

print(linear(a, w, b))
[[3.7695956 3.8361263 5.116064  5.2047744]
 [4.5182567 4.2103305 4.54666   5.6752048]
 [4.0728097 3.1566992 4.821034  4.7809625]
 [4.925565  3.558134  4.7679787 5.3899584]]