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
# 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