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-02-04 05:58:24.371365
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.046824001273224974, 'time_ort': 8.112872654402798e-05, 'time_mlprodict': 0.00659822559365156, 'time_mlprodict2': 0.00011165159098296003, 'time_mlprodict3': 0.012211281679232012}
bench 2 : {'n_obs': 10, 'nfeat': 30, 'max_depth': 6, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.047844640810840895, 'time_ort': 0.00043181847736594223, 'time_mlprodict': 0.0004922768622193308, 'time_mlprodict2': 0.0003770684790132301, 'time_mlprodict3': 0.01223254852396037}
bench 3 : {'n_obs': 100, 'nfeat': 30, 'max_depth': 6, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.054239802264706476, 'time_ort': 0.0019677445280218593, 'time_mlprodict': 0.0073170982068404555, 'time_mlprodict2': 0.007085977949349112, 'time_mlprodict3': 0.012463190739876345}
bench 4 : {'n_obs': 1000, 'nfeat': 30, 'max_depth': 6, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.08971791425331806, 'time_ort': 0.002335551088132585, 'time_mlprodict': 0.014738206499411413, 'time_mlprodict2': 0.01276848916313611, 'time_mlprodict3': 0.01461859032860957}
bench 5 : {'n_obs': 10000, 'nfeat': 30, 'max_depth': 6, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.12816017463046592, 'time_ort': 0.022938828740734607, 'time_mlprodict': 0.13100947038037702, 'time_mlprodict2': 0.1356982148718089, 'time_mlprodict3': 0.03585509386903141}
bench 6 : {'n_obs': 1, 'nfeat': 30, 'max_depth': 8, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.04706538513577967, 'time_ort': 8.58868165364997e-05, 'time_mlprodict': 0.006275462997357615, 'time_mlprodict2': 0.00013614131603389978, 'time_mlprodict3': 0.01229259363820099}
bench 7 : {'n_obs': 10, 'nfeat': 30, 'max_depth': 8, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.04836662452379685, 'time_ort': 0.0007203845251795082, 'time_mlprodict': 0.0007675139987397762, 'time_mlprodict2': 0.0007580103341578727, 'time_mlprodict3': 0.012285356049514598}
bench 8 : {'n_obs': 100, 'nfeat': 30, 'max_depth': 8, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.054783546102331264, 'time_ort': 0.0011283658432627195, 'time_mlprodict': 0.008060571100366743, 'time_mlprodict2': 0.005916006207515143, 'time_mlprodict3': 0.01293493589190276}
bench 9 : {'n_obs': 1000, 'nfeat': 30, 'max_depth': 8, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.09015101817203686, 'time_ort': 0.0035813540841142335, 'time_mlprodict': 0.022949589911149815, 'time_mlprodict2': 0.022991571992558118, 'time_mlprodict3': 0.015744912835846964}
bench 10 : {'n_obs': 10000, 'nfeat': 30, 'max_depth': 8, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.12918856274336576, 'time_ort': 0.02982376936415676, 'time_mlprodict': 0.17005822800274473, 'time_mlprodict2': 0.17033570000785403, 'time_mlprodict3': 0.03915524261537939}
bench 11 : {'n_obs': 1, 'nfeat': 30, 'max_depth': 10, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.046970076091714545, 'time_ort': 7.950827288864689e-05, 'time_mlprodict': 0.006540170272769915, 'time_mlprodict2': 0.00017123731826855376, 'time_mlprodict3': 0.003804267410569909}
bench 12 : {'n_obs': 10, 'nfeat': 30, 'max_depth': 10, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.04925348875778062, 'time_ort': 0.0009040169506555511, 'time_mlprodict': 0.0009855637160528982, 'time_mlprodict2': 0.001074648523215382, 'time_mlprodict3': 0.012479823570521105}
bench 13 : {'n_obs': 100, 'nfeat': 30, 'max_depth': 10, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.06369584306230536, 'time_ort': 0.0017485551870777272, 'time_mlprodict': 0.00862945475091692, 'time_mlprodict2': 0.008667226815305185, 'time_mlprodict3': 0.006903725559823215}
bench 14 : {'n_obs': 1000, 'nfeat': 30, 'max_depth': 10, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.09190410490952093, 'time_ort': 0.0057887961791658945, 'time_mlprodict': 0.025684633281674574, 'time_mlprodict2': 0.028402211737226356, 'time_mlprodict3': 0.017606272457421503}
bench 15 : {'n_obs': 10000, 'nfeat': 30, 'max_depth': 10, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.13303388337953947, 'time_ort': 0.042347337381215766, 'time_mlprodict': 0.20046561888011638, 'time_mlprodict2': 0.22357457838370465, 'time_mlprodict3': 0.04729508174932562}
bench 16 : {'n_obs': 1, 'nfeat': 30, 'max_depth': 12, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.04843824328522065, 'time_ort': 0.0001065022356453396, 'time_mlprodict': 0.0063315499075023195, 'time_mlprodict2': 0.00020421790291688273, 'time_mlprodict3': 0.012155406432048906}
bench 17 : {'n_obs': 10, 'nfeat': 30, 'max_depth': 12, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.051387542102020234, 'time_ort': 0.0010617911524605007, 'time_mlprodict': 0.001187512301839888, 'time_mlprodict2': 0.001325335947331041, 'time_mlprodict3': 0.012480162153951823}
bench 18 : {'n_obs': 100, 'nfeat': 30, 'max_depth': 12, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.06676212206172447, 'time_ort': 0.0018776941346004606, 'time_mlprodict': 0.008858928534512719, 'time_mlprodict2': 0.009235445402252178, 'time_mlprodict3': 0.013766374338107805}
bench 19 : {'n_obs': 1000, 'nfeat': 30, 'max_depth': 12, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.0972531319130212, 'time_ort': 0.008801296184008772, 'time_mlprodict': 0.030396176819604905, 'time_mlprodict2': 0.033091345547952435, 'time_mlprodict3': 0.02090568490199406}
bench 20 : {'n_obs': 10000, 'nfeat': 30, 'max_depth': 12, 'n_estimators': 100, 'method': 'predict', 'n_jobs': 8, 'time_skl': 0.14401230756526015, 'time_ort': 0.06827092500004385, 'time_mlprodict': 0.24750996299553663, 'time_mlprodict2': 0.2744046502879688, 'time_mlprodict3': 0.06602232657106859}
Total time = 221.675 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.048438   0.051388   0.066762   0.097253  0.144012
time_ort              0.000107   0.001062   0.001878   0.008801  0.068271
time_mlprodict        0.006332   0.001188   0.008859   0.030396   0.24751
time_mlprodict2       0.000204   0.001325   0.009235   0.033091  0.274405
time_mlprodict3       0.012155    0.01248   0.013766   0.020906  0.066022
speedup_ort         454.809638  48.397034  35.555377  11.049865  2.109424
speedup_mlprodict     7.650298  43.273271    7.53614   3.199519  0.581844
speedup_mlprodict2  237.189015   38.77322   7.228901    2.93893  0.524817
speedup_mlprodict3    3.984914   4.117538   4.849652   4.651995  2.181267
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 53.542 seconds)

Gallery generated by Sphinx-Gallery