Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate issue 1102 #1103

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions tests_onnxmltools/test_issues_onnxmltools_2024.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# SPDX-License-Identifier: Apache-2.0
import unittest
import packaging.version as pv
from sklearn.utils._testing import ignore_warnings
from sklearn.exceptions import ConvergenceWarning
from onnxruntime import __version__ as ort_version


class TestInvestigate(unittest.TestCase):

@unittest.skipIf(
pv.Version(ort_version) < pv.Version("1.17.3"),
reason="opset 19 not implemented",
)
@ignore_warnings(category=(ConvergenceWarning, FutureWarning))
def test_issue_1102(self):
from typing import Any
from sklearn.datasets import make_regression
import lightgbm
import numpy
import onnxruntime
import skl2onnx

Check notice

Code scanning / CodeQL

Module is imported with 'import' and 'import from' Note test

Module 'skl2onnx' is imported with both 'import' and 'import from'.
from onnx.reference import ReferenceEvaluator
from onnxmltools.convert.lightgbm.operator_converters.LightGbm import (
convert_lightgbm,
)
from skl2onnx import update_registered_converter, to_onnx
from skl2onnx.common import shape_calculator
from sklearn import base, multioutput, pipeline, preprocessing

def Normalizer() -> list[tuple[str, Any]]:
return [
("cast64", skl2onnx.sklapi.CastTransformer(dtype=numpy.float64)),
("scaler", preprocessing.StandardScaler()),
("cast32", skl2onnx.sklapi.CastTransformer(dtype=numpy.float32)),
]

def Embedder(**kwargs: dict[str, Any]) -> list[tuple[str, Any]]:
return [("basemodel", lightgbm.LGBMRegressor(**kwargs))]

def BoL2EmotionV2(
backbone_kwargs: dict[str, Any] | None = None,
) -> base.BaseEstimator:
backbone = Embedder(**(backbone_kwargs or {}))
normalizer = Normalizer()
model = pipeline.Pipeline([*normalizer, *backbone])
return multioutput.MultiOutputRegressor(model)

model = BoL2EmotionV2()
X, y = make_regression(100, n_features=4, n_targets=2)
model.fit(X, y)

update_registered_converter(
lightgbm.LGBMRegressor,
"LightGbmLGBMRegressor",
shape_calculator.calculate_linear_regressor_output_shapes,
convert_lightgbm,
options={"split": None},
)

sample = X.astype(numpy.float32)

exported = to_onnx(
model,
X=sample,
name="BoL2emotion",
target_opset={"": 19, "ai.onnx.ml": 2},
)
expected = model.predict(sample)

ref = ReferenceEvaluator(exported)
got = ref.run(None, dict(X=sample))[0]
numpy.testing.assert_allclose(expected, got, 1e-4)

sess = onnxruntime.InferenceSession(
exported.SerializeToString(), providers=["CPUExecutionProvider"]
)
got = sess.run(None, dict(X=sample))[0]
numpy.testing.assert_allclose(expected, got, 1e-4)


if __name__ == "__main__":
unittest.main(verbosity=2)
Loading