Repository navigation
Record narrow static M/N sizes in EncodingAttr and rationalize MaterializeEncoding for narrow shapes. - #15431
Conversation
d1f42e4 to
18a8295
Compare
dcaballe
left a comment
There was a problem hiding this comment.
Thanks! LGTM, just a minor comment.
18a8295 to
005d206
Compare
|
OK, the above benchmark results actually found an issue here, which should be fixed by the last commit here, 8d1b3df . I only realized that after writing a comment trying to rationalize why this wasn't really a regression :-P Waiting on new benchmark results, then will mark again as ready for review. |
|
Sorry for asking a dumb question, but I am not entirely clear why the set encoding needs to carry an extra information field here? What is it that it is trying to capture? (Not clear to me, since I probably dont have all the context). |
To ask a slightly better question, the encoding is meant to capture how a particular tensor value is used (like rhs of a batch_matmul, etc.). The choice of narrow_M/narrow_N does not seem to be related to that, but rather directly derived from the type of the tensor itself, unless I am missing something. |
8d1b3df to
690659d
Compare
Thanks for asking a good question! The answer is that the encoding needs to capture information about the shapes involved in the entire matmul, not just the shape of the operand that it's set as an attribute on. For example, if we have a vecmat, we have M==1, that's a property of the matmul at hand. Now if we are looking at the RHS operand of that matmul, its dimensions are N and K, the dimension M does not appear on the RHS operand at all. And yet, in order to materialize the RHS encoding, we need to know that it's a vecmat, i.e. that M==1, as we are going to choose a special dedicated tile shape for that case, with a higher K value, and K does appear in the shape of the RHS. We hadn't needed that so far as we were content to settle with not fully optimized tile selection for narrow matmuls. Then we could simply derive narrow matmul tile shapes from the general case, by truncating the general tile. Then that happened consistently for all operands, removing the need for operands to track any dimension that doesn't appear in their dims. But we are now (both motivations mentioned in the PR description) reaching the level of detail where we care about being able to select tile sizes for narrow cases independently of the general case, and that's what creates that need to track dimensions of the whole matmul, not just each operand separately. |
|
I have now updated this PR with a fix for a regression caught earlier by benchmarks. |
|
@hanhanW I have just rebased this past your #15450. There was a conflict in CPUMaterializeEncodingPass.cpp. Please check that it was properly resolved, preserving the logic you had just introduced. Also, while rebasing this, I realized that since you now introduced the ability to undo encodings, we don't need any generic fallback tile size anymore. I'm going to send a follow-up PR just replacing generic-tile by the fallback to undoing encoding. |
Ok thanks for the context! Instead of this would it be cleaner to just have a list of IntegerAttr that represent the size of the matmul? It could be |
I added tests about that, and reviewed the code. It looks good to me. I will rebase on your change and double-check for sure.
SG! |
Filed #15466 and referenced it from a TODO in LinalgExtBase.td where these are defined. |
hanhanW
left a comment
There was a problem hiding this comment.
I will take a deeper look later today, holding for now
hanhanW
left a comment
There was a problem hiding this comment.
Thanks for the waiting. The PR and idea look good to me. Just few comments about style.
An optional review comment:
I noticed that you are removing static keyword to local functions and move them to anonymous namespace. IREE follows mixed LLVM and Google style for compiler codebase. There is no convention on Google side, and there is a convention from LLVM style guide. I personally like LLVM style and the reason behind it. In LLVM, we prefer marking them static, see below. I'm not asking to clean all these things up since only few people care about it. Could you at least keep static keyword to local functions?
The problem with anonymous namespaces is that they naturally want to encourage indentation of their body, and they reduce locality of reference: if you see a random function definition in a C++ file, it is easy to see if it is marked static, but seeing if it is in an anonymous namespace requires scanning a big chunk of the file.
(I'm probably the only one who cares about it. I never see this kind of review comment in other reviews.)
|
Fwiw @hanhanW I agree with your preference of use of static over anonymous namespace. I have left similar comments as well |
|
(agreed - static is nice insurance as it makes functions more portable when refactoring and easily identifiable as compilation unit local without needing to inspect to see if it's in an anonymous namespace) |
…512-VNNI (#15525) This kernel is parametrized in N0, allowing N0==16 and N0==32. Performance on AMD Ryzen 9 7950X3D: - With N0=16: 180 Gop/s. - With N0=32: 240 Gop/s. These numbers show that there's a nice reward for going extra large, but that's also a liability for vecmat shapes whose N dimension isn't a multiple of 32. Maybe we can keep both for now. This is currently by far our fastest vecmat tile function --- it's fast even by general-matmul standards, while usually vecmat's low arithmetic intensity relegates it to lower performance levels. It shows what's possible now that we've decoupled vecmat tile shapes from general matmul tile shapes in #15431 . That 32x8 is not a truncation of a general matmul tile shape. Other element types and CPU architectures all need to get the same treatment. The idea of this kernel is to split the LHS s16 values into high and low 8-bit components to be able to use `_mm512_dpbusd_epi32`. In itself, that doesn't reduce the number of arithmetic instructions: while each now computes a 4D dot-product instead of a 2D one as in `_mm512_dpwssd_epi32`, we now need twice more of them to do separately the high and low 8bit parts of the LHS s16 values. The real benefit is that this removes the need to extend RHS u4 values to s16. Since this is a vecmat kernel, the LHS is small and the RHS is big, so it matters to avoid RHS-processing work. It's not trivial how to use `_mm512_dpbusd_epi32`, with its quirky unsigned * signed semantics. We take advantage of the fact that our u4 RHS values, when extended to u8, do not use the top bit -- so they are also interpretable as s8 values in place. So this is specific to RHS being less-than-8-bit values (it's not specific beyond that to 4bit). Meanwhile, when we split the LHS s16 values into high and low 8bit components the high 8bits are signed s8 and the low 8bit are unsigned u8. So, for each of the combinations of operands that we have to feed `_mm512_dpbusd_epi32`, we manage to find an operand order that accomodates the instruction's requirements on signednesses.
…rializeEncoding for narrow shapes. (iree-org#15431) This changes how we approach narrow matmul tile size selection (in particular, vecmat/matvec), from "we don't really care that much, so let's derive narrow tiles from general ones by just truncation", to "we actually care, at least in specific cases, about freely controlling narrow matmul tiles independently of the general wide matmul case." There are 2 immediate needs for this: @dcaballe was doing something comparable in iree-org#15421 to generally unlock better AVX-512 codegen for `f32` `vecmat`, and I have a specific need for this in iree-org#15158 for some `s16 x u4` quantized `vecmat` case. The solution proposed here is more general than the one in iree-org#15241 in that it is not only about `vecmat` and `matvec`, it supports any narrow-M / narrow-N case. Like iree-org#15241, it does so by extending `EncodingAttr` in some way. Unlike iree-org#15241, it does so by adding optional narrow-M / narrow-N integer attributes, instead of extending the `user` enum. Along the way, this rationalizes MaterializeEncoding code around tile-size selection. Narrow tile sizes are now explicitly enumerated, and the enumeration of tile sizes is now clearly decoupled from the choosing among the enumerated tile sizes. Another change made along the way: this changes the tile shape convention around here from MxKxN to MxNxK, bringing this in line with the convention in use in ukernels. The motivation for this convention is that the MxN part here is particularly important as the output tile shape, so it helps that the MxNxK convention has that as a contiguous subset. To avoid useless redundancy as the narrow-N case is almost 100% symmetrical to the narrow-M case, the enumeration only goes over narrow-M cases, and the handling of narrow-N is deferred to the choose function, transposing the problem to derive narrow-N tiles from narrow-M tiles. For `vecmat`/`matvec`, the symmetry is perfect, as the accumulator tile is 1D in these cases, there is no difference at all. For other non-vector narrow cases, there could conceivably be a difference someday motivating decoupling narrow-N from narrow-M, but this is sufficiently far-fetched that it's best to left that to be dealt with then a concrete use case arises, and enjoy the benefit of smaller code until then.
…512-VNNI (iree-org#15525) This kernel is parametrized in N0, allowing N0==16 and N0==32. Performance on AMD Ryzen 9 7950X3D: - With N0=16: 180 Gop/s. - With N0=32: 240 Gop/s. These numbers show that there's a nice reward for going extra large, but that's also a liability for vecmat shapes whose N dimension isn't a multiple of 32. Maybe we can keep both for now. This is currently by far our fastest vecmat tile function --- it's fast even by general-matmul standards, while usually vecmat's low arithmetic intensity relegates it to lower performance levels. It shows what's possible now that we've decoupled vecmat tile shapes from general matmul tile shapes in iree-org#15431 . That 32x8 is not a truncation of a general matmul tile shape. Other element types and CPU architectures all need to get the same treatment. The idea of this kernel is to split the LHS s16 values into high and low 8-bit components to be able to use `_mm512_dpbusd_epi32`. In itself, that doesn't reduce the number of arithmetic instructions: while each now computes a 4D dot-product instead of a 2D one as in `_mm512_dpwssd_epi32`, we now need twice more of them to do separately the high and low 8bit parts of the LHS s16 values. The real benefit is that this removes the need to extend RHS u4 values to s16. Since this is a vecmat kernel, the LHS is small and the RHS is big, so it matters to avoid RHS-processing work. It's not trivial how to use `_mm512_dpbusd_epi32`, with its quirky unsigned * signed semantics. We take advantage of the fact that our u4 RHS values, when extended to u8, do not use the top bit -- so they are also interpretable as s8 values in place. So this is specific to RHS being less-than-8-bit values (it's not specific beyond that to 4bit). Meanwhile, when we split the LHS s16 values into high and low 8bit components the high 8bits are signed s8 and the low 8bit are unsigned u8. So, for each of the combinations of operands that we have to feed `_mm512_dpbusd_epi32`, we manage to find an operand order that accomodates the instruction's requirements on signednesses.
This changes how we approach narrow matmul tile size selection (in particular, vecmat/matvec), from "we don't really care that much, so let's derive narrow tiles from general ones by just truncation", to "we actually care, at least in specific cases, about freely controlling narrow matmul tiles independently of the general wide matmul case."
There are 2 immediate needs for this: @dcaballe was doing something comparable in #15421 to generally unlock better AVX-512 codegen for
f32vecmat, and I have a specific need for this in #15158 for somes16 x u4quantizedvecmatcase.The solution proposed here is more general than the one in #15241 in that it is not only about
vecmatandmatvec, it supports any narrow-M / narrow-N case. Like #15241, it does so by extendingEncodingAttrin some way. Unlike #15241, it does so by adding optional narrow-M / narrow-N integer attributes, instead of extending theuserenum.Along the way, this rationalizes MaterializeEncoding code around tile-size selection. Narrow tile sizes are now explicitly enumerated, and the enumeration of tile sizes is now clearly decoupled from the choosing among the enumerated tile sizes.
Another change made along the way: this changes the tile shape convention around here from MxKxN to MxNxK, bringing this in line with the convention in use in ukernels. The motivation for this convention is that the MxN part here is particularly important as the output tile shape, so it helps that the MxNxK convention has that as a contiguous subset.
To avoid useless redundancy as the narrow-N case is almost 100% symmetrical to the narrow-M case, the enumeration only goes over narrow-M cases, and the handling of narrow-N is deferred to the choose function, transposing the problem to derive narrow-N tiles from narrow-M tiles. For
vecmat/matvec, the symmetry is perfect, as the accumulator tile is 1D in these cases, there is no difference at all. For other non-vector narrow cases, there could conceivably be a difference someday motivating decoupling narrow-N from narrow-M, but this is sufficiently far-fetched that it's best to left that to be dealt with then a concrete use case arises, and enjoy the benefit of smaller code until then.