Skip to content

Record narrow static M/N sizes in EncodingAttr and rationalize MaterializeEncoding for narrow shapes. - #15431

Merged
bjacob merged 3 commits into
iree-org:mainfrom
bjacob:encoding-narrow
Nov 8, 2023
Merged

bjacob merged 3 commits into
iree-org:mainfrom
bjacob:encoding-narrow

Conversation

@bjacob

@bjacob bjacob commented Nov 6, 2023 •

Copy link
Copy Markdown
Collaborator

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 f32 vecmat, and I have a specific need for this in #15158 for some s16 x u4 quantized vecmat case.

The solution proposed here is more general than the one in #15241 in that it is not only about vecmat and matvec, it supports any narrow-M / narrow-N case. Like #15241, it does so by extending EncodingAttr in some way. Unlike #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.

@bjacob bjacob added benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:android-cpu Run default Android CPU benchmarks labels Nov 6, 2023
@github-actions

github-actions Bot commented Nov 6, 2023 •

Copy link
Copy Markdown

Abbreviated Benchmark Summary

@ commit f81ea7ff5d7db41289625a055ae50a37c6cb6a08 (vs. base 38bfdbae7e94e57d932ca740b42589ea40d85196)

Regressed Latencies 🚩

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
MobileBertSquad\_int8(tflite) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][default-flags] local\_task(embedded\_elf)[8-thread,full-inference,default-flags] with default @ c2-standard-16[cpu] 140.859 (vs. 115.159, 22.32%↑) 141.956 18.124
MobileNetV2\_fp32(tflite) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][default-flags] local\_task(embedded\_elf)[1-thread,full-inference,default-flags] with default @ c2-standard-16[cpu] 14.714 (vs. 12.675, 16.09%↑) 14.751 0.174
DeepLabV3\_fp32(tflite) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,data-tiling,ukernel] local\_task(embedded\_elf)[8-thread,full-inference,default-flags] with default @ c2-standard-16[cpu] 13.112 (vs. 11.523, 13.79%↑) 13.129 0.045

[Top 3 out of 5 results showed]

Improved Latencies 🎉

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
MiniLML12H384Uncased(stablehlo) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][default-flags] local\_task(embedded\_elf)[8-thread,full-inference,default-flags] with default @ c2-standard-16[cpu] 19.102 (vs. 22.274, 14.24%↓) 19.088 0.136

Regressed Total Dispatch Sizes 🚩

Benchmark Name Total Dispatch Size (bytes)
GPT2\_117M\_TF\_1X1XI32(stablehlo) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,data-tiling,ukernel,compile-stats] 41568 (vs. 39376, 5.57%↑)

Improved Total Dispatch Sizes 🎉

Benchmark Name Total Dispatch Size (bytes)
BertLargeTF(stablehlo) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,data-tiling,ukernel,compile-stats] 85256 (vs. 91848, 7.18%↓)
MobileBertSquad\_fp32(tflite) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,data-tiling,ukernel,compile-stats] 99016 (vs. 105592, 6.23%↓)

For more information:

Source Workflow Run

@dcaballe dcaballe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM, just a minor comment.

@bjacob bjacob removed benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:android-cpu Run default Android CPU benchmarks labels Nov 7, 2023
@bjacob
bjacob marked this pull request as ready for review November 7, 2023 15:46
@bjacob
bjacob marked this pull request as draft November 7, 2023 16:01
@bjacob bjacob added benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:android-cpu Run default Android CPU benchmarks labels Nov 7, 2023
@bjacob

bjacob commented Nov 7, 2023

Copy link
Copy Markdown
Collaborator Author

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.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

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).

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

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.

@bjacob bjacob removed benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:android-cpu Run default Android CPU benchmarks labels Nov 7, 2023
@bjacob

bjacob commented Nov 7, 2023

Copy link
Copy Markdown
Collaborator Author

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.

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.

@bjacob

bjacob commented Nov 7, 2023

Copy link
Copy Markdown
Collaborator Author

I have now updated this PR with a fix for a regression caught earlier by benchmarks.
The updated benchmark results are OK. The latency fluctuations are noise. The minor dispatch size changes are OK.

@bjacob
bjacob marked this pull request as ready for review November 7, 2023 18:04
@bjacob

bjacob commented Nov 7, 2023

Copy link
Copy Markdown
Collaborator Author

@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.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

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.

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.

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 ShapedType::kDynamic for dynamic sizes (which looks ugly while printing, but that could be fixed if we need to and is cosmetic).

@hanhanW

hanhanW commented Nov 7, 2023

Copy link
Copy Markdown
Contributor

@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.

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.

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.

SG!

@bjacob

bjacob commented Nov 7, 2023

Copy link
Copy Markdown
Collaborator Author

@MaheshRavishankar

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 ShapedType::kDynamic for dynamic sizes (which looks ugly while printing, but that could be fixed if we need to and is cosmetic).

Filed #15466 and referenced it from a TODO in LinalgExtBase.td where these are defined.

@hanhanW hanhanW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will take a deeper look later today, holding for now

@hanhanW hanhanW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

https://llvm.org/docs/CodingStandards.html#anonymous-namespaces

(I'm probably the only one who cares about it. I never see this kind of review comment in other reviews.)

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

Fwiw @hanhanW I agree with your preference of use of static over anonymous namespace. I have left similar comments as well

@benvanik

benvanik commented Nov 8, 2023

Copy link
Copy Markdown
Collaborator

(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)

@bjacob
bjacob enabled auto-merge (squash) November 8, 2023 02:59
@bjacob
bjacob merged commit 7ab3509 into iree-org:main Nov 8, 2023
bjacob added a commit that referenced this pull request Nov 10, 2023
…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.
ramiro050 pushed a commit to ramiro050/iree that referenced this pull request Dec 19, 2023
…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.
ramiro050 pushed a commit to ramiro050/iree that referenced this pull request Dec 19, 2023
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants