Benchmark Random Forests, Tree Ensemble, Multi-Classification#

The script compares different implementations for the operator TreeEnsembleRegressor for a multi-regression. It replicates the benchmark Benchmark Random Forests, Tree Ensemble, (AoS and SoA) for multi-classification.

Import#

import warnings
from time import perf_counter as time
from multiprocessing import cpu_count
import numpy
from numpy.random import rand
from numpy.testing import assert_almost_equal
import pandas
import matplotlib.pyplot as plt
from sklearn import config_context
from sklearn.ensemble import RandomForestClassifier
from sklearn.utils._testing import ignore_warnings
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
from onnxruntime import InferenceSession
from mlprodict.onnxrt import OnnxInference

Available optimisation on this machine.

from mlprodict.testing.experimental_c_impl.experimental_c import code_optimisation
print(code_optimisation())
AVX-omp=8

Versions#

def version():
    from datetime import datetime
    import sklearn
    import numpy
    import onnx
    import onnxruntime
    import skl2onnx
    import mlprodict
    df = pandas.DataFrame([
        {"name": "date", "version": str(datetime.now())},
        {"name": "numpy", "version": numpy.__version__},
        {"name": "scikit-learn", "version": sklearn.__version__},
        {"name": "onnx", "version": onnx.__version__},
        {"name": "onnxruntime", "version": onnxruntime.__version__},
        {"name": "skl2onnx", "version": skl2onnx.__version__},
        {"name": "mlprodict", "version": mlprodict.__version__},
    ])
    return df


version()
name version
0 date 2023-01-26 10:19:24.370007
1 numpy 1.23.5
2 scikit-learn 1.2.1
3 onnx 1.13.0
4 onnxruntime 1.13.1
5 skl2onnx 1.13.1
6 mlprodict 0.9.1887


Implementations to benchmark#

def fcts_model(X, y, max_depth, n_estimators, n_jobs):
    "RandomForestClassifier."
    rf = RandomForestClassifier(max_depth=max_depth, n_estimators=n_estimators,
                                n_jobs=n_jobs)
    rf.fit(X, y)

    initial_types = [('X', FloatTensorType([None, X.shape[1]]))]
    onx = convert_sklearn(rf, initial_types=initial_types,
                          options={id(rf): {'zipmap': False}})
    sess = InferenceSession(onx.SerializeToString())
    outputs = [o.name for o in sess.get_outputs()]
    oinf = OnnxInference(onx, runtime="python")
    oinf.sequence_[0].ops_._init(numpy.float32, 1)
    name = outputs[1]
    oinf2 = OnnxInference(onx, runtime="python")
    oinf2.sequence_[0].ops_._init(numpy.float32, 2)
    oinf3 = OnnxInference(onx, runtime="python")
    oinf3.sequence_[0].ops_._init(numpy.float32, 3)

    def predict_skl_predict(X, model=rf):
        return rf.predict_proba(X)

    def predict_onnxrt_predict(X, sess=sess):
        return sess.run(outputs[:1], {'X': X})[0]

    def predict_onnx_inference(X, oinf=oinf):
        return oinf.run({'X': X})[name]

    def predict_onnx_inference2(X, oinf2=oinf2):
        return oinf2.run({'X': X})[name]

    def predict_onnx_inference3(X, oinf3=oinf3):
        return oinf3.run({'X': X})[name]

    return {'predict': (
        predict_skl_predict, predict_onnxrt_predict,
        predict_onnx_inference, predict_onnx_inference2,
        predict_onnx_inference3)}

Benchmarks#

def allow_configuration(**kwargs):
    return True


def bench(n_obs, n_features, max_depths, n_estimatorss, n_jobss,
          methods, repeat=10, verbose=False):
    res = []
    for nfeat in n_features:

        ntrain = 50000
        X_train = numpy.empty((ntrain, nfeat)).astype(numpy.float32)
        X_train[:, :] = rand(ntrain, nfeat)[:, :]
        eps = rand(ntrain) - 0.5
        y_train_f = X_train.sum(axis=1) + eps
        y_train = (y_train_f > 12).astype(numpy.int64)
        y_train[y_train_f > 15] = 2
        y_train[y_train_f < 10] = 3

        for n_jobs in n_jobss:
            for max_depth in max_depths:
                for n_estimators in n_estimatorss:
                    fcts = fcts_model(X_train, y_train,
                                      max_depth, n_estimators, n_jobs)

                    for n in n_obs:
                        for method in methods:

                            fct1, fct2, fct3, fct4, fct5 = fcts[method]

                            if not allow_configuration(
                                    n=n, nfeat=nfeat, max_depth=max_depth,
                                    n_estimator=n_estimators, n_jobs=n_jobs,
                                    method=method):
                                continue

                            obs = dict(n_obs=n, nfeat=nfeat,
                                       max_depth=max_depth,
                                       n_estimators=n_estimators,
                                       method=method,
                                       n_jobs=n_jobs)

                            # creates different inputs to avoid caching
                            Xs = []
                            for r in range(repeat):
                                x = numpy.empty((n, nfeat))
                                x[:, :] = rand(n, nfeat)[:, :]
                                Xs.append(x.astype(numpy.float32))

                            # measures the baseline
                            with config_context(assume_finite=True):
                                st = time()
                                repeated = 0
                                for X in Xs:
                                    p1 = fct1(X)
                                    repeated += 1
                                    if time() - st >= 1:
                                        break  # stops if longer than a second
                                end = time()
                                obs["time_skl"] = (end - st) / repeated

                            # measures the new implementation
                            st = time()
                            r2 = 0
                            for X in Xs:
                                p2 = fct2(X)
                                r2 += 1
                                if r2 >= repeated:
                                    break
                            end = time()
                            obs["time_ort"] = (end - st) / r2

                            # measures the other new implementation
                            st = time()
                            r2 = 0
                            for X in Xs:
                                p2 = fct3(X)
                                r2 += 1
                                if r2 >= repeated:
                                    break
                            end = time()
                            obs["time_mlprodict"] = (end - st) / r2

                            # measures the other new implementation 2
                            st = time()
                            r2 = 0
                            for X in Xs:
                                p2 = fct4(X)
                                r2 += 1
                                if r2 >= repeated:
                                    break
                            end = time()
                            obs["time_mlprodict2"] = (end - st) / r2

                            # measures the other new implementation 3
                            st = time()
                            r2 = 0
                            for X in Xs:
                                p2 = fct5(X)
                                r2 += 1
                                if r2 >= repeated:
                                    break
                            end = time()
                            obs["time_mlprodict3"] = (end - st) / r2

                            # final
                            res.append(obs)
                            if verbose and (len(res) % 1 == 0 or n >= 10000):
                                print("bench", len(res), ":", obs)

                            # checks that both produce the same outputs
                            if n <= 10000:
                                if len(p1.shape) == 1 and len(p2.shape) == 2:
                                    p2 = p2.ravel()
                                try:
                                    assert_almost_equal(
                                        p1.ravel(), p2.ravel(), decimal=5)
                                except AssertionError as e:
                                    warnings.warn(str(e))
    return res

Graphs#

