From 64a746033ae564914042e610d42b2a23b41fb35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20Dupr=C3=A9?= Date: Wed, 23 Feb 2022 17:03:49 +0100 Subject: [PATCH] Fix examples after update to onnx 1.11 (#831) * Fix examples after update to onnx 1.11 --- .azure-pipelines/linux-conda-CI.yml | 2 +- docs/examples/plot_pipeline_lightgbm.py | 2 +- docs/examples/plot_pipeline_xgboost.py | 4 +-- docs/tutorial/plot_cbegin_opset.py | 10 ++++--- docs/tutorial/plot_gexternal_lightgbm.py | 2 +- docs/tutorial/plot_gexternal_lightgbm_reg.py | 8 +++-- docs/tutorial/plot_gexternal_xgboost.py | 7 +++-- docs/tutorial/plot_usparse_xgboost.py | 3 +- docs/tutorial/plot_wext_pyod_forest.py | 2 +- skl2onnx/common/_topology.py | 31 ++++++++++++++++++-- 10 files changed, 52 insertions(+), 19 deletions(-) diff --git a/.azure-pipelines/linux-conda-CI.yml b/.azure-pipelines/linux-conda-CI.yml index 7baeee117..3fea8bdd6 100644 --- a/.azure-pipelines/linux-conda-CI.yml +++ b/.azure-pipelines/linux-conda-CI.yml @@ -24,7 +24,7 @@ jobs: sklearn.version: '>=1.0' lgbm.version: '' onnxcc.version: '>=1.8.1' # git - run.example: '0' + run.example: '1' Py39-Onnx1102-Rt110-Skl10: do.bench: '0' python.version: '3.9' diff --git a/docs/examples/plot_pipeline_lightgbm.py b/docs/examples/plot_pipeline_lightgbm.py index 183a614ab..2a3523607 100644 --- a/docs/examples/plot_pipeline_lightgbm.py +++ b/docs/examples/plot_pipeline_lightgbm.py @@ -86,7 +86,7 @@ model_onnx = convert_sklearn( pipe, 'pipeline_lightgbm', [('input', FloatTensorType([None, 2]))], - target_opset=12) + target_opset={'': 12, 'ai.onnx.ml': 2}) # And save. with open("pipeline_lightgbm.onnx", "wb") as f: diff --git a/docs/examples/plot_pipeline_xgboost.py b/docs/examples/plot_pipeline_xgboost.py index 6b126e559..a2b752bcd 100644 --- a/docs/examples/plot_pipeline_xgboost.py +++ b/docs/examples/plot_pipeline_xgboost.py @@ -60,7 +60,7 @@ try: convert_sklearn(pipe, 'pipeline_xgboost', [('input', FloatTensorType([None, 2]))], - target_opset=12) + target_opset={'': 12, 'ai.onnx.ml': 2}) except Exception as e: print(e) @@ -102,7 +102,7 @@ model_onnx = convert_sklearn( pipe, 'pipeline_xgboost', [('input', FloatTensorType([None, 2]))], - target_opset=12) + target_opset={'': 12, 'ai.onnx.ml': 2}) # And save. with open("pipeline_xgboost.onnx", "wb") as f: diff --git a/docs/tutorial/plot_cbegin_opset.py b/docs/tutorial/plot_cbegin_opset.py index a5e8ecf5c..182591e49 100644 --- a/docs/tutorial/plot_cbegin_opset.py +++ b/docs/tutorial/plot_cbegin_opset.py @@ -59,7 +59,8 @@ # ++++ -onx = to_onnx(model, X[:1].astype(numpy.float32)) +onx = to_onnx(model, X[:1].astype(numpy.float32), + target_opset={'': 15, 'ai.onnx.ml': 2}) print(onx) ########################## @@ -89,9 +90,10 @@ def get_domain_opset(onx): return {d['domain']: d['version'] for d in res} -for opset in range(1, onnx_opset_version() + 1): +for opset in range(6, onnx_opset_version() + 1): try: - onx = to_onnx(model, X[:1].astype(numpy.float32), target_opset=opset) + onx = to_onnx(model, X[:1].astype(numpy.float32), + target_opset={'': opset, 'ai.onnx.ml': 2}) except RuntimeError as e: print('target: %r error: %r' % (opset, e)) continue @@ -112,7 +114,7 @@ def get_domain_opset(onx): # ``''`` but the other opset domain can be changed as well. for opset in range(9, onnx_opset_version() + 1): - for opset_ml in range(1, 3): + for opset_ml in range(1, 4): tops = {'': opset, 'ai.onnx.ml': opset_ml} try: onx = to_onnx( diff --git a/docs/tutorial/plot_gexternal_lightgbm.py b/docs/tutorial/plot_gexternal_lightgbm.py index 2e624304b..da0dddfc1 100644 --- a/docs/tutorial/plot_gexternal_lightgbm.py +++ b/docs/tutorial/plot_gexternal_lightgbm.py @@ -73,7 +73,7 @@ model_onnx = convert_sklearn( pipe, 'pipeline_lightgbm', [('input', FloatTensorType([None, 2]))], - target_opset=12) + target_opset={'': 12, 'ai.onnx.ml': 2}) # And save. with open("pipeline_lightgbm.onnx", "wb") as f: diff --git a/docs/tutorial/plot_gexternal_lightgbm_reg.py b/docs/tutorial/plot_gexternal_lightgbm_reg.py index 7c0955a90..0f93edc67 100644 --- a/docs/tutorial/plot_gexternal_lightgbm_reg.py +++ b/docs/tutorial/plot_gexternal_lightgbm_reg.py @@ -102,8 +102,10 @@ def skl2onnx_convert_lightgbm(scope, operator, container): # TreeEnsembleRegressor node, or more. *split* parameter is the number of # trees per node TreeEnsembleRegressor. -model_onnx = to_onnx(reg, X[:1].astype(numpy.float32), target_opset=14) -model_onnx_split = to_onnx(reg, X[:1].astype(numpy.float32), target_opset=14, +model_onnx = to_onnx(reg, X[:1].astype(numpy.float32), + target_opset={'': 14, 'ai.onnx.ml': 2}) +model_onnx_split = to_onnx(reg, X[:1].astype(numpy.float32), + target_opset={'': 14, 'ai.onnx.ml': 2}, options={'split': 100}) ########################## @@ -158,7 +160,7 @@ def skl2onnx_convert_lightgbm(scope, operator, container): res = [] for i in tqdm(list(range(20, 170, 20)) + [200, 300, 400, 500]): model_onnx_split = to_onnx(reg, X[:1].astype(numpy.float32), - target_opset=14, + target_opset={'': 14, 'ai.onnx.ml': 2}, options={'split': i}) sess_split = InferenceSession(model_onnx_split.SerializeToString()) got_split = sess_split.run(None, {'X': X32})[0].ravel() diff --git a/docs/tutorial/plot_gexternal_xgboost.py b/docs/tutorial/plot_gexternal_xgboost.py index 3e8a37868..8c59bd837 100644 --- a/docs/tutorial/plot_gexternal_xgboost.py +++ b/docs/tutorial/plot_gexternal_xgboost.py @@ -60,7 +60,7 @@ try: convert_sklearn(pipe, 'pipeline_xgboost', [('input', FloatTensorType([None, 2]))], - target_opset=12) + target_opset={'': 12, 'ai.onnx.ml': 2}) except Exception as e: print(e) @@ -97,7 +97,7 @@ model_onnx = convert_sklearn( pipe, 'pipeline_xgboost', [('input', FloatTensorType([None, 2]))], - target_opset=12) + target_opset={'': 12, 'ai.onnx.ml': 2}) # And save. with open("pipeline_xgboost.onnx", "wb") as f: @@ -154,7 +154,8 @@ ############################# # ONNX -onx = to_onnx(pipe, X_train.astype(numpy.float32)) +onx = to_onnx(pipe, X_train.astype(numpy.float32), + target_opset={'': 12, 'ai.onnx.ml': 2}) sess = rt.InferenceSession(onx.SerializeToString()) pred_onx = sess.run(None, {"X": X_test[:5].astype(numpy.float32)}) diff --git a/docs/tutorial/plot_usparse_xgboost.py b/docs/tutorial/plot_usparse_xgboost.py index a9773a428..bc3fd6cdb 100644 --- a/docs/tutorial/plot_usparse_xgboost.py +++ b/docs/tutorial/plot_usparse_xgboost.py @@ -161,7 +161,8 @@ def make_pipelines(df_train, y_train, models=None, pipe, initial_types=[('input', FloatTensorType([None, 2])), ('text', StringTensorType([None, 1]))], - target_opset=12, options=options) + target_opset={'': 12, 'ai.onnx.ml': 2}, + options=options) with open('model.onnx', 'wb') as f: f.write(model_onnx.SerializeToString()) diff --git a/docs/tutorial/plot_wext_pyod_forest.py b/docs/tutorial/plot_wext_pyod_forest.py index af7255ae8..ffa06bc9e 100644 --- a/docs/tutorial/plot_wext_pyod_forest.py +++ b/docs/tutorial/plot_wext_pyod_forest.py @@ -172,7 +172,7 @@ def pyod_iforest_converter(scope, operator, container): if IForest is not None: onx = to_onnx(model1, initial_types=initial_type, - target_opset=14) + target_opset={'': 14, 'ai.onnx.ml': 2}) ############################################### # Checking discrepencies diff --git a/skl2onnx/common/_topology.py b/skl2onnx/common/_topology.py index caf91db2d..3c9ebaf6c 100644 --- a/skl2onnx/common/_topology.py +++ b/skl2onnx/common/_topology.py @@ -42,7 +42,7 @@ def _default_OPSET_TO_IR_VERSION(): return { 1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 3, 7: 3, 8: 4, 9: 4, 10: 5, 11: 6, 12: 7, - 13: 7, 14: 7, 15: 8 + 13: 7, 14: 7, 15: 8, 16: 8 } @@ -52,11 +52,29 @@ def _default_OPSET_TO_IR_VERSION(): except (ImportError, KeyError): OPSET_TO_IR_VERSION = _default_OPSET_TO_IR_VERSION() -OPSET_ML_TO_OPSET = {1: 11, 2: 15} +OPSET_ML_TO_OPSET = {1: 11, 2: 15, 3: 16} logger = getLogger('skl2onnx') +def get_default_opset_for_domain(domain): + """ + Returns the associated for a domain given the main opset. + """ + from .. import __max_supported_opset__ as main_opset + if domain == '': + return main_opset + if domain == 'ai.onnx.ml': + if main_opset >= 16: + return 3 + if main_opset < 6: + return 1 + return 2 + if domain == 'ai.onnx.training': + return 1 + return None + + class Variable: """ Defines a variable which holds any data defined @@ -1516,7 +1534,16 @@ def _update_domain_version(container, onnx_model, verbose=0): print('[_update_domain_version] +opset %d: name=%r, version=%s' % ( i, op_domain, op_version)) op_set.domain = op_domain + if op_set != '': + max_supported = get_default_opset_for_domain(op_domain) + if max_supported is not None and max_supported < op_version: + raise RuntimeError( + "The model is using version %d of domain %r not supported " + "yet by this library. You need to specify " + "target_opset={%r: %r}." % ( + op_version, op_domain, op_domain, max_supported)) op_set.version = op_version + i += 1 if container.target_opset_any_domain(op_domain) < op_version: raise RuntimeError(