Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion onnxscript/rewriter/rules/common/_fuse_relus_clips.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def extract_min_max(self, node: ir.Node):
min_clip = min_input.const_value.numpy()

if len(node.inputs) > 2:
max_clip = node.inputs[2].const_value.numpy()
max_clip = node.inputs[2]
if max_clip is not None:
max_clip = max_clip.const_value.numpy()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a check here whether max_clip.const_value is None. It is possible that it is being checked elsewhere before this method if called, but, even so, it would be better if all related logic is in one place (checking if an input is present, whether it is a constant, etc.). Overall, it feels like the structure of this implementation turns what should be a mathematically simple rule (easy to read and validate) into something opaque.

I realize that this is mostly a problem with the pre-existing rule, but it just makes updates like this harder to review.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the general point, but I think this case is a bit special. Normally we would perform these checks in the check method and fail the pattern if they don’t pass. However, in this pattern the max/min values can be None, and the rewrite can still proceed in that case. That’s why the logic was isolated in extract_min_max method instead of being handled entirely in check.
That said, I’m open to suggestions if you think there’s a cleaner way to structure this.


return min_clip, max_clip, dtype

Expand Down
36 changes: 30 additions & 6 deletions onnxscript/rewriter/rules/common/_fuse_relus_clips_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
import parameterized
from onnx_ir.passes.common import onnx_checker, shape_inference

from onnxscript.rewriter import (
MatchingTracer,
MatchStatus,
RewriteRule,
testing,
)
from onnxscript.rewriter import MatchingTracer, MatchStatus, RewriteRule, testing
from onnxscript.rewriter.rules.common import _fuse_relus_clips
from onnxscript.rewriter.rules.common._fuse_relus_clips import (
successive_clip_relu_rule,
Expand Down Expand Up @@ -206,6 +201,35 @@ def test_successful_fuse_successive_relu_clip_no_min(self, _, nodes):
""")
self.run_test(model, expected_op_types=["Clip"])

@parameterized.parameterized.expand(
[
(
"relu_then_clip",
"""
x1 = Relu(X)
Y = Clip(x1,min,"")
""",
),
(
"clip_then_relu",
"""
x1 = Clip(X,min,"")
Y = Relu(x1)
""",
),
]
)
def test_successful_fuse_successive_relu_clip_no_max(self, _, nodes):
model = ir.from_onnx_text(f"""
< ir_version: 10, opset_import: ["" : 20] >
test_model (float[N, 32, 14] X) => (float [N, ?, ?] Y)
<float min = {{1.0}}>
{{
{nodes}
}}
""")
self.run_test(model, expected_op_types=["Clip"])

@parameterized.parameterized.expand(
[
(
Expand Down
Loading