def plot_rf_models(dfr):

    def autolabel(ax, rects):
        for rect in rects:
            height = rect.get_height()
            ax.annotate(f'{height:1.1f}x',
                        xy=(rect.get_x() + rect.get_width() / 2, height),
                        xytext=(0, 3),  # 3 points vertical offset
                        textcoords="offset points",
                        ha='center', va='bottom',
                        fontsize=8)

    engines = [_.split('_')[-1] for _ in dfr.columns if _.startswith("time_")]
    engines = [_ for _ in engines if _ != 'skl']
    for engine in engines:
        dfr[f"speedup_{engine}"] = dfr["time_skl"] / dfr[f"time_{engine}"]
    print(dfr.tail().T)

    ncols = 4
    fig, axs = plt.subplots(len(engines), ncols, figsize=(
        14, 4 * len(engines)), sharey=True)

    row = 0
    for row, engine in enumerate(engines):
        pos = 0
        name = f"RandomForestClassifier - {engine}"
        for max_depth in sorted(set(dfr.max_depth)):
            for nf in sorted(set(dfr.nfeat)):
                for est in sorted(set(dfr.n_estimators)):
                    for n_jobs in sorted(set(dfr.n_jobs)):
                        sub = dfr[(dfr.max_depth == max_depth) &
                                  (dfr.nfeat == nf) &
                                  (dfr.n_estimators == est) &
                                  (dfr.n_jobs == n_jobs)]
                        ax = axs[row, pos]
                        labels = sub.n_obs
                        means = sub[f"speedup_{engine}"]

                        x = numpy.arange(len(labels))
                        width = 0.90

                        rects1 = ax.bar(x, means, width, label='Speedup')
                        if pos == 0:
                            ax.set_yscale('log')
                            ax.set_ylim([0.1, max(dfr[f"speedup_{engine}"])])

                        if pos == 0:
                            ax.set_ylabel('Speedup')
                        ax.set_title(
                            '%s\ndepth %d - %d features\n %d estimators '
                            '%d jobs' % (name, max_depth, nf, est, n_jobs))
                        if row == len(engines) - 1:
                            ax.set_xlabel('batch size')
                        ax.set_xticks(x)
                        ax.set_xticklabels(labels)
                        autolabel(ax, rects1)
                        for tick in ax.xaxis.get_major_ticks():
                            tick.label.set_fontsize(8)
                        for tick in ax.yaxis.get_major_ticks():
                            tick.label.set_fontsize(8)
                        pos += 1

    fig.tight_layout()
    return fig, ax

Run benchs#

@ignore_warnings(category=FutureWarning)
def run_bench(repeat=100, verbose=False):
    n_obs = [1, 10, 100, 1000, 10000]
    methods = ['predict']
    n_features = [30]
    max_depths = [6, 8, 10, 12]
    n_estimatorss = [100]
    n_jobss = [cpu_count()]

    start = time()
    results = bench(n_obs, n_features, max_depths, n_estimatorss, n_jobss,
                    methods, repeat=repeat, verbose=verbose)
    end = time()

    results_df = pandas.DataFrame(results)
    print("Total time = %0.3f sec cpu=%d\n" % (end - start, cpu_count()))

    # plot the results
    return results_df


