-
Notifications
You must be signed in to change notification settings - Fork 56
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
Fix conversion of scalar constants into numpy arrays in the evaluator #1970
Open
xadupre
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
xadupre:i1969
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
import unittest | ||
|
||
import numpy as np | ||
import onnx | ||
import onnxruntime as ort | ||
|
||
from onnxscript import FLOAT, script | ||
from onnxscript import opset20 as op | ||
from tests.common import testutils | ||
|
||
|
||
class TestIssues2024(testutils.TestBase): | ||
def test_issue_1969(self): | ||
# https://github.com/microsoft/onnxscript/issues/1969 | ||
|
||
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], dtype=np.float32) | ||
y = np.array(x[len(x) - 3 : len(x)], dtype=np.float32) | ||
z = np.array(x[len(x) - 8 : len(x) - 3], dtype=np.float32) | ||
|
||
x_coef = np.array( | ||
[1.1, 1.5, 1.7, 1.9, 1.2, 3.1, 4.5, 5.2, 8.5, 9.0, 11.0], dtype=np.float32 | ||
) | ||
y_coef = np.array([8.5, 9.0, 11.0], dtype=np.float32) | ||
z_coef = np.array([5, 7, 9, 9, 10, 11], dtype=np.float32) | ||
|
||
t1 = len(x) | ||
t2 = len(y) | ||
t3 = len(z) | ||
|
||
const_term = 2.37698 | ||
h1 = 1.5689 | ||
h2 = 1.799 | ||
|
||
@script() | ||
def conv(a1: FLOAT, a2: FLOAT, a3: FLOAT, a4: FLOAT, a5: FLOAT, a6: FLOAT) -> FLOAT: | ||
const = op.Constant(value_float=const_term) | ||
# Define constants | ||
coeff1 = op.Constant(value_float=h1) | ||
coeff2 = op.Constant(value_float=h2) | ||
s = coeff1 * a1 + coeff2 * a2 - const | ||
|
||
for inx in range(t1): | ||
if a4 == x[inx]: | ||
s = s + x_coef[inx] | ||
for inx2 in range(t2): | ||
if a5 == y[inx2]: | ||
s = s + y_coef[inx2] | ||
for inx3 in range(t3): | ||
if a6 == z[inx3]: | ||
s = s + z_coef[inx3] | ||
|
||
return op.Exp(s) | ||
|
||
onx = conv.to_model_proto() | ||
# To save to model and visualize it. | ||
# onnx.save(onx, "test_issue_1969.onnx") | ||
onnx.checker.check_model(onx) | ||
|
||
sess = ort.InferenceSession(onx.SerializeToString()) | ||
feeds = {f"a{i}": np.array([i], dtype=np.float32) for i in range(1, 7)} | ||
got = sess.run(None, feeds) | ||
|
||
def conv_np(a1, a2, a3, a4, a5, a6): | ||
|
||
del a3 | ||
const = const_term | ||
# Define constants | ||
coeff1 = h1 | ||
coeff2 = h2 | ||
s = coeff1 * a1 + coeff2 * a2 - const | ||
|
||
for inx in range(t1): | ||
if a4 == x[inx]: | ||
s = s + x_coef[inx] | ||
for inx2 in range(t2): | ||
if a5 == y[inx2]: | ||
s = s + y_coef[inx2] | ||
for inx3 in range(t3): | ||
if a6 == z[inx3]: | ||
s = s + z_coef[inx3] | ||
|
||
return np.exp(s) | ||
|
||
expected = conv_np(**feeds) | ||
np.allclose(expected, got[0], rtol=1e-6) | ||
# Unexpected onnxscript value type '<class 'numpy.float32'>'. | ||
# Valid value types are 'Tensor | list[Tensor] | None | np.ndarray | list[np.ndarray]' | ||
expected2 = conv(**feeds) | ||
np.allclose(expected2, got[0], rtol=1e-6) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main(verbosity=2) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / lintrunner
PYLINT/W0613 Warning test