ONNX 转换基准测试

示例 训练和部署 scikit-learn 管道 转换了一个简单的模型。此示例采用类似的示例,但使用随机数据,并比较每个选项计算预测所需的时间。

训练管道

import numpy
from pandas import DataFrame
from tqdm import tqdm
from onnx.reference import ReferenceEvaluator
from sklearn import config_context
from sklearn.datasets import make_regression
from sklearn.ensemble import (
    GradientBoostingRegressor,
    RandomForestRegressor,
    VotingRegressor,
)
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from onnxruntime import InferenceSession
from skl2onnx import to_onnx
from skl2onnx.tutorial import measure_time


N = 11000
X, y = make_regression(N, n_features=10)
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.01)
print("Train shape", X_train.shape)
print("Test shape", X_test.shape)

reg1 = GradientBoostingRegressor(random_state=1)
reg2 = RandomForestRegressor(random_state=1)
reg3 = LinearRegression()
ereg = VotingRegressor([("gb", reg1), ("rf", reg2), ("lr", reg3)])
ereg.fit(X_train, y_train)
Train shape (110, 10)
Test shape (10890, 10)
VotingRegressor(estimators=[('gb', GradientBoostingRegressor(random_state=1)),
                            ('rf', RandomForestRegressor(random_state=1)),
                            ('lr', LinearRegression())])
在 Jupyter 环境中,请重新运行此单元格以显示 HTML 表示形式或信任笔记本。
在 GitHub 上,HTML 表示形式无法渲染,请尝试使用 nbviewer.org 加载此页面。


测量处理时间

我们使用函数 skl2onnx.tutorial.measure_time()。关于 assume_finite 的页面可能对您很有用,如果您需要优化预测。我们测量每个观测值的处理时间,无论该观测值是否属于批次或单个观测值。

sizes = [(1, 50), (10, 50), (100, 10)]

with config_context(assume_finite=True):
    obs = []
    for batch_size, repeat in tqdm(sizes):
        context = {"ereg": ereg, "X": X_test[:batch_size]}
        mt = measure_time(
            "ereg.predict(X)", context, div_by_number=True, number=10, repeat=repeat
        )
        mt["size"] = context["X"].shape[0]
        mt["mean_obs"] = mt["average"] / mt["size"]
        obs.append(mt)

df_skl = DataFrame(obs)
df_skl
  0%|          | 0/3 [00:00<?, ?it/s]
 33%|███▎      | 1/3 [00:07<00:14,  7.06s/it]
 67%|██████▋   | 2/3 [00:12<00:06,  6.25s/it]
100%|██████████| 3/3 [00:14<00:00,  4.01s/it]
100%|██████████| 3/3 [00:14<00:00,  4.70s/it]
平均值 偏差 最小执行时间 最大执行时间 重复次数 数量 大小 平均观测值
0 0.014108 0.004352 0.008928 0.029686 50 10 1 0.014108
1 0.011358 0.003103 0.008228 0.020336 50 10 10 0.001136
2 0.013486 0.002790 0.009885 0.018525 10 10 100 0.000135


图形。

df_skl.set_index("size")[["mean_obs"]].plot(title="scikit-learn", logx=True, logy=True)
scikit-learn

ONNX 运行时

对两个可用的 ONNX 运行时执行相同的操作。

onx = to_onnx(ereg, X_train[:1].astype(numpy.float32), target_opset=14)
sess = InferenceSession(onx.SerializeToString(), providers=["CPUExecutionProvider"])
oinf = ReferenceEvaluator(onx)

obs = []
for batch_size, repeat in tqdm(sizes):
    # scikit-learn
    context = {"ereg": ereg, "X": X_test[:batch_size].astype(numpy.float32)}
    mt = measure_time(
        "ereg.predict(X)", context, div_by_number=True, number=10, repeat=repeat
    )
    mt["size"] = context["X"].shape[0]
    mt["skl"] = mt["average"] / mt["size"]

    # onnxruntime
    context = {"sess": sess, "X": X_test[:batch_size].astype(numpy.float32)}
    mt2 = measure_time(
        "sess.run(None, {'X': X})[0]",
        context,
        div_by_number=True,
        number=10,
        repeat=repeat,
    )
    mt["ort"] = mt2["average"] / mt["size"]

    # ReferenceEvaluator
    context = {"oinf": oinf, "X": X_test[:batch_size].astype(numpy.float32)}
    mt2 = measure_time(
        "oinf.run(None, {'X': X})[0]",
        context,
        div_by_number=True,
        number=10,
        repeat=repeat,
    )
    mt["pyrt"] = mt2["average"] / mt["size"]

    # end
    obs.append(mt)


df = DataFrame(obs)
df
  0%|          | 0/3 [00:00<?, ?it/s]
 33%|███▎      | 1/3 [00:15<00:31, 15.60s/it]
 67%|██████▋   | 2/3 [00:40<00:21, 21.10s/it]
100%|██████████| 3/3 [01:03<00:00, 21.84s/it]
100%|██████████| 3/3 [01:03<00:00, 21.09s/it]
平均值 偏差 最小执行时间 最大执行时间 重复次数 数量 大小 skl ort pyrt
0 0.012201 0.003428 0.008608 0.021660 50 10 1 0.012201 0.000030 0.018960
1 0.011550 0.003386 0.008357 0.023143 50 10 10 0.001155 0.000021 0.003811
2 0.013936 0.004899 0.009295 0.023858 10 10 100 0.000139 0.000004 0.002129


图形。

df.set_index("size")[["skl", "ort", "pyrt"]].plot(
    title="Average prediction time per runtime", logx=True, logy=True
)
Average prediction time per runtime

ONNX 运行时比 scikit-learn 预测单个观测值快得多。 scikit-learn 针对训练和批量预测进行了优化。这解释了为什么 scikit-learn 和 ONNX 运行时对于大型批次似乎会收敛。它们使用类似的实现、并行化和语言(C++openmp)。

脚本的总运行时间:(1 分 19.181 秒)

由 Sphinx-Gallery 生成的图库