Skip to content
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

Do not fuse locations when normalizing constants for Add and Mul #3016

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
8 changes: 4 additions & 4 deletions src/Dialect/ONNX/Transforms/ConstProp.td
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ def CreateScatterNDOfConst :
// Use commutativity to normalize constants in the second position of Add.
def AddConstCommutative1 : NamedPat<"AddConstCommutative1",
// From add(c, x).
(ONNXAddOp (ONNXConstantOp:$c $_, $_, $_, $_, $_, $_, $_, $_), $x),
(ONNXAddOp:$addOp (ONNXConstantOp:$c $_, $_, $_, $_, $_, $_, $_, $_), $x),
// To add(x, c).
(ONNXAddOp $x, $c),
(ONNXAddOp $x, $c, (location $addOp)),
// To avoid infinite loop, constrain the first arguments to be anything but a constant.
[(IsNotAConstant:$x)]>;

Expand Down Expand Up @@ -575,9 +575,9 @@ def SumConstProp : NamedPat<"SumConstProp",
// Use commutativity to normalize constants in the second position of Mul.
def MulConstCommutative1 : NamedPat<"MulConstCommutative1",
// From mul(c, x).
(ONNXMulOp (ONNXConstantOp:$c $_, $_, $_, $_, $_, $_, $_, $_), $x),
(ONNXMulOp:$mulOp (ONNXConstantOp:$c $_, $_, $_, $_, $_, $_, $_, $_), $x),
// To mul(x, c).
(ONNXMulOp $x, $c),
(ONNXMulOp $x, $c, (location $mulOp)),
// To avoid infinite loop, constrain the first arguments to be anything but a constant.
[(IsNotAConstant:$x)]>;

Expand Down
30 changes: 30 additions & 0 deletions test/mlir/onnx/onnx_constprop_locations.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: onnx-mlir-opt --shape-inference --constprop-onnx %s -split-input-file --mlir-print-debuginfo | FileCheck %s


//===----------------------------------------------------------------------===//
/// Commutative tests

// CHECK-LABEL: @test_add_constant_1_loc
func.func @test_add_constant_1_loc(%arg0 : tensor<3xf32>) -> tensor<3xf32> {
%0 = onnx.Constant dense<[0.0, 1.0, 2.0]> : tensor<3xf32> loc("Constant")
%1 = "onnx.Add"(%0, %arg0) : (tensor<3xf32> , tensor<3xf32>) -> tensor<3xf32> loc("Add")
"onnx.Return"(%1) : (tensor<3xf32>) -> ()
// CHECK-NEXT: [[CONST:%.+]] = onnx.Constant dense<[0.000000e+00, 1.000000e+00, 2.000000e+00]> : tensor<3xf32> loc([[LOC_CONST:#.+]])
// CHECK-NEXT: [[ADD:%.+]] = "onnx.Add"(%arg0, [[CONST]]) : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xf32> loc([[LOC_ADD:#.+]])
// CHECK-DAG: [[LOC_CONST]] = loc("Constant")
// CHECK-DAG: [[LOC_ADD]] = loc("Add")
}

// -----

// CHECK-LABEL: @test_mul_constant_1_loc
func.func @test_mul_constant_1_loc(%arg0 : tensor<3xf32>) -> tensor<3xf32> {
%0 = onnx.Constant dense<[0.0, 1.0, 2.0]> : tensor<3xf32> loc("Constant")
%1 = "onnx.Mul"(%0, %arg0) : (tensor<3xf32> , tensor<3xf32>) -> tensor<3xf32> loc("Mul")
"onnx.Return"(%1) : (tensor<3xf32>) -> ()
// CHECK-NEXT: [[CONST:%.+]] = onnx.Constant dense<[0.000000e+00, 1.000000e+00, 2.000000e+00]> : tensor<3xf32> loc([[LOC_CONST:#.+]])
// CHECK-NEXT: [[MUL:%.+]] = "onnx.Mul"(%arg0, [[CONST]]) : (tensor<3xf32>, tensor<3xf32>) -> tensor<3xf32> loc([[LOC_MUL:#.+]])
// CHECK-DAG: [[LOC_CONST]] = loc("Constant")
// CHECK-DAG: [[LOC_MUL]] = loc("Mul")
}

Loading