Repository navigation
[spirv] Add patterns for folding processor ID uses - #3877
Conversation
For processor IDs we have their upper bound in the workgroup count function or SPIR-V entry point ABI, which can be used to fold processor ID uses where possible. This commit add a pattern to fold affine.min ops using processor IDs. It allows us to get static constant value, which can then be used for memref subviews.
| /// affine.min affine_map<()[s0] -> (3, s0 * -2 + 225)>()[%id] | ||
| /// ``` | ||
| /// if the upper bound for thread ID along the x dimension is 112. | ||
| struct FoldAffineMinOverProcessorID : OpRewritePattern<AffineMinOp> { |
There was a problem hiding this comment.
Couple of issues I see with this pattern and adding it as a general pass.
In isolation replacing the block ID with its maximum value is wrong cause you will actually change the value of the affine.min. So this doesnt seem to be a legal transformation.
Further, you are substituting the upper bound for the thread ID. In the example above if the affine map were
affine_map<()[s0] -> (3, 225 - s0)>()[%id]
would that approach still hold.
IIUC the reason why we need this "canonicalization" is that the value of this affine map is used to compute the size of the vector needed (it also comes up during promotion cause it determines the size of the workgroup memory buffer). So you are trying to get a constant "over-estimate" of the size. I think we need to account for the uses of the affine.min operation, and also think a more general solution needs a more involved analysis of the AffineExpr in the result.
Hopefully I am not totally on the wrong track here. Please let me know.
There was a problem hiding this comment.
Good catch! There are indeed issues regarding linear expression check and how we check whether a value should be the min.
If an expression involving the processor ID is linear, then the expression is in the range we get by substituting the processor ID with its min and max. However, I should only check existing constant result in the original map. If that constant is <= the range gotten from processor ID and other constants in the original map, then the constant should be min.
Fixed check around linear expression and min candidates.
There was a problem hiding this comment.
Ok, looking through this again, the logic seems fine and its find to add this by default.
Overall though this is still very specific and is good to make forward progress. There is a more general solution that gets to the root cause of the issue that might catch more cases, especially when problem sizes are dynamic. Adding this here for posterity.
The affine.min in question is coming from tiling where the tile size does not exactly divide the problem size. So for example if the original loop where (using C syntax)
for (i = 0; i < N; i++) {
}
when tiled you get
for (i = 0; i < N; i+= TS) {
v = min(TS, N - i * TS);
}
v is effectively the "size" of the partial tiles that exist at the edge of the iteration space. This value is then used to determine the sizes of the subviews along the tiled dimension. For things like vectorization and promotion we want the sizes to be a compile-time constant. So TS is a compile time constant. Its OK to "over-estimate" the size for promotion or vectorization. You are not really over-estimating here. All you want to do is say the size of the vector operation and workgroup memory used in the partial tile is same as the other "full tiles", i.e TS. N - i * TS is always less than or equal to TS. So this is just some analysis needed to get the "true size" we need. For the case of tile+distribute N is same as number of processors and TS is the tile size (which for many cases is just the workgroup size but needn't be).
So the solution is that when we want to estimate size of something (during vectorization or promotion) we handle the affine.min correctly.
This might be working fine for the cases you have. My worry here is that it is only working since you have static shapes, and the TS exactly divides the problem size. Take some concrete values of N = 10, TS = 4. Then the range of the second expression in the min above can take the values [2, 9]. Then the check here finds that the constant value (TS) is not the minimum value for the expression and the affine.min stays as is.
There was a problem hiding this comment.
Yup, exactly. This only handles perfectly divisible cases, which are what I'm trying to handle at the moment. :)
So the solution is that when we want to estimate size of something (during vectorization or promotion) we handle the affine.min correctly.
Not sure I follow exactly here. Could you explain a bit more? These affine.min ops are feeding into subview ops. Without being static, we cannot vectorize. I guess we can explicitly throw in patterns that takes in, e.g., linalg.conv on memrefs with dynamic dimensions, and first "over-estimate" by treating the dynamic dimensions caused by affine.min as always of size TS, and then, just for the boundary tile perform scalar load/store and compose vectors by ourselves. That's something I'd like to explore. Is that what you meant?
But the above can still work with the current folding pattern. If this pattern kicks in we don't need all the complicated analysis in the above. So we can have both. :)
There was a problem hiding this comment.
So the solution is that when we want to estimate size of something (during vectorization or promotion) we handle the affine.min correctly.
Not sure I follow exactly here. Could you explain a bit more? These
affine.minops are feeding into subview ops. Without being static, we cannot vectorize. I guess we can explicitly throw in patterns that takes in, e.g.,linalg.convon memrefs with dynamic dimensions, and first "over-estimate" by treating the dynamic dimensions caused byaffine.minas always of sizeTS, and then, just for the boundary tile perform scalar load/store and compose vectors by ourselves. That's something I'd like to explore. Is that what you meant?
- The
affine.minops are feeding into the size operands of the subview. These subviews are either operands of the linalg operation being vectorized, or are the subviews that are promoted. So it is better to keep them static. I think what we need is to use sizeTSfor the boundary tiles also. You might overcompute/use more workgroup memory. So we dont need more patterns I think. We just want to make sure we get the sizeTSfor ALL tiles.
There was a problem hiding this comment.
Awesome, I think we are aligned over the general direction here; can figure details out when needed. :)
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Thanks! Just a few nits (and one main comment that is not really an AI)
| /// affine.min affine_map<()[s0] -> (3, s0 * -2 + 225)>()[%id] | ||
| /// ``` | ||
| /// if the upper bound for thread ID along the x dimension is 112. | ||
| struct FoldAffineMinOverProcessorID : OpRewritePattern<AffineMinOp> { |
There was a problem hiding this comment.
Ok, looking through this again, the logic seems fine and its find to add this by default.
Overall though this is still very specific and is good to make forward progress. There is a more general solution that gets to the root cause of the issue that might catch more cases, especially when problem sizes are dynamic. Adding this here for posterity.
The affine.min in question is coming from tiling where the tile size does not exactly divide the problem size. So for example if the original loop where (using C syntax)
for (i = 0; i < N; i++) {
}
when tiled you get
for (i = 0; i < N; i+= TS) {
v = min(TS, N - i * TS);
}
v is effectively the "size" of the partial tiles that exist at the edge of the iteration space. This value is then used to determine the sizes of the subviews along the tiled dimension. For things like vectorization and promotion we want the sizes to be a compile-time constant. So TS is a compile time constant. Its OK to "over-estimate" the size for promotion or vectorization. You are not really over-estimating here. All you want to do is say the size of the vector operation and workgroup memory used in the partial tile is same as the other "full tiles", i.e TS. N - i * TS is always less than or equal to TS. So this is just some analysis needed to get the "true size" we need. For the case of tile+distribute N is same as number of processors and TS is the tile size (which for many cases is just the workgroup size but needn't be).
So the solution is that when we want to estimate size of something (during vectorization or promotion) we handle the affine.min correctly.
This might be working fine for the cases you have. My worry here is that it is only working since you have static shapes, and the TS exactly divides the problem size. Take some concrete values of N = 10, TS = 4. Then the range of the second expression in the min above can take the values [2, 9]. Then the check here finds that the constant value (TS) is not the minimum value for the expression and the affine.min stays as is.
| /// affine.min affine_map<()[s0] -> (3, s0 * -2 + 225)>()[%id] | ||
| /// ``` | ||
| /// if the upper bound for thread ID along the x dimension is 112. | ||
| struct FoldAffineMinOverProcessorID : OpRewritePattern<AffineMinOp> { |
For processor IDs we have their upper bound in the workgroup
count function or SPIR-V entry point ABI, which can be used
to fold processor ID uses where possible.
This commit add a pattern to fold affine.min ops using processor
IDs. It allows us to get static constant value, which can then
be used for memref subviews.