Skip to content
Open
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
24 changes: 19 additions & 5 deletions onnxscript/rewriter/_pattern_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,12 +899,26 @@ def num_outputs(self) -> int:
return len(self._outputs)

def commute(self) -> Sequence[GraphPattern]:
# List all commutative elementwise (binary) operators for which we
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is fine ... but, please note that the existing implementation can lead to an exponential increase in the number of patterns that are checked, with efficiency implications. (The underlying implementation needs to be improved: for example, now that there is support for disjunctive (OR) patterns, it may be useful to use that instead of creating many patterns.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we make commute rewrite the pattern into disjunctive patterns instead?

# consider swapping the inputs
COMMUTATIVE_OPS = {
("", "Add", ""),
("", "Mul", ""),
("", "And", ""),
("", "Or", ""),
("", "Xor", ""),
("", "BitwiseAnd", ""),
("", "BitwiseOr", ""),
("", "BitwiseXor", ""),
("", "Equal", ""),
("", "Max", ""),
("", "Mean", ""),
("", "Min", ""),
("", "Sum", ""),
}

def commute_node(node: NodePattern) -> Iterable[bool]:
if node.op_identifier() == ("", "Add", "") or node.op_identifier() == (
"",
"Mul",
"",
):
if node.op_identifier() in COMMUTATIVE_OPS:
# Try with and without swapping inputs.
return [False, True]
# No swapping of inputs
Expand Down
Loading