openvino.genai: FullyConnected nodes use slow reference kernel on ARM

@Wovchena This is related to #406 but goes deeper, hence I decided to make this a new issue.

TL;DR

  • For greedy_causal_lm inference on arm, large matmuls (e.g. 1x2x4096:4096x4096 in query/key/value projection) fall back to using a slow reference matmul implementation (ref_any_bf16)
  • Smaller matmuls (e.g. 1x32x2x2:1x32x2x128 in dot-product attention) use a faster gemm_acl_f16 kernel, which indicates that ACL kernels through oneDNN are available but not being used for point (1).
  • On x86 most matmuls use brgemm_avx512_bf16 kernel resulting in much faster inference.

Question: Is there a heuristic in OpenVINO that causes large matmuls on arm to fall back to the reference implementation?

Details

Following #327, I added code in greedy_causal_lm.cpp to save profiling information after inference. The table below shows the kernels used on x86 vs arm and their runtime (in microseconds) from the first decoder block of meta-llama/Llama-2-7b-hf (HuggingFace, compressed to INT8_ASYM).

Operation Shape x86 kernel x86 time (mcs) arm kernel arm time (mcs)
q_proj 1x2x4096:4096x4096 brgemm_avx512_bf16 584 ref_any_f16 44714
k_proj 1x2x4096:4096x4096 brgemm_avx512_bf16 699 ref_any_f16 39713
v_proj 1x2x4096:4096x4096 brgemm_avx512_bf16 5775 ref_any_f16 37816
o_proj 1x2x4096:4096x4096 brgemm_avx512_bf16 833 ref_any_f16 36469
mlp.gate_proj 1x2x4096:4096x11008 brgemm_avx512_bf16 1023 ref_any_f16 97537
mlp.up_proj 1x2x4096:4096x11008 brgemm_avx512_bf16 965 ref_any_f16 101677
mlp.down_proj 1x2x11008:11008x4096 brgemm_avx512_bf16 953 ref_any_f16 98749
classifier (logits) 1x2x4096:4096x32000 brgemm_avx512_bf16 1813 ref_any_f16 284708

For small matmuls however, I noticed gemm_acl_f16 being called on arm.

Operation Shape arm kernel arm time (mcs)
A = q_proj * k_proj ^ T 1x32x2x128:1x32x2x128 gemm_acl_f16 226
A * v_proj 1x32x2x2:1x32x2x128 gemm_acl_f16 69

This indicates that ACL kernels are available during inference but not being used only for large matmuls. To check that this diversion is not coming from oneDNN, I compiled oneDNN with ACL with following version combinations and benchmarked the above matmul sizes with benchdnn. Results are shown below, which indicate that gemm:acl is used on arm for all of the above sizes when the two compile correctly.

oneDNN source oneDNN version ACL version Kernel used on arm Comments
oneapi-src/oneDNN 3.3.3 24.02.1 Build fails; error from ACL References to ACL v24.02.1 were found in openvino docs.
oneapi-src/oneDNN 3.3.3 23.11 Build fails; error from ACL N/A
oneapi-src/oneDNN 3.3.3 23.08 gemm:acl Expected kernel is used.
openvinotoolkit/oneDNN 3.3.3 24.02.1 gemm:jit_f32 ACL compilation flag ignored by cmake
openvinotoolkit/oneDNN 3.3.3 23.11 gemm:jit_f32 ACL compilation flag ignored by cmake
openvinotoolkit/oneDNN 3.3.3 23.08 gemm:jit_f32 ACL compilation flag ignored by cmake

I think ACL kernels should ideally be used for any matmul on arm since they are available and being used for some operations. Since oneDNN is not the one causing a reference kernel to be used, I am led to believe that a heuristic within OpenVINO is causing this fallback. I’m hoping to know whether that is indeed the case, and (if yes) where this heuristic has been implemented.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18

Most upvoted comments

@dmitry-gorokhov @alvoron Thanks for your response.

  1. Regarding performance related points:

    • The poor throughput from optimum is most likely due to the right openvino package not being installed, like you pointed out. I ran several experiments today but I can’t derive any conclusions right now due to large variations in performance I’m observing for the other two experiments (compared to yesterday). This variation is due to issues with the instances I’m using for my experiments, not with OpenVINO. I’ll try and resolve them at my end and come back to you with some reliable data soon.
    • Between C++ and Python API, I noticed that the python script (third experiment in my previous message) was slightly faster than C++ even on x86. For example, in one experiment the C++ script yielded 13.40 tokens/sec while the Python script yielded 14.02 tokens/sec. Regardless, I will take these readings again once I resolve the issue I mentioned above.
    • I tried out llm_bench on ARM with Llama2, and got roughly the same performance numbers as I did from my script. The output of llm_bench is much more informative however, so I’ll probably continue using that going forward 😄 .
    • #334 -> very useful; eagerly awaited.
  2. I discussed with some of my colleagues, and they found it interesting that FP16 and BF16 had comparable accuracy as per your findings. We’ve almost always had BF16 give better accuracy, across many architectures (not limited to LLMs) and tasks. However, I can understand that there could be situations where this is not the case. It’s fine to take this up at a later point; not very urgent.

  3. Thanks for clarifying.

Please see

If you’d like to know how I’ve run my experiments (such as in point 1.2), see below. Before running, please export OPENVINO_SRC_DIR with the path where openvino is being cloned, without the trailing slash.

# My fork of openvino with @alvoron's changes
git clone https://github.com/NishantPrabhuFujitsu/openvino -b llm_inference
cd openvino

# Installation
git submodule update --init --recursive
chmod +x install_build_dependencies.sh
sudo ./install_build_dependencies.sh

# Build project along with python wheel
./build.sh | tee build.log

This should build the project and the wheel files in the usual locations. If some step errors out, please run the appropriate commands from build.sh with required adjustments separately. If the step corresponding to building the wheel file errors out, set -DPython3_EXECUTABLE to ${OPENVINO_SRC_DIR}/.venv/bin/python3.11 instead of /usr/bin/python3.11 in build.sh. Then, the scripts can be run as follows.

Export Llama2 to IR format

mkdir -p .ov_models/Llama2
optimum-cli export openvino --trust-remote-code --weight-format int8 --model meta-llama/Llama-2-7b-hf .ov_models/Llama2/

C++ script

cd ~/openvino_cpp_samples_build/aarch64/Release
./greedy_causal_lm /path/to/.ov_models/Llama2/ "The sun is yellow because"

Python script with HuggingFace optimum

cd "${OPENVINO_SRC_DIR}/samples/python/greedy_causal_lm"
python with_optimum.py

Python script using openvino package directly

cd "${OPENVINO_SRC_DIR}/samples/python/greedy_causal_lm"
python with_ov_core.py

Each script should print throughput information at the end once it finishes running.