Skip to content

[CPU][ARM] DT+UK 50x slower than Default flags on BF16 on Android #15504

Closed

Description

@mariecwhite

What happened?

name threads Default flags latency (ms) DT+UK latency (ms)
BERT_BASE_BF16_JAX_I32_SEQLEN8 1 69.1 3963.0
BERT_BASE_BF16_JAX_I32_SEQLEN32 1 172.0 15964.0
BERT_BASE_BF16_JAX_I32_SEQLEN64 1 326.0 32015.0

Steps to reproduce your issue

  1. Download MLIR: https://storage.googleapis.com/iree-model-artifacts/jax/jax_models_0.4.19_1698302455/BERT_BASE_BF16_JAX_I32_SEQLEN8/stablehlo.mlirbc

  2. Compile:

ARM_CPU_FEATURES="+v9a,+fullfp16,fp-armv8,+neon,+aes,+sha2,+crc,+lse,+rdm,+complxnum,+rcpc,+sha3,+sm4,+dotprod,+fp16fml,+dit,+flagm,+ssbs,+sb,+altnzcv,+fptoint,+bf16,+i8mm,+bti"

iree-compile stablehlo.mlirbc \
    --iree-hal-target-backends="llvm-cpu" \
    --iree-input-type="stablehlo" \
    --iree-llvmcpu-link-embedded=false \
    --iree-input-demote-f64-to-f32=false \
    --iree-input-demote-i64-to-i32=false \
    --iree-llvmcpu-target-cpu-features="${ARM_CPU_FEATURES}" \
    --iree-llvmcpu-target-triple="aarch64-none-linux-android34" \
    # For DT+UK.
    --iree-opt-data-tiling \
    --iree-llvmcpu-enable-microkernels \
    -o module.vmfb
  1. Run on device
iree-benchmark-module --module=module.vmfb --task_topology_cpu_ids=0 --device=local-task --function=main --input=1x8xi32=0 --input=1x8xi32=0

What component(s) does this issue relate to?

Compiler

Version information

675aafb

Additional context

No response

Activity

  1. added
    bug 🐞
    on Nov 9, 2023
  2. added
    performance ⚡
    on Nov 9, 2023
  3. assigned and unassigned on Nov 9, 2023
  4. hanhanW commented on Nov 9, 2023

    @hanhanW
    Contributor

    I was thinking to take an initial look, but I don't have pixels now...

  5. dcaballe commented on Nov 9, 2023

    @dcaballe
    Contributor

    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.

    I think I'll create an issue for that.

  6. benvanik commented on Nov 9, 2023

    @benvanik
    Collaborator

    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

  7. benvanik commented on Nov 9, 2023

    @benvanik
    Collaborator

    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

  8. ScottTodd commented on Nov 9, 2023

    @ScottTodd
    Member

    +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:

  9. dcaballe commented on Nov 9, 2023

    @dcaballe
    Contributor

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

  10. mariecwhite commented on Nov 10, 2023

    @mariecwhite
    ContributorAuthor

    Just tried the first model with ukernels disabled but data tiling enabled and it's a lot faster:

    name threads Default flags latency (ms) DT latency (ms) DT+UK latency (ms)
    BERT_BASE_BF16_JAX_I32_SEQLEN8 1 69.1 23.5 3963.0
  11. bjacob commented on Nov 10, 2023

    @bjacob
    Collaborator

    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.

  12. bjacob commented on Nov 10, 2023

    @bjacob
    Collaborator

    Just checked the IR, confirmed: while the model has a mix of bf16 and f32, the matmuls seem to be bf16 * 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 bf16 accumulation is going to suffer from the narrow mantissa.

  13. bjacob commented on Nov 10, 2023

    @bjacob
    Collaborator

    #15543 should fix this!

  14. bjacob commented on Nov 10, 2023

    @bjacob
    Collaborator

    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.

  15. dcaballe commented on Nov 10, 2023

    @dcaballe
    Contributor

    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?

  16. bjacob commented on Nov 10, 2023

    @bjacob
    Collaborator
  17. dcaballe commented on Nov 11, 2023

    @dcaballe
    Contributor

    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?

  18. bjacob commented on Nov 13, 2023

    @bjacob
    Collaborator

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

    1. 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.
    2. 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).
  19. bjacob commented on Nov 14, 2023

    @bjacob
    Collaborator

    #15543 has been merged finally :-) @mariecwhite can you retry and hopefully confirm it fixed this?

  20. dcaballe commented on Nov 15, 2023

    @dcaballe
    Contributor

    Thanks for clarifying, Benoit. This is one of the things I would like to discuss for the UK enablement

  21. mariecwhite commented on Nov 21, 2023

    @mariecwhite
    ContributorAuthor

    #15543 has been merged finally :-) @mariecwhite can you retry and hopefully confirm it fixed this?

    This is fixed. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐞performance ⚡

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions