You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[CPU][ARM] DT+UK 50x slower than Default flags on BF16 on Android #15504
This is probably a case where having a --iree-print-compilation-statistics would be enough to spot the problem without actually running anything. The flag should show information about the dispatches created, the ops per dispatch, if the ops were mapped to ukernels or not, if the ops were vectorized or note, the tile sizes used, etc., etc., etc.
that seems like a really hard flag to implement - can you get this from existing sources we have instead? --iree-hal-dump-executable-sources-to= and co should have what you need, combined with grep you can find whether ukernels were used, etc
pass statistics would be the other route and is built-in to mlir - the ukernel conversion pass, for example, can have statistics for how many replacements it made
I haven't thought much about the implementation. I've been using --iree-hal-dump-executable-sources-to, which has been super helpful, but only when I already knew what I was looking for. I think that + grep are not powerful enough. Sometimes I have to generate a profile and copy it to a spreadsheet and process the data there with filters, etc. Not very efficient, unfortunately.
LLVM has some pass report mechanism (from the top of my mind, rpass-something flags?) that dumps useful information about what happens during the compilation. Apple built a tool on top of it to process and summarize that information, map it to the source code, etc. Really powerful. ICC has also a good performance report along these lines. I don't think we have the resources to do something that awesome but I think we need some improvements here.
I think @qcolombet was interested in exploring this area. He may have more context about the Apple's work and LLVM side of things.
(sorry, I just realized I'm kind of hijacking the thread. Perhaps we should move this to another issue/thread?)
I can get the answer from looking at the graph myself later, but maybe you know the answer out of hand: are the matmul element types in this model bf16 * bf16 -> bf16 (so the accumulator is bf16) or bf16 * bf16 -> f32 (so the accumulator is f32) ?
I hadn't bothered to add a microkernel case for bf16 * bf16 -> bf16 because I thought that the narrow mantissa would prevent bf16 from being a useful accumulator type, but I can easily tweak the existing bf16 * bf16 -> f32 to also support that.
So this would currently cause the microkernel to fall back to slow generic code. I'll fix that to avoid causing benchmarking friction, but again, unless this comes from a real user who already knows that this is sufficiently accurate, I suspect that bf16 accumulation is going to suffer from the narrow mantissa.
Note @mariecwhite@dcaballe : earlier draft versions of #15543 had debug logic left in that was preventing the optimized tile function from being selected. That's fixed now, but if you tested earlier and got no performance impact, that would have been the reason.
Trying to understand the situation... If the optimized function is not selected, what happens then? Are we trying to generate code for this using the selected tile sizes or are we selecting some other ukernel?
When the arch specific tile function selection returns nullptr, as it was
doing in my leftover debug helper code, that causes fallback to a slow
generic tile function. So that was not broken, just slow like before.
Anyway, that's fixed now.
Le ven. 10 nov. 2023 17 h 05, Diego Caballero ***@***.***> a
écrit :
When the arch specific tile function selection returns nullptr, as it was
doing in my leftover debug helper code, that causes fallback to a slow
generic tile function. So that was not broken, just slow like before.
Anyway, that's fixed now.
Thanks for clarifying. I'm still not sure I'm getting the full picture so let follow up with another question: beyond this bug, why are we invoking a generic but slow ukernel when a good one hasn't been found instead of just falling back to non-ukernel code generation? Is there a reason not to do that?
This is a really good question! I can answer why things are the way they currently are, and why I can't see any low-hanging fruit from here. But that doesn't mean that the question isn't valid or that there isn't potentially some high-hanging fruit.
The one-line answer is that when at compile time (codegen) we would need to decide to fall back to non-ukernel, we don't have yet any information about the ukernel's internal logic, so we don't know if it's going to be a fast code path or a slow generic fallback.
There is no very easy way to fix that in a way that is 100% robust. There are different trade-offs here between "100% robust, but complex to implement" and "Has false negatives, but is lighter implementation". Here are just 2 possible points on the spectrum of compromises here:
In order to guarantee eliminating discrepancy between compile-time logic ("should we fall back to non-ukernel") and ukernel implementation logic ("should I fall back to a slow generic tile function"), we would need the code implementing that logic to be shared among both sides. It's not impossible to do - technically, the compiler itself could link to some ukernel code. And actually, now that I think about it, the logic that is relevant here is already isolated into separate "*_entry_point.c" files that are compiled as generic C files, not with the architecture-specific CPU feature flags that we add to the actual tile function implementations. Concretely, it's not impossible to imagine the compiler linking to https://github.com/openxla/iree/blob/main/runtime/src/iree/builtins/ukernel/arch/x86_64/mmt4d_x86_64_entry_point.c.
An imperfect but easier way to catch this is to maintain only a grid of ukernel x element_types x CPU architecture (the latter being coarse-grained e.g. just "x86_64" not going into specific CPU features). This would be simple enough to fit in a declarative way in the https://github.com/openxla/iree/blob/main/runtime/src/iree/builtins/ukernel/exported_bits.h header file that is the one piece of code that we are already today sharing between the compiler and the ukernels. It wouldn't catch fine-grained things such as "on x86_64, we support bf16 * bf16 -> bf16 only if the +avx512bf16 feature is present. But it would have caught the simpler thing that was at stake here, which was that we didn't have any t bf16 * bf16 -> bf16 at all, and it would still have caught it if it were on a per-arch basis (e.g. x86_64 vs arm64).
Activity
hanhanW commentedon Nov 9, 2023
I was thinking to take an initial look, but I don't have pixels now...
dcaballe commentedon Nov 9, 2023
This is probably a case where having a
--iree-print-compilation-statisticswould be enough to spot the problem without actually running anything. The flag should show information about the dispatches created, the ops per dispatch, if the ops were mapped to ukernels or not, if the ops were vectorized or note, the tile sizes used, etc., etc., etc.I think I'll create an issue for that.
benvanik commentedon Nov 9, 2023
that seems like a really hard flag to implement - can you get this from existing sources we have instead? --iree-hal-dump-executable-sources-to= and co should have what you need, combined with grep you can find whether ukernels were used, etc
benvanik commentedon Nov 9, 2023
pass statistics would be the other route and is built-in to mlir - the ukernel conversion pass, for example, can have statistics for how many replacements it made
ScottTodd commentedon Nov 9, 2023
+1 to using pass statistics, possibly with grep or a helper script to summarize the output. There are some grep recipes here: #6161 (comment)
Some other PRs to reference:
dcaballe commentedon Nov 9, 2023
I haven't thought much about the implementation. I've been using
--iree-hal-dump-executable-sources-to, which has been super helpful, but only when I already knew what I was looking for. I think that + grep are not powerful enough. Sometimes I have to generate a profile and copy it to a spreadsheet and process the data there with filters, etc. Not very efficient, unfortunately.LLVM has some pass report mechanism (from the top of my mind,
rpass-something flags?) that dumps useful information about what happens during the compilation. Apple built a tool on top of it to process and summarize that information, map it to the source code, etc. Really powerful. ICC has also a good performance report along these lines. I don't think we have the resources to do something that awesome but I think we need some improvements here.I think @qcolombet was interested in exploring this area. He may have more context about the Apple's work and LLVM side of things.
(sorry, I just realized I'm kind of hijacking the thread. Perhaps we should move this to another issue/thread?)
mariecwhite commentedon Nov 10, 2023
Just tried the first model with ukernels disabled but data tiling enabled and it's a lot faster:
mariecwhite commentedon Nov 10, 2023
Here is the trace: https://storage.googleapis.com/iree-shared-files/mariewhite/profiles/bert_bf16.tracy
bjacob commentedon Nov 10, 2023
I can get the answer from looking at the graph myself later, but maybe you know the answer out of hand: are the matmul element types in this model
bf16 * bf16 -> bf16(so the accumulator isbf16) orbf16 * bf16 -> f32(so the accumulator isf32) ?I hadn't bothered to add a microkernel case for
bf16 * bf16 -> bf16because I thought that the narrow mantissa would preventbf16from being a useful accumulator type, but I can easily tweak the existingbf16 * bf16 -> f32to also support that.bjacob commentedon Nov 10, 2023
Just checked the IR, confirmed: while the model has a mix of
bf16andf32, the matmuls seem to bebf16 * bf16 -> bf16.... %248 = stablehlo.dot_general %245, %5, contracting_dims = [2] x [0], precision = [DEFAULT, DEFAULT] : (tensor<1x8x768xbf16>, tensor<768x768xbf16>) -> tensor<1x8x768xbf16> ... %252 = stablehlo.dot_general %245, %7, contracting_dims = [2] x [0], precision = [DEFAULT, DEFAULT] : (tensor<1x8x768xbf16>, tensor<768x768xbf16>) -> tensor<1x8x768xbf16> ... %256 = stablehlo.dot_general %245, %9, contracting_dims = [2] x [0], precision = [DEFAULT, DEFAULT] : (tensor<1x8x768xbf16>, tensor<768x768xbf16>) -> tensor<1x8x768xbf16>So this would currently cause the microkernel to fall back to slow generic code. I'll fix that to avoid causing benchmarking friction, but again, unless this comes from a real user who already knows that this is sufficiently accurate, I suspect that
bf16accumulation is going to suffer from the narrow mantissa.bjacob commentedon Nov 10, 2023
#15543 should fix this!
bjacob commentedon Nov 10, 2023
Note @mariecwhite @dcaballe : earlier draft versions of #15543 had debug logic left in that was preventing the optimized tile function from being selected. That's fixed now, but if you tested earlier and got no performance impact, that would have been the reason.
dcaballe commentedon Nov 10, 2023
Trying to understand the situation... If the optimized function is not selected, what happens then? Are we trying to generate code for this using the selected tile sizes or are we selecting some other ukernel?
bjacob commentedon Nov 10, 2023
dcaballe commentedon Nov 11, 2023
Thanks for clarifying. I'm still not sure I'm getting the full picture so let follow up with another question: beyond this bug, why are we invoking a generic but slow ukernel when a good one hasn't been found instead of just falling back to non-ukernel code generation? Is there a reason not to do that?
bjacob commentedon Nov 13, 2023
@dcaballe
This is a really good question! I can answer why things are the way they currently are, and why I can't see any low-hanging fruit from here. But that doesn't mean that the question isn't valid or that there isn't potentially some high-hanging fruit.
The one-line answer is that when at compile time (codegen) we would need to decide to fall back to non-ukernel, we don't have yet any information about the ukernel's internal logic, so we don't know if it's going to be a fast code path or a slow generic fallback.
There is no very easy way to fix that in a way that is 100% robust. There are different trade-offs here between "100% robust, but complex to implement" and "Has false negatives, but is lighter implementation". Here are just 2 possible points on the spectrum of compromises here:
ukernel x element_types x CPU architecture(the latter being coarse-grained e.g. just "x86_64" not going into specific CPU features). This would be simple enough to fit in a declarative way in the https://github.com/openxla/iree/blob/main/runtime/src/iree/builtins/ukernel/exported_bits.h header file that is the one piece of code that we are already today sharing between the compiler and the ukernels. It wouldn't catch fine-grained things such as "on x86_64, we supportbf16 * bf16 -> bf16only if the+avx512bf16feature is present. But it would have caught the simpler thing that was at stake here, which was that we didn't have any tbf16 * bf16 -> bf16at all, and it would still have caught it if it were on a per-arch basis (e.g. x86_64 vs arm64).ukernels: add `bf16 * bf16 -> bf16` optimized tile functions for x86 …
bjacob commentedon Nov 14, 2023
#15543 has been merged finally :-) @mariecwhite can you retry and hopefully confirm it fixed this?
dcaballe commentedon Nov 15, 2023
Thanks for clarifying, Benoit. This is one of the things I would like to discuss for the UK enablement
mariecwhite commentedon Nov 21, 2023
This is fixed. Thank you!
ukernels: add `bf16 * bf16 -> bf16` optimized tile functions for x86 …