目录

使用 Qualcomm AI Engine Direct 后端构建和运行 Llama 3 8B Instruct

本教程演示如何为高通 AI 引擎直连后端导出 Llama 3 8B Instruct 模型,并在高通设备上运行该模型。

预备知识

说明

步骤 1:从 Spin Quant 准备模型的检查点和优化矩阵

  1. 对于 Llama 3 分词器和检查点,请参阅 https://github.com/meta-llama/llama-models/blob/main/README.md 以获取有关如何下载 tokenizer.modelconsolidated.00.pthparams.json 的进一步说明。

  2. 要获取优化后的矩阵,请参阅 GitHub 上的 SpinQuant。你可以在量化模型部分下载优化后的旋转矩阵。请选择 LLaMA-3-8B/8B_W4A16KV16_lr_1.5_seed_0

步骤 2:使用高通 AI 引擎直接后端导出到 ExecuTorch

在设备上部署像 Llama 3 这样的大型语言模型面临以下挑战:

  1. 模型尺寸过大,无法在设备内存中进行推理。

  2. 模型加载和推理时间较长。

  3. 量化难度。

为应对这些挑战,我们实施了以下解决方案:

  1. 使用 --pt2e_quantize qnn_16a4w 对激活值和权重进行量化,从而减小磁盘上的模型大小并缓解推理过程中的内存压力。

  2. 使用 --num_sharding 8 将模型分片为子部分。

  3. 执行图变换,将操作转换或分解为更利于加速器处理的操作。

  4. 使用 --optimized_rotation_path <path_to_optimized_matrix> 应用 Spin Quant 的 R1 和 R2 以提高精度。

  5. 使用 --calibration_data "<|start_header_id|>system<|end_header_id|..." 确保在量化 Llama 3 8B instruct 时,校准包括提示模板中的特殊标记。有关提示模板的更多详细信息,请参阅 meta llama3 instruct 的模型卡片

要使用高通 AI 引擎直连后端导出 Llama 3 8B 指令模型,请确保满足以下条件:

  1. 主机内存(RAM + 交换空间)超过 100GB。

  2. 整个过程需要几个小时。

# Please note that calibration_data must include the prompt template for special tokens.
python -m examples.models.llama.export_llama -t <path_to_tokenizer.model>
llama3/Meta-Llama-3-8B-Instruct/tokenizer.model -p <path_to_params.json> -c <path_to_checkpoint_for_Meta-Llama-3-8B-Instruct>  --use_kv_cache  --qnn --pt2e_quantize qnn_16a4w --disable_dynamic_shape --num_sharding 8 --calibration_tasks wikitext --calibration_limit 1 --calibration_seq_length 128 --optimized_rotation_path <path_to_optimized_matrix> --calibration_data "<|start_header_id|>system<|end_header_id|>\n\nYou are a funny chatbot.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nCould you tell me about Facebook?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"

步骤 3:在搭载高通 SoC 的 Android 智能手机上调用运行时

  1. 为 Android 构建带有高通 AI Engine Direct 后端的 Executorch

    cmake \
        -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake" \
        -DANDROID_ABI=arm64-v8a \
        -DCMAKE_INSTALL_PREFIX=cmake-android-out \
        -DCMAKE_BUILD_TYPE=Release \
        -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
        -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
        -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
        -DEXECUTORCH_BUILD_QNN=ON \
        -DQNN_SDK_ROOT=${QNN_SDK_ROOT} \
        -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
        -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
        -DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
        -Bcmake-android-out .
    
    cmake --build cmake-android-out -j16 --target install --config Release
    
  2. 为 Android 构建 llama runner

    cmake \
        -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK_ROOT}"/build/cmake/android.toolchain.cmake  \
        -DANDROID_ABI=arm64-v8a \
        -DCMAKE_INSTALL_PREFIX=cmake-android-out \
        -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=python \
        -DEXECUTORCH_BUILD_QNN=ON \
        -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
        -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
        -DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
        -Bcmake-android-out/examples/models/llama examples/models/llama

    cmake --build cmake-android-out/examples/models/llama -j16 --config Release
  1. 通过 adb shell 在 Android 上运行 前提条件:请确保已在手机开发者选项中启用 USB 调试

3.1 连接您的安卓手机

3.2 我们需要将所需的 QNN 库推送到设备上。

# make sure you have write-permission on below path.
DEVICE_DIR=/data/local/tmp/llama
adb shell mkdir -p ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtp.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnSystem.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtpV69Stub.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtpV73Stub.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/aarch64-android/libQnnHtpV75Stub.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/hexagon-v69/unsigned/libQnnHtpV69Skel.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/hexagon-v73/unsigned/libQnnHtpV73Skel.so ${DEVICE_DIR}
adb push ${QNN_SDK_ROOT}/lib/hexagon-v75/unsigned/libQnnHtpV75Skel.so ${DEVICE_DIR}

3.3 将模型、分词器和 Llama Runner 二进制文件上传到手机

adb push <model.pte> ${DEVICE_DIR}
adb push <tokenizer.model> ${DEVICE_DIR}
adb push cmake-android-out/lib/libqnn_executorch_backend.so ${DEVICE_DIR}
adb push cmake-out-android/examples/models/llama/llama_main ${DEVICE_DIR}

3.4 运行模型

adb shell "cd ${DEVICE_DIR} && ./llama_main --model_path <model.pte> --tokenizer_path <tokenizer.model> --prompt \"<|start_header_id|>system<|end_header_id|>\n\nYou are a funny chatbot.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nCould you tell me about Facebook?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n\" --seq_len 128"

你应该看到消息:

<|start_header_id|>system<|end_header_id|>\n\nYou are a funny chatbot.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\nCould you tell me about Facebook?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\nHello! I'd be delighted to chat with you about Facebook. Facebook is a social media platform that was created in 2004 by Mark Zuckerberg and his colleagues while he was a student at Harvard University. It was initially called "Facemaker" but later changed to Facebook, which is a combination of the words "face" and "book". The platform was initially intended for people to share their thoughts and share information with their friends, but it quickly grew to become one of the

什么是即将来临的?

  • 提升 Llama 3 Instruct 的性能

  • 降低推理过程中的内存压力,以支持 12GB 的 Qualcomm 设备

  • 支持更多大型语言模型

FAQ

如果您在复现教程时遇到任何问题,请在ExecuTorch仓库中提交一个github issue并标记使用 #qcom_aisw 标签

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源