Skip to content

Add class to yield results form onnx model and computes differences between two runs #71

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

Merged
merged 16 commits into from
Feb 6, 2024
Merged
Prev Previous commit
Next Next commit
example
  • Loading branch information
xadupre committed Feb 5, 2024
commit d1f276b67608e7d9ec413c01f8f0dcf621c37e65
68 changes: 68 additions & 0 deletions _doc/examples/plot_onnx_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""

.. _l-onnx-diff-example:

Compares the conversions of the same model with different options
=================================================================

The script compares two onnx models obtained with the same trained
scikit-learn models but converted with different options.

A model
+++++++
"""

from sklearn.mixture import GaussianMixture
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from skl2onnx import to_onnx
from onnx_array_api.reference import compare_onnx_execution
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot


data = load_iris()
X_train, X_test = train_test_split(data.data)
model = GaussianMixture()
model.fit(X_train)

#################################
# Conversion to onnx
# ++++++++++++++++++

onx = to_onnx(
model, X_train[:1], options={id(model): {"score_samples": True}}, target_opset=12
)

print(onnx_simple_text_plot(onx))

##################################
# Conversion to onnx without ReduceLogSumExp
# ++++++++++++++++++++++++++++++++++++++++++

onx2 = to_onnx(
model,
X_train[:1],
options={id(model): {"score_samples": True}},
black_op={"ReduceLogSumExp"},
target_opset=12,
)

print(onnx_simple_text_plot(onx2))


#############################################
# Differences
# +++++++++++
#
# Function :func:`onnx_array_api.reference.compare_onnx_execution`
# compares the intermediate results of two onnx models. Then it finds
# the best alignmet between the two models using an edit distance.

res1, res2, align, dc = compare_onnx_execution(onx, onx2, verbose=1)
print("------------")
text = dc.to_str(res1, res2, align)
print(text)

###############################
# The display shows that ReduceSumSquare was replaced by Mul + ReduceSum,
# and ReduceLogSumExp by ReduceMax + Sub + Exp + Log + Add.
2 changes: 1 addition & 1 deletion _unittests/ut_xrun_doc/test_command_lines1.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_command_compare(self):

code = st.getvalue()
self.assertIn("[compare_onnx_execution]", code)
self.assertIn("AAAA", code)
self.assertIn("ADFF", code)


if __name__ == "__main__":
Expand Down
8 changes: 5 additions & 3 deletions onnx_array_api/reference/evaluator_yield.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __str__(self):
_align(str(self.dtype).replace("dtype(", "").replace(")", ""), 8),
_align("x".join(map(str, self.shape)), 15),
self.summary,
_align(self.op_type or "", 8),
_align(self.op_type or "", 10),
self.name or "",
]
return " ".join(els)
Expand Down Expand Up @@ -316,7 +316,7 @@ def to_str(
s1: List[ResultExecution],
s2: List[ResultExecution],
alignment: List[Tuple[int, int]],
column_size: int = 50,
column_size: int = 60,
) -> str:
"""
Prints out the alignment between two sequences into a string.
Expand Down Expand Up @@ -384,7 +384,7 @@ def generate_input(info: ValueInfoProto) -> np.ndarray:
return (value.astype(np.float32) / p).astype(np.float32).reshape(new_shape)
if elem_type == TensorProto.FLOAT16:
return (value.astype(np.float16) / p).astype(np.float16).reshape(new_shape)
if elem_type == TensorProto.FLOAT64:
if elem_type == TensorProto.DOUBLE:
return (value.astype(np.float64) / p).astype(np.float64).reshape(new_shape)
raise RuntimeError(f"Unexpected element_type {elem_type} for info={info}")

Expand Down Expand Up @@ -414,6 +414,8 @@ def compare_onnx_execution(
"""
Compares the execution of two onnx models.
The function assumes both models takes the same inputs.
See :ref:`l-onnx-diff-example` to see a full example using
this function.

:param model1: first model
:param model2: second model
Expand Down
2 changes: 1 addition & 1 deletion onnx_array_api/reference/ops/op_fused_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def _run(
self,
A,
B,
alpha: float = 0,
alpha: float = 1,
transA: int = 0,
transB: int = 0,
transBatchA: int = 0,
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy