NumPy function#

This page answers common “how do I…” questions for exporting a Python function that processes numpy arrays into ONNX.


How to export a function processing numpy arrays#

Use yobx.sql.trace_numpy_to_onnx() with a representative sample input. Only the input dtype and shape are used during export.

<<<

import numpy as np
from yobx.helpers.onnx_helper import pretty_onnx
from yobx.sql import trace_numpy_to_onnx


def normalize_rows(X):
    norms = np.sqrt(np.sum(X ** np.float32(2), axis=1, keepdims=True))
    safe_norms = np.where(norms < np.float32(1e-8), np.float32(1), norms)
    return X / safe_norms


X_sample = np.zeros((3, 4), dtype=np.float32)
onx = trace_numpy_to_onnx(normalize_rows, X_sample)
print(pretty_onnx(onx))

>>>

    opset: domain='' version=29
    opset: domain='ai.onnx.ml' version=1
    input: name='X' type=dtype('float32') shape=['batch', 4]
    init: name='init' type=float32 shape=() -- array([2.], dtype=float32)
    init: name='init_2' type=int64 shape=(1,) -- array([1])
    init: name='init_3' type=float32 shape=() -- array([1.e-08], dtype=float32)
    init: name='init_4' type=float32 shape=() -- array([1.], dtype=float32)
    Pow(X, init) -> pow_2
      ReduceSum(pow_2, init_2, keepdims=1) -> reducesum_2
        Sqrt(reducesum_2) -> sqrt_2
          Less(sqrt_2, init_3) -> less_2
          Where(less_2, init_4, sqrt_2) -> where_2
            Div(X, where_2) -> div_2
    output: name='div_2' type=dtype('float32') shape=['batch', 4]

How to export a function with multiple inputs and outputs#

Use input_names and output_names when your function takes more than one input array and returns multiple arrays.

<<<

import numpy as np
from yobx.helpers.onnx_helper import pretty_onnx
from yobx.sql import trace_numpy_to_onnx


def combine(A, B):
    return A + B, A - B


A = np.random.randn(2, 3).astype(np.float32)
B = np.random.randn(2, 3).astype(np.float32)
onx = trace_numpy_to_onnx(
    combine,
    A,
    B,
    input_names=["A", "B"],
    output_names=["sum", "diff"],
)
print(pretty_onnx(onx))

>>>

    opset: domain='' version=29
    opset: domain='ai.onnx.ml' version=1
    input: name='A' type=dtype('float32') shape=['batch', 3]
    input: name='B' type=dtype('float32') shape=['batch', 3]
    Add(A, B) -> sum
    Sub(A, B) -> diff
    output: name='sum' type=dtype('float32') shape=['batch', 3]
    output: name='diff' type=dtype('float32') shape=['batch', 3]

How to validate ONNX outputs against numpy#

Run the exported graph with onnxruntime and compare with the original numpy implementation.

<<<

import numpy as np
import onnxruntime
from yobx.sql import trace_numpy_to_onnx


def normalize_rows(X):
    norms = np.sqrt(np.sum(X ** np.float32(2), axis=1, keepdims=True))
    safe_norms = np.where(norms < np.float32(1e-8), np.float32(1), norms)
    return X / safe_norms


rng = np.random.default_rng(0)
X_sample = rng.standard_normal((8, 4)).astype(np.float32)
X_test = rng.standard_normal((5, 4)).astype(np.float32)

onx = trace_numpy_to_onnx(normalize_rows, X_sample, target_opset=21)
sess = onnxruntime.InferenceSession(
    onx.SerializeToString(), providers=["CPUExecutionProvider"]
)

(got,) = sess.run(None, {"X": X_test})
expected = normalize_rows(X_test)
print(f"max|diff|={np.abs(got - expected).max():.2e}")
assert np.allclose(got, expected, atol=1e-5)
print("Outputs match ✓")

>>>

    max|diff|=0.00e+00
    Outputs match ✓

See also

Numpy-Tracing and FunctionTransformer — implementation details on numpy tracing and supported operators.