Investigate the ability of `linalg.elementwise` to hold more than one internal operation
linalg.elementwise could allow multiple operations for simpler fusion:
// Before fusion
%exp = linalg.elementwise <exp> ins(%arg0)
%add = linalg.elementwise <add> ins(%exp, %arg1) // %exp is _chain_
%abs = linalg.elementwise <abs> ins(%add) // %add is _chain_
%select = linalg.elementwise <select> ins(%arg2, %abs, %arg3) // %abs is _chain_
// After fusion
%select = linalg.elementwise <exp, add, abs, select> ins(%arg0, %arg1, %arg2, %arg3)
(see #221194 for the syntax above)
The argument tree is guaranteed to be unique. The first operation takes in only from arguments (as many as required). Subsequent operations take the chain from the previous operation as the first operand and the following arguments when necessary. select is different because the first operand is the mask, so the chain goes into the second operand.
Fusion may not work IF not every operation in the elementwise sequence:
- is in canonical form: chain as the first argument.
- takes extra operands from arguments and not the chain. [1]
- has compatible projected permutation affine maps.
[1] %select = linalg.elementwise <select> ins(%arg2, %abs, %add) does not form a tree.
Partial fusion should be encouraged. Elementwise fusion algorithms should scan the chain until it can no longer fuse. Greedy algorithms should find as many "fusion bubbles" as possible in a given IR region. Further transforms and canonicalization of remaining operations may encourage nearby "bubbles" to fuse together.
Partially splitting a fused elementwise can also be done trivially by separating the arguments and creating two smaller fused operations (or a single operation in case of peeling).
@llvm/issue-subscribers-mlir
Author: Renato Golin (rengolin)
// After fusion %select = linalg.elementwise <exp, add, abs, select> ins(%arg0, %arg1, %arg2, %arg3)
_(see #<!-- -->221194 for the syntax above)_
The argument tree is guaranteed to be unique. The first operation takes in _only_ from arguments (as many as required). Subsequent operations take the _chain_ from the previous operation as the first operand and the following arguments when necessary. `select` is different because the first operand is the mask, so the _chain_ goes into the second operand.
Fusion may not work IF **not every operation** in the elementwise sequence:
* is in _canonical form_: chain as the first argument.
* takes extra operands from arguments and **not** the _chain_. [1]
* has compatible projected permutation affine maps.
_[1] `%select = linalg.elementwise <select> ins(%arg2, %abs, %add)` does not form a tree._
Partial fusion should be encouraged. Elementwise fusion algorithms should scan the chain until it can no longer fuse. Greedy algorithms should find as many _"fusion bubbles"_ as possible in a given IR region. Further transforms and canonicalization of remaining operations may encourage nearby _"bubbles"_ to fuse together.
Partially splitting a fused elementwise can also be done trivially by separating the arguments and creating two smaller fused operations (or a single operation in case of peeling).
</details>
@llvm/issue-subscribers-mlir-linalg
Author: Renato Golin (rengolin)
// After fusion %select = linalg.elementwise <exp, add, abs, select> ins(%arg0, %arg1, %arg2, %arg3)
_(see #<!-- -->221194 for the syntax above)_
The argument tree is guaranteed to be unique. The first operation takes in _only_ from arguments (as many as required). Subsequent operations take the _chain_ from the previous operation as the first operand and the following arguments when necessary. `select` is different because the first operand is the mask, so the _chain_ goes into the second operand.
Fusion may not work IF **not every operation** in the elementwise sequence:
* is in _canonical form_: chain as the first argument.
* takes extra operands from arguments and **not** the _chain_. [1]
* has compatible projected permutation affine maps.
_[1] `%select = linalg.elementwise <select> ins(%arg2, %abs, %add)` does not form a tree._
Partial fusion should be encouraged. Elementwise fusion algorithms should scan the chain until it can no longer fuse. Greedy algorithms should find as many _"fusion bubbles"_ as possible in a given IR region. Further transforms and canonicalization of remaining operations may encourage nearby _"bubbles"_ to fuse together.
Partially splitting a fused elementwise can also be done trivially by separating the arguments and creating two smaller fused operations (or a single operation in case of peeling).
</details>
More thoughts...
The DAG example above:
%exp = linalg.elementwise <exp> ins(%arg0)
%add = linalg.elementwise <add> ins(%exp, %arg1) // %exp is _chain_
%abs = linalg.elementwise <abs> ins(%add) // %add is _chain_
%select = linalg.elementwise <select> ins(%arg2, %abs, %add) // %abs is _chain_, %add is not
Even though perfectly fine in a generic loop, cannot be entirely fused elementwise, due to not forming a chain (and making it hard to build the argument validation). Partial fusion also cannot happen, unless both %add and %abs can be returned:
// Partial fusion
%add, %abs = linalg.elementwise <exp, add, abs> ins(%arg0, %arg1)
// Uses both
%select = linalg.elementwise <select> ins(%arg2, %abs, %add)
But returning intermediate values forces a copy to be made between operations inside the operation's body, which counters fusion benefits. While a generic body could have intermediate values, they'd be held in an extra register between creation and last usage, which increases register pressure and can be hard to cost model from a higher (elementwise) level. At this level, however, the temporaries would likely become entire tensors (even if just tiles).
So, in that case, the only (partial) fusion that can happen in between the two first operations:
%add = linalg.elementwise <exp, add> ins(%arg0, %arg1)
%abs = linalg.elementwise <abs> ins(%add)
%select = linalg.elementwise <select> ins(%arg2, %abs, %add)
Which makes the multiple-users property clear on %add and plain bufferization can create copies at the appropriate place without further fusion considerations.
With the objective to make cost modeling simple from a graph level (contractions/elementwise/reduction/transform operations), we should have "elementwise fusion" slightly separate than "generic fusion" (hopefully using the same code).
Good investigation Renato. From time to time I have reflected on the need for something like this too.
There are more than one gap between 'single elementwise' that exists today to the composite one such as above.
For instance, linalg.elementwise
For instance, linalg.elementwise where the output of one has to flow into the first argument of the second (otherwise a-b and b-a cannot be be dis-ambiguated).
Exactly! That's the tree-shape I mention that we need to match to be valid.
This works fine for this tree IR:
%exp = linalg.elementwise <exp> ins(%arg0)
%add = linalg.elementwise <add> ins(%exp, %arg1)
%abs = linalg.elementwise <abs> ins(%add)
%select = linalg.elementwise <select> ins(%arg2, %abs, %arg3)
tree:
%arg0
│
▼
%exp
│
│ %arg1
│ │
└────┬─────┘
▼
%add
│
│
│
└────┬
▼
%abs
│
│ %arg2 %arg3
│ │ │
└────┬──┘───┘
▼
%select
But not so much for the DAG IR:
%exp = linalg.elementwise <exp> ins(%arg0)
%add = linalg.elementwise <add> ins(%exp, %arg1)
%abs = linalg.elementwise <abs> ins(%add)
%select = linalg.elementwise <select> ins(%arg2, %abs, %exp)
dag:
%arg0 %arg1
│ │
▼ ▼
%exp ──────────────> %add
│ │
│ ▼
│ %abs
│ │
└────────┬─────────┘
│
▼
%select <──── %arg2
│
▼
(result)
There is no %arg3, so if we lower the DAG one to:
%select = linalg.elementwise <exp, add, abs, select> ins(%arg0, %arg1, %arg2)
We can't ungroup/generalize, because we don't know where the arguments for select come from: %exp, %add or %abs.
Writing this reply, I also identify some actual tree structures that are still undefined, so we need to be more restrictive than just allowing any tree. It has to have the three properties listed in the initial post.
The question is: how much this actually helps in actual pattern rewrites. Especially focused in pre/post op fusion, and a mix of elementwise and reduction patterns.
Writing this reply, I also identify some actual tree structures that are still undefined, so we need to be more restrictive than just allowing any tree. It has to have the three properties listed in the initial post.
The main property is to know what is the chain. If there is only one linear chain, the reconstruction fidelity is complete. If there can be more than one chain, then the combinations increase with the number of ops.
Example:
%res = linalg.elementwise <add, sub, mul> ins(%0, %1, %2, %3)
Could be:
%0 %1
\ /
add %2
\ /
sub %3
\ /
mul
|
%res
or
%0 %1 %2 %3
\ / \ /
add sub
\ /
mul
|
%res
Which now may make the whole effort too restrictive to be useful in the wild... :/