name = "plot_random_forest_cls_multi"
df = run_bench(verbose=True)
df.to_csv(f"{name}.csv", index=False)
df.to_excel(f"{name}.xlsx", index=False)
fig, ax = plot_rf_models(df)
fig.savefig(f"{name}.png")
plt.show()
RandomForestClassifier - ort depth 6 - 30 features  100 estimators 8 jobs, RandomForestClassifier - ort depth 8 - 30 features  100 estimators 8 jobs, RandomForestClassifier - ort depth 10 - 30 features  100 estimators 8 jobs, RandomForestClassifier - ort depth 12 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict depth 6 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict depth 8 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict depth 10 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict depth 12 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict2 depth 6 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict2 depth 8 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict2 depth 10 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict2 depth 12 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict3 depth 6 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict3 depth 8 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict3 depth 10 - 30 features  100 estimators 8 jobs, RandomForestClassifier - mlprodict3 depth 12 - 30 features  100 estimators 8 jobs
bench 1 : {'n_obs': 1, 'nfeat': 30, 'max_depth': 6, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.04765076647024779, 'time_ort': 7.470637293798583e-05, 'time_mlprodict': 0.00248402861567835, 'time_mlprodict2': 0.00010005799343898183, 'time_mlprodict3': 8.814433766972451e-05}
bench 2 : {'n_obs': 10, 'nfeat': 30, 'max_depth': 6, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.04812958252261437, 'time_ort': 0.000464777151743571, 'time_mlprodict': 0.0005142051903974442, 'time_mlprodict2': 0.0004033286656652178, 'time_mlprodict3': 0.00010933933247412954}
bench 3 : {'n_obs': 100, 'nfeat': 30, 'max_depth': 6, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.05467034694983771, 'time_ort': 0.0006469648919607463, 'time_mlprodict': 0.004160436997680287, 'time_mlprodict2': 0.0014160474835845985, 'time_mlprodict3': 0.0002504979431825249}
bench 4 : {'n_obs': 1000, 'nfeat': 30, 'max_depth': 6, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.09081739125152428, 'time_ort': 0.0023643614258617163, 'time_mlprodict': 0.012102204918240508, 'time_mlprodict2': 0.009387286244115481, 'time_mlprodict3': 0.0016603594995103776}
bench 5 : {'n_obs': 10000, 'nfeat': 30, 'max_depth': 6, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.12503014874528162, 'time_ort': 0.022492028743727133, 'time_mlprodict': 0.09427896048873663, 'time_mlprodict2': 0.08740275274612941, 'time_mlprodict3': 0.015035680873552337}
bench 6 : {'n_obs': 1, 'nfeat': 30, 'max_depth': 8, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.04778254699582855, 'time_ort': 0.000332275139433997, 'time_mlprodict': 0.0031919755773352726, 'time_mlprodict2': 0.00013669332977206934, 'time_mlprodict3': 8.068342382709186e-05}
bench 7 : {'n_obs': 10, 'nfeat': 30, 'max_depth': 8, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.048955388189781276, 'time_ort': 0.0007574107564453568, 'time_mlprodict': 0.0008130259035776058, 'time_mlprodict2': 0.000798524623470647, 'time_mlprodict3': 0.0006977413404023363}
bench 8 : {'n_obs': 100, 'nfeat': 30, 'max_depth': 8, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.05498143637209738, 'time_ort': 0.0009036658968972532, 'time_mlprodict': 0.004169576360206855, 'time_mlprodict2': 0.0015404967370590097, 'time_mlprodict3': 0.0005466243693310963}
bench 9 : {'n_obs': 1000, 'nfeat': 30, 'max_depth': 8, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.0923984640023925, 'time_ort': 0.003965903539210558, 'time_mlprodict': 0.016983262623067607, 'time_mlprodict2': 0.013369255466386676, 'time_mlprodict3': 0.0022972208151424475}
bench 10 : {'n_obs': 10000, 'nfeat': 30, 'max_depth': 8, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.12652424213592894, 'time_ort': 0.03095408625085838, 'time_mlprodict': 0.1365099489921704, 'time_mlprodict2': 0.12974844872951508, 'time_mlprodict3': 0.019627700879937038}
bench 11 : {'n_obs': 1, 'nfeat': 30, 'max_depth': 10, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.047797711566090584, 'time_ort': 0.00025866161810145493, 'time_mlprodict': 0.00301460976091524, 'time_mlprodict2': 0.00018026481293851422, 'time_mlprodict3': 9.063099111829485e-05}
bench 12 : {'n_obs': 10, 'nfeat': 30, 'max_depth': 10, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.05004141420358792, 'time_ort': 0.0009473536978475749, 'time_mlprodict': 0.0010013921419158578, 'time_mlprodict2': 0.0011273278505541384, 'time_mlprodict3': 0.0003045463585294783}
bench 13 : {'n_obs': 100, 'nfeat': 30, 'max_depth': 10, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.0641665641887812, 'time_ort': 0.0014383788220584393, 'time_mlprodict': 0.0055228866840479895, 'time_mlprodict2': 0.002110758810886182, 'time_mlprodict3': 0.0008604667527833953}
bench 14 : {'n_obs': 1000, 'nfeat': 30, 'max_depth': 10, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.09443969372659922, 'time_ort': 0.005848687734793533, 'time_mlprodict': 0.019459723367948423, 'time_mlprodict2': 0.018619762098586016, 'time_mlprodict3': 0.0035390679047188974}
bench 15 : {'n_obs': 10000, 'nfeat': 30, 'max_depth': 10, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.13157362985657528, 'time_ort': 0.043568309629336, 'time_mlprodict': 0.15533498051809147, 'time_mlprodict2': 0.17656907974742353, 'time_mlprodict3': 0.026038672134745866}
bench 16 : {'n_obs': 1, 'nfeat': 30, 'max_depth': 12, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.04808341723955458, 'time_ort': 9.259476237708615e-05, 'time_mlprodict': 0.0026264214289507697, 'time_mlprodict2': 0.00023082571680701914, 'time_mlprodict3': 9.850805093135153e-05}
bench 17 : {'n_obs': 10, 'nfeat': 30, 'max_depth': 12, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.051831495692022146, 'time_ort': 0.0011683639022521675, 'time_mlprodict': 0.001267771900165826, 'time_mlprodict2': 0.0015561394044198095, 'time_mlprodict3': 0.001023954397533089}
bench 18 : {'n_obs': 100, 'nfeat': 30, 'max_depth': 12, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.06584856180415954, 'time_ort': 0.0021204155636951327, 'time_mlprodict': 0.00532802494126372, 'time_mlprodict2': 0.002713993191719055, 'time_mlprodict3': 0.001159551742603071}
bench 19 : {'n_obs': 1000, 'nfeat': 30, 'max_depth': 12, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.09783531227995726, 'time_ort': 0.009001969634978608, 'time_mlprodict': 0.022340936374596575, 'time_mlprodict2': 0.024623551829294724, 'time_mlprodict3': 0.005195507187057625}
bench 20 : {'n_obs': 10000, 'nfeat': 30, 'max_depth': 12, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.1335054574883543, 'time_ort': 0.07088223350001499, 'time_mlprodict': 0.1967973895079922, 'time_mlprodict2': 0.24313312163576484, 'time_mlprodict3': 0.040155497379601}
Total time = 213.657 sec cpu=8

                            15         16         17         18        19
