Should we reorder elementwise(concat(x), ...)?
I found that swap the ordering of tensor.concat and elementwise op can be helpful for optimization.
For example:
module {
func.func @before(
%x0: tensor<3xf32>, %x1: tensor<4xf32>,
%y0: tensor<3xf32>, %y1: tensor<4xf32>) -> tensor<7xf32> {
%c = arith.constant 2.0 : f32
%x0e = tensor.empty() : tensor<3xf32>
%x1e = tensor.empty() : tensor<4xf32>
%y0e = tensor.empty() : tensor<3xf32>
%y1e = tensor.empty() : tensor<4xf32>
%px0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x0 : tensor<3xf32>) outs(%x0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%px1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x1 : tensor<4xf32>) outs(%x1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%py0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y0 : tensor<3xf32>) outs(%y0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%py1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y1 : tensor<4xf32>) outs(%y1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%x = tensor.concat dim(0) %px0, %px1 :
(tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%y = tensor.concat dim(0) %py0, %py1 :
(tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%r = arith.addf %x, %y : tensor<7xf32>
return %r : tensor<7xf32>
}
}
this struct is, abstractly, add(concat(x0, x1), concat(y0, y1))
after -convert-elementwise-to-linalg -linalg-fuse-elementwise-ops we have:
#map = affine_map<(d0) -> (d0)>
module {
func.func @before(%arg0: tensor<3xf32>, %arg1: tensor<4xf32>, %arg2: tensor<3xf32>, %arg3: tensor<4xf32>) -> tensor<7xf32> {
%cst = arith.constant 2.000000e+00 : f32
%0 = tensor.empty() : tensor<3xf32>
%1 = tensor.empty() : tensor<4xf32>
%2 = tensor.empty() : tensor<3xf32>
%3 = tensor.empty() : tensor<4xf32>
%4 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg0 : tensor<3xf32>) outs(%0 : tensor<3xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<3xf32>
%5 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg1 : tensor<4xf32>) outs(%1 : tensor<4xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<4xf32>
%6 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg2 : tensor<3xf32>) outs(%2 : tensor<3xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<3xf32>
%7 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg3 : tensor<4xf32>) outs(%3 : tensor<4xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<4xf32>
%concat = tensor.concat dim(0) %4, %5 : (tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%concat_0 = tensor.concat dim(0) %6, %7 : (tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%8 = tensor.empty() : tensor<7xf32>
%9 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%concat, %concat_0 : tensor<7xf32>, tensor<7xf32>) outs(%8 : tensor<7xf32>) {
^bb0(%in: f32, %in_1: f32, %out: f32):
%10 = arith.addf %in, %in_1 : f32
linalg.yield %10 : f32
} -> tensor<7xf32>
return %9 : tensor<7xf32>
}
}
but if we reordering concat and add to concat(add(x0, x1), add(y0, y1)), we have:
module {
func.func @after(
%x0: tensor<3xf32>, %x1: tensor<4xf32>,
%y0: tensor<3xf32>, %y1: tensor<4xf32>) -> tensor<7xf32> {
%c = arith.constant 2.0 : f32
%x0e = tensor.empty() : tensor<3xf32>
%x1e = tensor.empty() : tensor<4xf32>
%y0e = tensor.empty() : tensor<3xf32>
%y1e = tensor.empty() : tensor<4xf32>
%px0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x0 : tensor<3xf32>) outs(%x0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%px1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x1 : tensor<4xf32>) outs(%x1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%py0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y0 : tensor<3xf32>) outs(%y0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%py1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y1 : tensor<4xf32>) outs(%y1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%r0e = tensor.empty() : tensor<3xf32>
%r0 = linalg.generic {
indexing_maps = [
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>
],
iterator_types = ["parallel"]
} ins(%px0, %py0 : tensor<3xf32>, tensor<3xf32>)
outs(%r0e : tensor<3xf32>) {
^bb0(%a: f32, %b: f32, %o: f32):
%v = arith.addf %a, %b : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%r1e = tensor.empty() : tensor<4xf32>
%r1 = linalg.generic {
indexing_maps = [
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>
],
iterator_types = ["parallel"]
} ins(%px1, %py1 : tensor<4xf32>, tensor<4xf32>)
outs(%r1e : tensor<4xf32>) {
^bb0(%a: f32, %b: f32, %o: f32):
%v = arith.addf %a, %b : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%r = tensor.concat dim(0) %r0, %r1 :
(tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
return %r : tensor<7xf32>
}
}
after -convert-elementwise-to-linalg -linalg-fuse-elementwise-ops we have,
#map = affine_map<(d0) -> (d0)>
module {
func.func @after(%arg0: tensor<3xf32>, %arg1: tensor<4xf32>, %arg2: tensor<3xf32>, %arg3: tensor<4xf32>) -> tensor<7xf32> {
%cst = arith.constant 2.000000e+00 : f32
%0 = tensor.empty() : tensor<3xf32>
%1 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%arg0, %arg2 : tensor<3xf32>, tensor<3xf32>) outs(%0 : tensor<3xf32>) {
^bb0(%in: f32, %in_0: f32, %out: f32):
%4 = arith.mulf %in_0, %cst : f32
%5 = arith.mulf %in, %cst : f32
%6 = arith.addf %5, %4 : f32
linalg.yield %6 : f32
} -> tensor<3xf32>
%2 = tensor.empty() : tensor<4xf32>
%3 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%arg1, %arg3 : tensor<4xf32>, tensor<4xf32>) outs(%2 : tensor<4xf32>) {
^bb0(%in: f32, %in_0: f32, %out: f32):
%4 = arith.mulf %in_0, %cst : f32
%5 = arith.mulf %in, %cst : f32
%6 = arith.addf %5, %4 : f32
linalg.yield %6 : f32
} -> tensor<4xf32>
%concat = tensor.concat dim(0) %1, %3 : (tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
return %concat : tensor<7xf32>
}
}
it is clearly shorter and we fused the loops together.
The reason may be, for add(concat(x0, x1), concat(y0, y1)), the optimizer can't see the dataflow between add and the operands (x0, x1, y0 and y1). But after reordering to concat(add(x0, x1), add(y0, y1)), the dataflow is much simpler. So the optimizer can handle it.
But the transformation itself is not an optimization. So I have some questions,
(1) Do we want such transformation? (reordering concat and elementwise)? (2) If yes, where should we make it? In a standalone new pass? Or an existing pass?
Thanks in ahead. I tried to implement this but I realized it may be better to get align ahead.
@llvm/issue-subscribers-mlir-linalg
Author: Chuanqi Xu (ChuanqiXu9)
For example:
module {
func.func @<!-- -->before(
%x0: tensor<3xf32>, %x1: tensor<4xf32>,
%y0: tensor<3xf32>, %y1: tensor<4xf32>) -> tensor<7xf32> {
%c = arith.constant 2.0 : f32
%x0e = tensor.empty() : tensor<3xf32>
%x1e = tensor.empty() : tensor<4xf32>
%y0e = tensor.empty() : tensor<3xf32>
%y1e = tensor.empty() : tensor<4xf32>
%px0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x0 : tensor<3xf32>) outs(%x0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%px1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x1 : tensor<4xf32>) outs(%x1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%py0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y0 : tensor<3xf32>) outs(%y0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%py1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y1 : tensor<4xf32>) outs(%y1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%x = tensor.concat dim(0) %px0, %px1 :
(tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%y = tensor.concat dim(0) %py0, %py1 :
(tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%r = arith.addf %x, %y : tensor<7xf32>
return %r : tensor<7xf32>
}
}
this struct is, abstractly, add(concat(x0, x1), concat(y0, y1))
after -convert-elementwise-to-linalg -linalg-fuse-elementwise-ops we have:
#map = affine_map<(d0) -> (d0)>
module {
func.func @<!-- -->before(%arg0: tensor<3xf32>, %arg1: tensor<4xf32>, %arg2: tensor<3xf32>, %arg3: tensor<4xf32>) -> tensor<7xf32> {
%cst = arith.constant 2.000000e+00 : f32
%0 = tensor.empty() : tensor<3xf32>
%1 = tensor.empty() : tensor<4xf32>
%2 = tensor.empty() : tensor<3xf32>
%3 = tensor.empty() : tensor<4xf32>
%4 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg0 : tensor<3xf32>) outs(%0 : tensor<3xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<3xf32>
%5 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg1 : tensor<4xf32>) outs(%1 : tensor<4xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<4xf32>
%6 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg2 : tensor<3xf32>) outs(%2 : tensor<3xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<3xf32>
%7 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg3 : tensor<4xf32>) outs(%3 : tensor<4xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<4xf32>
%concat = tensor.concat dim(0) %4, %5 : (tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%concat_0 = tensor.concat dim(0) %6, %7 : (tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%8 = tensor.empty() : tensor<7xf32>
%9 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%concat, %concat_0 : tensor<7xf32>, tensor<7xf32>) outs(%8 : tensor<7xf32>) {
^bb0(%in: f32, %in_1: f32, %out: f32):
%10 = arith.addf %in, %in_1 : f32
linalg.yield %10 : f32
} -> tensor<7xf32>
return %9 : tensor<7xf32>
}
}
but if we reordering concat and add to concat(add(x0, x1), add(y0, y1)), we have:
module {
func.func @<!-- -->after(
%x0: tensor<3xf32>, %x1: tensor<4xf32>,
%y0: tensor<3xf32>, %y1: tensor<4xf32>) -> tensor<7xf32> {
%c = arith.constant 2.0 : f32
%x0e = tensor.empty() : tensor<3xf32>
%x1e = tensor.empty() : tensor<4xf32>
%y0e = tensor.empty() : tensor<3xf32>
%y1e = tensor.empty() : tensor<4xf32>
%px0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x0 : tensor<3xf32>) outs(%x0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%px1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x1 : tensor<4xf32>) outs(%x1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%py0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y0 : tensor<3xf32>) outs(%y0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%py1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y1 : tensor<4xf32>) outs(%y1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%r0e = tensor.empty() : tensor<3xf32>
%r0 = linalg.generic {
indexing_maps = [
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>
],
iterator_types = ["parallel"]
} ins(%px0, %py0 : tensor<3xf32>, tensor<3xf32>)
outs(%r0e : tensor<3xf32>) {
^bb0(%a: f32, %b: f32, %o: f32):
%v = arith.addf %a, %b : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%r1e = tensor.empty() : tensor<4xf32>
%r1 = linalg.generic {
indexing_maps = [
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>
],
iterator_types = ["parallel"]
} ins(%px1, %py1 : tensor<4xf32>, tensor<4xf32>)
outs(%r1e : tensor<4xf32>) {
^bb0(%a: f32, %b: f32, %o: f32):
%v = arith.addf %a, %b : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%r = tensor.concat dim(0) %r0, %r1 :
(tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
return %r : tensor<7xf32>
}
}
after -convert-elementwise-to-linalg -linalg-fuse-elementwise-ops we have,
#map = affine_map<(d0) -> (d0)>
module {
func.func @<!-- -->after(%arg0: tensor<3xf32>, %arg1: tensor<4xf32>, %arg2: tensor<3xf32>, %arg3: tensor<4xf32>) -> tensor<7xf32> {
%cst = arith.constant 2.000000e+00 : f32
%0 = tensor.empty() : tensor<3xf32>
%1 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%arg0, %arg2 : tensor<3xf32>, tensor<3xf32>) outs(%0 : tensor<3xf32>) {
^bb0(%in: f32, %in_0: f32, %out: f32):
%4 = arith.mulf %in_0, %cst : f32
%5 = arith.mulf %in, %cst : f32
%6 = arith.addf %5, %4 : f32
linalg.yield %6 : f32
} -> tensor<3xf32>
%2 = tensor.empty() : tensor<4xf32>
%3 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%arg1, %arg3 : tensor<4xf32>, tensor<4xf32>) outs(%2 : tensor<4xf32>) {
^bb0(%in: f32, %in_0: f32, %out: f32):
%4 = arith.mulf %in_0, %cst : f32
%5 = arith.mulf %in, %cst : f32
%6 = arith.addf %5, %4 : f32
linalg.yield %6 : f32
} -> tensor<4xf32>
%concat = tensor.concat dim(0) %1, %3 : (tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
return %concat : tensor<7xf32>
}
}
it is clearly shorter and we fused the loops together.
The reason may be, for add(concat(x0, x1), concat(y0, y1)), the optimizer can't see the dataflow between add and the operands (x0, x1, y0 and y1). But after reordering to concat(add(x0, x1), add(y0, y1)), the dataflow is much simpler. So the optimizer can handle it.
But the transformation itself is not an optimization. So I have some questions,
(1) Do we want such transformation? (reordering concat and elementwise)? (2) If yes, where should we make it? In a standalone new pass? Or an existing pass?
Thanks in ahead. I tried to implement this but I realized it may be better to get align ahead.
@llvm/issue-subscribers-mlir
Author: Chuanqi Xu (ChuanqiXu9)
For example:
module {
func.func @<!-- -->before(
%x0: tensor<3xf32>, %x1: tensor<4xf32>,
%y0: tensor<3xf32>, %y1: tensor<4xf32>) -> tensor<7xf32> {
%c = arith.constant 2.0 : f32
%x0e = tensor.empty() : tensor<3xf32>
%x1e = tensor.empty() : tensor<4xf32>
%y0e = tensor.empty() : tensor<3xf32>
%y1e = tensor.empty() : tensor<4xf32>
%px0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x0 : tensor<3xf32>) outs(%x0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%px1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x1 : tensor<4xf32>) outs(%x1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%py0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y0 : tensor<3xf32>) outs(%y0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%py1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y1 : tensor<4xf32>) outs(%y1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%x = tensor.concat dim(0) %px0, %px1 :
(tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%y = tensor.concat dim(0) %py0, %py1 :
(tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%r = arith.addf %x, %y : tensor<7xf32>
return %r : tensor<7xf32>
}
}
this struct is, abstractly, add(concat(x0, x1), concat(y0, y1))
after -convert-elementwise-to-linalg -linalg-fuse-elementwise-ops we have:
#map = affine_map<(d0) -> (d0)>
module {
func.func @<!-- -->before(%arg0: tensor<3xf32>, %arg1: tensor<4xf32>, %arg2: tensor<3xf32>, %arg3: tensor<4xf32>) -> tensor<7xf32> {
%cst = arith.constant 2.000000e+00 : f32
%0 = tensor.empty() : tensor<3xf32>
%1 = tensor.empty() : tensor<4xf32>
%2 = tensor.empty() : tensor<3xf32>
%3 = tensor.empty() : tensor<4xf32>
%4 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg0 : tensor<3xf32>) outs(%0 : tensor<3xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<3xf32>
%5 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg1 : tensor<4xf32>) outs(%1 : tensor<4xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<4xf32>
%6 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg2 : tensor<3xf32>) outs(%2 : tensor<3xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<3xf32>
%7 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} ins(%arg3 : tensor<4xf32>) outs(%3 : tensor<4xf32>) {
^bb0(%in: f32, %out: f32):
%10 = arith.mulf %in, %cst : f32
linalg.yield %10 : f32
} -> tensor<4xf32>
%concat = tensor.concat dim(0) %4, %5 : (tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%concat_0 = tensor.concat dim(0) %6, %7 : (tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
%8 = tensor.empty() : tensor<7xf32>
%9 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%concat, %concat_0 : tensor<7xf32>, tensor<7xf32>) outs(%8 : tensor<7xf32>) {
^bb0(%in: f32, %in_1: f32, %out: f32):
%10 = arith.addf %in, %in_1 : f32
linalg.yield %10 : f32
} -> tensor<7xf32>
return %9 : tensor<7xf32>
}
}
but if we reordering concat and add to concat(add(x0, x1), add(y0, y1)), we have:
module {
func.func @<!-- -->after(
%x0: tensor<3xf32>, %x1: tensor<4xf32>,
%y0: tensor<3xf32>, %y1: tensor<4xf32>) -> tensor<7xf32> {
%c = arith.constant 2.0 : f32
%x0e = tensor.empty() : tensor<3xf32>
%x1e = tensor.empty() : tensor<4xf32>
%y0e = tensor.empty() : tensor<3xf32>
%y1e = tensor.empty() : tensor<4xf32>
%px0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x0 : tensor<3xf32>) outs(%x0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%px1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%x1 : tensor<4xf32>) outs(%x1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%py0 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y0 : tensor<3xf32>) outs(%y0e : tensor<3xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%py1 = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>],
iterator_types = ["parallel"]
} ins(%y1 : tensor<4xf32>) outs(%y1e : tensor<4xf32>) {
^bb0(%a: f32, %o: f32):
%v = arith.mulf %a, %c : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%r0e = tensor.empty() : tensor<3xf32>
%r0 = linalg.generic {
indexing_maps = [
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>
],
iterator_types = ["parallel"]
} ins(%px0, %py0 : tensor<3xf32>, tensor<3xf32>)
outs(%r0e : tensor<3xf32>) {
^bb0(%a: f32, %b: f32, %o: f32):
%v = arith.addf %a, %b : f32
linalg.yield %v : f32
} -> tensor<3xf32>
%r1e = tensor.empty() : tensor<4xf32>
%r1 = linalg.generic {
indexing_maps = [
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>
],
iterator_types = ["parallel"]
} ins(%px1, %py1 : tensor<4xf32>, tensor<4xf32>)
outs(%r1e : tensor<4xf32>) {
^bb0(%a: f32, %b: f32, %o: f32):
%v = arith.addf %a, %b : f32
linalg.yield %v : f32
} -> tensor<4xf32>
%r = tensor.concat dim(0) %r0, %r1 :
(tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
return %r : tensor<7xf32>
}
}
after -convert-elementwise-to-linalg -linalg-fuse-elementwise-ops we have,
#map = affine_map<(d0) -> (d0)>
module {
func.func @<!-- -->after(%arg0: tensor<3xf32>, %arg1: tensor<4xf32>, %arg2: tensor<3xf32>, %arg3: tensor<4xf32>) -> tensor<7xf32> {
%cst = arith.constant 2.000000e+00 : f32
%0 = tensor.empty() : tensor<3xf32>
%1 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%arg0, %arg2 : tensor<3xf32>, tensor<3xf32>) outs(%0 : tensor<3xf32>) {
^bb0(%in: f32, %in_0: f32, %out: f32):
%4 = arith.mulf %in_0, %cst : f32
%5 = arith.mulf %in, %cst : f32
%6 = arith.addf %5, %4 : f32
linalg.yield %6 : f32
} -> tensor<3xf32>
%2 = tensor.empty() : tensor<4xf32>
%3 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%arg1, %arg3 : tensor<4xf32>, tensor<4xf32>) outs(%2 : tensor<4xf32>) {
^bb0(%in: f32, %in_0: f32, %out: f32):
%4 = arith.mulf %in_0, %cst : f32
%5 = arith.mulf %in, %cst : f32
%6 = arith.addf %5, %4 : f32
linalg.yield %6 : f32
} -> tensor<4xf32>
%concat = tensor.concat dim(0) %1, %3 : (tensor<3xf32>, tensor<4xf32>) -> tensor<7xf32>
return %concat : tensor<7xf32>
}
}
it is clearly shorter and we fused the loops together.
The reason may be, for add(concat(x0, x1), concat(y0, y1)), the optimizer can't see the dataflow between add and the operands (x0, x1, y0 and y1). But after reordering to concat(add(x0, x1), add(y0, y1)), the dataflow is much simpler. So the optimizer can handle it.
But the transformation itself is not an optimization. So I have some questions,
(1) Do we want such transformation? (reordering concat and elementwise)? (2) If yes, where should we make it? In a standalone new pass? Or an existing pass?
Thanks in ahead. I tried to implement this but I realized it may be better to get align ahead.
CC @rengolin @dcaballe @nicolasvasilache as linalg owners.
The reason may be, for
add(concat(x0, x1), concat(y0, y1)), the optimizer can't see the dataflow betweenaddand the operands (x0, x1, y0 and y1). But after reordering toconcat(add(x0, x1), add(y0, y1)), the dataflow is much simpler. So the optimizer can handle it.
Simply put: the fuser cannot "see through" concat.
(1) Do we want such transformation? (reordering concat and elementwise)?
I don't see why not. concat does not reorder floating point operations on its own, as long as they keep element-wise. A reduction would change the numerical behaviour and may not be safe under strict regimes. Some analysis will be needed, and potentially relaxed in the presence of fast-math flags.
(2) If yes, where should we make it? In a standalone new pass? Or an existing pass?
It could be argued this should be a canonicalization, but the benefit seems to be from allowing better fusion, which in itself, isn't a canonicalization, so I would not do so this way.
I would create a small rewrite, and possibly reuse this as a preamble in the linalg fusion.
Hey folks, I'm relatively new to MLIR, but this issue caught my eye and I’d be interested in contributing.
@ChuanqiXu9, I saw you’d already experimented locally and were waiting for alignment, and that you’re also in the middle of other MLIR reordering / fusion-related work. If that’s keeping you busy and you’re happy for someone else to pick this up, I’d love to take a crack at it along the lines @rengolin suggested.
v1 proposal:
- Match
isElementwiselinalg.generics (after-convert-elementwise-to-linalg) whose tensor inputs aretensor.concatswith the same dim / segment sizes, preferably single-use. - Rewrite
ew(concat(a0, a1, …), concat(b0, b1, …), …) →
concat(ew(a0, b0, …), ew(a1, b1, …), …)
- Expose via a
populate*pattern and run it as a preamble to elementwise fusion (same idea as the reshape bubble patterns already in that pass)
I’ll leave reductions, linalg.index, and teach-the-fuser-to-see-through-concat for later.
Happy to start with a small pattern + lit tests on the example in the issue. Also, please drop any “please don’t do X” guidance.
Hey folks, I'm relatively new to MLIR, but this issue caught my eye and I’d be interested in contributing.
@ChuanqiXu9, I saw you’d already experimented locally and were waiting for alignment, and that you’re also in the middle of other MLIR reordering / fusion-related work. If that’s keeping you busy and you’re happy for someone else to pick this up, I’d love to take a crack at it along the lines @rengolin suggested.
v1 proposal:
- Match
isElementwiselinalg.generics (after-convert-elementwise-to-linalg) whose tensor inputs aretensor.concatswith the same dim / segment sizes, preferably single-use.- Rewrite
ew(concat(a0, a1, …), concat(b0, b1, …), …) → concat(ew(a0, b0, …), ew(a1, b1, …), …)
- Expose via a
populate*pattern and run it as a preamble to elementwise fusion (same idea as the reshape bubble patterns already in that pass)I’ll leave reductions,
linalg.index, and teach-the-fuser-to-see-through-concat for later.Happy to start with a small pattern + lit tests on the example in the issue. Also, please drop any “please don’t do X” guidance.
Hi, thanks for chime in. But given I've already have a patch locally and relatively ready to be sent, so I prefer to pick this up. I'll add you as reviewers. Hope this looks good.
@ktanishqk I sent https://github.com/llvm/llvm-project/pull/213630. You can leave comment there. And I mentioned multiple TODO there. You can work on that later if you're interested.