n_obs                        1         10        100       1000     10000
nfeat                       30         30         30         30        30
max_depth                   12         12         12         12        12
n_estimators               100        100        100        100       100
method                 predict    predict    predict    predict   predict
n_jobs                       8          8          8          8         8
time_skl              0.048083   0.051831   0.065849   0.097835  0.133505
time_ort              0.000093   0.001168    0.00212   0.009002  0.070882
time_mlprodict        0.002626   0.001268   0.005328   0.022341  0.196797
time_mlprodict2       0.000231   0.001556   0.002714   0.024624  0.243133
time_mlprodict3       0.000099   0.001024    0.00116   0.005196  0.040155
speedup_ort         519.288737  44.362459  31.054555  10.868212  1.883483
speedup_mlprodict    18.307579  40.883928  12.358906   4.379195   0.67839
speedup_mlprodict2  208.310486  33.307746  24.262611   3.973241  0.549104
speedup_mlprodict3  488.116624  50.618949  56.787946  18.830753  3.324712
somewhere/workspace/mlprodict/mlprodict_UT_39_std/_doc/examples/plot_opml_random_forest_cls_multi.py:299: MatplotlibDeprecationWarning: The label function was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use Tick.label1 instead.
  tick.label.set_fontsize(8)
somewhere/workspace/mlprodict/mlprodict_UT_39_std/_doc/examples/plot_opml_random_forest_cls_multi.py:301: MatplotlibDeprecationWarning: The label function was deprecated in Matplotlib 3.1 and will be removed in 3.8. Use Tick.label1 instead.
  tick.label.set_fontsize(8)

Total running time of the script: ( 3 minutes 47.792 seconds)

Gallery generated by Sphinx-Gallery